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 |
– |
// By means of example, print out some things from the packet: - |
44 |
– |
System.out.println("bung.wibble="+packet.getParam("bung.wibble")); |
45 |
– |
System.out.println("bung.ping.value1="+packet.getParam("bung.ping.value1")); |
46 |
– |
|
47 |
– |
// Print out the entire contents of the packet. |
48 |
– |
packet.printAll(); |
49 |
– |
|
50 |
– |
} catch (Throwable t) { |
51 |
– |
t.printStackTrace (); |
39 |
|
} |
40 |
< |
System.exit (0); |
41 |
< |
} |
42 |
< |
|
43 |
< |
static private Writer out; |
57 |
< |
private String indentString = " "; // Amount to indent |
58 |
< |
private int indentLevel = 0; |
59 |
< |
|
60 |
< |
// For storing the tag heirarchy. |
61 |
< |
private ArrayList tagList = new ArrayList(); |
62 |
< |
static private XMLPacket packet = null; |
63 |
< |
|
64 |
< |
//=========================================================== |
65 |
< |
// SAX DocumentHandler methods |
66 |
< |
//=========================================================== |
67 |
< |
|
68 |
< |
public void startDocument () throws SAXException { |
69 |
< |
nl(); |
70 |
< |
nl(); |
71 |
< |
emit ("START DOCUMENT"); |
72 |
< |
nl(); |
73 |
< |
emit ("<?xml version='1.0' encoding='UTF-8'?>"); |
74 |
< |
} |
75 |
< |
|
76 |
< |
public void endDocument () throws SAXException { |
77 |
< |
nl(); emit ("END DOCUMENT"); |
78 |
< |
try { |
79 |
< |
nl(); |
80 |
< |
out.flush (); |
81 |
< |
} catch (IOException e) { |
82 |
< |
throw new SAXException ("I/O error", e); |
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 |
|
} |
85 |
– |
|
86 |
– |
public void startElement (String name, AttributeList attrs) throws SAXException { |
87 |
– |
indentLevel++; |
88 |
– |
tagList.add(name); |
89 |
– |
nl(); emit ("ELEMENT: "); |
90 |
– |
emit ("<"+name); |
91 |
– |
if (attrs != null) { |
92 |
– |
for (int i = 0; i < attrs.getLength (); i++) { |
93 |
– |
nl(); |
94 |
– |
emit(" ATTR: "); |
95 |
– |
emit (attrs.getName (i)); |
96 |
– |
emit ("\t\""); |
97 |
– |
emit (attrs.getValue (i)); |
98 |
– |
emit ("\""); |
99 |
– |
} |
100 |
– |
} |
101 |
– |
if (attrs.getLength() > 0) nl(); |
102 |
– |
emit (">"); |
103 |
– |
} |
104 |
– |
|
105 |
– |
public void endElement (String name) throws SAXException { |
106 |
– |
nl(); |
107 |
– |
emit ("END_ELM: "); |
108 |
– |
emit ("</"+name+">"); |
109 |
– |
tagList.remove(tagList.size()-1); |
110 |
– |
indentLevel--; |
111 |
– |
} |
112 |
– |
|
113 |
– |
public void characters (char[] buf, int offset, int len) throws SAXException { |
114 |
– |
nl(); emit ("CHARS: "); |
115 |
– |
String s = new String(buf, offset, len); |
116 |
– |
if (!s.trim().equals("")) { |
117 |
– |
emit (s); |
118 |
– |
packet.addParam(getPath(), s); |
119 |
– |
} |
120 |
– |
} |
121 |
– |
|
122 |
– |
//=========================================================== |
123 |
– |
// Helpers ... |
124 |
– |
//=========================================================== |
125 |
– |
|
126 |
– |
// Wrap I/O exceptions in SAX exceptions, to |
127 |
– |
// suit handler signature requirements |
128 |
– |
private void emit (String s) throws SAXException { |
129 |
– |
try { |
130 |
– |
out.write (s); |
131 |
– |
out.flush (); |
132 |
– |
} catch (IOException e) { |
133 |
– |
throw new SAXException ("I/O error", e); |
134 |
– |
} |
135 |
– |
} |
136 |
– |
|
137 |
– |
// Start a new line |
138 |
– |
// and indent the next line appropriately |
139 |
– |
private void nl () throws SAXException { |
140 |
– |
String lineEnd = System.getProperty("line.separator"); |
141 |
– |
try { |
142 |
– |
out.write (lineEnd); |
143 |
– |
for (int i=0; i < indentLevel; i++) out.write(indentString); |
144 |
– |
} catch (IOException e) { |
145 |
– |
throw new SAXException ("I/O error", e); |
146 |
– |
} |
147 |
– |
} |
48 |
|
|
49 |
< |
// Return the heirarchical string to be used as a key value |
150 |
< |
// in the XMLPacket. |
151 |
< |
private String getPath () { |
152 |
< |
String path = (String)tagList.get(0); |
153 |
< |
if (tagList.size() > 0) { |
154 |
< |
for (int i = 1 ; i < tagList.size() ; i++) { |
155 |
< |
path = path + "." + (String)tagList.get(i); |
156 |
< |
} |
157 |
< |
} |
158 |
< |
return path; |
159 |
< |
} |
49 |
> |
String xml; |
50 |
|
} |