summaryrefslogtreecommitdiffstats
blob: 28cde785b90503cf415ffe11f791a02c4a9336e8 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (c) 2013 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.parser.GenericPayloadParser;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.elements.UserLocation;
import com.isode.stroke.base.NotNull;
import com.isode.stroke.base.DateTime;

public class UserLocationParser extends GenericPayloadParser<UserLocation> {

	private int level = 0;
	private String currentText = "";

	public UserLocationParser() {
		super(new UserLocation());
	}

	/**
	* @param element, NotNull.
	* @param ns.
	* @param attributes.
	*/
	@Override
	public void handleStartElement(String element, String ns, AttributeMap attributes) {
		if (level == 1) {
			currentText = "";
		}
		++level;
	}

	/**
	* @param element, NotNull.
	* @param ns.
	*/
	@Override
	public void handleEndElement(String element, String ns) {
		NotNull.exceptIfNull(element, "element");
		--level;
		if (level == 1) {
			try {
				if (element.equals("accuracy")) {
					getPayloadInternal().setAccuracy(Float.parseFloat(currentText));
				}
				else if (element.equals("alt")) {
					getPayloadInternal().setAltitude(Float.parseFloat(currentText));
				}
				else if (element.equals("area")) {
					getPayloadInternal().setArea(currentText);
				}
				else if (element.equals("bearing")) {
					getPayloadInternal().setBearing(Float.parseFloat(currentText));
				}
				else if (element.equals("building")) {
					getPayloadInternal().setBuilding(currentText);
				}
				else if (element.equals("country")) {
					getPayloadInternal().setCountry(currentText);
				}
				else if (element.equals("countrycode")) {
					getPayloadInternal().setCountryCode(currentText);
				}
				else if (element.equals("datum")) {
					getPayloadInternal().setDatum(currentText);
				}
				else if (element.equals("description")) {
					getPayloadInternal().setDescription(currentText);
				}
				else if (element.equals("error")) {
					getPayloadInternal().setError(Float.parseFloat(currentText));
				}
				else if (element.equals("floor")) {
					getPayloadInternal().setFloor(currentText);
				}
				else if (element.equals("lat")) {
					getPayloadInternal().setLatitude(Float.parseFloat(currentText));
				}
				else if (element.equals("locality")) {
					getPayloadInternal().setLocality(currentText);
				}
				else if (element.equals("lon")) {
					getPayloadInternal().setLongitude(Float.parseFloat(currentText));
				}
				else if (element.equals("postalcode")) {
					getPayloadInternal().setPostalCode(currentText);
				}
				else if (element.equals("region")) {
					getPayloadInternal().setRegion(currentText);
				}
				else if (element.equals("room")) {
					getPayloadInternal().setRoom(currentText);
				}
				else if (element.equals("speed")) {
					getPayloadInternal().setSpeed(Float.parseFloat(currentText));
				}
				else if (element.equals("street")) {
					getPayloadInternal().setStreet(currentText);
				}
				else if (element.equals("text")) {
					getPayloadInternal().setText(currentText);
				}
				else if (element.equals("timestamp")) {
					getPayloadInternal().setTimestamp(DateTime.stringToDate(currentText));
				}
				else if (element.equals("uri")) {
					getPayloadInternal().setURI(currentText);
				}
			}
			catch (NumberFormatException e) {

			}
		}
	}

	/**
	* @param data, NotNull.
	*/
	@Override
	public void handleCharacterData(String data) {
		NotNull.exceptIfNull(data, "data");
		currentText += data;
	}
}