/
ExampleHandler.java
177 lines (166 loc) · 4.92 KB
/
ExampleHandler.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package edu.hawaii.wattdroid;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
/**
* Handler for XML - this is called by SAX. This is used to handle and traverse/grep certain
* elements/content in an XML file.
*
*/
public class ExampleHandler extends DefaultHandler {
/**
* Switch fields - used to check location of xml
*/
private boolean in_outertag = false;
private boolean in_innertag = false;
private boolean name = false;
private boolean location = false;
private boolean description = false;
private boolean timestamp = false;
private boolean foundValue = false;
private boolean coords = false;
private boolean foundKey = false;
private boolean foundConsumedKey = false;
/**
* The parsed data.
*/
private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
/**
* Getter - parsed data.
*
* @return the extracted data
*/
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
/**
* Called when new XML is loaded.
*/
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}
/**
* Called when xml ends.
*/
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/**
* Called when opening tags like: <tag> provide attributes.
*
* @param namespaceURI - namespace
* @param localName - xml tag
* @param atts - attributes
*
*/
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
throws SAXException {
if (localName.equals("SourceIndex") || localName.equals("SensorData")) {
this.in_outertag = true;
}
else if (localName.equals("SourceRef")) {
this.in_innertag = true;
String attrValue = atts.getValue("Href");
myParsedExampleDataSet.setExtractedString(attrValue);
}
else if (localName.equals("Name")) {
name = true;
}
else if (localName.equals("Location")) {
location = true;
}
else if (localName.equals("Description")) {
description = true;
}
else if (localName.equals("Coordinates")) {
coords = true;
}
else if (localName.equals("Key")) {
Log.d("wattdroid", "I Found a Key");
foundKey = true;
}
else if (localName.equals("Value")) {
Log.d("wattdroid", "I Found a Value");
foundValue = true;
}
}
/**
* Called on closing tags like: </tag>.
*
* @param namespaceURI - namespace
* @param localName - xml tag
* @param atts = attributes
*/
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if (localName.equals("SourceIndex") || localName.equals("SensorData")) {
this.in_outertag = false;
}
else if (localName.equals("SourceRef")) {
this.in_innertag = false;
}
else if (localName.equals("Name")) {
name = false;
}
else if (localName.equals("Location")) {
location = false;
}
else if (localName.equals("Description")) {
description = false;
}
else if (localName.equals("Coordinates")) {
coords = false;
}
else if (localName.equals("Key")) {
foundKey = false;
Log.d("wattdroid", "I Dismissed a Key");
}
else if (localName.equals("Value")) {
foundValue = false;
Log.d("wattdroid", "I Dismissed a Value");
}
}
/**
* Called on the following structure: <tag>characters</tag>.
*
* @param ch - characters in tag
* @param start - where to start
* @param length - how many to copy
*/
@Override
public void characters(char ch[], int start, int length) {
String test = new String(ch, start, length);
if (in_innertag) {
myParsedExampleDataSet.setExtractedStringInnerContent(new String(ch, start, length));
}
else if (name) {
myParsedExampleDataSet.setName(new String(ch, start, length));
Log.d("wattdroid", "I set the name " + test);
}
else if (location) {
myParsedExampleDataSet.setLocation(new String(ch, start, length));
Log.d("wattdroid", "I set the location " + test);
}
else if (description) {
myParsedExampleDataSet.setDescription(new String(ch, start, length));
Log.d("wattdroid", "I set the desc " + test);
}
else if (coords) {
myParsedExampleDataSet.setCoords(new String(ch, start, length)); // set coords
Log.d("wattdroid", "I set the coords " + test);
}
else if (foundKey && test.equals("powerConsumed")) {
Log.d("wattdroid", "foundKey is True and powerConsumed is Current Tag");
foundConsumedKey = true;
}
else if (foundValue && foundConsumedKey) {
myParsedExampleDataSet.setTotalSensorData(new String(ch, start, length));
foundConsumedKey = false;
Log.d("wattdroid", "****I set the sensordata " + test);
}
}
}