--- experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 10:04:01 1.5 +++ experimental/server/XMLReader/XMLPacketMaker.java 2000/11/17 14:49:13 1.14 @@ -3,140 +3,48 @@ import java.util.ArrayList; import org.xml.sax.*; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +// Paul Mutton, pjm2@ukc.ac.uk +// XMLPacketMaker - Creates an XMLPacket object. public class XMLPacketMaker extends HandlerBase { + // No-args constructor. Generally not used. public XMLPacketMaker () { - // default no-args constructor. + this.xml = ""; } - // Constructor for accepting a reference to an XMLPacket - public XMLPacketMaker (XMLPacket packet) { - this.packet = packet; + // Constructor for accepting XML input. + public XMLPacketMaker (String xml) { + this.xml = xml; } - public static void main(String[] args){ - if (args.length != 1) { - System.err.println ("Usage: cmd filename"); - System.exit (1); - } + public XMLPacket createXMLPacket() { + // Create the XMLPacket to store values in. + XMLPacket packet = new XMLPacket(); + // Use the default (non-validating) parser SAXParserFactory factory = SAXParserFactory.newInstance(); try { - // Set up output stream - out = new OutputStreamWriter (System.out, "UTF8"); - // Create the XMLPacket to store values in. - packet = new XMLPacket(); - // Parse the input + InputSource inputSource = new InputSource(new StringReader(xml)); SAXParser saxParser = factory.newSAXParser(); - saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); + saxParser.parse(inputSource, new XMLPacketParser(packet)); - // Print out some things from the packet: - - - - } catch (Throwable t) { - t.printStackTrace (); } - System.exit (0); - } - - static private Writer out; - private String indentString = " "; // Amount to indent - private int indentLevel = 0; - - // For storing the tag heirarchy. - private ArrayList tagList = new ArrayList(); - static private XMLPacket packet = null; - - //=========================================================== - // SAX DocumentHandler methods - //=========================================================== - - public void startDocument () throws SAXException { - nl(); - nl(); - emit ("START DOCUMENT"); - nl(); - emit (""); - } - - public void endDocument () throws SAXException { - nl(); emit ("END DOCUMENT"); - try { - nl(); - out.flush (); - } catch (IOException e) { - throw new SAXException ("I/O error", e); + catch (Exception e) { + System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML."); + //t.printStackTrace(); + return null; } + + return packet; } - - public void startElement (String name, AttributeList attrs) throws SAXException { - indentLevel++; - tagList.add(name); - nl(); emit ("ELEMENT: "); - emit ("<"+name); - if (attrs != null) { - for (int i = 0; i < attrs.getLength (); i++) { - nl(); - emit(" ATTR: "); - emit (attrs.getName (i)); - emit ("\t\""); - emit (attrs.getValue (i)); - emit ("\""); - } - } - if (attrs.getLength() > 0) nl(); - emit (">"); - } - - public void endElement (String name) throws SAXException { - nl(); - emit ("END_ELM: "); - emit (""); - tagList.remove(tagList.size()-1); - indentLevel--; - } - - public void characters (char[] buf, int offset, int len) throws SAXException { - nl(); emit ("CHARS: "); - String s = new String(buf, offset, len); - if (!s.trim().equals("")) { - emit (s); - packet.addParam((String)tagList.get(tagList.size()-1), s); - } - } - - //=========================================================== - // Helpers ... - //=========================================================== - - // Wrap I/O exceptions in SAX exceptions, to - // suit handler signature requirements - private void emit (String s) throws SAXException { - try { - out.write (s); - out.flush (); - } catch (IOException e) { - throw new SAXException ("I/O error", e); - } - } - - // Start a new line - // and indent the next line appropriately - private void nl () throws SAXException { - String lineEnd = System.getProperty("line.separator"); - try { - out.write (lineEnd); - for (int i=0; i < indentLevel; i++) out.write(indentString); - } catch (IOException e) { - throw new SAXException ("I/O error", e); - } - } + + String xml; } \ No newline at end of file