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; |
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 |
+ |
// 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){ |
23 |
< |
if (args.length != 1) { |
24 |
< |
System.err.println ("Usage: cmd filename"); |
25 |
< |
System.exit (1); |
26 |
< |
} |
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 { |
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 |
– |
|
34 |
|
// Parse the input |
35 |
|
SAXParser saxParser = factory.newSAXParser(); |
36 |
< |
saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); |
36 |
> |
saxParser.parse(xml, new XMLPacketParser(packet)); |
37 |
|
|
38 |
< |
// Print out some things from the packet: - |
39 |
< |
|
38 |
> |
// Print out the entire contents of the packet's HashMap. |
39 |
> |
System.out.println("XMLPacket contents: -"); |
40 |
> |
packet.printAll(); |
41 |
|
|
44 |
– |
} catch (Throwable t) { |
45 |
– |
t.printStackTrace (); |
42 |
|
} |
43 |
< |
System.exit (0); |
44 |
< |
} |
45 |
< |
|
46 |
< |
static private Writer out; |
51 |
< |
private String indentString = " "; // Amount to indent |
52 |
< |
private int indentLevel = 0; |
53 |
< |
|
54 |
< |
// For storing the tag heirarchy. |
55 |
< |
private ArrayList tagList = new ArrayList(); |
56 |
< |
static private XMLPacket packet = null; |
57 |
< |
|
58 |
< |
//=========================================================== |
59 |
< |
// SAX DocumentHandler methods |
60 |
< |
//=========================================================== |
61 |
< |
|
62 |
< |
public void startDocument () throws SAXException { |
63 |
< |
nl(); |
64 |
< |
nl(); |
65 |
< |
emit ("START DOCUMENT"); |
66 |
< |
nl(); |
67 |
< |
emit ("<?xml version='1.0' encoding='UTF-8'?>"); |
68 |
< |
} |
69 |
< |
|
70 |
< |
public void endDocument () throws SAXException { |
71 |
< |
nl(); emit ("END DOCUMENT"); |
72 |
< |
try { |
73 |
< |
nl(); |
74 |
< |
out.flush (); |
75 |
< |
} catch (IOException e) { |
76 |
< |
throw new SAXException ("I/O error", e); |
43 |
> |
catch (Exception e) { |
44 |
> |
System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML."); |
45 |
> |
System.out.println(e); |
46 |
> |
return null; |
47 |
|
} |
48 |
+ |
|
49 |
+ |
return packet; |
50 |
|
} |
51 |
< |
|
52 |
< |
public void startElement (String name, AttributeList attrs) throws SAXException { |
81 |
< |
indentLevel++; |
82 |
< |
tagList.add(name); |
83 |
< |
nl(); emit ("ELEMENT: "); |
84 |
< |
emit ("<"+name); |
85 |
< |
if (attrs != null) { |
86 |
< |
for (int i = 0; i < attrs.getLength (); i++) { |
87 |
< |
nl(); |
88 |
< |
emit(" ATTR: "); |
89 |
< |
emit (attrs.getName (i)); |
90 |
< |
emit ("\t\""); |
91 |
< |
emit (attrs.getValue (i)); |
92 |
< |
emit ("\""); |
93 |
< |
} |
94 |
< |
} |
95 |
< |
if (attrs.getLength() > 0) nl(); |
96 |
< |
emit (">"); |
97 |
< |
} |
98 |
< |
|
99 |
< |
public void endElement (String name) throws SAXException { |
100 |
< |
nl(); |
101 |
< |
emit ("END_ELM: "); |
102 |
< |
emit ("</"+name+">"); |
103 |
< |
tagList.remove(tagList.size()-1); |
104 |
< |
indentLevel--; |
105 |
< |
} |
106 |
< |
|
107 |
< |
public void characters (char[] buf, int offset, int len) throws SAXException { |
108 |
< |
nl(); emit ("CHARS: "); |
109 |
< |
String s = new String(buf, offset, len); |
110 |
< |
if (!s.trim().equals("")) { |
111 |
< |
emit (s); |
112 |
< |
packet.addParam((String)tagList.get(tagList.size()-1), s); |
113 |
< |
} |
114 |
< |
} |
115 |
< |
|
116 |
< |
//=========================================================== |
117 |
< |
// Helpers ... |
118 |
< |
//=========================================================== |
119 |
< |
|
120 |
< |
// Wrap I/O exceptions in SAX exceptions, to |
121 |
< |
// suit handler signature requirements |
122 |
< |
private void emit (String s) throws SAXException { |
123 |
< |
try { |
124 |
< |
out.write (s); |
125 |
< |
out.flush (); |
126 |
< |
} catch (IOException e) { |
127 |
< |
throw new SAXException ("I/O error", e); |
128 |
< |
} |
129 |
< |
} |
130 |
< |
|
131 |
< |
// Start a new line |
132 |
< |
// and indent the next line appropriately |
133 |
< |
private void nl () throws SAXException { |
134 |
< |
String lineEnd = System.getProperty("line.separator"); |
135 |
< |
try { |
136 |
< |
out.write (lineEnd); |
137 |
< |
for (int i=0; i < indentLevel; i++) out.write(indentString); |
138 |
< |
} catch (IOException e) { |
139 |
< |
throw new SAXException ("I/O error", e); |
140 |
< |
} |
141 |
< |
} |
51 |
> |
|
52 |
> |
String xml; |
53 |
|
} |