Class StAXOMBuilder
- java.lang.Object
-
- org.apache.axiom.om.impl.builder.StAXBuilder
-
- org.apache.axiom.om.impl.builder.StAXOMBuilder
-
- All Implemented Interfaces:
OMXMLParserWrapper
- Direct Known Subclasses:
StAXSOAPModelBuilder,XOPAwareStAXOMBuilder
public class StAXOMBuilder extends StAXBuilder
StAX based builder that produces a pure XML infoset compliant object model.This class supports the
XMLStreamReaderextension defined byDataHandlerReaderas well as the legacy extension mechanism defined in the documentation ofXMLStreamReaderUtils.Error handling
Usually, code that uses StAX directly just stops processing of an XML document once the first parsing error has been reported. However, since Axiom uses deferred parsing, and client code accesses the XML infoset using an object model, things are more complicated. Indeed, if the XML document is not well formed, the corresponding error might be reported as a runtime exception by any call to a method of an OM node.Typically the client code will have some error handling that will intercept runtime exceptions and take appropriate action. Very often this error handling code might want to access the object model again, for example to log the request that caused the failure. This causes no problem except if the runtime exception was caused by a parsing error, in which case Axiom would again try to pull events from the parser.
This would lead to a situation where Axiom accesses a parser that has reported a parsing error before. While one would expect that after a first error reported by the parser, all subsequent invocations of the parser will fail, this is not the case for all parsers (at least not in all situations). Instead, the parser might be left in an inconsistent state after the error. E.g. WSCOMMONS-372 describes a case where Woodstox encounters an error in
XMLStreamReader.getText()but continues to return (incorrect) events afterwards. The explanation for this behaviour might be that the situation described here is quite uncommon when StAX is used directly (i.e. not through Axiom).To avoid this, the builder remembers exceptions thrown by the parser and rethrows them during a call to
next().
-
-
Field Summary
-
Fields inherited from class org.apache.axiom.om.impl.builder.StAXBuilder
_isClosed, _releaseParserOnClose, cache, charEncoding, customBuilderForPayload, customBuilders, dataHandlerReader, document, done, elementLevel, lastNode, maxDepthForCustomBuilders, omfactory, parser, parserAccessed, parserException
-
-
Constructor Summary
Constructors Constructor Description StAXOMBuilder()Init() *must* be called after creating the builder using this constructor.StAXOMBuilder(InputStream inStream)StAXOMBuilder(String filePath)StAXOMBuilder(XMLStreamReader parser)Constructor StAXOMBuilder.StAXOMBuilder(OMFactory ombuilderFactory, XMLStreamReader parser)Constructor StAXOMBuilder.StAXOMBuilder(OMFactory factory, XMLStreamReader parser, OMElement element)Constructor linked to existing element.StAXOMBuilder(OMFactory factory, XMLStreamReader parser, OMElement element, String characterEncoding)Constructor linked to existing element.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected OMNodecreateComment()Method createOMText.protected OMNodecreateDTD()Method createDTD.protected OMNodecreateNextOMElement()Creates a new OMElement using either a CustomBuilder or the default Builder mechanism.protected OMNodecreateOMElement()Method createOMElement.protected OMNodecreatePI()Method createPI.protected StringcreatePrefix()protected OMNodecreateWithCustomBuilder(CustomBuilder customBuilder, OMFactory factory)protected voidendElement()OMElementgetDocumentElement()Get the document element, i.e.OMElementgetDocumentElement(boolean discardDocument)Get the document element, optionally discarding the document.booleanisLookahead()Check if the node for the current token has already been created or if the parser is ahead of the builder.booleanisNamespaceURIInterning()protected voidlogParserState()Dump the current event of the parser.booleanlookahead()This method looks ahead to the next start element.intnext()Method next.protected voidprocessNamespaceData(OMElement node)Method processNamespaceData.voidsetDoDebug(boolean doDebug)Deprecated.voidsetNamespaceURIInterning(boolean b)Set namespace uri interning-
Methods inherited from class org.apache.axiom.om.impl.builder.StAXBuilder
close, createOMText, discard, getAttributeCount, getAttributeName, getAttributeNamespace, getAttributePrefix, getBuilderType, getCharacterEncoding, getCharsetEncoding, getCustomBuilder, getDocument, getLastNode, getName, getNamespace, getNamespaceCount, getNamespacePrefix, getNamespaceUri, getParser, getPrefix, getReaderProperty, getRegisteredContentHandler, getText, init, isCache, isClosed, isCompleted, processAttributes, registerCustomBuilder, registerCustomBuilderForPayload, registerExternalContentHandler, releaseParserOnClose, reset, setCache, setOMBuilderFactory
-
-
-
-
Constructor Detail
-
StAXOMBuilder
public StAXOMBuilder(OMFactory ombuilderFactory, XMLStreamReader parser)
Constructor StAXOMBuilder.- Parameters:
ombuilderFactory-parser-
-
StAXOMBuilder
public StAXOMBuilder(OMFactory factory, XMLStreamReader parser, OMElement element, String characterEncoding)
Constructor linked to existing element.- Parameters:
factory-parser-element-characterEncoding- of existing element
-
StAXOMBuilder
public StAXOMBuilder(OMFactory factory, XMLStreamReader parser, OMElement element)
Constructor linked to existing element.- Parameters:
factory-parser-element-
-
StAXOMBuilder
public StAXOMBuilder(String filePath) throws XMLStreamException, FileNotFoundException
- Parameters:
filePath- - Path to the XML file- Throws:
XMLStreamExceptionFileNotFoundException
-
StAXOMBuilder
public StAXOMBuilder(XMLStreamReader parser)
Constructor StAXOMBuilder.- Parameters:
parser-
-
StAXOMBuilder
public StAXOMBuilder(InputStream inStream) throws XMLStreamException
- Parameters:
inStream- - instream which contains the XML- Throws:
XMLStreamException
-
StAXOMBuilder
public StAXOMBuilder()
Init() *must* be called after creating the builder using this constructor.
-
-
Method Detail
-
next
public int next() throws OMExceptionMethod next.- Specified by:
nextin interfaceOMXMLParserWrapper- Specified by:
nextin classStAXBuilder- Returns:
- Returns int.
- Throws:
OMException
-
createNextOMElement
protected OMNode createNextOMElement()
Creates a new OMElement using either a CustomBuilder or the default Builder mechanism.- Returns:
- TODO
-
createWithCustomBuilder
protected OMNode createWithCustomBuilder(CustomBuilder customBuilder, OMFactory factory)
-
logParserState
protected void logParserState()
Dump the current event of the parser.
-
createOMElement
protected OMNode createOMElement() throws OMException
Method createOMElement.- Specified by:
createOMElementin classStAXBuilder- Returns:
- Returns OMNode.
- Throws:
OMException
-
createComment
protected OMNode createComment() throws OMException
Method createOMText.- Returns:
- Returns OMNode.
- Throws:
OMException
-
createDTD
protected OMNode createDTD() throws OMException
Method createDTD.- Returns:
- Returns OMNode.
- Throws:
OMException
-
createPI
protected OMNode createPI() throws OMException
Method createPI.- Returns:
- Returns OMNode.
- Throws:
OMException
-
endElement
protected void endElement()
-
getDocumentElement
public OMElement getDocumentElement()
Description copied from interface:OMXMLParserWrapperGet the document element, i.e. the root element of the document. Using this method is equivalent to usingOMDocument.getOMDocumentElement()on the document returned byOMXMLParserWrapper.getDocument().- Returns:
- the document element
-
getDocumentElement
public OMElement getDocumentElement(boolean discardDocument)
Description copied from interface:OMXMLParserWrapperGet the document element, optionally discarding the document. The return value of this method is the same asOMXMLParserWrapper.getDocumentElement(). However, if thediscardDocumentparameter is set totrue, then the document element is removed from the document and the document itself is discarded. In contrast to usingOMNode.detach()this will not build the element. The implementation also ensures that the element is not built when it is added to another OM tree. This makes it possible to add the content of a document to an existing OM tree while preserving the deferred parsing feature. It is even possible to create an OM tree where different subtrees are associated with different builder instances.- Parameters:
discardDocument- specifies whether the document should be discarded- Returns:
- the document element
-
processNamespaceData
protected void processNamespaceData(OMElement node)
Method processNamespaceData.- Specified by:
processNamespaceDatain classStAXBuilder- Parameters:
node-
-
setDoDebug
public void setDoDebug(boolean doDebug)
Deprecated.- Parameters:
doDebug-
-
createPrefix
protected String createPrefix()
-
setNamespaceURIInterning
public void setNamespaceURIInterning(boolean b)
Set namespace uri interning- Parameters:
b-
-
isNamespaceURIInterning
public boolean isNamespaceURIInterning()
- Returns:
- if namespace uri interning
-
lookahead
public boolean lookahead()
This method looks ahead to the next start element.- Returns:
- true if successful
-
isLookahead
public boolean isLookahead()
Check if the node for the current token has already been created or if the parser is ahead of the builder.- Returns:
- A return value of
trueindicates that the parser is one token ahead of the builder, i.e. that the node for the current token has not been created yet. In this caseStAXBuilder.getLastNode()returns the node corresponding to the previous token. This state can only be reached by a call tolookahead(), and the current token is always aSTART_ELEMENT. The information related to that element can be obtained by calls toStAXBuilder.getName(),StAXBuilder.getNamespace(),StAXBuilder.getPrefix(),StAXBuilder.getAttributeCount(),StAXBuilder.getAttributeName(int),StAXBuilder.getAttributeNamespace(int),StAXBuilder.getAttributePrefix(int),StAXBuilder.getNamespaceCount(),StAXBuilder.getNamespacePrefix(int)andStAXBuilder.getNamespaceUri(int).A return value of
falseindicates that the node corresponding to the current token hold by the parser has already been created, i.e.StAXBuilder.getLastNode()returns the node corresponding to the current token.
-
-