1 | package org.tipple.mapDisplay;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 |
|
---|
6 | import org.xml.sax.Attributes;
|
---|
7 | import org.xml.sax.SAXException;
|
---|
8 | import org.xml.sax.helpers.DefaultHandler;
|
---|
9 |
|
---|
10 | public class xmlHandler extends DefaultHandler {
|
---|
11 | String lockon_element;
|
---|
12 | Boolean locked_on_;
|
---|
13 | ArrayList<HashMap<String, String>> lockon_list;
|
---|
14 | HashMap<String, String> hashmap;
|
---|
15 | String active_element;
|
---|
16 | StringBuffer inner_text;
|
---|
17 | String active_attr;
|
---|
18 | Boolean recordElement;
|
---|
19 | String elementType;
|
---|
20 |
|
---|
21 | // ===========================================================
|
---|
22 | // Getter & Setter
|
---|
23 | // ===========================================================
|
---|
24 | public xmlHandler(String lockon_element) {
|
---|
25 | // initial settings to parse a file
|
---|
26 | this.lockon_element = lockon_element;
|
---|
27 | lockon_list = new ArrayList<HashMap<String, String>>();
|
---|
28 | locked_on_ = false;
|
---|
29 | recordElement = false;
|
---|
30 | elementType = "";
|
---|
31 | hashmap = new HashMap<String, String>();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public ArrayList<HashMap<String, String>> getHashmapList() {
|
---|
35 | return lockon_list;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Gets be called on opening tags like: <tag> Can provide attribute(s), when
|
---|
40 | * xml was like: <tag attribute="attributeValue">
|
---|
41 | */
|
---|
42 |
|
---|
43 | public void startElement(String namespaceURI, String localName,
|
---|
44 | String qName, Attributes atts) throws SAXException {
|
---|
45 |
|
---|
46 | if ((localName.equalsIgnoreCase(lockon_element))
|
---|
47 | && (atts.getValue("nodeType")).equalsIgnoreCase("leaf")) {
|
---|
48 |
|
---|
49 | hashmap = new HashMap<String, String>();
|
---|
50 | locked_on_ = true; // => covert each child element inside this to a
|
---|
51 | String attrValueNode = atts.getValue("nodeID");
|
---|
52 | hashmap.put("nodeID", attrValueNode);
|
---|
53 |
|
---|
54 | } else if (locked_on_) {
|
---|
55 | if (localName.equalsIgnoreCase("metadata")
|
---|
56 | || localName.equalsIgnoreCase("nodeContent")) {
|
---|
57 | recordElement = true;
|
---|
58 | inner_text = new StringBuffer();
|
---|
59 | if (localName.equalsIgnoreCase("metadata")) {
|
---|
60 | active_element = atts.getValue("name");
|
---|
61 |
|
---|
62 | } else if (localName.equalsIgnoreCase("nodeContent")) {
|
---|
63 | active_element = "text";
|
---|
64 |
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|
69 | // else ignore element as outside area of interest
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Gets be called on closing tags like: </tag>
|
---|
74 | */
|
---|
75 | public void endElement(String namespaceURI, String localName, String qName)
|
---|
76 | throws SAXException {
|
---|
77 | if (localName.equalsIgnoreCase(lockon_element)) {
|
---|
78 | // push hashmap onto arraylist
|
---|
79 | lockon_list.add(hashmap);
|
---|
80 | hashmap = null;
|
---|
81 | locked_on_ = false;
|
---|
82 | } else if ((localName.equalsIgnoreCase("metadata"))
|
---|
83 | || (localName.equalsIgnoreCase("nodeContent"))) {
|
---|
84 | active_element = null;
|
---|
85 | inner_text = null;
|
---|
86 | recordElement = false;
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Gets be called on the following structure: <tag>characters</tag>
|
---|
94 | */
|
---|
95 |
|
---|
96 | public void characters(char ch[], int start, int length) {
|
---|
97 | if (this.recordElement) {
|
---|
98 | String text = new String(ch, start, length);
|
---|
99 | inner_text.append(text);
|
---|
100 | hashmap.put(active_element, inner_text.toString());
|
---|
101 |
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|