uk.co.demon.ursus.dom
Class SAXAdapter

java.lang.Object
  |
  +--org.xml.sax.HandlerBase
        |
        +--uk.co.demon.ursus.dom.SAXAdapter
Direct Known Subclasses:
SAXAtomCounter, SAXDOMBuilder

public class SAXAdapter
extends org.xml.sax.HandlerBase

David Megginson's SAX demo, adapted gently to form a generic SAX implementation - SAXAdapter implies all methods have minimal implementations. Comments are from both authors. Where possible I have left in David's comments in case others wish to hack this code. David's code is commented out through slash-star-dash-dash.

Must be subclassed


Field Summary
static int AELFRED
           
static java.lang.String defaultParserClassName
           
static int defaultParserType
           
static int DXP
           
static int LARK
           
static int MSXML
           
static int nParserTypes
           
protected  org.xml.sax.Parser parser
           
static java.lang.String[] parserTypeStrings
           
protected  java.io.StringReader reader
           
protected  java.lang.String urlString
           
static int XERCESNV
           
static int XERCESSAX
           
static int XML4J
           
static int XP
           
 
Constructor Summary
SAXAdapter()
          Uses default parser if none supplied later
SAXAdapter(org.xml.sax.Parser parser)
          Uses given parser
SAXAdapter(java.lang.String parserClassName)
          Uses given parser name
 
Method Summary
static void addAttributes(org.w3c.dom.Element element, org.xml.sax.AttributeList attributeList)
           
static java.lang.String attributeString(org.xml.sax.AttributeList attributeList)
           
 void characters(char[] ch, int start, int length)
          Receive notification of character data inside an element.
protected  void elementCounter()
           
 void endDocument()
          Receive notification of the end of the document.
 void endElement(java.lang.String name)
          Receive notification of the end of an element.
 void error(org.xml.sax.SAXParseException e)
          Receive notification of a recoverable parser error.
 void fatalError(org.xml.sax.SAXParseException e)
          Report a fatal XML parsing error.
 java.lang.String getErrorMessage(org.xml.sax.SAXParseException e)
          get an error message string from a SAXParseException
 void ignorableWhitespace(char[] ch, int start, int length)
          Receive notification of ignorable whitespace in element content.
static void main(java.lang.String[] args)
          Main entry point for an application.
 void notationDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          Receive notification of a notation declaration.
 void parse(java.lang.String urlString)
          create a new SAXTree.
 void parse(java.io.StringReader reader)
          parse.
 void parse(java.lang.String urlString, java.io.StringReader reader, java.lang.String parserClassName)
          parse.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Receive notification of a processing instruction.
 org.xml.sax.InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
          Resolve an external entity.
 void setDocumentLocator(org.xml.sax.Locator locator)
          Receive a Locator object for document events.
 void setParser(org.xml.sax.Parser p)
          installs a parser
 org.xml.sax.Parser setParser(java.lang.String parserClassName)
          creates a parser from the parserClassName and installs
 void startDocument()
          Receive notification of the beginning of the document.
 void startElement(java.lang.String name, org.xml.sax.AttributeList attributes)
          Receive notification of the start of an element.
 void unparsedEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId, java.lang.String notationName)
          Receive notification of an unparsed entity declaration.
 void warning(org.xml.sax.SAXParseException e)
          Receive notification of a parser warning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AELFRED

public static final int AELFRED

DXP

public static final int DXP

LARK

public static final int LARK

MSXML

public static final int MSXML

XML4J

public static final int XML4J

XP

public static final int XP

XERCESNV

public static final int XERCESNV

XERCESSAX

public static final int XERCESSAX

nParserTypes

public static final int nParserTypes

parserTypeStrings

public static final java.lang.String[] parserTypeStrings

defaultParserType

public static final int defaultParserType

defaultParserClassName

public static final java.lang.String defaultParserClassName

urlString

protected java.lang.String urlString

reader

protected java.io.StringReader reader

parser

protected org.xml.sax.Parser parser
Constructor Detail

SAXAdapter

public SAXAdapter()
Uses default parser if none supplied later

SAXAdapter

public SAXAdapter(java.lang.String parserClassName)
           throws java.lang.Exception
Uses given parser name

SAXAdapter

public SAXAdapter(org.xml.sax.Parser parser)
Uses given parser
Method Detail

setParser

public org.xml.sax.Parser setParser(java.lang.String parserClassName)
                             throws java.lang.Exception
creates a parser from the parserClassName and installs

setParser

public void setParser(org.xml.sax.Parser p)
installs a parser

parse

public void parse(java.lang.String urlString,
                  java.io.StringReader reader,
                  java.lang.String parserClassName)
           throws java.lang.Exception
parse. Obsolete signature.
Parameters:
String - urlString the URL with the XML document Create a SAX from a UTF-16 character stream. From DM's CharacterStreamDemo; the default parser is used. Precede this by a call of the form:
StringReader reader = new StringReader(string);
StringReader - reader the StringReader created from the String
Throws:
XException - IO problems or parsing errors

parse

public void parse(java.lang.String urlString)
           throws java.lang.Exception
create a new SAXTree.
Parameters:
String - urlString the URL with the XML document Create a SAX from a UTF-16 character stream. From DM's CharacterStreamDemo; the default parser is used. Precede this by a call of the form:
StringReader reader = new StringReader(string);
StringReader - reader the StringReader created from the String
Throws:
XException - IO problems or parsing errors

parse

public void parse(java.io.StringReader reader)
           throws java.lang.Exception
parse.
Parameters:
String - urlString the URL with the XML document Create a SAX from a UTF-16 character stream. From DM's CharacterStreamDemo; the default parser is used. Precede this by a call of the form:
StringReader reader = new StringReader(string);
StringReader - reader the StringReader created from the String
Throws:
XException - IO problems or parsing errors

resolveEntity

public org.xml.sax.InputSource resolveEntity(java.lang.String publicId,
                                             java.lang.String systemId)
                                      throws org.xml.sax.SAXException
Resolve an external entity. Always return null, so that the parser will use the system identifier provided in the XML document. This method implements the SAX default behaviour: application writers can override it in a subclass to do special translations such as catalog lookups or URI redirection.
Overrides:
resolveEntity in class org.xml.sax.HandlerBase
Parameters:
publicId - The public identifer, or null if none is available.
systemId - The system identifier provided in the XML document.
Returns:
The new input source, or null to require the default behaviour.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
EntityResolver.resolveEntity(java.lang.String, java.lang.String)

notationDecl

public void notationDecl(java.lang.String name,
                         java.lang.String publicId,
                         java.lang.String systemId)
Receive notification of a notation declaration. By default, do nothing. Application writers may override this method in a subclass if they wish to keep track of the notations declared in a document.
Overrides:
notationDecl in class org.xml.sax.HandlerBase
Parameters:
name - The notation name.
publicId - The notation public identifier, or null if not available.
systemId - The notation system identifier.
See Also:
DTDHandler.notationDecl(java.lang.String, java.lang.String, java.lang.String)

unparsedEntityDecl

public void unparsedEntityDecl(java.lang.String name,
                               java.lang.String publicId,
                               java.lang.String systemId,
                               java.lang.String notationName)
Receive notification of an unparsed entity declaration. By default, do nothing. Application writers may override this method in a subclass to keep track of the unparsed entities declared in a document.
Overrides:
unparsedEntityDecl in class org.xml.sax.HandlerBase
Parameters:
name - The entity name.
publicId - The entity public identifier, or null if not available.
systemId - The entity system identifier.
notationName - The name of the associated notation.
See Also:
DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Receive a Locator object for document events. By default, do nothing. Application writers may override this method in a subclass if they wish to store the locator for use with other document events.
Overrides:
setDocumentLocator in class org.xml.sax.HandlerBase
Parameters:
locator - A locator for all SAX document events.
See Also:
DocumentHandler.setDocumentLocator(org.xml.sax.Locator), Locator

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Receive notification of the beginning of the document. By default, do nothing. Application writers may override this method in a subclass to take specific actions at the beginning of a document (such as allocating the root node of a tree or creating an output file).
Overrides:
startDocument in class org.xml.sax.HandlerBase
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.startDocument()

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Receive notification of the end of the document. By default, do nothing. Application writers may override this method in a subclass to take specific actions at the beginning of a document (such as finalising a tree or closing an output file).
Overrides:
endDocument in class org.xml.sax.HandlerBase
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.endDocument()

startElement

public void startElement(java.lang.String name,
                         org.xml.sax.AttributeList attributes)
                  throws org.xml.sax.SAXException
Receive notification of the start of an element. By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).
Overrides:
startElement in class org.xml.sax.HandlerBase
Parameters:
name - The element type name.
attributes - The specified or defaulted attributes.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.startElement(java.lang.String, org.xml.sax.AttributeList)

elementCounter

protected void elementCounter()

endElement

public void endElement(java.lang.String name)
                throws org.xml.sax.SAXException
Receive notification of the end of an element. By default, do nothing. Application writers may override this method in a subclass to take specific actions at the end of each element (such as finalising a tree node or writing output to a file).
Overrides:
endElement in class org.xml.sax.HandlerBase
Parameters:
name - The element type name.
attributes - The specified or defaulted attributes.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.endElement(java.lang.String)

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Receive notification of character data inside an element. By default, do nothing. Application writers may override this method to take specific actions for each chunk of character data (such as adding the data to a node or buffer, or printing it to a file).
Overrides:
characters in class org.xml.sax.HandlerBase
Parameters:
ch - The characters.
start - The start position in the character array.
length - The number of characters to use from the character array.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.characters(char[], int, int)

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Receive notification of ignorable whitespace in element content. By default, do nothing. Application writers may override this method to take specific actions for each chunk of ignorable whitespace (such as adding data to a node or buffer, or printing it to a file).
Overrides:
ignorableWhitespace in class org.xml.sax.HandlerBase
Parameters:
ch - The whitespace characters.
start - The start position in the character array.
length - The number of characters to use from the character array.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.ignorableWhitespace(char[], int, int)

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Receive notification of a processing instruction. By default, do nothing. Application writers may override this method in a subclass to take specific actions for each processing instruction, such as setting status variables or invoking other methods.
Overrides:
processingInstruction in class org.xml.sax.HandlerBase
Parameters:
target - The processing instruction target.
data - The processing instruction data, or null if none is supplied.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
DocumentHandler.processingInstruction(java.lang.String, java.lang.String)

warning

public void warning(org.xml.sax.SAXParseException e)
             throws org.xml.sax.SAXException
Receive notification of a parser warning. The default implementation does nothing. Application writers may override this method in a subclass to take specific actions for each warning, such as inserting the message in a log file or printing it to the console.
Overrides:
warning in class org.xml.sax.HandlerBase
Parameters:
e - The warning information encoded as an exception.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
ErrorHandler.warning(org.xml.sax.SAXParseException), SAXParseException

error

public void error(org.xml.sax.SAXParseException e)
           throws org.xml.sax.SAXException
Receive notification of a recoverable parser error. The default implementation does nothing. Application writers may override this method in a subclass to take specific actions for each error, such as inserting the message in a log file or printing it to the console.
Overrides:
error in class org.xml.sax.HandlerBase
Parameters:
e - The warning information encoded as an exception.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
ErrorHandler.warning(org.xml.sax.SAXParseException), SAXParseException

fatalError

public void fatalError(org.xml.sax.SAXParseException e)
                throws org.xml.sax.SAXException
Report a fatal XML parsing error. The default implementation throws a SAXParseException. Application writers may override this method in a subclass if they need to take specific actions for each fatal error (such as collecting all of the errors into a single report): in any case, the application must stop all regular processing when this method is invoked, since the document is no longer reliable, and the parser may no longer report non-error events.
Overrides:
fatalError in class org.xml.sax.HandlerBase
Parameters:
e - The error information encoded as an exception.
Throws:
org.xml.sax.SAXException - IO problems or parsing errors
See Also:
ErrorHandler.fatalError(org.xml.sax.SAXParseException), SAXParseException

getErrorMessage

public java.lang.String getErrorMessage(org.xml.sax.SAXParseException e)
get an error message string from a SAXParseException

attributeString

public static java.lang.String attributeString(org.xml.sax.AttributeList attributeList)

addAttributes

public static void addAttributes(org.w3c.dom.Element element,
                                 org.xml.sax.AttributeList attributeList)

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Main entry point for an application.
Throws:
java.lang.Exception - almost anything in the galaxy could go wrong