]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/OWMForecastHandler.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / weather / OWMForecastHandler.java
1 package tim.prune.function.weather;
2
3 import java.util.ArrayList;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.DefaultHandler;
8
9
10 /**
11  * XML handler for dealing with the XML of weather forecasts
12  * returned from openweathermap.org (current weather has different structure)
13  */
14 public class OWMForecastHandler extends DefaultHandler
15 {
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>();
25
26
27         /**
28          * React to the start of an XML tag
29          */
30         public void startElement(String inUri, String inLocalName, String inTagName,
31                 Attributes inAttributes) throws SAXException
32         {
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"));
37                 }
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"));
41                 }
42                 else if (inTagName.equals("windSpeed")) {
43                         _forecast.setWindDesc(inAttributes.getValue("name"));
44                 }
45                 else if (inTagName.equals("temperature")) {
46                         _forecast.setTemps(inAttributes.getValue("min"), inAttributes.getValue("max"));
47                 }
48                 else if (inTagName.equals("humidity")) {
49                         _forecast.setHumidity(inAttributes.getValue("value") + inAttributes.getValue("unit"));
50                 }
51                 _value = null;
52                 super.startElement(inUri, inLocalName, inTagName, inAttributes);
53         }
54
55         /**
56          * React to the end of an XML tag
57          */
58         public void endElement(String inUri, String inLocalName, String inTagName)
59         throws SAXException
60         {
61                 if (inTagName.equals("name")) {
62                         _locName = _value;
63                 }
64                 else if (inTagName.equals("lastupdate")) {
65                         _updateTime = _value;
66                 }
67                 else if (inTagName.equals("time"))
68                 {
69                         // End of a time tag, add the current forecast to the list
70                         _forecastList.add(_forecast);
71                 }
72                 super.endElement(inUri, inLocalName, inTagName);
73         }
74
75         /**
76          * React to characters received inside tags
77          */
78         public void characters(char[] inCh, int inStart, int inLength)
79         throws SAXException
80         {
81                 String value = new String(inCh, inStart, inLength);
82                 _value = (_value==null?value:_value+value);
83                 super.characters(inCh, inStart, inLength);
84         }
85
86         /** @return location name of forecast */
87         public String getLocationName() {
88                 return _locName;
89         }
90
91         /** @return update time of forecast */
92         public String getUpdateTime() {
93                 return _updateTime;
94         }
95
96         /**
97          * @return the list of forecasts
98          */
99         public ArrayList<SingleForecast> getForecasts() {
100                 return _forecastList;
101         }
102 }