--- experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 12:01:19 1.7 +++ experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 12:23:17 1.8 @@ -40,11 +40,8 @@ public class XMLPacketMaker extends HandlerBase { SAXParser saxParser = factory.newSAXParser(); saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); - // By means of example, print out some things from the packet: - - System.out.println("bung.wibble="+packet.getParam("bung.wibble")); - System.out.println("bung.ping.value1="+packet.getParam("bung.ping.value1")); - - // Print out the entire contents of the packet. + // Print out the entire contents of the packet's HashMap. + System.out.println("XMLPacket contents: -"); packet.printAll(); } catch (Throwable t) { @@ -66,86 +63,56 @@ public class XMLPacketMaker extends HandlerBase { //=========================================================== public void startDocument () throws SAXException { - nl(); - nl(); - emit ("START DOCUMENT"); - nl(); - emit (""); + // No purpose currently. } public void endDocument () throws SAXException { - nl(); emit ("END DOCUMENT"); - try { - nl(); - out.flush (); - } catch (IOException e) { - throw new SAXException ("I/O error", e); - } + // 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); - 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 ("\""); + packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); } } - if (attrs.getLength() > 0) nl(); - emit (">"); } + // 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 { - nl(); - emit ("END_ELM: "); - emit (""); 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 { - nl(); emit ("CHARS: "); String s = new String(buf, offset, len); if (!s.trim().equals("")) { - emit (s); packet.addParam(getPath(), 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); - } - } + // Return the heirarchical string to be used as a key value // in the XMLPacket. private String getPath () { @@ -157,4 +124,5 @@ public class XMLPacketMaker extends HandlerBase { } return path; } + } \ No newline at end of file