--- experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 09:32:31 1.3 +++ experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 10:15:54 1.6 @@ -9,6 +9,16 @@ import javax.xml.parsers.SAXParser; public class XMLPacketMaker extends HandlerBase { + + public XMLPacketMaker () { + // default no-args constructor. + } + + // Constructor for accepting a reference to an XMLPacket + public XMLPacketMaker (XMLPacket packet) { + this.packet = packet; + } + public static void main(String[] args){ if (args.length != 1) { System.err.println ("Usage: cmd filename"); @@ -21,10 +31,18 @@ public class XMLPacketMaker extends HandlerBase { // 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()); + saxParser.parse(new File(args[0]), new XMLPacketMaker(packet)); + // 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")); + + } catch (Throwable t) { t.printStackTrace (); } @@ -35,7 +53,9 @@ public class XMLPacketMaker extends HandlerBase { 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 @@ -61,6 +81,7 @@ public class XMLPacketMaker extends HandlerBase { public void startElement (String name, AttributeList attrs) throws SAXException { indentLevel++; + tagList.add(name); nl(); emit ("ELEMENT: "); emit ("<"+name); if (attrs != null) { @@ -81,13 +102,17 @@ public class XMLPacketMaker extends HandlerBase { 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); + if (!s.trim().equals("")) { + emit (s); + packet.addParam(getPath(), s); + } } //=========================================================== @@ -115,5 +140,15 @@ public class XMLPacketMaker extends HandlerBase { } catch (IOException e) { throw new SAXException ("I/O error", e); } + } + + 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; } } \ No newline at end of file