4 |
|
//---IMPORTS--- |
5 |
|
import java.io.*; |
6 |
|
import java.util.ArrayList; |
7 |
– |
|
7 |
|
import org.xml.sax.*; |
8 |
< |
import javax.xml.parsers.SAXParserFactory; |
9 |
< |
import javax.xml.parsers.ParserConfigurationException; |
11 |
< |
import javax.xml.parsers.SAXParser; |
8 |
> |
import org.xml.sax.helpers.*; |
9 |
> |
import javax.xml.parsers.*; |
10 |
|
|
11 |
|
/** |
12 |
|
* XMLStringParser - Used to assist in creating XMLPacket objects. |
14 |
|
* @author $Author$ |
15 |
|
* @version $Id$ |
16 |
|
*/ |
17 |
< |
public class XMLStringParser extends HandlerBase { |
17 |
> |
public class XMLStringParser extends DefaultHandler { |
18 |
|
|
19 |
|
//---FINAL ATTRIBUTES--- |
20 |
|
|
31 |
|
* No-args constructor. Generally not used. |
32 |
|
*/ |
33 |
|
public XMLStringParser () { |
34 |
< |
this.packet = new XMLPacket(); |
34 |
> |
_packet = new XMLPacket(); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
|
* Constructor for accepting a reference to an XMLPacket |
39 |
|
*/ |
40 |
|
public XMLStringParser (XMLPacket packet) { |
41 |
< |
this.packet = packet; |
41 |
> |
_packet = packet; |
42 |
|
} |
43 |
|
|
44 |
|
//---PUBLIC METHODS--- |
45 |
|
|
48 |
– |
/** |
49 |
– |
* Accessor to the XMLPacket. |
50 |
– |
*/ |
51 |
– |
public XMLPacket getXMLPacket() { |
52 |
– |
return packet; |
53 |
– |
} |
54 |
– |
|
46 |
|
//=========================================================== |
47 |
|
// SAX DocumentHandler methods |
48 |
|
//=========================================================== |
49 |
|
|
50 |
< |
public void startDocument () throws SAXException { |
51 |
< |
//System.out.println("XMLPacketParser - Starting parse process..."); |
61 |
< |
} |
62 |
< |
|
63 |
< |
public void endDocument () throws SAXException { |
64 |
< |
//System.out.println("XMLPacketParser - I just finished parsing an XML String."); |
65 |
< |
} |
66 |
< |
|
67 |
< |
/** Add each tag's attribute to the XMLPacket. |
50 |
> |
/** |
51 |
> |
* Add each tag's attribute to the XMLPacket. |
52 |
|
* Note that all attributes within an opening tag are |
53 |
|
* stored as "someroot.sometag.attributes.attribute_name" |
54 |
|
* E.g. If <packet> is the root node, then: |
57 |
|
* "packet.attributes.machine_name" |
58 |
|
* within the XMLPacket. |
59 |
|
*/ |
60 |
< |
public void startElement (String name, AttributeList attrs) throws SAXException { |
61 |
< |
indentLevel++; |
62 |
< |
tagList.add(name); |
63 |
< |
if (attrs != null) { |
64 |
< |
for (int i = 0; i < attrs.getLength (); i++) { |
65 |
< |
packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
60 |
> |
public void startElement (String uri, String name, String qName, Attributes atts) { |
61 |
> |
_indentLevel++; |
62 |
> |
_tagList.add(name); |
63 |
> |
if (atts != null) { |
64 |
> |
for (int i = 0; i < atts.getLength (); i++) { |
65 |
> |
_packet.addParam(getPath()+".attributes."+atts.getLocalName(i), atts.getValue(i)); |
66 |
|
} |
67 |
|
} |
68 |
|
} |
69 |
< |
|
69 |
> |
|
70 |
|
/** |
71 |
|
* When an XML element is finished with, we must remove |
72 |
|
* the tag name from the tagList and decrement the indent |
73 |
|
* level. |
74 |
|
*/ |
75 |
< |
public void endElement (String name) throws SAXException { |
76 |
< |
tagList.remove(tagList.size()-1); |
77 |
< |
indentLevel--; |
75 |
> |
public void endElement (String uri, String name, String qName) { |
76 |
> |
_tagList.remove(_tagList.size() - 1); |
77 |
> |
_indentLevel--; |
78 |
|
} |
79 |
< |
|
79 |
> |
|
80 |
|
/** |
81 |
|
* Any text falling within a pair of terminal tags must |
82 |
|
* be added to the XMLPacket. Trim leading and trailing |
85 |
|
*/ |
86 |
|
public void characters (char[] buf, int offset, int len) throws SAXException { |
87 |
|
String s = new String(buf, offset, len); |
88 |
< |
packet.addParam(getPath(), s); |
88 |
> |
_packet.addParam(getPath(), s); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
115 |
|
* in the XMLPacket. |
116 |
|
*/ |
117 |
|
private String getPath () { |
118 |
< |
String path = (String)tagList.get(0); |
119 |
< |
if (tagList.size() > 0) { |
120 |
< |
for (int i = 1 ; i < tagList.size() ; i++) { |
121 |
< |
path = path + "." + (String)tagList.get(i); |
118 |
> |
String path = (String) _tagList.get(0); |
119 |
> |
if (_tagList.size() > 0) { |
120 |
> |
for (int i = 1 ; i < _tagList.size() ; i++) { |
121 |
> |
path = path + "." + (String) _tagList.get(i); |
122 |
|
} |
123 |
|
} |
124 |
|
return path; |
126 |
|
|
127 |
|
//---ACCESSOR/MUTATOR METHODS--- |
128 |
|
|
129 |
+ |
/** |
130 |
+ |
* Accessor to the XMLPacket. |
131 |
+ |
*/ |
132 |
+ |
public XMLPacket getXMLPacket() { |
133 |
+ |
return _packet; |
134 |
+ |
} |
135 |
+ |
|
136 |
|
//---ATTRIBUTES--- |
137 |
|
|
138 |
|
/** |
139 |
|
* To keep track of our identation level |
140 |
|
*/ |
141 |
< |
private int indentLevel = 0; |
141 |
> |
private int _indentLevel = 0; |
142 |
|
|
143 |
|
/** |
144 |
|
* An ArrayList of tags |
145 |
|
*/ |
146 |
< |
private ArrayList tagList = new ArrayList(); |
146 |
> |
private ArrayList _tagList = new ArrayList(); |
147 |
|
|
148 |
|
/** |
149 |
|
* A reference to the XMLPacket we are making |
150 |
|
*/ |
151 |
< |
private XMLPacket packet; |
151 |
> |
private XMLPacket _packet; |
152 |
|
|
153 |
|
/** |
154 |
|
* This is the friendly identifier of the |