File: OWMCurrentHandler.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (92 lines) | stat: -rw-r--r-- 2,594 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package tim.prune.function.weather;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * XML handler for dealing with the XML of current weather reports
 * returned from openweathermap.org (forecasts have different structure)
 */
public class OWMCurrentHandler extends DefaultHandler
{
	/** The location name */
	private String _locName = null;
	/** The location id */
	private String _locId = null;
	/** The last update time */
	private String _updateTime = null;
	/** Sunrise and sunset times */
	private String _sunriseTime = null, _sunsetTime = null;
	/** The currently open forecast */
	private SingleForecast _forecast = new SingleForecast();


	/**
	 * React to the start of an XML tag
	 */
	public void startElement(String inUri, String inLocalName, String inTagName,
		Attributes inAttributes) throws SAXException
	{
		if (inTagName.equals("city")) {
			_locName = inAttributes.getValue("name");
			_locId   = inAttributes.getValue("id");
		}
		else if (inTagName.equals("weather")) {
			// numeric code, owm image name, description
			_forecast.setSymbol(inAttributes.getValue("number"), inAttributes.getValue("icon"), inAttributes.getValue("value"));
		}
		else if (inTagName.equals("speed")) {
			_forecast.setWindDesc(inAttributes.getValue("name"));
		}
		else if (inTagName.equals("temperature"))
		{
			String currTemp = inAttributes.getValue("value");
			_forecast.setTemps(currTemp, currTemp);
			// We can ignore the min and max here
		}
		else if (inTagName.equals("humidity")) {
			_forecast.setHumidity(inAttributes.getValue("value") + inAttributes.getValue("unit"));
		}
		else if (inTagName.equals("lastupdate")) {
			_updateTime = inAttributes.getValue("value");
		}
		else if (inTagName.equals("sun"))
		{
			_sunriseTime = inAttributes.getValue("rise");
			_sunsetTime  = inAttributes.getValue("set");
		}

		super.startElement(inUri, inLocalName, inTagName, inAttributes);
	}

	/** @return location name of forecast */
	public String getLocationName() {
		return _locName;
	}

	/** @return location id of forecast */
	public String getLocationId() {
		return _locId;
	}

	/** @return update time of report */
	public String getUpdateTime() {
		return _updateTime;
	}

	/** @return current weather conditions */
	public SingleForecast getCurrentWeather() {
		return _forecast;
	}

	/** @return sunrise time as 2013-07-25T03:55:14 */
	public String getSunriseTime() {
		return _sunriseTime;
	}
	/** @return sunset time as 2013-07-25T19:07:25 */
	public String getSunsetTime() {
		return _sunsetTime;
	}
}