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 |
+ |
|
36 |
+ |
xml.trim(); |
37 |
+ |
|
38 |
+ |
//debug stuff |
39 |
+ |
byte[] temp = xml.getBytes(); |
40 |
+ |
System.out.println("String length: "+temp.length+" "); |
41 |
+ |
for (int i = 0 ; i < temp.length ; i++){ |
42 |
+ |
System.out.print((char)temp[i]); |
43 |
+ |
} |
44 |
+ |
|
45 |
+ |
InputSource inputSource = new InputSource(new StringReader(xml)); |
46 |
|
SAXParser saxParser = factory.newSAXParser(); |
47 |
< |
saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); |
47 |
> |
saxParser.parse(inputSource, new XMLPacketParser(packet)); |
48 |
|
|
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 |
– |
t.printStackTrace (); |
49 |
|
} |
50 |
< |
System.exit (0); |
51 |
< |
} |
52 |
< |
|
53 |
< |
static private Writer out; |
54 |
< |
private String indentString = " "; // Amount to indent |
55 |
< |
private int indentLevel = 0; |
56 |
< |
|
57 |
< |
// For storing the tag heirarchy. |
58 |
< |
private ArrayList tagList = new ArrayList(); |
59 |
< |
static private XMLPacket packet = null; |
60 |
< |
|
61 |
< |
//=========================================================== |
62 |
< |
// SAX DocumentHandler methods |
63 |
< |
//=========================================================== |
64 |
< |
|
65 |
< |
public void startDocument () throws SAXException { |
66 |
< |
// No purpose currently. |
67 |
< |
} |
68 |
< |
|
69 |
< |
public void endDocument () throws SAXException { |
70 |
< |
// No purpose currently. |
71 |
< |
} |
72 |
< |
|
73 |
< |
// Add each tag's attribute to the XMLPacket. |
74 |
< |
// Note that all attributes within an opening tag are |
75 |
< |
// stored as "someroot.sometag.attributes.attribute_name" |
76 |
< |
// E.g. If <packet> is the root node, then: |
77 |
< |
// <packet machine_name="raptor"> |
78 |
< |
// is stored as: |
79 |
< |
// "packet.attributes.machine_name" |
80 |
< |
// within the XMLPacket. |
81 |
< |
public void startElement (String name, AttributeList attrs) throws SAXException { |
82 |
< |
indentLevel++; |
83 |
< |
tagList.add(name); |
84 |
< |
if (attrs != null) { |
85 |
< |
for (int i = 0; i < attrs.getLength (); i++) { |
86 |
< |
packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
87 |
< |
} |
50 |
> |
//catch (IOException e){ |
51 |
> |
// System.out.println("IOException: "+e); |
52 |
> |
//} |
53 |
> |
catch (Throwable t) { |
54 |
> |
System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML."); |
55 |
> |
//System.out.println(e); |
56 |
> |
t.printStackTrace(); |
57 |
> |
return null; |
58 |
|
} |
59 |
+ |
|
60 |
+ |
return packet; |
61 |
|
} |
90 |
– |
|
91 |
– |
// When an XML element is finished with, we must remove |
92 |
– |
// the tag name from the tagList and decrement the indent |
93 |
– |
// level. |
94 |
– |
public void endElement (String name) throws SAXException { |
95 |
– |
tagList.remove(tagList.size()-1); |
96 |
– |
indentLevel--; |
97 |
– |
} |
98 |
– |
|
99 |
– |
// Any text falling within a pair of terminal tags must |
100 |
– |
// be added to the XMLPacket. Trim leading and trailing |
101 |
– |
// spaces and do not bother to add if there is no data |
102 |
– |
// specified within the tags. |
103 |
– |
public void characters (char[] buf, int offset, int len) throws SAXException { |
104 |
– |
String s = new String(buf, offset, len); |
105 |
– |
if (!s.trim().equals("")) { |
106 |
– |
packet.addParam(getPath(), s); |
107 |
– |
} |
108 |
– |
} |
109 |
– |
|
110 |
– |
|
111 |
– |
//=========================================================== |
112 |
– |
// Helpers ... |
113 |
– |
//=========================================================== |
62 |
|
|
63 |
< |
|
116 |
< |
// Return the heirarchical string to be used as a key value |
117 |
< |
// in the XMLPacket. |
118 |
< |
private String getPath () { |
119 |
< |
String path = (String)tagList.get(0); |
120 |
< |
if (tagList.size() > 0) { |
121 |
< |
for (int i = 1 ; i < tagList.size() ; i++) { |
122 |
< |
path = path + "." + (String)tagList.get(i); |
123 |
< |
} |
124 |
< |
} |
125 |
< |
return path; |
126 |
< |
} |
127 |
< |
|
63 |
> |
String xml; |
64 |
|
} |