1 |
pjm2 |
1.1 |
import java.io.*; |
2 |
|
|
import java.util.ArrayList; |
3 |
|
|
|
4 |
|
|
import org.xml.sax.*; |
5 |
|
|
|
6 |
|
|
import javax.xml.parsers.SAXParserFactory; |
7 |
|
|
import javax.xml.parsers.ParserConfigurationException; |
8 |
|
|
import javax.xml.parsers.SAXParser; |
9 |
|
|
|
10 |
|
|
// Paul Mutton, pjm2@ukc.ac.uk |
11 |
|
|
|
12 |
|
|
// XMLPacketParser - Creates an XMLPacket object. |
13 |
|
|
public class XMLPacketParser extends HandlerBase { |
14 |
|
|
|
15 |
|
|
// No-args constructor. Generally not used. |
16 |
|
|
public XMLPacketParser () { |
17 |
|
|
this.packet = new XMLPacket(); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
// Constructor for accepting a reference to an XMLPacket |
21 |
|
|
public XMLPacketParser (XMLPacket packet) { |
22 |
|
|
this.packet = packet; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
// Accessor to the XMLPacket. |
26 |
|
|
public XMLPacket getXMLPacket() { |
27 |
|
|
return packet; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
private int indentLevel = 0; |
31 |
|
|
private ArrayList tagList = new ArrayList(); |
32 |
|
|
private XMLPacket packet; |
33 |
|
|
|
34 |
|
|
//=========================================================== |
35 |
|
|
// SAX DocumentHandler methods |
36 |
|
|
//=========================================================== |
37 |
|
|
|
38 |
|
|
public void startDocument () throws SAXException { |
39 |
pjm2 |
1.2 |
System.out.println("XMLPacketParser - Starting parse process..."); |
40 |
pjm2 |
1.1 |
} |
41 |
|
|
|
42 |
|
|
public void endDocument () throws SAXException { |
43 |
pjm2 |
1.2 |
System.out.println("XMLPacketParser - I just finished parsing an XML String."); |
44 |
pjm2 |
1.1 |
} |
45 |
|
|
|
46 |
|
|
// Add each tag's attribute to the XMLPacket. |
47 |
|
|
// Note that all attributes within an opening tag are |
48 |
|
|
// stored as "someroot.sometag.attributes.attribute_name" |
49 |
|
|
// E.g. If <packet> is the root node, then: |
50 |
|
|
// <packet machine_name="raptor"> |
51 |
|
|
// is stored as: |
52 |
|
|
// "packet.attributes.machine_name" |
53 |
|
|
// within the XMLPacket. |
54 |
|
|
public void startElement (String name, AttributeList attrs) throws SAXException { |
55 |
|
|
indentLevel++; |
56 |
|
|
tagList.add(name); |
57 |
|
|
if (attrs != null) { |
58 |
|
|
for (int i = 0; i < attrs.getLength (); i++) { |
59 |
|
|
packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
// When an XML element is finished with, we must remove |
65 |
|
|
// the tag name from the tagList and decrement the indent |
66 |
|
|
// level. |
67 |
|
|
public void endElement (String name) throws SAXException { |
68 |
|
|
tagList.remove(tagList.size()-1); |
69 |
|
|
indentLevel--; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
// Any text falling within a pair of terminal tags must |
73 |
|
|
// be added to the XMLPacket. Trim leading and trailing |
74 |
|
|
// spaces and do not bother to add if there is no data |
75 |
|
|
// specified within the tags. |
76 |
|
|
public void characters (char[] buf, int offset, int len) throws SAXException { |
77 |
|
|
String s = new String(buf, offset, len); |
78 |
|
|
if (!s.trim().equals("")) { |
79 |
|
|
packet.addParam(getPath(), s); |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
//=========================================================== |
85 |
|
|
// Helpers ... |
86 |
|
|
//=========================================================== |
87 |
|
|
|
88 |
|
|
|
89 |
|
|
// Return the heirarchical string to be used as a key value |
90 |
|
|
// in the XMLPacket. |
91 |
|
|
private String getPath () { |
92 |
|
|
String path = (String)tagList.get(0); |
93 |
|
|
if (tagList.size() > 0) { |
94 |
|
|
for (int i = 1 ; i < tagList.size() ; i++) { |
95 |
|
|
path = path + "." + (String)tagList.get(i); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
return path; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
} |