| 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 |
// 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 |
this.xml = "<packet></packet>"; |
| 18 |
} |
| 19 |
|
| 20 |
// Constructor for accepting XML input. |
| 21 |
public XMLPacketMaker (String xml) { |
| 22 |
this.xml = xml; |
| 23 |
} |
| 24 |
|
| 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 |
|
| 34 |
// Parse the input |
| 35 |
|
| 36 |
xml.trim(); |
| 37 |
|
| 38 |
//debug stuff |
| 39 |
byte[] temp = xml.getBytes(); |
| 40 |
System.out.println("String length: "+temp.length+" "); |
| 41 |
for (int i = 0 ; i < temp.length ; i++){ |
| 42 |
System.out.print((char)temp[i]); |
| 43 |
} |
| 44 |
|
| 45 |
InputSource inputSource = new InputSource(new StringReader(xml)); |
| 46 |
SAXParser saxParser = factory.newSAXParser(); |
| 47 |
saxParser.parse(inputSource, new XMLPacketParser(packet)); |
| 48 |
|
| 49 |
} |
| 50 |
//catch (IOException e){ |
| 51 |
// System.out.println("IOException: "+e); |
| 52 |
//} |
| 53 |
catch (Throwable t) { |
| 54 |
System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML."); |
| 55 |
//System.out.println(e); |
| 56 |
t.printStackTrace(); |
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
return packet; |
| 61 |
} |
| 62 |
|
| 63 |
String xml; |
| 64 |
} |