diff options
author | wjoe <joe@lc8n.com> | 2011-12-22 15:45:55 +0000 |
---|---|---|
committer | wjoe <joe@lc8n.com> | 2011-12-22 15:45:55 +0000 |
commit | e07b7204f1bdae24b95187f41efb6975f05c0e04 (patch) | |
tree | 69943d55611392731c9d0de8df0734e6f5220822 | |
parent | 10c36849c58f8177ed931a49098c870c3345d7c4 (diff) |
Attempting to add euro support (probably won't work)
-rw-r--r-- | blaconvert.java | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/blaconvert.java b/blaconvert.java index 2baa58f..19e3e99 100644 --- a/blaconvert.java +++ b/blaconvert.java @@ -1 +1 @@ -/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
blaconvert - Java program for converting units of measurement and currency using Google APIs
by blatech
Copyright 2011 Joe Robinson <joe@lc8n.com>
based on code by Jamie Walters <jagw@jagw.co.uk>
based on code by Joe Robinson <joe@lc8n.com>
Version 1.3
CHANGELOG
1.0 - First version
1.1 - Allow units with more than one word eg "light year"
- Added "same unit" error
- Ensure standard imperial measurements are used for some units - google defaults to US
1.2 - Replace crazy non unicode spaces with commas
1.21 - Added quart to imperial measurements
1.3 - Added csupport for currency symbols
TODO
- Raw output option - result only with no spaces/commas/units
*/
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class blaconvert {
public static void main(String[] args) throws IOException
{
String sourceUnit = "";
String targetUnit = "";
String amount = "";
String version = "1.3";
/* Build query from query or arguments, depending on whether
arguments were given. */
if (args.length == 0) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
int start = 0;
int to = 0;
int output = 0;
if (split[0].startsWith("-"))
{
if (split[0].equals("-v") || split[0].equals("--version"))
{
System.out.println("v"+version);
System.exit(0);
} else if (split[0].charAt(1) == 'r')
{
//reserved for raw output option
output = 1;
start = 1;
}
}
for(int i = 0; i < split.length; i++)
{
if (split[i].toLowerCase().equals("to") || split[i].toLowerCase().equals("in"))
{
to = i;
}
}
amount = split[start];
for (int i = start+1; i < to; i++)
{
sourceUnit += split[i]+"%20";
}
for (int i = to+1; i < split.length; i++)
{
targetUnit += split[i]+"%20";
}
} catch (Exception e) {
System.err.println("Error:" + e.getMessage());
System.exit(1);
}
} else {
if (args[0].equals("-v") || args[0].equals("--version"))
{
System.out.println("v"+version);
System.exit(0);
}
amount = args[0];
sourceUnit = args[1];
targetUnit = args[2];
}
String[] imperial = {"pint", "gallon", "gal", "cup", "teaspoon", "tablespoon", "fl oz", "fluid ounce", "quart"};
for(int i = 0; i < imperial.length; i++)
{
if (sourceUnit.startsWith(imperial[i]))
{
sourceUnit = "Imperial%20"+sourceUnit;
}
if (targetUnit.startsWith(imperial[i]))
{
targetUnit = "Imperial%20"+targetUnit;
}
}
boolean currency = false;
if(amount.startsWith("£") || amount.startsWith("$") || amount.startsWith("¥"))
{
amount.replaceAll("£","%A3");
amount.replaceAll("$","%24");
amount.replaceAll("¥","%A5");
currency = true;
}
//URL which provides XML response
URL url = new URL("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit);
//System.out.println("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit);
URLConnection urlc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line;
String[] lines;
String tempString = "";
String lhs = "";
String rhs = "";
String error = "";
//Read every line
while ((line = br.readLine()) != null)
{
// Splits the google output by the commas
lines = line.split(",");
for(int i=0; i<lines.length; i++){
//System.out.println(lines[i]);
// Get the useful information (the urls!)
if(lines[i].trim().startsWith("{lhs")){
tempString = lines[i];
// Blat the non-url information
lhs = tempString.substring(7, tempString.length()-1);
}
else if(lines[i].trim().startsWith("rhs")){
tempString = lines[i];
// Blat the non-url information
rhs = tempString.substring(6, tempString.length()-1);
}
else if(lines[i].trim().startsWith("error")){
tempString = lines[i];
// Blat the non-url information
error = tempString.substring(8, tempString.length()-1);
}
}
}
String[] parts = rhs.split(" ");
String result = fixCrazySpaces(parts[0]) + " ";
//Convert weird output into something that makes sense
for (int i = 1; i < parts.length; i++)
{
//System.out.println(parts[i]);
if (parts[i].equals("\\x26#215;"))
{
parts[i] = "*";
} else if (parts[i].startsWith("10\\"))
{
String[] exp = parts[i].split("\\\\");
parts[i] = "10^"+exp[2].substring(3, exp[2].length());
}
result += parts[i] + " ";
}
parts = lhs.split(" ");
lhs = "";
if(!currency) {
parts[0] = fixCrazySpaces(parts[0]);
} else {
parts[1] = fixCrazySpaces(parts[1]);
}
for (int i = 0; i < parts.length; i ++)
{
lhs += parts[i]+" ";
}
// Print results
if(error.isEmpty()) {
System.out.println(lhs+"= "+result);
System.exit(0);
} else {
if(error.equals("4"))
{
error = "Unknown unit";
} else if (error.equals("0"))
{
error = "Both units are the same";
}
System.out.print("Error: "+error);
System.out.println("\n");
System.exit(1);
}
}
private static String fixCrazySpaces(String string)
{
char[] chars = string.toCharArray();
String fixed = "";
for (int i = 0; i < chars.length; i++)
{
if (Character.getNumericValue(chars[i]) < 0 && chars[i] != '.')
{
fixed += ",";
} else {
fixed += chars[i];
}
}
return fixed;
}
}
\ No newline at end of file +/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
blaconvert - Java program for converting units of measurement and currency using Google APIs
by blatech
Copyright 2011 Joe Robinson <joe@lc8n.com>
based on code by Jamie Walters <jagw@jagw.co.uk>
based on code by Joe Robinson <joe@lc8n.com>
Version 1.31
CHANGELOG
1.0 - First version
1.1 - Allow units with more than one word eg "light year"
- Added "same unit" error
- Ensure standard imperial measurements are used for some units - google defaults to US
1.2 - Replace crazy non unicode spaces with commas
1.21 - Added quart to imperial measurements
1.3 - Added support for currency symbols
1.31 - Trying to add euro (probably won't work)
TODO
- Raw output option - result only with no spaces/commas/units
*/
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class blaconvert {
public static void main(String[] args) throws IOException
{
String sourceUnit = "";
String targetUnit = "";
String amount = "";
String version = "1.31";
/* Build query from query or arguments, depending on whether
arguments were given. */
if (args.length == 0) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
int start = 0;
int to = 0;
int output = 0;
if (split[0].startsWith("-"))
{
if (split[0].equals("-v") || split[0].equals("--version"))
{
System.out.println("v"+version);
System.exit(0);
} else if (split[0].charAt(1) == 'r')
{
//reserved for raw output option
output = 1;
start = 1;
}
}
for(int i = 0; i < split.length; i++)
{
if (split[i].toLowerCase().equals("to") || split[i].toLowerCase().equals("in"))
{
to = i;
}
}
amount = split[start];
for (int i = start+1; i < to; i++)
{
sourceUnit += split[i]+"%20";
}
for (int i = to+1; i < split.length; i++)
{
targetUnit += split[i]+"%20";
}
} catch (Exception e) {
System.err.println("Error:" + e.getMessage());
System.exit(1);
}
} else {
if (args[0].equals("-v") || args[0].equals("--version"))
{
System.out.println("v"+version);
System.exit(0);
}
amount = args[0];
sourceUnit = args[1];
targetUnit = args[2];
}
String[] imperial = {"pint", "gallon", "gal", "cup", "teaspoon", "tablespoon", "fl oz", "fluid ounce", "quart"};
for(int i = 0; i < imperial.length; i++)
{
if (sourceUnit.startsWith(imperial[i]))
{
sourceUnit = "Imperial%20"+sourceUnit;
}
if (targetUnit.startsWith(imperial[i]))
{
targetUnit = "Imperial%20"+targetUnit;
}
}
boolean currency = false;
if(amount.startsWith("£") || amount.startsWith("$") || amount.startsWith("¥"))
{
amount.replaceAll("£","%A3");
amount.replaceAll("$","%24");
amount.replaceAll("€","%8010");
amount.replaceAll("\u20AC","%8010");
amount.replaceAll("¥","%A5");
currency = true;
}
//URL which provides XML response
URL url = new URL("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit);
//System.out.println("http://www.google.com/ig/calculator?hl=en&q="+amount+sourceUnit+"=?"+targetUnit);
URLConnection urlc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line;
String[] lines;
String tempString = "";
String lhs = "";
String rhs = "";
String error = "";
//Read every line
while ((line = br.readLine()) != null)
{
// Splits the google output by the commas
lines = line.split(",");
for(int i=0; i<lines.length; i++){
//System.out.println(lines[i]);
// Get the useful information (the urls!)
if(lines[i].trim().startsWith("{lhs")){
tempString = lines[i];
// Blat the non-url information
lhs = tempString.substring(7, tempString.length()-1);
}
else if(lines[i].trim().startsWith("rhs")){
tempString = lines[i];
// Blat the non-url information
rhs = tempString.substring(6, tempString.length()-1);
}
else if(lines[i].trim().startsWith("error")){
tempString = lines[i];
// Blat the non-url information
error = tempString.substring(8, tempString.length()-1);
}
}
}
String[] parts = rhs.split(" ");
String result = fixCrazySpaces(parts[0]) + " ";
//Convert weird output into something that makes sense
for (int i = 1; i < parts.length; i++)
{
//System.out.println(parts[i]);
if (parts[i].equals("\\x26#215;"))
{
parts[i] = "*";
} else if (parts[i].startsWith("10\\"))
{
String[] exp = parts[i].split("\\\\");
parts[i] = "10^"+exp[2].substring(3, exp[2].length());
}
result += parts[i] + " ";
}
parts = lhs.split(" ");
lhs = "";
if(!currency) {
parts[0] = fixCrazySpaces(parts[0]);
} else {
parts[1] = fixCrazySpaces(parts[1]);
}
for (int i = 0; i < parts.length; i ++)
{
lhs += parts[i]+" ";
}
// Print results
if(error.isEmpty()) {
System.out.println(lhs+"= "+result);
System.exit(0);
} else {
if(error.equals("4"))
{
error = "Unknown unit";
} else if (error.equals("0"))
{
error = "Both units are the same";
}
System.out.print("Error: "+error);
System.out.println("\n");
System.exit(1);
}
}
private static String fixCrazySpaces(String string)
{
char[] chars = string.toCharArray();
String fixed = "";
for (int i = 0; i < chars.length; i++)
{
if (Character.getNumericValue(chars[i]) < 0 && chars[i] != '.')
{
fixed += ",";
} else {
fixed += chars[i];
}
}
return fixed;
}
}
\ No newline at end of file |