package examples.jms.messageformat;
import java.util.Hashtable;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* This class extends HandlerBase and is used to handle events
* created as an XML document is parsed by a SAX compliant
* parser.
*
* @author Copyright (c) 2005 by BEA Systems, Inc. All Rights Reserved.
*/
public class RequestHandler extends DefaultHandler {
/** stocktrade document model */
private Hashtable trade;
/** Default constructor. */
public RequestHandler() {}
//==========================================================================
// DocumentHandler methods
//==========================================================================
// Amount to indent
private String indentString = " ";
private int indentLevel = 0;
/**
* Receives notification of the beginning of the XML document.
* Starts printing information to the shell from which you started the
* WebLogic server and starts a new trade hashtable to store trade information.
*
* @exception SAXException If an error occurred starting to parse the XML doc
*/
public void startDocument() throws SAXException {
nl();
nl();
trade = new Hashtable();
log("START DOCUMENT");
nl();
log("");
}
/**
* Receives notification of the end of the XML document. Prints a
* final message to the shell from which you started the WebLogic server.
*
* @exception SAXException If an error occurred while parsing the XML doc
*/
public void endDocument() throws SAXException {
nl();
log("END DOCUMENT");
try {
nl();
} catch (Exception e) {
throw new SAXException ("I/O error", e);
}
}
/**
* Receives notification of the start of an element. Prints out the
* name and value of the element, properly indented, to the shell from
* which you started the WebLogic server. Updates the trade hashtable
* with the trade information.
*
* @param uri
* @param localName
* @param qName
* @param attrs
* @exception SAXException if an error occurred while parsing the XML doc
*/
public void startElement(String uri, String localName, String qName,
Attributes attrs)
throws SAXException
{
indentLevel++;
nl();
log("ELEMENT: " +qName);
if (attrs != null) {
for (int i = 0; i < attrs.getLength (); i++) {
nl();
trade.put(attrs.getQName(i), attrs.getValue(i));
log(" ATTR: " +attrs.getQName(i)+" = "+attrs.getValue(i));
}
}
}
/**
* Receives notification of the end of an element. Prints
* notice to the shell from which you started the WebLogic server.
*
* @param name the string name of the element
* @exception SAXException If an error occurred while parsing the XML doc
*/
public void endElement(String name) throws SAXException {
nl();
log ("END_ELM: " + name);
indentLevel--;
}
/**
* Receives notification of character data inside an element. Prints out
* characters to the shell from which you started the WebLogic server.
*
* @param buf array of characters
* @param offset the start position in the character array
* @param len the number of characters to use from the array
* @exception SAXException if an error occurred while parsing the XML doc
*/
public void characters (char buf [], int offset, int len)
throws SAXException
{
nl();
String s = new String(buf, offset, len);
if (!s.trim().equals("")) log ("CHARS: " + s);
}
//===========================================================
// Helpers ...
//===========================================================
/**
* Starts a new line and indents the next line appropriately.
*
* @exception SAXException If an error occurred while parsing the XML doc
*/
private void nl () throws SAXException {
String lineEnd = System.getProperty("line.separator");
try {
System.out.print(lineEnd);
for (int i=0; i < indentLevel; i++) log(indentString);
} catch (Exception e) {
throw new SAXException ("I/O error", e);
}
}
//=============================================================
// ErrorHandler methods
//=============================================================
/**
* Receives notification of a recoverable parser error.
*
* @param x the parser exception that occurred
* @exception SAXException if an error occurred while receiving the original error
*/
public void error (SAXParseException x) throws SAXException {
throw x;
}
/** Returns parsed data as a hashtable. */
public Hashtable getData() { return trade; }
private void log(String str) { System.out.println(str); }
}