| 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 { |
| 12 |
– |
public static void main(String[] args){ |
| 13 |
– |
if (args.length != 1) { |
| 14 |
– |
System.err.println ("Usage: cmd filename"); |
| 15 |
– |
System.exit (1); |
| 16 |
– |
} |
| 14 |
|
|
| 15 |
< |
// Use the default (non-validating) parser |
| 16 |
< |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 17 |
< |
try { |
| 18 |
< |
// Set up output stream |
| 22 |
< |
out = new OutputStreamWriter (System.out, "UTF8"); |
| 15 |
> |
// No-args constructor. Generally not used. |
| 16 |
> |
public XMLPacketMaker () { |
| 17 |
> |
this.xml = "<packet></packet>"; |
| 18 |
> |
} |
| 19 |
|
|
| 20 |
< |
// Parse the input |
| 21 |
< |
SAXParser saxParser = factory.newSAXParser(); |
| 22 |
< |
saxParser.parse(new File(args[0]), new XMLPacketMaker()); |
| 27 |
< |
|
| 28 |
< |
} catch (Throwable t) { |
| 29 |
< |
t.printStackTrace (); |
| 30 |
< |
} |
| 31 |
< |
System.exit (0); |
| 20 |
> |
// Constructor for accepting XML input. |
| 21 |
> |
public XMLPacketMaker (String xml) { |
| 22 |
> |
this.xml = xml; |
| 23 |
|
} |
| 24 |
|
|
| 25 |
< |
static private Writer out; |
| 35 |
< |
private String indentString = " "; // Amount to indent |
| 36 |
< |
private int indentLevel = 0; |
| 25 |
> |
public XMLPacket createXMLPacket() { |
| 26 |
|
|
| 27 |
< |
// For storing the tag heirarchy. |
| 28 |
< |
private ArrayList tagList = new ArrayList(); |
| 27 |
> |
// Create the XMLPacket to store values in. |
| 28 |
> |
XMLPacket packet = new XMLPacket(); |
| 29 |
|
|
| 30 |
< |
//=========================================================== |
| 31 |
< |
// SAX DocumentHandler methods |
| 43 |
< |
//=========================================================== |
| 44 |
< |
|
| 45 |
< |
public void startDocument () throws SAXException { |
| 46 |
< |
nl(); |
| 47 |
< |
nl(); |
| 48 |
< |
emit ("START DOCUMENT"); |
| 49 |
< |
nl(); |
| 50 |
< |
emit ("<?xml version='1.0' encoding='UTF-8'?>"); |
| 51 |
< |
} |
| 52 |
< |
|
| 53 |
< |
public void endDocument () throws SAXException { |
| 54 |
< |
nl(); emit ("END DOCUMENT"); |
| 30 |
> |
// Use the default (non-validating) parser |
| 31 |
> |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 32 |
|
try { |
| 56 |
– |
nl(); |
| 57 |
– |
out.flush (); |
| 58 |
– |
} catch (IOException e) { |
| 59 |
– |
throw new SAXException ("I/O error", e); |
| 60 |
– |
} |
| 61 |
– |
} |
| 33 |
|
|
| 34 |
< |
public void startElement (String name, AttributeList attrs) throws SAXException { |
| 35 |
< |
indentLevel++; |
| 36 |
< |
nl(); emit ("ELEMENT: "); |
| 66 |
< |
emit ("<"+name); |
| 67 |
< |
if (attrs != null) { |
| 68 |
< |
for (int i = 0; i < attrs.getLength (); i++) { |
| 69 |
< |
nl(); |
| 70 |
< |
emit(" ATTR: "); |
| 71 |
< |
emit (attrs.getName (i)); |
| 72 |
< |
emit ("\t\""); |
| 73 |
< |
emit (attrs.getValue (i)); |
| 74 |
< |
emit ("\""); |
| 75 |
< |
} |
| 76 |
< |
} |
| 77 |
< |
if (attrs.getLength() > 0) nl(); |
| 78 |
< |
emit (">"); |
| 79 |
< |
} |
| 34 |
> |
// Parse the input |
| 35 |
> |
SAXParser saxParser = factory.newSAXParser(); |
| 36 |
> |
saxParser.parse(xml, new XMLPacketParser(packet)); |
| 37 |
|
|
| 38 |
< |
public void endElement (String name) throws SAXException { |
| 39 |
< |
nl(); |
| 40 |
< |
emit ("END_ELM: "); |
| 84 |
< |
emit ("</"+name+">"); |
| 85 |
< |
indentLevel--; |
| 86 |
< |
} |
| 38 |
> |
// Print out the entire contents of the packet's HashMap. |
| 39 |
> |
System.out.println("XMLPacket contents: -"); |
| 40 |
> |
packet.printAll(); |
| 41 |
|
|
| 88 |
– |
public void characters (char[] buf, int offset, int len) throws SAXException { |
| 89 |
– |
nl(); emit ("CHARS: "); |
| 90 |
– |
String s = new String(buf, offset, len); |
| 91 |
– |
if (!s.trim().equals("")) emit (s); |
| 92 |
– |
} |
| 93 |
– |
|
| 94 |
– |
//=========================================================== |
| 95 |
– |
// Helpers ... |
| 96 |
– |
//=========================================================== |
| 97 |
– |
|
| 98 |
– |
// Wrap I/O exceptions in SAX exceptions, to |
| 99 |
– |
// suit handler signature requirements |
| 100 |
– |
private void emit (String s) throws SAXException { |
| 101 |
– |
try { |
| 102 |
– |
out.write (s); |
| 103 |
– |
out.flush (); |
| 104 |
– |
} catch (IOException e) { |
| 105 |
– |
throw new SAXException ("I/O error", e); |
| 42 |
|
} |
| 43 |
< |
} |
| 44 |
< |
|
| 45 |
< |
// Start a new line |
| 46 |
< |
// and indent the next line appropriately |
| 111 |
< |
private void nl () throws SAXException { |
| 112 |
< |
String lineEnd = System.getProperty("line.separator"); |
| 113 |
< |
try { |
| 114 |
< |
out.write (lineEnd); |
| 115 |
< |
for (int i=0; i < indentLevel; i++) out.write(indentString); |
| 116 |
< |
} catch (IOException e) { |
| 117 |
< |
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 |
+ |
String xml; |
| 53 |
|
} |