redstone.xmlrpc
Class XmlRpcParser

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by redstone.xmlrpc.XmlRpcParser
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler
Direct Known Subclasses:
XmlRpcClient, XmlRpcDispatcher

public abstract class XmlRpcParser
extends org.xml.sax.helpers.DefaultHandler

An XmlRpcParser converts inbound XML-RPC messages to their Java counterparts through the use of a SAX compliant parser. This is an abstract class that is only concerned with the XML-RPC values contained in a message. Deriving classes supply a handleParsedValue() method that is called whenever an XML-RPC value has been parsed.

If a class needs to be notified of additional parts of an XML-RPC message, the startElement() or endElement() methods are overridden and extended with checks for the appropriate element. This is the case with XmlRpcClient that wants to know if a fault element is present. Also, the XmlRpcServer wants to know the name of the method for which values are supplied.

Internally, the implementation uses pre-calculated hash values of the element names to allow for switch() constructs when comparing elements supplied by the SAX parser.

Author:
Greger Olsson

Field Summary
static int ARRAY
          The hash value of array elements
static int BASE64
          The hash value of double elements
static int BOOLEAN
          The hash value of boolean elements
static int DATE
          The hash value of double elements
static int DOUBLE
          The hash value of double elements
static int I4
          The hash value of i4 elements
static int INT
          The hash value of int elements
static int MEMBER
          The hash value of member elements
static int NAME
          The hash value of name elements
static int STRING
          The hash value of string elements
static int STRUCT
          The hash value of struct elements
static int VALUE
          The hash value of value elements
 
Constructor Summary
XmlRpcParser()
           
 
Method Summary
 void characters(char[] data, int start, int length)
          Called by the SAX driver when character data is available.
 void endElement(java.lang.String uri, java.lang.String name, java.lang.String qualifiedName)
          Called by SAX driver when a new end-element has been found in the message.
 void parse(java.io.InputStream is)
          Parses the XML-RPC message contained in the supplied input stream.
 void startElement(java.lang.String uri, java.lang.String name, java.lang.String qualifiedName, org.xml.sax.Attributes attributes)
          Called by SAX driver when a new element has been found in the message.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VALUE

public static final int VALUE
The hash value of value elements

See Also:
Constant Field Values

STRING

public static final int STRING
The hash value of string elements

See Also:
Constant Field Values

I4

public static final int I4
The hash value of i4 elements

See Also:
Constant Field Values

INT

public static final int INT
The hash value of int elements

See Also:
Constant Field Values

BOOLEAN

public static final int BOOLEAN
The hash value of boolean elements

See Also:
Constant Field Values

DOUBLE

public static final int DOUBLE
The hash value of double elements

See Also:
Constant Field Values

DATE

public static final int DATE
The hash value of double elements

See Also:
Constant Field Values

BASE64

public static final int BASE64
The hash value of double elements

See Also:
Constant Field Values

STRUCT

public static final int STRUCT
The hash value of struct elements

See Also:
Constant Field Values

ARRAY

public static final int ARRAY
The hash value of array elements

See Also:
Constant Field Values

MEMBER

public static final int MEMBER
The hash value of member elements

See Also:
Constant Field Values

NAME

public static final int NAME
The hash value of name elements

See Also:
Constant Field Values
Constructor Detail

XmlRpcParser

public XmlRpcParser()
Method Detail

parse

public void parse(java.io.InputStream is)
           throws XmlRpcException
Parses the XML-RPC message contained in the supplied input stream. It does so by using the current SAX driver, and will call handleParsedValue() for every top-level value contained in the message. This method can be overridden to supply additional processing, like identifying method names and such. This implementation is only concerned with the values of the message.

Parameters:
is - The input stream containing the XML-RPC message
Throws:
XmlRpcException

startElement

public void startElement(java.lang.String uri,
                         java.lang.String name,
                         java.lang.String qualifiedName,
                         org.xml.sax.Attributes attributes)
                  throws org.xml.sax.SAXException
Called by SAX driver when a new element has been found in the message.

This implementation uses a switch construct on the hash value of the element name. This increases readability, in my opinion, and perhaps performance as well (only one loop -- in hashCode() -- instead of in every equals() call).

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
See - SAX documentation
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String name,
                       java.lang.String qualifiedName)
                throws org.xml.sax.SAXException
Called by SAX driver when a new end-element has been found in the message.

This implementation determines if our current state is that we have a current value that needs to be processed, and if that value has some character data in the buffer required to finalize the value. The handleParsedValue() method is only called if a top-level value element has ended. If not, the value is added to the enclosing value, which may be an array or a struct.

Note that struct values are processed when the member element ends and not when the value element ends. This is because we need to use the included member name.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
See - SAX documentation
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] data,
                       int start,
                       int length)
Called by the SAX driver when character data is available.

This implementation appends the data to an internal string buffer. The method is called for every element, wether characters are included int the element or not. This leads to the buffer being prepended with whitespace until actual character data is aquired. This is removed using the trim() method when the character data is consumed.

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Parameters:
See - SAX documentation