1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import java.io.*; |
5 |
import java.util.ArrayList; |
6 |
|
7 |
import org.xml.sax.*; |
8 |
import javax.xml.parsers.SAXParserFactory; |
9 |
import javax.xml.parsers.ParserConfigurationException; |
10 |
import javax.xml.parsers.SAXParser; |
11 |
|
12 |
/** |
13 |
* XMLStreamParser - Used to build XMLPacket objects. |
14 |
* |
15 |
* @author $Author: $ |
16 |
* @version $Id: $ |
17 |
*/ |
18 |
public class XMLStreamParser extends HandlerBase { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public final String REVISION = "$Revision: $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
31 |
/** |
32 |
* Constructor for the XMLStreamParser. |
33 |
*/ |
34 |
public XMLStreamParser (XMLPacket packet) { |
35 |
_packet = packet; |
36 |
} |
37 |
|
38 |
//---PUBLIC METHODS--- |
39 |
|
40 |
|
41 |
public void startDocument () throws SAXException { |
42 |
//System.out.println("Packet found"); |
43 |
} |
44 |
|
45 |
public void endDocument () throws SAXException { |
46 |
//System.out.println("Packet Ended"); |
47 |
} |
48 |
|
49 |
/** |
50 |
* Add each tag's attribute to the XMLPacket. |
51 |
* Note that all attributes within an opening tag are |
52 |
* stored as "someroot.sometag.attributes.attribute_name" |
53 |
* E.g. If <packet> is the root node, then: |
54 |
* <packet machine_name="raptor"> |
55 |
* is stored as: |
56 |
* "packet.attributes.machine_name" |
57 |
* within the XMLPacket. |
58 |
*/ |
59 |
public void startElement (String name, AttributeList attrs) throws SAXException { |
60 |
_indentLevel++; |
61 |
_tagList.add(name); |
62 |
if (attrs != null) { |
63 |
for (int i = 0; i < attrs.getLength (); i++) { |
64 |
_packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
/** |
70 |
* When an XML element is finished with, we must remove |
71 |
* the tag name from the tagList and decrement the indent |
72 |
* level. |
73 |
*/ |
74 |
public void endElement (String name) throws SAXException { |
75 |
_tagList.remove(_tagList.size()-1); |
76 |
_indentLevel--; |
77 |
} |
78 |
|
79 |
/** |
80 |
* Any text falling within a pair of terminal tags must |
81 |
* be added to the XMLPacket. Trim leading and trailing |
82 |
* spaces and do not bother to add if there is no data |
83 |
* specified within the tags. |
84 |
*/ |
85 |
public void characters (char[] buf, int offset, int len) throws SAXException { |
86 |
String s = new String(buf, offset, len); |
87 |
if (!s.trim().equals("")) { |
88 |
_packet.addParam(getPath(), s); |
89 |
} |
90 |
} |
91 |
|
92 |
//---PRIVATE METHODS--- |
93 |
|
94 |
/** |
95 |
* Return the heirarchical string to be used as a key value |
96 |
* in the XMLPacket. |
97 |
*/ |
98 |
private String getPath () { |
99 |
String path = (String)_tagList.get(0); |
100 |
if (_tagList.size() > 0) { |
101 |
for (int i = 1 ; i < _tagList.size() ; i++) { |
102 |
path = path + "." + (String)_tagList.get(i); |
103 |
} |
104 |
} |
105 |
return path; |
106 |
} |
107 |
|
108 |
//---ACCESSOR/MUTATOR METHODS--- |
109 |
|
110 |
//---ATTRIBUTES--- |
111 |
|
112 |
private int _indentLevel = 0; |
113 |
private ArrayList _tagList = new ArrayList(); |
114 |
private XMLPacket _packet; |
115 |
|
116 |
//---STATIC ATTRIBUTES--- |
117 |
|
118 |
} |