Class Parser
- Object
-
- Parser
-
public class Parser extends Object
GeoTools XML parser.This parser uses a sax based driver to parse an input stream into a single object. For streaming look at
StreamingParser. If the source document being parsed as already been parsed into aDocumenttheDOMParserclass may be used.Schema Resolution
SeeConfigurationjavadocs for instructions on how to customize schema resolution. This is often desirable in the case that the instance document being parsed contains invalid uri's in schema imports and includes.- Author:
- Justin Deoliveira, The Open Planning Project
-
-
Constructor Summary
Constructors Constructor Description Parser(Configuration configuration)Creates a new instance of the parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<Integer>getEntityExpansionLimit()EntityResolvergetEntityResolver()Get EntityResolverParserNamespaceSupportgetNamespaces()Returns the namespace mappings maintained by the parser.QNamegetRootElementType()The type name of the root element being parsed.XSDSchema[]getSchemas()Returns the schema objects referenced by the instance document being parsed.List<URIHandler>getURIHandlers()Returns the list ofURIHandlerused when parsing schemas.List<Exception>getValidationErrors()Returns a list of any validation errors that occured while parsing.booleanisFailOnValidationError()booleanisForceParserDelegate()Flag that forces of the check forParserDelegateeven in cases where an element can be parsed normally.booleanisHandleMixedContent()Flag that controls whether the parser will process mixed content in a way that preserves order of child elements and text.booleanisValidating()Objectparse(InputStream input)Parses an instance documented defined by an input stream.Objectparse(Reader reader)Parses an instance documented defined by a reader.Objectparse(Source source)Parses an instance document defined by a transformer source.Objectparse(InputSource source)Parses an instance documented defined by a sax input source.protected SAXParserparser()protected SAXParserparser(boolean validate)voidsetContextCustomizer(ParserHandler.ContextCustomizer contextCustomizer)Allows the caller to customize the Pico context used for parsingvoidsetEntityExpansionLimit(Integer entityExpansionLimit)voidsetEntityResolver(EntityResolver entityResolver)Set EntityResolvervoidsetFailOnValidationError(boolean fail)Sets the flag which controls how the parser handles validation errors.voidsetForceParserDelegate(boolean forceParserDelegate)Sets Flag that forces of the check forParserDelegateeven in cases where an element can be parsed normally.voidsetHandleMixedContent(boolean handleMixedContent)Sets flag that controls whether the parser will process mixed content in a way that preserves order of child elements and text.voidsetRootElementType(QName typeName)Informs the parser of the type of the root element to be used in cases where it can not be inferred.voidsetStrict(boolean strict)Sets the strict parsing flag.voidsetValidating(boolean validating)Sets the flag controlling whether the parser should validate or not.voidvalidate(InputStream in)Validates an instance document defined by a input stream.voidvalidate(Reader reader)Validates an instance document defined by a reader.voidvalidate(InputSource source)Validates an instance document defined by a input source.
-
-
-
Constructor Detail
-
Parser
public Parser(Configuration configuration)
Creates a new instance of the parser.- Parameters:
configuration- The parser configuration, bindings and context, must never benull.
-
-
Method Detail
-
setContextCustomizer
public void setContextCustomizer(ParserHandler.ContextCustomizer contextCustomizer)
Allows the caller to customize the Pico context used for parsing
-
parse
public Object parse(InputStream input) throws IOException, SAXException, ParserConfigurationException
Parses an instance documented defined by an input stream.The object returned from the parse is the object which has been bound to the root element of the document. This method should only be called once for a single instance document.
- Returns:
- The object representation of the root element of the document.
- Throws:
IOExceptionSAXExceptionParserConfigurationException
-
parse
public Object parse(Reader reader) throws IOException, SAXException, ParserConfigurationException
Parses an instance documented defined by a reader.The object returned from the parse is the object which has been bound to the root element of the document. This method should only be called once for a single instance document.
- Returns:
- The object representation of the root element of the document.
- Throws:
IOExceptionSAXExceptionParserConfigurationException
-
parse
public Object parse(Source source) throws IOException, SAXException, ParserConfigurationException, TransformerException
Parses an instance document defined by a transformer source.Note: Currently this method reads the entire source into memory in order to validate it. If large documents must be parsed one of {@link #
- Parameters:
source- THe source of the instance document.- Returns:
- Throws:
IOExceptionSAXExceptionParserConfigurationExceptionTransformerException- Since:
- 2.6
-
parse
public Object parse(InputSource source) throws IOException, SAXException, ParserConfigurationException
Parses an instance documented defined by a sax input source.The object returned from the parse is the object which has been bound to the root element of the document. This method should only be called once for a single instance document.
- Returns:
- The object representation of the root element of the document.
- Throws:
IOExceptionSAXExceptionParserConfigurationException
-
setStrict
public void setStrict(boolean strict)
Sets the strict parsing flag.When set to
true, this will cause the parser to operate in a strict mode, which means that xml being parsed must be exactly correct with respect to the schema it references.Some examples of cases in which the parser will throw an exception while operating in strict mode:
- no 'schemaLocation' specified, or specified incorrectly
- element found which is not declared in the schema
- Parameters:
strict- The strict flag.
-
setValidating
public void setValidating(boolean validating)
Sets the flag controlling whether the parser should validate or not.- Parameters:
validating- Validation flag,trueto validate, otherwisefalse
-
isValidating
public boolean isValidating()
- Returns:
- Flag determining if the parser is validatin or not.
-
setFailOnValidationError
public void setFailOnValidationError(boolean fail)
Sets the flag which controls how the parser handles validation errors.When this flag is set, the parser will throw an exception when it encounters a validation error. Otherwise the error will be stored, retrievable from
getValidationErrors().The default behavior is to set this flag to
false. So client code should explicitly set this flag if it is desired that the exception be thrown when the validation error occurs.- Parameters:
fail- failure flag,trueto fail, otherwisefalse
-
isFailOnValidationError
public boolean isFailOnValidationError()
- Returns:
- The flag determining how the parser deals with validation errors.
-
setHandleMixedContent
public void setHandleMixedContent(boolean handleMixedContent)
Sets flag that controls whether the parser will process mixed content in a way that preserves order of child elements and text.- Since:
- 2.7
-
isHandleMixedContent
public boolean isHandleMixedContent()
Flag that controls whether the parser will process mixed content in a way that preserves order of child elements and text.By default the parser will simply concatenate blindly all child text and not preserve order with respect to other elements within a mixed content type.
- Since:
- 2.7
-
setForceParserDelegate
public void setForceParserDelegate(boolean forceParserDelegate)
Sets Flag that forces of the check forParserDelegateeven in cases where an element can be parsed normally.- Since:
- 8.0
- See Also:
isForceParserDelegate()
-
isForceParserDelegate
public boolean isForceParserDelegate()
Flag that forces of the check forParserDelegateeven in cases where an element can be parsed normally.By default the parser will only lookup parser delegates when the element is unrecognized with regard to the schema and can't be parsed normally.
- Since:
- 8.0
-
setEntityResolver
public void setEntityResolver(EntityResolver entityResolver)
Set EntityResolver
-
getEntityResolver
public EntityResolver getEntityResolver()
Get EntityResolver- Returns:
- entityResolver
-
setRootElementType
public void setRootElementType(QName typeName)
Informs the parser of the type of the root element to be used in cases where it can not be inferred.This method is used in cases where the element being parsed is not declared as global in the schema.
- Parameters:
typeName- The type name of the root element.- Since:
- 8.0
-
getRootElementType
public QName getRootElementType()
The type name of the root element being parsed.- See Also:
setRootElementType(QName)
-
getValidationErrors
public List<Exception> getValidationErrors()
Returns a list of any validation errors that occured while parsing.- Returns:
- A list of errors, or an empty list if none.
-
validate
public void validate(InputStream in) throws IOException, SAXException, ParserConfigurationException
Validates an instance document defined by a input stream.Clients should call
getValidationErrors()after this method to retrieve any validation errors that occurred. Clients do not need to callsetValidating(boolean)when using this method to validate.This method does not do any of the work done by
parse(InputSource), it only validates.
-
validate
public void validate(Reader reader) throws IOException, SAXException, ParserConfigurationException
Validates an instance document defined by a reader.Clients should call
getValidationErrors()after this method to retrieve any validation errors that occurred. Clients do not need to callsetValidating(boolean)when using this method to validate.This method does not do any of the work done by
parse(InputSource), it only validates.
-
validate
public void validate(InputSource source) throws IOException, SAXException, ParserConfigurationException
Validates an instance document defined by a input source.Clients should call
getValidationErrors()after this method to retrieve any validation errors that occurred. Clients do not need to callsetValidating(boolean)when using this method to validate.This method does not do any of the work done by
parse(InputSource), it only validates.
-
getSchemas
public XSDSchema[] getSchemas()
Returns the schema objects referenced by the instance document being parsed. This method can only be called after a successful parse has begun.- Returns:
- The schema objects used to parse the document, or null if parsing has not commenced.
-
getNamespaces
public ParserNamespaceSupport getNamespaces()
Returns the namespace mappings maintained by the parser.Clients may register additional namespace mappings. This is useful when an application wishes to provide some "default" namespace mappings.
Clients should register namespace mappings in the current "context", ie do not call
NamespaceSupport.pushContext(). Example:Parser parser = new Parser( ... ); parser.getNamespaces().declarePrefix( "foo", "http://www.foo.com" ); ...- Returns:
- The namespace support containing prefix to uri mappings.
- Since:
- 2.4
-
getURIHandlers
public List<URIHandler> getURIHandlers()
Returns the list ofURIHandlerused when parsing schemas.URI handlers are invoked to handle external references that occur during parsing.
- Since:
- 2.7
-
parser
protected SAXParser parser() throws ParserConfigurationException, SAXException
-
parser
protected SAXParser parser(boolean validate) throws ParserConfigurationException, SAXException
-
setEntityExpansionLimit
public void setEntityExpansionLimit(Integer entityExpansionLimit)
-
-