--- experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 12:36:11 1.9 +++ experimental/server/XMLReader/XMLPacketMaker.java 2000/11/17 11:07:32 1.10 @@ -12,118 +12,41 @@ import javax.xml.parsers.SAXParser; // 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 SAXParser saxParser = factory.newSAXParser(); - saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); + saxParser.parse(xml, new XMLPacketParser(packet)); // Print out the entire contents of the packet's HashMap. System.out.println("XMLPacket contents: -"); packet.printAll(); - } catch (Throwable t) { + } + catch (Throwable t) { System.out.println("The recieved packet did not contain valid XML, so I'm gonna reject it."); //t.printStackTrace (); } - System.exit (0); + + return packet; } - - 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 { - // No purpose currently. - } - - public void endDocument () throws SAXException { - // No purpose currently. - } - - // Add each tag's attribute to the XMLPacket. - // Note that all attributes within an opening tag are - // stored as "someroot.sometag.attributes.attribute_name" - // E.g. If is the root node, then: - // - // is stored as: - // "packet.attributes.machine_name" - // within the XMLPacket. - public void startElement (String name, AttributeList attrs) throws SAXException { - indentLevel++; - tagList.add(name); - if (attrs != null) { - for (int i = 0; i < attrs.getLength (); i++) { - packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); - } - } - } - - // When an XML element is finished with, we must remove - // the tag name from the tagList and decrement the indent - // level. - public void endElement (String name) throws SAXException { - tagList.remove(tagList.size()-1); - indentLevel--; - } - - // Any text falling within a pair of terminal tags must - // be added to the XMLPacket. Trim leading and trailing - // spaces and do not bother to add if there is no data - // specified within the tags. - public void characters (char[] buf, int offset, int len) throws SAXException { - String s = new String(buf, offset, len); - if (!s.trim().equals("")) { - packet.addParam(getPath(), s); - } - } - - - //=========================================================== - // Helpers ... - //=========================================================== - - // Return the heirarchical string to be used as a key value - // in the XMLPacket. - private String getPath () { - String path = (String)tagList.get(0); - if (tagList.size() > 0) { - for (int i = 1 ; i < tagList.size() ; i++) { - path = path + "." + (String)tagList.get(i); - } - } - return path; - } - + String xml; } \ No newline at end of file