public class

XmlParser

extends DefaultHandler
implements WireFormatParser
java.lang.Object
   ↳ org.xml.sax.helpers.DefaultHandler
     ↳ com.google.gdata.wireformats.XmlParser

Class Overview

XML parser, branched from XmlParser.

The plan is to integrate it tighter with the WireFormatParser interface, remove unnecessary interfaces and parameters (in our new data model, there is only one XmlParser.ElementHandler), and move it away from the util package.

Existing data classes continue to use the old XML parser until they've been migrated to the new data model, at which point they will start to use the new 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(Element) method.

See Also
  • XmlParser.ElementHandler

Summary

Fields
XmlParser.ElementHandler curHandler Top of the element handler stack.
ArrayList<XmlNamespace> elementNamespaces Namespace declarations for the current element.
private final XmlEventSource eventSource XML event source, usually a SAX parser.
Locator locator Document locator used to get line and column numbers for SAX events.
private static final Logger logger
protected Map<String, Stack<XmlParser.NamespaceDecl>> namespaceMap Set of all namespace declarations valid at the current location.
protected final StreamProperties props Input properties for parsing
protected String rootElementName Root element name.
protected XmlParser.ElementHandler rootHandler Root element handler.
protected String rootNamespace Root element namespace URI.
int unrecognizedElements Number of unrecognized elements on the stack.
Public Constructors
XmlParser(StreamProperties props, Reader r, Charset cs)
Construct XML parser for given reader.
XmlParser(StreamProperties props, XmlEventSource eventSource)
Construct XML parser for a given event source.
Public Methods
void characters(char[] text, int start, int len)
SAX callback.
void endElement(String namespace, String localName, String qName)
SAX callback.
void endPrefixMapping(String alias)
SAX callback.
void ignorableWhitespace(char[] text, int start, int len)
SAX callback.
Element parse(Element element)
Parse content from a source provided to the parser at construction time.
void setDocumentLocator(Locator newLocator)
SAX callback.
void startElement(String namespace, String localName, String qName, Attributes attrs)
SAX callback.
void startPrefixMapping(String alias, String uri)
SAX callback.
Protected Methods
XmlHandler createRootHandler(ValidationContext vc, Element element, ElementMetadata<?, ?> metadata)
Create the xml handler for the root element.
void throwParseException(ParseException e)
Throws a parse exception with line/column information.
[Expand]
Inherited Methods
From class org.xml.sax.helpers.DefaultHandler
From class java.lang.Object
From interface com.google.gdata.wireformats.WireFormatParser
From interface org.xml.sax.ContentHandler
From interface org.xml.sax.DTDHandler
From interface org.xml.sax.EntityResolver
From interface org.xml.sax.ErrorHandler

Fields

XmlParser.ElementHandler curHandler

Top of the element handler stack.

ArrayList<XmlNamespace> elementNamespaces

Namespace declarations for the current element. Valid during startElement(String, String, String, Attributes).

private final XmlEventSource eventSource

XML event source, usually a SAX parser.

Locator locator

Document locator used to get line and column numbers for SAX events.

private static final Logger logger

protected Map<String, Stack<XmlParser.NamespaceDecl>> namespaceMap

Set of all namespace declarations valid at the current location. Includes namespace declarations from all ancestor elements.

protected final StreamProperties props

Input properties for parsing

protected String rootElementName

Root element name.

protected XmlParser.ElementHandler rootHandler

Root element handler.

protected String rootNamespace

Root element namespace URI.

int unrecognizedElements

Number of unrecognized elements on the stack.

Public Constructors

public XmlParser (StreamProperties props, Reader r, Charset cs)

Construct XML parser for given reader.

Parameters
props Stream properties for parsing
r Reader where input is retrieved from
cs Character set used to encode input

public XmlParser (StreamProperties props, XmlEventSource eventSource)

Construct XML parser for a given event source.

Parameters
props Stream properties for parsing
eventSource Event source

Public Methods

public void characters (char[] text, int start, int len)

SAX callback.

Parameters
text
start
len
Throws
SAXException

public void endElement (String namespace, String localName, String qName)

SAX callback.

Parameters
namespace
localName
qName
Throws
SAXException

public void endPrefixMapping (String alias)

SAX callback.

Parameters
alias

public void ignorableWhitespace (char[] text, int start, int len)

SAX callback.

Parameters
text
start
len
Throws
SAXException

public Element parse (Element element)

Parse content from a source provided to the parser at construction time.

Parameters
element Root of parsed element tree
Returns
  • root of parsed element tree

public void setDocumentLocator (Locator newLocator)

SAX callback.

Parameters
newLocator

public void startElement (String namespace, String localName, String qName, Attributes attrs)

SAX callback.

Parameters
namespace
localName
qName
attrs
Throws
SAXException

public void startPrefixMapping (String alias, String uri)

SAX callback.

Parameters
alias
uri

Protected Methods

protected XmlHandler createRootHandler (ValidationContext vc, Element element, ElementMetadata<?, ?> metadata)

Create the xml handler for the root element. Subclasses can supply their own parse handlers.

Parameters
vc
element
metadata

protected void throwParseException (ParseException e)

Throws a parse exception with line/column information.

Parameters
e