public class XmlParser extends org.xml.sax.helpers.DefaultHandler implements WireFormatParser
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.
XmlParser.ElementHandler| Modifier and Type | Field and Description |
|---|---|
protected Map<String,Stack<com.google.gdata.wireformats.XmlParser.NamespaceDecl>> |
namespaceMap
Set of all namespace declarations valid at the current location.
|
protected StreamProperties |
props
Input properties for parsing
|
protected String |
rootElementName
Root element name.
|
protected com.google.gdata.wireformats.XmlParser.ElementHandler |
rootHandler
Root element handler.
|
protected String |
rootNamespace
Root element namespace URI.
|
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
characters(char[] text,
int start,
int len)
SAX callback.
|
protected XmlHandler |
createRootHandler(ValidationContext vc,
Element element,
ElementMetadata<?,?> metadata)
Create the xml handler for the root element.
|
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 void |
throwParseException(ParseException e)
Throws a parse exception with line/column information.
|
endDocument, error, fatalError, notationDecl, processingInstruction, resolveEntity, skippedEntity, startDocument, unparsedEntityDecl, warningprotected final StreamProperties props
protected com.google.gdata.wireformats.XmlParser.ElementHandler rootHandler
protected String rootNamespace
protected String rootElementName
public XmlParser(StreamProperties props, Reader r, Charset cs)
props - stream properties for parsingr - reader where input is retrieved fromcs - character set used to encode inputpublic XmlParser(StreamProperties props, XmlEventSource eventSource)
props - stream properties for parsingeventSource - event sourcepublic Element parse(Element element) throws IOException, ParseException, ContentValidationException
WireFormatParserparse in interface WireFormatParserelement - root of parsed element treeIOException - if content cannot be accessedParseException - if content cannot be parsed invalidContentValidationException - if content fails metadata validationprotected XmlHandler createRootHandler(ValidationContext vc, Element element, ElementMetadata<?,?> metadata)
protected void throwParseException(ParseException e) throws ParseException
ParseExceptionpublic void startElement(String namespace, String localName, String qName, Attributes attrs) throws SAXException
startElement in interface ContentHandlerstartElement in class org.xml.sax.helpers.DefaultHandlerSAXExceptionpublic void endElement(String namespace, String localName, String qName) throws SAXException
endElement in interface ContentHandlerendElement in class org.xml.sax.helpers.DefaultHandlerSAXExceptionpublic void characters(char[] text,
int start,
int len)
throws SAXException
characters in interface ContentHandlercharacters in class org.xml.sax.helpers.DefaultHandlerSAXExceptionpublic void ignorableWhitespace(char[] text,
int start,
int len)
throws SAXException
ignorableWhitespace in interface ContentHandlerignorableWhitespace in class org.xml.sax.helpers.DefaultHandlerSAXExceptionpublic void setDocumentLocator(Locator newLocator)
setDocumentLocator in interface ContentHandlersetDocumentLocator in class org.xml.sax.helpers.DefaultHandlerpublic void startPrefixMapping(String alias, String uri)
startPrefixMapping in interface ContentHandlerstartPrefixMapping in class org.xml.sax.helpers.DefaultHandlerpublic void endPrefixMapping(String alias)
endPrefixMapping in interface ContentHandlerendPrefixMapping in class org.xml.sax.helpers.DefaultHandlerCopyright © 2012. All Rights Reserved.