--- experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 09:18:20 1.2 +++ experimental/server/XMLReader/XMLPacketMaker.java 2000/11/14 09:32:31 1.3 @@ -1,127 +1,119 @@ -import java.io.*; - -import org.xml.sax.*; - -import javax.xml.parsers.SAXParserFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; - - -public class XMLPacketMaker extends HandlerBase -{ - public static void main (String argv []) - { - if (argv.length != 1) { - System.err.println ("Usage: cmd filename.xml"); - System.exit (1); - } - - // Use the default (non-validating) parser - SAXParserFactory factory = SAXParserFactory.newInstance(); - try { - // Set up output stream - out = new OutputStreamWriter (System.out, "UTF8"); - - // Parse the input - SAXParser saxParser = factory.newSAXParser(); - saxParser.parse( new File(argv [0]), new XMLPacketMaker() ); - - } catch (Throwable t) { - t.printStackTrace (); - } - System.exit (0); - } - - static private Writer out; - private String indentString = " "; // Amount to indent - private int indentLevel = 0; - - - //=========================================================== - // 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); - } - } - - public void startElement (String name, AttributeList attrs) - throws SAXException - { - indentLevel++; - //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 (""); - indentLevel--; - } - - public void characters (char buf [], int offset, int len) throws SAXException { - nl(); emit ("Value: "); - String s = new String(buf, offset, len); - if (!s.trim().equals("")) emit (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); - } - } -} +import java.io.*; +import java.util.ArrayList; + +import org.xml.sax.*; + +import javax.xml.parsers.SAXParserFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; + + +public class XMLPacketMaker extends HandlerBase { + public static void main(String[] args){ + if (args.length != 1) { + System.err.println ("Usage: cmd filename"); + System.exit (1); + } + + // Use the default (non-validating) parser + SAXParserFactory factory = SAXParserFactory.newInstance(); + try { + // Set up output stream + out = new OutputStreamWriter (System.out, "UTF8"); + + // Parse the input + SAXParser saxParser = factory.newSAXParser(); + saxParser.parse(new File(args[0]), new XMLPacketMaker()); + + } catch (Throwable t) { + t.printStackTrace (); + } + System.exit (0); + } + + static private Writer out; + private String indentString = " "; // Amount to indent + private int indentLevel = 0; + + private ArrayList tagList = new ArrayList(); + + //=========================================================== + // 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); + } + } + + public void startElement (String name, AttributeList attrs) throws SAXException { + indentLevel++; + 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 (""); + 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); + } + + //=========================================================== + // 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); + } + } +} \ No newline at end of file