[tuxdroid-svn] r5655 - software_suite_v3/software/plugin/plugin-weather/branches/random_sentences/p
Status: Beta
Brought to you by:
ks156
|
From: ks156 <c2m...@c2...> - 2009-10-14 11:36:12
|
Author: ks156
Date: 2009-10-14 13:36:01 +0200 (Wed, 14 Oct 2009)
New Revision: 5655
Modified:
software_suite_v3/software/plugin/plugin-weather/branches/random_sentences/plugin-weather/src/net/karmaLab/tuxDroid/plugins/WeatherPlugin.java
Log:
* Retab
* Removed trailing spaces
* Recoded with the std java style
Modified: software_suite_v3/software/plugin/plugin-weather/branches/random_sentences/plugin-weather/src/net/karmaLab/tuxDroid/plugins/WeatherPlugin.java
===================================================================
--- software_suite_v3/software/plugin/plugin-weather/branches/random_sentences/plugin-weather/src/net/karmaLab/tuxDroid/plugins/WeatherPlugin.java 2009-10-14 11:25:47 UTC (rev 5654)
+++ software_suite_v3/software/plugin/plugin-weather/branches/random_sentences/plugin-weather/src/net/karmaLab/tuxDroid/plugins/WeatherPlugin.java 2009-10-14 11:36:01 UTC (rev 5655)
@@ -44,238 +44,238 @@
import net.karmaLab.xml.parser.XMLParser;
public class WeatherPlugin extends SimplePlugin<WeatherPlugin.Configuration> {
- public static class Configuration extends SimplePluginConfiguration {
- private String location = "Paris";
- private boolean tomorrow = true;
- private Unit unit = Unit.celsius;
- private String googleURL;
+ public static class Configuration extends SimplePluginConfiguration {
+ private String location = "Paris";
+ private boolean tomorrow = true;
+ private Unit unit = Unit.celsius;
+ private String googleURL;
- /**
- * get googleURL property value.
- *
- * @return googleURL value
- */
- public String getGoogleURL() {
- return googleURL;
- }
+ /**
+ * get googleURL property value.
+ *
+ * @return googleURL value
+ */
+ public String getGoogleURL() {
+ return googleURL;
+ }
- /**
- * set googleURL property value.
- *
- * @param googleURL
- * new value
- */
- public void setGoogleURL(String googleURL) {
- this.googleURL = googleURL;
- }
+ /**
+ * set googleURL property value.
+ *
+ * @param googleURL
+ * new value
+ */
+ public void setGoogleURL(String googleURL) {
+ this.googleURL = googleURL;
+ }
- public String getLocation() {
- return location;
- }
+ public String getLocation() {
+ return location;
+ }
- public Unit getUnit() {
- return unit;
- }
+ public Unit getUnit() {
+ return unit;
+ }
- public boolean isTomorrow() {
- return tomorrow;
- }
+ public boolean isTomorrow() {
+ return tomorrow;
+ }
- public void setLocation(String location) {
- this.location = location;
- }
+ public void setLocation(String location) {
+ this.location = location;
+ }
- public void setTomorrow(boolean tomorrow) {
- this.tomorrow = tomorrow;
+ public void setTomorrow(boolean tomorrow) {
+ this.tomorrow = tomorrow;
- }
+ }
- public void setUnit(Unit unit) {
- this.unit = unit;
- }
+ public void setUnit(Unit unit) {
+ this.unit = unit;
+ }
- }
+ }
- String[] todayWeatherSentences = {
- "Current weather at {0} is \"{1}\" with a temperature of {2} degrees {3}; Humidity level is {4} percent.",
- "In {0}, the weather is {1} with a temperature of {2} degrees {3}. The humidity level is {4} per cent.",
- "The weather is currently {1} in {0} with a humidity of {4} per cent. The temperature is around {2} degrees {3}.",
- "With a temperature of {2} degrees {3}, the weather in {0} is {1}, and the humidity level is {4} per cent."
- };
+ String[] todayWeatherSentences = {
+ "Current weather at {0} is \"{1}\" with a temperature of {2} degrees {3}; Humidity level is {4} percent.",
+ "In {0}, the weather is {1} with a temperature of {2} degrees {3}. The humidity level is {4} per cent.",
+ "The weather is currently {1} in {0} with a humidity of {4} per cent. The temperature is around {2} degrees {3}.",
+ "With a temperature of {2} degrees {3}, the weather in {0} is {1}, and the humidity level is {4} per cent."
+ };
- String[] todayUnknownWeatherSentences = {
- "Current weather at {0} is \"unknown\" with a temperature of {1} degrees {2}; Humidity level is {3} percent.",
- "In {0}, the weather is unknown with a temperature of {1} degrees {2}. The humidity level is {3} per cent.",
- "The weather is currently unknown in {0} with a humidity of {3} per cent. The temperature is around {1} degrees {2}.",
- "With a temperature of {1} degrees {2}, the weather in {0} is unknown, and the humidity level is {3} per cent."
- };
+ String[] todayUnknownWeatherSentences = {
+ "Current weather at {0} is \"unknown\" with a temperature of {1} degrees {2}; Humidity level is {3} percent.",
+ "In {0}, the weather is unknown with a temperature of {1} degrees {2}. The humidity level is {3} per cent.",
+ "The weather is currently unknown in {0} with a humidity of {3} per cent. The temperature is around {1} degrees {2}.",
+ "With a temperature of {1} degrees {2}, the weather in {0} is unknown, and the humidity level is {3} per cent."
+ };
- String[] tomorrowWeatherSentences = {
- "Tomorrow's forecast. \"{0}\" temperatures from {1} to {2} degrees.",
- "Tomorrow, the weather will be {0} with temperatures between {1} and {2} degrees.",
- "With temperatures between {1} and {2} degrees, tomorrow's forecast is expected to be {0}.",
- "Tomorrow's weather will be {0} with temperatures between {1} and {2} degrees.",
- "Tomorrow, the weather will be {0} with temperatures from {1} up to {2} degrees."
- };
+ String[] tomorrowWeatherSentences = {
+ "Tomorrow's forecast. \"{0}\" temperatures from {1} to {2} degrees.",
+ "Tomorrow, the weather will be {0} with temperatures between {1} and {2} degrees.",
+ "With temperatures between {1} and {2} degrees, tomorrow's forecast is expected to be {0}.",
+ "Tomorrow's weather will be {0} with temperatures between {1} and {2} degrees.",
+ "Tomorrow, the weather will be {0} with temperatures from {1} up to {2} degrees."
+ };
- String[] errorSentences = {
- "Location could not be found. Please check the city name or enter another close by location.",
- "Impossible to find the specified city name. Are you sure this city really exists ?",
- "The city you specified could not be found in my database. Make sure the city name exists, or specify another close by city name.",
- "I'm sorry, but I cannot give you the weather for the specified location. Please try another close by city name."
- };
+ String[] errorSentences = {
+ "Location could not be found. Please check the city name or enter another close by location.",
+ "Impossible to find the specified city name. Are you sure this city really exists ?",
+ "The city you specified could not be found in my database. Make sure the city name exists, or specify another close by city name.",
+ "I'm sorry, but I cannot give you the weather for the specified location. Please try another close by city name."
+ };
- private String pickSentence(String[] list) {
- Random rand = new Random();
- return list[rand.nextInt(list.length)];
- }
+ private String pickSentence(String[] list) {
+ Random rand = new Random();
+ return list[rand.nextInt(list.length)];
+ }
- public static enum Unit {
- celsius, fahrenheit
- }
+ public static enum Unit {
+ celsius, fahrenheit
+ }
- public static void main(String[] args) throws Exception {
- new WeatherPlugin().boot(args, new Configuration());
- }
+ public static void main(String[] args) throws Exception {
+ new WeatherPlugin().boot(args, new Configuration());
+ }
- private String getData(Element node, String label) {
- return node.getElement(label).getAttribute("data"); //$NON-NLS-1$
- }
+ private String getData(Element node, String label) {
+ return node.getElement(label).getAttribute("data"); //$NON-NLS-1$
+ }
- @Override
- public void start() throws SimplePluginException {
- File outputFile;
- File file;
- String inputLine;
- Writer streamOutput = null;
- XMLParser parser = new XMLParser();
- Element xmlReply;
+ @Override
+ public void start() throws SimplePluginException {
+ File outputFile;
+ File file;
+ String inputLine;
+ Writer streamOutput = null;
+ XMLParser parser = new XMLParser();
+ Element xmlReply;
- try {
- // Replace the space by %20 (HTML code)
- configuration().setLocation(configuration().getLocation().replace(" ", "%20"));
- /* create a new URL object */
- URL xml = new URL(String.format(configuration().getGoogleURL(), configuration().getLocation()));
- /* Open the connection */
- HttpURLConnection.setDefaultRequestProperty("User-Agent", "Mozilla/4.0 ( compatible ) ");
- HttpURLConnection yc = (HttpURLConnection) xml.openConnection();
- yc.setRequestProperty ("User-Agent", "Mozilla/4.0 ( compatible ) ");
+ try {
+ // Replace the space by %20 (HTML code)
+ configuration().setLocation(configuration().getLocation().replace(" ", "%20"));
+ /* create a new URL object */
+ URL xml = new URL(String.format(configuration().getGoogleURL(), configuration().getLocation()));
+ /* Open the connection */
+ HttpURLConnection.setDefaultRequestProperty("User-Agent", "Mozilla/4.0 ( compatible ) ");
+ HttpURLConnection yc = (HttpURLConnection) xml.openConnection();
+ yc.setRequestProperty ("User-Agent", "Mozilla/4.0 ( compatible ) ");
- /* And read the stream input */
- BufferedReader streamInput = new BufferedReader(new InputStreamReader(yc.getInputStream()));
+ /* And read the stream input */
+ BufferedReader streamInput = new BufferedReader(new InputStreamReader(yc.getInputStream()));
- /* Create a new file */
- file = new File("unknowEncoding"); //$NON-NLS-1$
- /* Connect the writer with the file */
- streamOutput = new BufferedWriter(new FileWriter(file));
- /* ... and write the file content */
- while ((inputLine = streamInput.readLine()) != null) {
- streamOutput.write(inputLine);
- }
- /* Close the streams */
- streamInput.close();
- streamOutput.close();
+ /* Create a new file */
+ file = new File("unknowEncoding"); //$NON-NLS-1$
+ /* Connect the writer with the file */
+ streamOutput = new BufferedWriter(new FileWriter(file));
+ /* ... and write the file content */
+ while ((inputLine = streamInput.readLine()) != null) {
+ streamOutput.write(inputLine);
+ }
+ /* Close the streams */
+ streamInput.close();
+ streamOutput.close();
- int c;
- /* Create a new file to convert the first file */
- outputFile = new File("recoding");
- /*
- * Create a stream reader and a stream writer, and connect them with
- * the two files
- */
- InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file));
- /* Specify the codage for the output file */
- OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"); //$NON-NLS-1$
- /* Then, write the file char by char */
- while ((c = inputStream.read()) != -1)
- outputStream.write(c);
- /* And close the streams */
- outputStream.close();
- inputStream.close();
+ int c;
+ /* Create a new file to convert the first file */
+ outputFile = new File("recoding");
+ /*
+ * Create a stream reader and a stream writer, and connect them with
+ * the two files
+ */
+ InputStreamReader inputStream = new InputStreamReader(new FileInputStream(file));
+ /* Specify the codage for the output file */
+ OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"); //$NON-NLS-1$
+ /* Then, write the file char by char */
+ while ((c = inputStream.read()) != -1)
+ outputStream.write(c);
+ /* And close the streams */
+ outputStream.close();
+ inputStream.close();
- /* Parse the XML file, with an UTF-8 codage */
- xmlReply = parser.parse(outputFile).getRootElement().getElementAt(0);
+ /* Parse the XML file, with an UTF-8 codage */
+ xmlReply = parser.parse(outputFile).getRootElement().getElementAt(0);
- } catch (Exception e) {
- throw new SimplePluginException(e);
- }
+ } catch (Exception e) {
+ throw new SimplePluginException(e);
+ }
- try {
- Element current = xmlReply.getElementAt(1);
- String weather = getData(current, "condition");
- String temperature = getData(current, configuration().getUnit() == Unit.celsius ? "temp_c" : "temp_f");
- String humidity = getData(current, "humidity"); //$NON-NLS-1$
- humidity = humidity.split(":")[1]; //$NON-NLS-1$
- humidity = ((String) humidity.subSequence(0, humidity.length() - 1)).trim();
- humidity = humidity.replaceAll("[^a-zA-Z0-9]", "");
- if (weather.length() == 0) {
- throwMessage(this.pickSentence(todayUnknownWeatherSentences), configuration().getLocation().replace("%20", " "), temperature, configuration().getUnit() == Unit.celsius ? "celsius" : "fahrenheit",
- humidity);
- } else {
- throwMessage(this.pickSentence(todayWeatherSentences), configuration().getLocation().replace("%20", " "), weather, temperature, configuration().getUnit() == Unit.celsius ? "celsius" : "fahrenheit",
- humidity);
- }
+ try {
+ Element current = xmlReply.getElementAt(1);
+ String weather = getData(current, "condition");
+ String temperature = getData(current, configuration().getUnit() == Unit.celsius ? "temp_c" : "temp_f");
+ String humidity = getData(current, "humidity"); //$NON-NLS-1$
+ humidity = humidity.split(":")[1]; //$NON-NLS-1$
+ humidity = ((String) humidity.subSequence(0, humidity.length() - 1)).trim();
+ humidity = humidity.replaceAll("[^a-zA-Z0-9]", "");
+ if (weather.length() == 0) {
+ throwMessage(this.pickSentence(todayUnknownWeatherSentences), configuration().getLocation().replace("%20", " "),
+ temperature, configuration().getUnit() == Unit.celsius ? "celsius" : "fahrenheit", humidity);
+ } else {
+ throwMessage(this.pickSentence(todayWeatherSentences), configuration().getLocation().replace("%20", " "),
+ weather, temperature, configuration().getUnit() == Unit.celsius ? "celsius" : "fahrenheit", humidity);
+ }
- if (configuration().isTomorrow()) {
- String temperatureHigh;
- current = xmlReply.getElementAt(3);
- weather = getData(current, "condition");
+ if (configuration().isTomorrow()) {
+ String temperatureHigh;
+ current = xmlReply.getElementAt(3);
+ weather = getData(current, "condition");
- /*
- * There is an annoying difference between google FR and EN. EN :
- * Temperatures on the forecast are in Fahrenheit FR : Temperatures
- * on the forecast are in Celsius Depending of the control center
- * language, the temperature must be converted to respect the CC
- * configuration.
- */
- if (configuration().getLanguage().equals("en") && configuration().getUnit() == Unit.celsius) {
- temperature = fahrenheitToCelsius(getData(current, "low"));
- temperatureHigh = fahrenheitToCelsius(getData(current, "high"));
- } else if (!(configuration().getLanguage().equals("en")) && configuration().getUnit() == Unit.fahrenheit) {
- temperature = celsiusToFahrenheit(getData(current, "low"));
- temperatureHigh = celsiusToFahrenheit(getData(current, "high"));
- } else {
- temperature = getData(current, "low");
- temperatureHigh = getData(current, "high");
- }
+ /*
+ * There is an annoying difference between google FR and EN. EN :
+ * Temperatures on the forecast are in Fahrenheit FR : Temperatures
+ * on the forecast are in Celsius Depending of the control center
+ * language, the temperature must be converted to respect the CC
+ * configuration.
+ */
+ if (configuration().getLanguage().equals("en") && configuration().getUnit() == Unit.celsius) {
+ temperature = fahrenheitToCelsius(getData(current, "low"));
+ temperatureHigh = fahrenheitToCelsius(getData(current, "high"));
+ } else if (!(configuration().getLanguage().equals("en")) && configuration().getUnit() == Unit.fahrenheit) {
+ temperature = celsiusToFahrenheit(getData(current, "low"));
+ temperatureHigh = celsiusToFahrenheit(getData(current, "high"));
+ } else {
+ temperature = getData(current, "low");
+ temperatureHigh = getData(current, "high");
+ }
- throwMessage(this.pickSentence(tomorrowWeatherSentences), weather, temperature, temperatureHigh);
- }
- outputFile.deleteOnExit();
- file.deleteOnExit();
- } catch (Exception e) {
- // When a location is unknown, the Google API send back a XML content without data.
- throwMessage(this.pickSentence(errorSentences));
- }
- }
+ throwMessage(this.pickSentence(tomorrowWeatherSentences), weather, temperature, temperatureHigh);
+ }
+ outputFile.deleteOnExit();
+ file.deleteOnExit();
+ } catch (Exception e) {
+ // When a location is unknown, the Google API send back a XML content without data.
+ throwMessage(this.pickSentence(errorSentences));
+ }
+ }
- private String celsiusToFahrenheit(String temp) {
- float celsiusValue = Float.valueOf(temp).floatValue();
- float farhenheitValue = (float) ((celsiusValue * 1.8) + 32);
- return String.valueOf((int) farhenheitValue);
- }
+ private String celsiusToFahrenheit(String temp) {
+ float celsiusValue = Float.valueOf(temp).floatValue();
+ float farhenheitValue = (float) ((celsiusValue * 1.8) + 32);
+ return String.valueOf((int) farhenheitValue);
+ }
- private String fahrenheitToCelsius(String temp) {
- float farhenheitValue = Float.valueOf(temp).floatValue();
- float celsiusValue = (float) ((farhenheitValue - 32) / 1.8);
- return String.valueOf((int) celsiusValue);
- }
+ private String fahrenheitToCelsius(String temp) {
+ float farhenheitValue = Float.valueOf(temp).floatValue();
+ float celsiusValue = (float) ((farhenheitValue - 32) / 1.8);
+ return String.valueOf((int) celsiusValue);
+ }
-
- public void onGadgetStop()
+
+ public void onGadgetStop()
{
- ;
+ ;
}
- @Override
- protected void onPluginEvent(String arg0, String[] arg1) throws Throwable {
- // TODO Auto-generated method stub
- ;
- }
+ @Override
+ protected void onPluginEvent(String arg0, String[] arg1) throws Throwable {
+ // TODO Auto-generated method stub
+ ;
+ }
- @Override
- protected void onPluginStop() throws Throwable {
- // TODO Auto-generated method stub
- ;
- }
+ @Override
+ protected void onPluginStop() throws Throwable {
+ // TODO Auto-generated method stub
+ ;
+ }
}
|