9 |
|
|
10 |
|
|
11 |
|
public class XMLPacketMaker extends HandlerBase { |
12 |
+ |
|
13 |
+ |
public XMLPacketMaker () { |
14 |
+ |
// default no-args constructor. |
15 |
+ |
} |
16 |
+ |
|
17 |
+ |
// Constructor for accepting a reference to an XMLPacket |
18 |
+ |
public XMLPacketMaker (XMLPacket packet) { |
19 |
+ |
this.packet = packet; |
20 |
+ |
} |
21 |
+ |
|
22 |
|
public static void main(String[] args){ |
23 |
|
if (args.length != 1) { |
24 |
|
System.err.println ("Usage: cmd filename"); |
31 |
|
// Set up output stream |
32 |
|
out = new OutputStreamWriter (System.out, "UTF8"); |
33 |
|
|
34 |
+ |
// Create the XMLPacket to store values in. |
35 |
+ |
packet = new XMLPacket(); |
36 |
+ |
|
37 |
|
// Parse the input |
38 |
|
SAXParser saxParser = factory.newSAXParser(); |
39 |
< |
saxParser.parse(new File(args[0]), new XMLPacketMaker()); |
39 |
> |
saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); |
40 |
|
|
41 |
+ |
// Print out some things from the packet: - |
42 |
+ |
System.out.println("bung.wibble="+packet.getParam("bung.wibble")); |
43 |
+ |
System.out.println("bung.ping.value1="+packet.getParam("bung.ping.value1")); |
44 |
+ |
|
45 |
+ |
|
46 |
|
} catch (Throwable t) { |
47 |
|
t.printStackTrace (); |
48 |
|
} |
53 |
|
private String indentString = " "; // Amount to indent |
54 |
|
private int indentLevel = 0; |
55 |
|
|
56 |
+ |
// For storing the tag heirarchy. |
57 |
|
private ArrayList tagList = new ArrayList(); |
58 |
+ |
static private XMLPacket packet = null; |
59 |
|
|
60 |
|
//=========================================================== |
61 |
|
// SAX DocumentHandler methods |
81 |
|
|
82 |
|
public void startElement (String name, AttributeList attrs) throws SAXException { |
83 |
|
indentLevel++; |
84 |
+ |
tagList.add(name); |
85 |
|
nl(); emit ("ELEMENT: "); |
86 |
|
emit ("<"+name); |
87 |
|
if (attrs != null) { |
102 |
|
nl(); |
103 |
|
emit ("END_ELM: "); |
104 |
|
emit ("</"+name+">"); |
105 |
+ |
tagList.remove(tagList.size()-1); |
106 |
|
indentLevel--; |
107 |
|
} |
108 |
|
|
109 |
|
public void characters (char[] buf, int offset, int len) throws SAXException { |
110 |
|
nl(); emit ("CHARS: "); |
111 |
|
String s = new String(buf, offset, len); |
112 |
< |
if (!s.trim().equals("")) emit (s); |
112 |
> |
if (!s.trim().equals("")) { |
113 |
> |
emit (s); |
114 |
> |
packet.addParam(getPath(), s); |
115 |
> |
} |
116 |
|
} |
117 |
|
|
118 |
|
//=========================================================== |
140 |
|
} catch (IOException e) { |
141 |
|
throw new SAXException ("I/O error", e); |
142 |
|
} |
143 |
+ |
} |
144 |
+ |
|
145 |
+ |
private String getPath () { |
146 |
+ |
String path = (String)tagList.get(0); |
147 |
+ |
if (tagList.size() > 0) { |
148 |
+ |
for (int i = 1 ; i < tagList.size() ; i++) { |
149 |
+ |
path = path + "." + (String)tagList.get(i); |
150 |
+ |
} |
151 |
+ |
} |
152 |
+ |
return path; |
153 |
|
} |
154 |
|
} |