]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/weather/OWMCurrentHandler.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / function / weather / OWMCurrentHandler.java
1 package tim.prune.function.weather;
2
3 import org.xml.sax.Attributes;
4 import org.xml.sax.SAXException;
5 import org.xml.sax.helpers.DefaultHandler;
6
7
8 /**
9  * XML handler for dealing with the XML of current weather reports
10  * returned from openweathermap.org (forecasts have different structure)
11  */
12 public class OWMCurrentHandler extends DefaultHandler
13 {
14         /** The location name */
15         private String _locName = null;
16         /** The location id */
17         private String _locId = null;
18         /** The last update time */
19         private String _updateTime = null;
20         /** Sunrise and sunset times */
21         private String _sunriseTime = null, _sunsetTime = null;
22         /** The currently open forecast */
23         private SingleForecast _forecast = new SingleForecast();
24
25
26         /**
27          * React to the start of an XML tag
28          */
29         public void startElement(String inUri, String inLocalName, String inTagName,
30                 Attributes inAttributes) throws SAXException
31         {
32                 if (inTagName.equals("city")) {
33                         _locName = inAttributes.getValue("name");
34                         _locId   = inAttributes.getValue("id");
35                 }
36                 else if (inTagName.equals("weather")) {
37                         // numeric code, owm image name, description
38                         _forecast.setSymbol(inAttributes.getValue("number"), inAttributes.getValue("icon"), inAttributes.getValue("value"));
39                 }
40                 else if (inTagName.equals("speed")) {
41                         _forecast.setWindDesc(inAttributes.getValue("name"));
42                 }
43                 else if (inTagName.equals("temperature"))
44                 {
45                         String currTemp = inAttributes.getValue("value");
46                         _forecast.setTemps(currTemp, currTemp);
47                         // We can ignore the min and max here
48                 }
49                 else if (inTagName.equals("humidity")) {
50                         _forecast.setHumidity(inAttributes.getValue("value") + inAttributes.getValue("unit"));
51                 }
52                 else if (inTagName.equals("lastupdate")) {
53                         _updateTime = inAttributes.getValue("value");
54                 }
55                 else if (inTagName.equals("sun"))
56                 {
57                         _sunriseTime = inAttributes.getValue("rise");
58                         _sunsetTime  = inAttributes.getValue("set");
59                 }
60
61                 super.startElement(inUri, inLocalName, inTagName, inAttributes);
62         }
63
64         /** @return location name of forecast */
65         public String getLocationName() {
66                 return _locName;
67         }
68
69         /** @return location id of forecast */
70         public String getLocationId() {
71                 return _locId;
72         }
73
74         /** @return update time of report */
75         public String getUpdateTime() {
76                 return _updateTime;
77         }
78
79         /** @return current weather conditions */
80         public SingleForecast getCurrentWeather() {
81                 return _forecast;
82         }
83
84         /** @return sunrise time as 2013-07-25T03:55:14 */
85         public String getSunriseTime() {
86                 return _sunriseTime;
87         }
88         /** @return sunset time as 2013-07-25T19:07:25 */
89         public String getSunsetTime() {
90                 return _sunsetTime;
91         }
92 }