From 8d7481c6c1705f2e40e40894fe90cb95af466eaf Mon Sep 17 00:00:00 2001 From: wjoe Date: Thu, 22 Dec 2011 15:50:45 +0000 Subject: Fix newlines --- blaconvert.java | 250 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 249 insertions(+), 1 deletion(-) (limited to 'blaconvert.java') diff --git a/blaconvert.java b/blaconvert.java index 19e3e99..7a1d087 100644 --- a/blaconvert.java +++ b/blaconvert.java @@ -1 +1,249 @@ -/* 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 . */ /* blaconvert - Java program for converting units of measurement and currency using Google APIs by blatech Copyright 2011 Joe Robinson based on code by Jamie Walters based on code by Joe Robinson 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. +*/ + +/* + blaconvert - Java program for converting units of measurement and currency using Google APIs + by blatech + + Copyright 2011 Joe Robinson + based on code by Jamie Walters + based on code by Joe Robinson + + 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