Google Data APIs Client Library (1.41.1)



com.google.gdata.util
Class XmlParser

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.google.gdata.util.XmlParser
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler
Direct Known Subclasses:
EventSourceParser

public class XmlParser
extends org.xml.sax.helpers.DefaultHandler

XML parser.

This is a thin layer on top of a SAX parser. The key concept necessary to understand this parser is Element Handler. Element handlers are type-specific parsers. Each handler instance contains an instance of the Java type corresponding to the XML type it parses. At any given time, one handler is active, and zero or more handlers are kept on the stack. This corresponds directly to the set of currently opened XML tags.

To use this parser, one must define an XmlParser.ElementHandler type (usually one per XML schema type), specify the root element handler, and pass a reader to the parse(Reader, com.google.gdata.util.XmlParser.ElementHandler, String, String) method.

See Also:
XmlParser.ElementHandler

Nested Class Summary
static class XmlParser.ElementHandler
          Base class for custom element handlers.
 
Field Summary
protected  java.util.HashMap<java.lang.String,java.util.Stack<com.google.gdata.util.XmlParser.NamespaceDecl>> namespaceMap
          Set of all namespace declarations valid at the current location.
protected  java.lang.String rootElementName
          Root element name.
protected  XmlParser.ElementHandler rootHandler
          Root element handler.
protected  java.lang.String rootNamespace
          Root element namespace URI.
 
Constructor Summary
XmlParser()
           
 
Method Summary
 void characters(char[] text, int start, int len)
          SAX callback.
 void endElement(java.lang.String namespace, java.lang.String localName, java.lang.String qName)
          SAX callback.
 void endPrefixMapping(java.lang.String alias)
          SAX callback.
 void ignorableWhitespace(char[] text, int start, int len)
          SAX callback.
protected  void parse(org.xml.sax.InputSource is)
          Parses XML.
 void parse(java.io.InputStream input, XmlParser.ElementHandler rootHandler, java.lang.String rootNamespace, java.lang.String rootElementName)
          Parses XML.
 void parse(java.io.Reader reader, XmlParser.ElementHandler rootHandler, java.lang.String rootNamespace, java.lang.String rootElementName)
          Parses XML.
 void parse(XmlParser.ElementHandler rootHandler, java.lang.String rootNamespace, java.lang.String rootElementName)
          Parses XML from a content source provided to the parser at construction time.
 void setDocumentLocator(org.xml.sax.Locator locator)
          SAX callback.
 void startElement(java.lang.String namespace, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attrs)
          SAX callback.
 void startPrefixMapping(java.lang.String alias, java.lang.String uri)
          SAX callback.
protected  void throwParseException(ParseException e)
          Throws a parse exception with line/column information.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, error, fatalError, notationDecl, processingInstruction, resolveEntity, skippedEntity, startDocument, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rootHandler

protected XmlParser.ElementHandler rootHandler
Root element handler.


rootNamespace

protected java.lang.String rootNamespace
Root element namespace URI.


rootElementName

protected java.lang.String rootElementName
Root element name.


namespaceMap

protected java.util.HashMap<java.lang.String,java.util.Stack<com.google.gdata.util.XmlParser.NamespaceDecl>> namespaceMap
Set of all namespace declarations valid at the current location. Includes namespace declarations from all ancestor elements.

Constructor Detail

XmlParser

public XmlParser()
Method Detail

parse

public void parse(java.io.Reader reader,
                  XmlParser.ElementHandler rootHandler,
                  java.lang.String rootNamespace,
                  java.lang.String rootElementName)
           throws java.io.IOException,
                  ParseException
Parses XML.

Parameters:
reader - Supplies the XML to parse.
rootHandler - The root element handler corresponding to the expected document type.
rootNamespace - Root element namespace URI.
rootElementName - Root element name.
Throws:
java.io.IOException - Thrown by reader.
ParseException - XML failed to validate against the schema implemented by rootHandler.

parse

public void parse(java.io.InputStream input,
                  XmlParser.ElementHandler rootHandler,
                  java.lang.String rootNamespace,
                  java.lang.String rootElementName)
           throws java.io.IOException,
                  ParseException
Parses XML.

Parameters:
input - Supplies the XML to parse.
rootHandler - The root element handler corresponding to the expected document type.
rootNamespace - Root element namespace URI.
rootElementName - Root element name.
Throws:
java.io.IOException - Thrown by input.
ParseException - XML failed to validate against the schema implemented by rootHandler.

parse

public void parse(XmlParser.ElementHandler rootHandler,
                  java.lang.String rootNamespace,
                  java.lang.String rootElementName)
           throws java.io.IOException,
                  ParseException
Parses XML from a content source provided to the parser at construction time.

Parameters:
rootHandler - The root element handler corresponding to the expected document type.
rootNamespace - Root element namespace URI.
rootElementName - Root element name.
Throws:
java.io.IOException - Thrown by reader.
ParseException - XML failed to validate against the schema implemented by rootHandler.

parse

protected void parse(org.xml.sax.InputSource is)
              throws java.io.IOException,
                     ParseException
Parses XML.

Parameters:
is - Supplies the XML to parse.
Throws:
java.io.IOException - Thrown by is.
ParseException - XML failed to validate against the schema implemented by rootHandler.

throwParseException

protected void throwParseException(ParseException e)
                            throws ParseException
Throws a parse exception with line/column information.

Throws:
ParseException

startElement

public void startElement(java.lang.String namespace,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
SAX callback.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String namespace,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
SAX callback.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] text,
                       int start,
                       int len)
                throws org.xml.sax.SAXException
SAX callback.

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] text,
                                int start,
                                int len)
                         throws org.xml.sax.SAXException
SAX callback.

Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
SAX callback.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler

startPrefixMapping

public void startPrefixMapping(java.lang.String alias,
                               java.lang.String uri)
SAX callback.

Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Overrides:
startPrefixMapping in class org.xml.sax.helpers.DefaultHandler

endPrefixMapping

public void endPrefixMapping(java.lang.String alias)
SAX callback.

Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Overrides:
endPrefixMapping in class org.xml.sax.helpers.DefaultHandler