public class Parser extends Object
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 a Document
the DOMParser
class may be used.
Configuration
javadocs 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.Modifier and Type | Class and Description |
---|---|
static interface |
Parser.Properties
Deprecated.
|
Constructor and Description |
---|
Parser(Configuration configuration)
Creates a new instance of the parser.
|
Parser(Configuration configuration,
InputStream input)
Deprecated.
use
Parser(Configuration) and parse(InputStream) . |
Parser(Configuration configuration,
String input)
Deprecated.
use
Parser(Configuration) and parse(InputStream) . |
Modifier and Type | Method and Description |
---|---|
EntityResolver |
getEntityResolver()
Get EntityResolver
|
ParserNamespaceSupport |
getNamespaces()
Returns the namespace mappings maintained by the parser.
|
QName |
getRootElementType()
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 of
URIHandler used when parsing schemas. |
List |
getValidationErrors()
Returns a list of any validation errors that occured while parsing.
|
boolean |
isFailOnValidationError() |
boolean |
isForceParserDelegate()
Flag that forces of the check for
ParserDelegate even in cases where an element can
be parsed normally. |
boolean |
isHandleMixedContent()
Flag that controls whether the parser will process mixed content in a way that preserves
order of child elements and text.
|
boolean |
isValidating() |
Object |
parse()
Deprecated.
|
Object |
parse(InputSource source)
Parses an instance documented defined by a sax input source.
|
Object |
parse(InputStream input)
Parses an instance documented defined by an input stream.
|
Object |
parse(Reader reader)
Parses an instance documented defined by a reader.
|
Object |
parse(Source source)
Parses an instance document defined by a transformer source.
|
protected SAXParser |
parser() |
protected SAXParser |
parser(boolean validate) |
void |
setContextCustomizer(ParserHandler.ContextCustomizer contextCustomizer)
Allows the caller to customize the Pico context used for parsing
|
void |
setEntityResolver(EntityResolver entityResolver)
Set EntityResolver
|
void |
setFailOnValidationError(boolean fail)
Sets the flag which controls how the parser handles validation errors.
|
void |
setForceParserDelegate(boolean forceParserDelegate)
Sets Flag that forces of the check for
ParserDelegate even in cases where an element
can be parsed normally. |
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.
|
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.
|
void |
setStrict(boolean strict)
Sets the strict parsing flag.
|
void |
setValidating(boolean validating)
Sets the flag controlling wether the parser should validate or not.
|
void |
validate(InputSource source)
Validates an instance document defined by a input source.
|
void |
validate(InputStream in)
Validates an instance document defined by a input stream.
|
void |
validate(Reader reader)
Validates an instance document defined by a reader.
|
public Parser(Configuration configuration)
configuration
- The parser configuration, bindings and context, must never be null
.public Parser(Configuration configuration, String input) throws IOException, URISyntaxException
Parser(Configuration)
and parse(InputStream)
.configuration
- Object representing the configuration of the parser.input
- A uri representing the instance document to be parsed.ParserConfigurationException
SAXException
- If a sax parser can not be created.URISyntaxException
- If input
is not a valid uri.IOException
public Parser(Configuration configuration, InputStream input)
Parser(Configuration)
and parse(InputStream)
.configuration
- Object representing the configuration of the parser.input
- The stream representing the instance document to be parsed.public void setContextCustomizer(ParserHandler.ContextCustomizer contextCustomizer)
contextCustomizer
- public Object parse() throws IOException, SAXException, ParserConfigurationException
parse(InputStream)
IOException
SAXException
ParserConfigurationException
public Object parse(InputStream input) throws IOException, SAXException, ParserConfigurationException
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.
IOException
SAXException
ParserConfigurationException
public Object parse(Reader reader) throws IOException, SAXException, ParserConfigurationException
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.
IOException
SAXException
ParserConfigurationException
public Object parse(Source source) throws IOException, SAXException, ParserConfigurationException, TransformerException
Note: Currently this method reads the entire source into memory in order to validate it. If large documents must be parsed one of {@link #
source
- THe source of the instance document.IOException
SAXException
ParserConfigurationException
TransformerException
public Object parse(InputSource source) throws IOException, SAXException, ParserConfigurationException
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.
IOException
SAXException
ParserConfigurationException
public void setStrict(boolean strict)
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:
strict
- The strict flag.public void setValidating(boolean validating)
validating
- Validation flag, true
to validate, otherwise false
public boolean isValidating()
public void setFailOnValidationError(boolean fail)
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.
fail
- failure flag, true
to fail, otherwise false
public boolean isFailOnValidationError()
public void setHandleMixedContent(boolean handleMixedContent)
public boolean isHandleMixedContent()
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.
public void setForceParserDelegate(boolean forceParserDelegate)
ParserDelegate
even in cases where an element
can be parsed normally.isForceParserDelegate()
public boolean isForceParserDelegate()
ParserDelegate
even 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.
public void setEntityResolver(EntityResolver entityResolver)
entityResolver
- public EntityResolver getEntityResolver()
public void setRootElementType(QName typeName)
This method is used in cases where the element being parsed is not declared as global in the schema.
typeName
- The type name of the root element.public QName getRootElementType()
setRootElementType(QName)
public List getValidationErrors()
public void validate(InputStream in) throws IOException, SAXException, ParserConfigurationException
Clients should call getValidationErrors()
after this method to retrieve any
validation errors that occurred. Clients do not need to call setValidating(boolean)
when using this method to validate.
This method does not do any of the work done by parse(InputSource)
, it only
validates.
public void validate(Reader reader) throws IOException, SAXException, ParserConfigurationException
Clients should call getValidationErrors()
after this method to retrieve any
validation errors that occurred. Clients do not need to call setValidating(boolean)
when using this method to validate.
This method does not do any of the work done by parse(InputSource)
, it only
validates.
public void validate(InputSource source) throws IOException, SAXException, ParserConfigurationException
Clients should call getValidationErrors()
after this method to retrieve any
validation errors that occurred. Clients do not need to call setValidating(boolean)
when using this method to validate.
This method does not do any of the work done by parse(InputSource)
, it only
validates.
public XSDSchema[] getSchemas()
public ParserNamespaceSupport getNamespaces()
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" );
...
public List<URIHandler> getURIHandlers()
URIHandler
used when parsing schemas.
URI handlers are invoked to handle external references that occur during parsing.
protected SAXParser parser() throws ParserConfigurationException, SAXException
protected SAXParser parser(boolean validate) throws ParserConfigurationException, SAXException
Copyright © 1996–2019 Geotools. All rights reserved.