1 package tim.prune.function.weather;
3 import java.util.ArrayList;
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.DefaultHandler;
11 * XML handler for dealing with the XML of weather forecasts
12 * returned from openweathermap.org (current weather has different structure)
14 public class OWMForecastHandler extends DefaultHandler
16 private String _value = null;
17 /** The location name */
18 private String _locName = null;
19 /** The forecast update time */
20 private String _updateTime = null;
21 /** The currently open forecast */
22 private SingleForecast _forecast = null;
23 /** List of all the forecasts found so far */
24 private ArrayList<SingleForecast> _forecastList = new ArrayList<SingleForecast>();
28 * React to the start of an XML tag
30 public void startElement(String inUri, String inLocalName, String inTagName,
31 Attributes inAttributes) throws SAXException
33 if (inTagName.equals("time")) { // start of a new forecast
34 _forecast = new SingleForecast();
35 // date, timefrom, timeto
36 _forecast.setTime(inAttributes.getValue("day"), inAttributes.getValue("from"), inAttributes.getValue("to"));
38 else if (inTagName.equals("symbol")) {
39 // numeric code, owm image name, description
40 _forecast.setSymbol(inAttributes.getValue("number"), inAttributes.getValue("var"), inAttributes.getValue("name"));
42 else if (inTagName.equals("windSpeed")) {
43 _forecast.setWindDesc(inAttributes.getValue("name"));
45 else if (inTagName.equals("temperature")) {
46 _forecast.setTemps(inAttributes.getValue("min"), inAttributes.getValue("max"));
48 else if (inTagName.equals("humidity")) {
49 _forecast.setHumidity(inAttributes.getValue("value") + inAttributes.getValue("unit"));
52 super.startElement(inUri, inLocalName, inTagName, inAttributes);
56 * React to the end of an XML tag
58 public void endElement(String inUri, String inLocalName, String inTagName)
61 if (inTagName.equals("name")) {
64 else if (inTagName.equals("lastupdate")) {
67 else if (inTagName.equals("time"))
69 // End of a time tag, add the current forecast to the list
70 _forecastList.add(_forecast);
72 super.endElement(inUri, inLocalName, inTagName);
76 * React to characters received inside tags
78 public void characters(char[] inCh, int inStart, int inLength)
81 String value = new String(inCh, inStart, inLength);
82 _value = (_value==null?value:_value+value);
83 super.characters(inCh, inStart, inLength);
86 /** @return location name of forecast */
87 public String getLocationName() {
91 /** @return update time of forecast */
92 public String getUpdateTime() {
97 * @return the list of forecasts
99 public ArrayList<SingleForecast> getForecasts() {
100 return _forecastList;