1 |
< |
import java.io.*; |
2 |
< |
|
3 |
< |
import org.xml.sax.*; |
4 |
< |
|
5 |
< |
import javax.xml.parsers.SAXParserFactory; |
6 |
< |
import javax.xml.parsers.ParserConfigurationException; |
7 |
< |
import javax.xml.parsers.SAXParser; |
8 |
< |
|
9 |
< |
|
10 |
< |
public class XMLPacketMaker extends HandlerBase |
11 |
< |
{ |
12 |
< |
public static void main (String argv []) |
13 |
< |
{ |
14 |
< |
if (argv.length != 1) { |
15 |
< |
System.err.println ("Usage: cmd filename.xml"); |
16 |
< |
System.exit (1); |
17 |
< |
} |
18 |
< |
|
19 |
< |
// Use the default (non-validating) parser |
20 |
< |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
21 |
< |
try { |
22 |
< |
// Set up output stream |
23 |
< |
out = new OutputStreamWriter (System.out, "UTF8"); |
24 |
< |
|
25 |
< |
// Parse the input |
26 |
< |
SAXParser saxParser = factory.newSAXParser(); |
27 |
< |
saxParser.parse( new File(argv [0]), new Echo03() ); |
28 |
< |
|
29 |
< |
} catch (Throwable t) { |
30 |
< |
t.printStackTrace (); |
31 |
< |
} |
32 |
< |
System.exit (0); |
33 |
< |
} |
34 |
< |
|
35 |
< |
static private Writer out; |
36 |
< |
private String indentString = " "; // Amount to indent |
37 |
< |
private int indentLevel = 0; |
38 |
< |
|
39 |
< |
|
40 |
< |
//=========================================================== |
41 |
< |
// SAX DocumentHandler methods |
42 |
< |
//=========================================================== |
43 |
< |
|
44 |
< |
public void startDocument () |
45 |
< |
throws SAXException |
46 |
< |
{ |
47 |
< |
nl(); |
48 |
< |
nl(); |
49 |
< |
emit ("START DOCUMENT"); |
50 |
< |
nl(); |
51 |
< |
emit ("<?xml version='1.0' encoding='UTF-8'?>"); |
52 |
< |
} |
53 |
< |
|
54 |
< |
public void endDocument () |
55 |
< |
throws SAXException |
56 |
< |
{ |
57 |
< |
nl(); emit ("END DOCUMENT"); |
58 |
< |
try { |
59 |
< |
nl(); |
60 |
< |
out.flush (); |
61 |
< |
} catch (IOException e) { |
62 |
< |
throw new SAXException ("I/O error", e); |
63 |
< |
} |
64 |
< |
} |
65 |
< |
|
66 |
< |
public void startElement (String name, AttributeList attrs) |
67 |
< |
throws SAXException |
68 |
< |
{ |
69 |
< |
indentLevel++; |
70 |
< |
nl(); emit ("ELEMENT: "); |
71 |
< |
emit ("<"+name); |
72 |
< |
if (attrs != null) { |
73 |
< |
for (int i = 0; i < attrs.getLength (); i++) { |
74 |
< |
nl(); |
75 |
< |
emit(" ATTR: "); |
76 |
< |
emit (attrs.getName (i)); |
77 |
< |
emit ("\t\""); |
78 |
< |
emit (attrs.getValue (i)); |
79 |
< |
emit ("\""); |
80 |
< |
} |
81 |
< |
} |
82 |
< |
if (attrs.getLength() > 0) nl(); |
83 |
< |
emit (">"); |
84 |
< |
} |
85 |
< |
|
86 |
< |
public void endElement (String name) |
87 |
< |
throws SAXException |
88 |
< |
{ |
89 |
< |
nl(); |
90 |
< |
emit ("END_ELM: "); |
91 |
< |
emit ("</"+name+">"); |
92 |
< |
indentLevel--; |
93 |
< |
} |
94 |
< |
|
95 |
< |
public void characters (char buf [], int offset, int len) throws SAXException { |
96 |
< |
nl(); emit ("CHARS: "); |
97 |
< |
String s = new String(buf, offset, len); |
98 |
< |
if (!s.trim().equals("")) emit (s); |
99 |
< |
} |
100 |
< |
|
101 |
< |
//=========================================================== |
102 |
< |
// Helpers ... |
103 |
< |
//=========================================================== |
104 |
< |
|
105 |
< |
// Wrap I/O exceptions in SAX exceptions, to |
106 |
< |
// suit handler signature requirements |
107 |
< |
private void emit (String s) throws SAXException { |
108 |
< |
try { |
109 |
< |
out.write (s); |
110 |
< |
out.flush (); |
111 |
< |
} catch (IOException e) { |
112 |
< |
throw new SAXException ("I/O error", e); |
113 |
< |
} |
114 |
< |
} |
115 |
< |
|
116 |
< |
// Start a new line |
117 |
< |
// and indent the next line appropriately |
118 |
< |
private void nl () throws SAXException { |
119 |
< |
String lineEnd = System.getProperty("line.separator"); |
120 |
< |
try { |
121 |
< |
out.write (lineEnd); |
122 |
< |
for (int i=0; i < indentLevel; i++) out.write(indentString); |
123 |
< |
} catch (IOException e) { |
124 |
< |
throw new SAXException ("I/O error", e); |
125 |
< |
} |
126 |
< |
} |
127 |
< |
} |
1 |
> |
import java.io.*; |
2 |
> |
import java.util.ArrayList; |
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; |
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"); |
25 |
> |
System.exit (1); |
26 |
> |
} |
27 |
> |
|
28 |
> |
// Use the default (non-validating) parser |
29 |
> |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
30 |
> |
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 |
> |
|
37 |
> |
// Parse the input |
38 |
> |
SAXParser saxParser = factory.newSAXParser(); |
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 |
> |
} |
49 |
> |
System.exit (0); |
50 |
> |
} |
51 |
> |
|
52 |
> |
static private Writer out; |
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 |
62 |
> |
//=========================================================== |
63 |
> |
|
64 |
> |
public void startDocument () throws SAXException { |
65 |
> |
nl(); |
66 |
> |
nl(); |
67 |
> |
emit ("START DOCUMENT"); |
68 |
> |
nl(); |
69 |
> |
emit ("<?xml version='1.0' encoding='UTF-8'?>"); |
70 |
> |
} |
71 |
> |
|
72 |
> |
public void endDocument () throws SAXException { |
73 |
> |
nl(); emit ("END DOCUMENT"); |
74 |
> |
try { |
75 |
> |
nl(); |
76 |
> |
out.flush (); |
77 |
> |
} catch (IOException e) { |
78 |
> |
throw new SAXException ("I/O error", e); |
79 |
> |
} |
80 |
> |
} |
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) { |
88 |
> |
for (int i = 0; i < attrs.getLength (); i++) { |
89 |
> |
nl(); |
90 |
> |
emit(" ATTR: "); |
91 |
> |
emit (attrs.getName (i)); |
92 |
> |
emit ("\t\""); |
93 |
> |
emit (attrs.getValue (i)); |
94 |
> |
emit ("\""); |
95 |
> |
} |
96 |
> |
} |
97 |
> |
if (attrs.getLength() > 0) nl(); |
98 |
> |
emit (">"); |
99 |
> |
} |
100 |
> |
|
101 |
> |
public void endElement (String name) throws SAXException { |
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("")) { |
113 |
> |
emit (s); |
114 |
> |
packet.addParam(getPath(), s); |
115 |
> |
} |
116 |
> |
} |
117 |
> |
|
118 |
> |
//=========================================================== |
119 |
> |
// Helpers ... |
120 |
> |
//=========================================================== |
121 |
> |
|
122 |
> |
// Wrap I/O exceptions in SAX exceptions, to |
123 |
> |
// suit handler signature requirements |
124 |
> |
private void emit (String s) throws SAXException { |
125 |
> |
try { |
126 |
> |
out.write (s); |
127 |
> |
out.flush (); |
128 |
> |
} catch (IOException e) { |
129 |
> |
throw new SAXException ("I/O error", e); |
130 |
> |
} |
131 |
> |
} |
132 |
> |
|
133 |
> |
// Start a new line |
134 |
> |
// and indent the next line appropriately |
135 |
> |
private void nl () throws SAXException { |
136 |
> |
String lineEnd = System.getProperty("line.separator"); |
137 |
> |
try { |
138 |
> |
out.write (lineEnd); |
139 |
> |
for (int i=0; i < indentLevel; i++) out.write(indentString); |
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 |
> |
} |