12 |
|
// XMLPacketMaker - Creates an XMLPacket object. |
13 |
|
public class XMLPacketMaker extends HandlerBase { |
14 |
|
|
15 |
+ |
// No-args constructor. Generally not used. |
16 |
|
public XMLPacketMaker () { |
17 |
< |
// default no-args constructor. |
17 |
> |
this.xml = "<packet></packet>"; |
18 |
|
} |
19 |
|
|
20 |
< |
// Constructor for accepting a reference to an XMLPacket |
21 |
< |
public XMLPacketMaker (XMLPacket packet) { |
22 |
< |
this.packet = packet; |
20 |
> |
// Constructor for accepting XML input. |
21 |
> |
public XMLPacketMaker (String xml) { |
22 |
> |
this.xml = xml; |
23 |
|
} |
24 |
|
|
25 |
< |
public static void main(String[] args){ |
25 |
< |
if (args.length != 1) { |
26 |
< |
System.err.println ("Usage: cmd filename"); |
27 |
< |
System.exit (1); |
28 |
< |
} |
25 |
> |
public XMLPacket createXMLPacket() { |
26 |
|
|
27 |
+ |
// Create the XMLPacket to store values in. |
28 |
+ |
XMLPacket packet = new XMLPacket(); |
29 |
+ |
|
30 |
|
// Use the default (non-validating) parser |
31 |
|
SAXParserFactory factory = SAXParserFactory.newInstance(); |
32 |
|
try { |
33 |
– |
// Set up output stream |
34 |
– |
out = new OutputStreamWriter (System.out, "UTF8"); |
33 |
|
|
36 |
– |
// Create the XMLPacket to store values in. |
37 |
– |
packet = new XMLPacket(); |
38 |
– |
|
34 |
|
// Parse the input |
35 |
+ |
InputSource inputSource = new InputSource(new StringReader(xml)); |
36 |
|
SAXParser saxParser = factory.newSAXParser(); |
37 |
< |
saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); |
37 |
> |
saxParser.parse(inputSource, new XMLPacketParser(packet)); |
38 |
|
|
43 |
– |
// Print out the entire contents of the packet's HashMap. |
44 |
– |
System.out.println("XMLPacket contents: -"); |
45 |
– |
packet.printAll(); |
46 |
– |
|
47 |
– |
} catch (Throwable t) { |
48 |
– |
System.out.println("The recieved packet did not contain valid XML, so I'm gonna reject it."); |
49 |
– |
//t.printStackTrace (); |
39 |
|
} |
40 |
< |
System.exit (0); |
41 |
< |
} |
42 |
< |
|
43 |
< |
static private Writer out; |
55 |
< |
private String indentString = " "; // Amount to indent |
56 |
< |
private int indentLevel = 0; |
57 |
< |
|
58 |
< |
// For storing the tag heirarchy. |
59 |
< |
private ArrayList tagList = new ArrayList(); |
60 |
< |
static private XMLPacket packet = null; |
61 |
< |
|
62 |
< |
//=========================================================== |
63 |
< |
// SAX DocumentHandler methods |
64 |
< |
//=========================================================== |
65 |
< |
|
66 |
< |
public void startDocument () throws SAXException { |
67 |
< |
// No purpose currently. |
68 |
< |
} |
69 |
< |
|
70 |
< |
public void endDocument () throws SAXException { |
71 |
< |
// No purpose currently. |
72 |
< |
} |
73 |
< |
|
74 |
< |
// Add each tag's attribute to the XMLPacket. |
75 |
< |
// Note that all attributes within an opening tag are |
76 |
< |
// stored as "someroot.sometag.attributes.attribute_name" |
77 |
< |
// E.g. If <packet> is the root node, then: |
78 |
< |
// <packet machine_name="raptor"> |
79 |
< |
// is stored as: |
80 |
< |
// "packet.attributes.machine_name" |
81 |
< |
// within the XMLPacket. |
82 |
< |
public void startElement (String name, AttributeList attrs) throws SAXException { |
83 |
< |
indentLevel++; |
84 |
< |
tagList.add(name); |
85 |
< |
if (attrs != null) { |
86 |
< |
for (int i = 0; i < attrs.getLength (); i++) { |
87 |
< |
packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
88 |
< |
} |
40 |
> |
catch (Exception e) { |
41 |
> |
System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML."); |
42 |
> |
//t.printStackTrace(); |
43 |
> |
return null; |
44 |
|
} |
45 |
+ |
|
46 |
+ |
return packet; |
47 |
|
} |
91 |
– |
|
92 |
– |
// When an XML element is finished with, we must remove |
93 |
– |
// the tag name from the tagList and decrement the indent |
94 |
– |
// level. |
95 |
– |
public void endElement (String name) throws SAXException { |
96 |
– |
tagList.remove(tagList.size()-1); |
97 |
– |
indentLevel--; |
98 |
– |
} |
99 |
– |
|
100 |
– |
// Any text falling within a pair of terminal tags must |
101 |
– |
// be added to the XMLPacket. Trim leading and trailing |
102 |
– |
// spaces and do not bother to add if there is no data |
103 |
– |
// specified within the tags. |
104 |
– |
public void characters (char[] buf, int offset, int len) throws SAXException { |
105 |
– |
String s = new String(buf, offset, len); |
106 |
– |
if (!s.trim().equals("")) { |
107 |
– |
packet.addParam(getPath(), s); |
108 |
– |
} |
109 |
– |
} |
110 |
– |
|
111 |
– |
|
112 |
– |
//=========================================================== |
113 |
– |
// Helpers ... |
114 |
– |
//=========================================================== |
48 |
|
|
49 |
< |
|
117 |
< |
// Return the heirarchical string to be used as a key value |
118 |
< |
// in the XMLPacket. |
119 |
< |
private String getPath () { |
120 |
< |
String path = (String)tagList.get(0); |
121 |
< |
if (tagList.size() > 0) { |
122 |
< |
for (int i = 1 ; i < tagList.size() ; i++) { |
123 |
< |
path = path + "." + (String)tagList.get(i); |
124 |
< |
} |
125 |
< |
} |
126 |
< |
return path; |
127 |
< |
} |
128 |
< |
|
49 |
> |
String xml; |
50 |
|
} |