<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="GENERATOR" content="Mozilla/4.7 [en] (X11; I; SunOS 5.7 sun4u) [Netscape]">
</head>
<body>
&nbsp;
<br>/*
<br>* @(#)Echo04.java 1.5 99/02/09
<br>*
<br>* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
<br>*
<br>* Sun grants you ("Licensee") a non-exclusive, royalty free, license
to use,
<br>* modify and redistribute this software in source and binary code form,
<br>* provided that i) this copyright notice and license appear on all
copies of
<br>* the software; and ii) Licensee does not utilize the software in a
manner
<br>* which is disparaging to Sun.
<br>*
<br>* This software is provided "AS IS," without a warranty of any kind.
ALL
<br>* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
ANY
<br>* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
OR
<br>* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL
NOT BE
<br>* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
MODIFYING
<br>* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
SUN OR ITS
<br>* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
DIRECT,
<br>* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
HOWEVER
<br>* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
THE USE OF
<br>* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
<br>* POSSIBILITY OF SUCH DAMAGES.
<br>*
<br>* This software is not designed or intended for use in on-line control
of
<br>* aircraft, air traffic, aircraft navigation or aircraft communications;
or in
<br>* the design, construction, operation or maintenance of any nuclear
<br>* facility. Licensee represents and warrants that it will not use or
<br>* redistribute the Software for such purposes.
<br>*/
<p>import java.io.*;
<p>import org.xml.sax.*;
<p>import javax.xml.parsers.SAXParserFactory;
<br>import javax.xml.parsers.ParserConfigurationException;
<br>import javax.xml.parsers.SAXParser;
<br>&nbsp;
<p>public class Echo04 extends HandlerBase
<br>{
<br>public static void main (String argv [])
<br>{
<br>if (argv.length != 1) {
<br>System.err.println ("Usage: cmd filename");
<br>System.exit (1);
<br>}
<p>// Use the default (non-validating) parser
<br>SAXParserFactory factory = SAXParserFactory.newInstance();
<br>try {
<br>// Set up output stream
<br>out = new OutputStreamWriter (System.out, "UTF8");
<p>// Parse the input
<br>SAXParser saxParser = factory.newSAXParser();
<br>saxParser.parse( new File(argv [0]), new Echo04() );
<p>} catch (Throwable t) {
<br>t.printStackTrace ();
<br>}
<br>System.exit (0);
<br>}
<p>static private Writer out;
<br>private String indentString = " "; // Amount to indent
<br>private int indentLevel = 0;
<p>//===========================================================
<br>// SAX DocumentHandler methods
<br>//===========================================================
<p>public void setDocumentLocator (Locator l)
<br>{
<br>// Save this to resolve relative URIs or to give diagnostics.
<br>try {
<br>out.write ("LOCATOR");
<br>out.write ("\n SYS ID: " + l.getSystemId() );
<br>out.flush ();
<br>} catch (IOException e) {
<br>// Ignore errors
<br>}
<br>}
<p>public void startDocument ()
<br>throws SAXException
<br>{
<br>nl();
<br>nl();
<br>emit ("START DOCUMENT");
<br>nl();
<br>emit ("&lt;?xml version='1.0' encoding='UTF-8'?>");
<br>}
<p>public void endDocument ()
<br>throws SAXException
<br>{
<br>nl(); emit ("END DOCUMENT");
<br>try {
<br>nl();
<br>out.flush ();
<br>} catch (IOException e) {
<br>throw new SAXException ("I/O error", e);
<br>}
<br>}
<p>public void startElement (String name, AttributeList attrs)
<br>throws SAXException
<br>{
<br>indentLevel++;
<br>nl(); emit ("ELEMENT: ");
<br>emit ("&lt;"+name);
<br>if (attrs != null) {
<br>for (int i = 0; i &lt; attrs.getLength (); i++) {
<br>nl();
<br>emit(" ATTR: ");
<br>emit (attrs.getName (i));
<br>emit ("\t\"");
<br>emit (attrs.getValue (i));
<br>emit ("\"");
<br>}
<br>}
<br>if (attrs.getLength() > 0) nl();
<br>emit (">");
<br>}
<p>public void endElement (String name)
<br>throws SAXException
<br>{
<br>nl();
<br>emit ("END_ELM: ");
<br>emit ("&lt;/"+name+">");
<br>indentLevel--;
<br>}
<p>public void characters (char buf [], int offset, int len)
<br>throws SAXException
<br>{
<br>nl(); emit ("CHARS: ");
<br>String s = new String(buf, offset, len);
<br>if (!s.trim().equals("")) emit (s);
<br>}
<p>//===========================================================
<br>// Helpers ...
<br>//===========================================================
<p>// Wrap I/O exceptions in SAX exceptions, to
<br>// suit handler signature requirements
<br>private void emit (String s)
<br>throws SAXException
<br>{
<br>try {
<br>out.write (s);
<br>out.flush ();
<br>} catch (IOException e) {
<br>throw new SAXException ("I/O error", e);
<br>}
<br>}
<p>// Start a new line
<br>// and indent the next line appropriately
<br>private void nl ()
<br>throws SAXException
<br>{
<br>String lineEnd = System.getProperty("line.separator");
<br>try {
<br>out.write (lineEnd);
<br>for (int i=0; i &lt; indentLevel; i++) out.write(indentString);
<br>} catch (IOException e) {
<br>throw new SAXException ("I/O error", e);
<br>}
<br>}
<br>}
</body>
</html>
