1 package tim.prune.function.weather;
3 import org.xml.sax.Attributes;
4 import org.xml.sax.SAXException;
5 import org.xml.sax.helpers.DefaultHandler;
9 * XML handler for dealing with the XML of current weather reports
10 * returned from openweathermap.org (forecasts have different structure)
12 public class OWMCurrentHandler extends DefaultHandler
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();
27 * React to the start of an XML tag
29 public void startElement(String inUri, String inLocalName, String inTagName,
30 Attributes inAttributes) throws SAXException
32 if (inTagName.equals("city")) {
33 _locName = inAttributes.getValue("name");
34 _locId = inAttributes.getValue("id");
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"));
40 else if (inTagName.equals("speed")) {
41 _forecast.setWindDesc(inAttributes.getValue("name"));
43 else if (inTagName.equals("temperature"))
45 String currTemp = inAttributes.getValue("value");
46 _forecast.setTemps(currTemp, currTemp);
47 // We can ignore the min and max here
49 else if (inTagName.equals("humidity")) {
50 _forecast.setHumidity(inAttributes.getValue("value") + inAttributes.getValue("unit"));
52 else if (inTagName.equals("lastupdate")) {
53 _updateTime = inAttributes.getValue("value");
55 else if (inTagName.equals("sun"))
57 _sunriseTime = inAttributes.getValue("rise");
58 _sunsetTime = inAttributes.getValue("set");
61 super.startElement(inUri, inLocalName, inTagName, inAttributes);
64 /** @return location name of forecast */
65 public String getLocationName() {
69 /** @return location id of forecast */
70 public String getLocationId() {
74 /** @return update time of report */
75 public String getUpdateTime() {
79 /** @return current weather conditions */
80 public SingleForecast getCurrentWeather() {
84 /** @return sunrise time as 2013-07-25T03:55:14 */
85 public String getSunriseTime() {
88 /** @return sunset time as 2013-07-25T19:07:25 */
89 public String getSunsetTime() {