Author: Paul_R
Date: 2008-08-27 14:30:21 +0200 (Wed, 27 Aug 2008)
New Revision: 1614
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/.classpath
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/gadgets.xml
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/sources/net/karmaLab/tuxDroid/gadgets/WeatherGadget.java
Log:
* BUG : Text encoding problems :
With the FR website, the encoding isn't UTF-8. To fix this bug, I save the
stream
on a file, and I convert this file with an UTF-8 code.
* Added a test to skip the forecast if the forecast config is disabled.
* Added protection to convert the temperature :
EN website : forecast on fahrenheit
FR website : forecast on celsius
* Typo : Fixed "celsius" and "fahrenheit".
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/.classpath
===================================================================
---
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/.classpath
2008-08-27 12:14:16 UTC (rev 1613)
+++
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/.classpath
2008-08-27 12:30:21 UTC (rev 1614)
@@ -1,11 +1,10 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path="tests"/>
- <classpathentry kind="src" path="sources"/>
- <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="con"
path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
- <classpathentry combineaccessrules="false" kind="src"
path="/karmalab-commons"/>
- <classpathentry combineaccessrules="false" kind="src"
path="/tuxdroid-gadget-framework"/>
- <classpathentry combineaccessrules="false" kind="src"
path="/tuxdroid-gadget-tester"/>
- <classpathentry kind="output" path="targets/eclipse"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="tests"/>
+ <classpathentry kind="src" path="sources"/>
+ <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con"
path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
+ <classpathentry kind="lib" path="G:/Projects/tux/Gadget
Ressources/tuxdroid-gadget-framework_fat.jar"/>
+ <classpathentry kind="lib" path="G:/Projects/tux/Gadget
Ressources/tuxdroid-gadget-tester_fat.jar"/>
+ <classpathentry kind="output" path="targets/eclipse"/>
+</classpath>
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/gadgets.xml
===================================================================
---
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/gadgets.xml
2008-08-27 12:14:16 UTC (rev 1613)
+++
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/gadgets.xml
2008-08-27 12:30:21 UTC (rev 1614)
@@ -21,14 +21,14 @@
defaultValue="paris" />
<parameter
name="unit"
- description="Temperature unit (celcius,
fareneight)"
- type="enum(celcius,fareneight)"
- defaultValue="celcius" />
+ description="Temperature unit
(celsius,fahrenheit)"
+ type="enum(celsius,fahrenheit)"
+ defaultValue="celsius" />
<parameter
name="tomorrow"
description="Give tomorrow weather too"
type="boolean"
- defaultValue="false" />
+ defaultValue="false" ></parameter>
</parameters>
<commands>
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/sources/net/karmaLab/tuxDroid/gadgets/WeatherGadget.java
===================================================================
---
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/sources/net/karmaLab/tuxDroid/gadgets/WeatherGadget.java
2008-08-27 12:14:16 UTC (rev 1613)
+++
software_suite_v2/software/gadgets/tuxdroid-gadget-weather/trunk/tuxdroid-gadget-weather/sources/net/karmaLab/tuxDroid/gadgets/WeatherGadget.java
2008-08-27 12:30:21 UTC (rev 1614)
@@ -22,12 +22,15 @@
package net.karmaLab.tuxDroid.gadgets;
-import java.net.MalformedURLException;
import java.net.URL;
+import java.io.*;
+import java.net.*;
+import java.util.Locale;
import net.karmaLab.xml.elements.Element;
import net.karmaLab.xml.parser.XMLParser;
+import com.kysoh.tuxdroid.gadget.framework.container.I18N;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadget;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadgetConfiguration;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadgetException;
@@ -36,7 +39,7 @@
public static class Configuration extends SimpleGadgetConfiguration {
private String location = "Paris";
private boolean tomorrow = true;
- private Unit unit = Unit.celcius;
+ private Unit unit = Unit.celsius;
public String getLocation() {
return location;
@@ -65,7 +68,7 @@
}
public static enum Unit {
- celcius, fareneight
+ celsius, fahrenheit
}
public static void main(String[] args) throws Exception {
@@ -78,16 +81,51 @@
@Override
public void start() throws SimpleGadgetException {
- URL url;
- try {
- url = new
URL("http://www.google.com/ig/api?hl=fr&weather=" +
configuration().getLocation());
- } catch (MalformedURLException e) {
- throw new SimpleGadgetException(e);
- }
+ File outputFile;
+ File file;
+ String inputLine;
+ Writer streamOutput = null;
+
XMLParser parser = new XMLParser();
Element xmlReply;
+
try {
- xmlReply =
parser.parse(url.openStream()).getRootElement().getElementAt(0);
+ /* create a new URL object */
+ URL xml = new
URL(String.format("http://www.google.com/ig/api?hl=%s&weather=%s",
I18N.cc_Locale, configuration().getLocation()));
+ /* Open the connection */
+ URLConnection yc = xml.openConnection();
+ /* And read the stream input */
+ BufferedReader streamInput = new BufferedReader(new
InputStreamReader(yc.getInputStream()));
+
+ /* Create a new file */
+ file = new File("unknowEncoding");
+ /* 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");
+ /* 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);
+
} catch (Exception e) {
throw new SimpleGadgetException(e);
}
@@ -96,29 +134,60 @@
if (weather.length() == 0) {
weather = "inconnues";
}
- String temperature = getData(current, configuration().getUnit()
== Unit.celcius ? "temp_c" : "temp_f");
+ String temperature = getData(current, configuration().getUnit()
== Unit.celsius ? "temp_c" : "temp_f");
String humidity = getData(current, "humidity");
- humidity = humidity.split("\\s+:\\s+")[1];
+ humidity = humidity.split(":")[1];
+ humidity = (String) humidity.subSequence(0,
humidity.length()-1);
+
// Icon icon = new ImageIcon(new URL("http://www.google.com" +
// getData(current, "icon")));
// String wind = getData(current, "wind_condition");
// wind = humidity.split("\\s+:\\s+")[1];
- String message = String.format("Les conditions actuelles sur %s
sont \"%s\" avec une température de %s degrés %s. Le taux d'humidité est de
%s.", configuration().getLocation(), weather, temperature,
- configuration().getUnit() == Unit.celcius ?
"celcius" : "fareneight", humidity);
+ String message = String.format("Les conditions actuelles sur %s
sont \"%s\" avec une température de %s degrès %s. Le taux d'humidité est de %s
pour cent.", configuration().getLocation(), weather, temperature,
+ configuration().getUnit() == Unit.celsius ?
"celsius" : "fahrenheit", humidity);
throwMessage(message);
- message = String.format("Les conditions actuelles sur %s sont
\"%s\" avec une température de %s degrés %s. Le taux d'humidité est de %s.",
configuration().getLocation(), weather, temperature, configuration().getUnit()
== Unit.celcius ? "celcius"
- : "fareneight", humidity);
- throwMessage(message);
- current = xmlReply.getElementAt(3);
- weather = getData(current, "condition");
- temperature = getData(current, "low");
- String temperatureHigh = getData(current, "high");
- // icon = new ImageIcon(new URL("http://www.google.com" +
- // getData(current, "icon")));
- message = String.format("Le temps demain sera \"%s\" avec des
températures allant de %s à %s degrés.", weather, temperature,
temperatureHigh);
- throwMessage(message);
- message = String.format("Le temps demain sera \"%s\" avec des
températures allant de %s à %s degrés.", weather, temperature, temperatureHigh);
- throwMessage(message);
- throwTrace("Test trace");
+
+ 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 (I18N.cc_Locale.equals(Locale.ENGLISH) &&
configuration().getUnit() == Unit.celsius) {
+ temperature =
fahrenheitToCelsius(getData(current, "low"));
+ temperatureHigh =
fahrenheitToCelsius(getData(current, "high"));
+ }
+ else if (!(I18N.cc_Locale.equals(Locale.ENGLISH)) &&
configuration().getUnit() == Unit.fahrenheit) {
+ temperature =
celsiusToFahrenheit(getData(current, "low"));
+ temperatureHigh =
celsiusToFahrenheit(getData(current, "high"));
+ }
+ else {
+ temperature = getData(current, "low");
+ temperatureHigh = getData(current, "high");
+ }
+
+ message = String.format("Le temps demain sera \"%s\"
avec des températures allant de %s à %s degrés.", weather, temperature,
temperatureHigh);
+ throwMessage(message);
+ }
+ outputFile.deleteOnExit();
+ file.deleteOnExit();
}
+
+ 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);
+ }
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn