public class XMLSAXParser extends Object implements InputSourceCaching
XMLSAXParser parser = new XMLSAXParser("My Parser"); parser.setValidating(true); parser.setHandlerDTD(dtd); parser.setHandler(handler); parse.parse(file);
XMLSAXParser parser = new XMLSAXParser("My Parser"); parser.setValidating(true); parser.setSchema(schema); parser.setHandler(handler); parse.parse(file);
XMLSAXParser parser = new XMLSAXParser("My Parser"); parser.setValidating(true); parser.setSchema(schema); parser.setHandler(handler); parser.showExceptions(false); parse.parse(file); if (handler.hasParserExceptions()) { List>ResolverSAXHandler.ExceptionResult> results = handler.getExceptionResults(); }
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ERROR_TITLE
The default error title used for the dialog.
|
protected URL |
dtd
The DTD used to validate the parsing.
|
protected String |
encoding
The encoding.
|
protected String |
errorTitle
The error title.
|
protected ParserExceptionListener |
exListener
The listener which is fired if exceptions (including SAXExceptions) are encountered during the parsing.
|
protected Map<String,Boolean> |
features
The features of the parser factory.
|
protected Throwable |
firstException
The first throwable thrown by the parser, if the parsing failed beca use of an exception in the handler.
|
protected ResolverSAXHandler |
handler
The associated SAX handler.
|
protected boolean |
hasDefaultLSResolver
True if a default resource resolver should be set even if no resolver has been specified.
|
protected boolean |
isNameSpaceAware
True if the parser is namespace-aware.
|
protected boolean |
isSchemaAware
True if the parser is XML-Schema aware.
|
protected boolean |
isShowingExceptions
True if the parser should show exceptions.
|
protected boolean |
isShowingWarnings
True if the parser should show warnings.
|
protected boolean |
isValidating
True if the parser is validating.
|
protected boolean |
keepPrefixes
True if the parser keep the prefixes.
|
protected Map<String,Object> |
properties
The properties of the parser.
|
protected String |
publicID
The public ID used to validate the parsing.
|
protected EntityListResolver |
resolver
The associated entity list resolver.
|
protected URL |
schemaURL
The URL of the schema used for parsing the content, if a Schema is used for the parsing.
|
CACHING_DEFAULT, CACHING_SOURCES, NOT_CACHING_SOURCES
Constructor and Description |
---|
XMLSAXParser()
Create a new non validating XML SAX Parser.
|
XMLSAXParser(boolean isValidating)
Create a new XML SAX Parser.
|
XMLSAXParser(ResolverSAXHandler handler)
Create a new non validating XML SAX Parser.
|
XMLSAXParser(String errorTitle)
Create a new non validating XML SAX Parser.
|
XMLSAXParser(String errorTitle,
boolean isValidating)
Create a new XML SAX Parser.
|
Modifier and Type | Method and Description |
---|---|
void |
allowSchemaValidation(boolean isSchemaAware)
Set if the parser allow a Schema validation.
|
protected boolean |
configureParser(SAXParserFactory fac,
InputSource source)
Configure the parser and validate an XML source if a Schema URL has been provided.
|
protected boolean |
configureSchemaValidation(SAXParserFactory factory)
Configure the features of the SAX parser factory for validation.
|
Charset |
getCharset()
Return the charset.
|
ClassLoader |
getClassLoader()
Return the ClassLoader.
|
URL |
getDefaultBaseDirectory()
Return the default base directory which will be used for relative URLs if the parent URL is unknown.
|
String |
getEncoding()
Return the encoding (can be null if not set).
|
ParserExceptionListener |
getExceptionListener()
Return the listener which will be fired if exceptions (including SAXExceptions) are encountered during the parsing.
|
Map<String,Boolean> |
getFeatures()
Return the parser factory features.
|
Throwable |
getFirstException()
Return the first exception encountered by the parser if it encountered a Java exception during the parsing.
|
ResolverSAXHandler |
getHandler()
Return the SAX Handler used for the parsing.
|
short |
getInputSourceCaching()
Return the InputSources caching behavior.
|
Locale |
getLocale()
Return the locale for SAX and validation error messages.
|
URL |
getParsedURL()
Return the URL which was parsed by this Parser.
|
Map<String,Object> |
getProperties()
Return the parser properties.
|
static SAXParserFactory |
getStaticParserFactory()
Return a static SAX parser factory.
|
static SAXParserFactory |
getStaticParserFactory(ClassLoader loader)
Return a SAX parser factory.
|
protected void |
handleException(String message)
Handle an exception message (not a SAX Exception).
|
protected void |
handleException(String title,
Exception e)
Handle an exception (not a SAX Exception).
|
void |
handleSAXExceptions(String title)
Handle the SAX Exceptions resulting from the parsing.
|
protected void |
handleSAXExceptions(String title,
List<ResolverSAXHandler.ExceptionResult> results)
Handle the SAX Exceptions resulting from the parsing.
|
boolean |
hasDefaultLSResourceResolver()
Return true if a default LSResourceResolver must be used even if no specific resolver has been set
|
boolean |
hasException()
Return true if the parser encountered a Java exception during the parsing.
|
boolean |
hasSchemaValidator()
Return true if this SAX Parser uses a Schema Validator to validate XML files (true by default).
|
boolean |
hasStoppedParsing()
Return true if the parsing has been stopped by the handler before the end of the XML stream.
|
boolean |
isAllowingInternalReaders()
Return true if internal readers creation are allowed in this parser instance.
|
boolean |
isAllowingNestableConnections()
Return true if nestable connections are allowed (ie, an xml file inside a zip file, for example).
|
boolean |
isConcatenatingIncludes()
Return true if xInclude content must be concatenated.
|
static boolean |
isDefaultAllowingNestableConnections()
Return true if nestable connections are allowed by default (ie, an xml file inside a zip file, for example).
|
boolean |
isKeepingPrefixes()
Return true if the parser must keep the prefixes.
|
boolean |
isNameSpaceAware()
Return true if the parser is namespace-aware.
|
static boolean |
isRevertingOldReaderBehavior()
True if the parser is reverting to the previous reader behavior prior to 1.2.7.
|
boolean |
isShowingExceptions()
Return true if the parser shows the exceptions encountered during the parsing.
|
boolean |
isShowingWarnings()
Return true if the parser shows the warnings encountered during the parsing.
|
boolean |
isValidating()
Return true if the parser is validating.
|
boolean |
isXIncludeAware()
Return true if the parser is XInclude-aware.
|
boolean |
isXIncludeSupported()
Return true if the parser support XInclude.
|
void |
parse(File file)
Parse an XML file.
|
void |
parse(File file,
BufferedReaderFactory bufFac)
Parse an XML file.
|
void |
parse(InputStream stream)
Parse an XML file with an input Stream.
|
void |
parse(Reader reader)
Parse an XML file with a Reader.
|
void |
parse(ReaderProvider provider)
Parse an XML file with a ReaderProvider.
|
void |
parse(URL url)
Parse an XML file with an URL.
|
void |
parse(URL url,
BufferedReaderFactory bufFac)
Parse an XML file with an URL.
|
static void |
resetSAXParserFactoryImplementation()
Reset the SAXParser factory implementation to the default.
|
static void |
revertOldReaderBehavior(boolean revert)
Revert to the previous reader behavior prior to 1.2.7.
|
void |
setAllowInternalReaders(boolean allowInternalReaders)
Set if internal readers creation are allowed in this parser instance.
|
void |
setAllowNestableConnections(boolean allow)
Set if nestable connections are allowed (ie, an xml file inside a zip file, for example).
|
void |
setClassLoader(ClassLoader loader)
Set the ClassLoader.
|
void |
setConcatenateIncludes(boolean concatenateIncludes)
Set if xInclude content must be concatenated.
|
static void |
setDefaultAllowInternalReaders(boolean allowInternalReaders)
Set if internal readers creation are allowed by default.
|
static void |
setDefaultAllowNestableConnections(boolean allow)
Set if nestable connections are allowed by default (ie, an xml file inside a zip file, for example).
|
void |
setDefaultBaseDirectory(URL baseDir)
Set the default base directory which will be used for relative URLs if the parent URL is unknown.
|
static void |
setDefaultLocale(Locale locale)
Set the default locale for all subsequent SAX and validation error messages.
|
void |
setDefaultLSResourceResolver(boolean hasDefaultLSResolver)
Set if a default LSResourceResolver must be used even if no specific resolver has been set.
|
void |
setEncoding(String encoding)
Set the encoding.
|
void |
setEntityListResolver(EntityListResolver resolver)
Set the EntityListResolver which can manage entity resolution in XML files.
|
void |
setErrorTitle(String errorTitle)
Set the parser error dialog title.
|
void |
setExceptionListener(ParserExceptionListener listener)
Set the listener which will be fired if exceptions (including SAXExceptions) are encountered during the parsing.
|
void |
setFeature(String name,
boolean value)
Set a parser factory feature.
|
void |
setFeatures(Map<String,Boolean> features)
Set the parser factory features.
|
void |
setHandler(ResolverSAXHandler handler)
Set the SAX Handler used for the parsing.
|
void |
setHandlerDTD(URL dtd,
String publicID)
Set the DTD used to validate the parsing.
|
void |
setInputSourceCaching(short cachingType)
Set the InputSources caching behavior.
|
void |
setKeepPrefixes(boolean keepPrefixes)
Set if the parser must keep the prefixes.
|
void |
setLocale(Locale locale)
Set the locale for SAX and validation error messages.
|
void |
setNameSpaceAware(boolean isNameSpaceAware)
Set if the parser is namespace-aware.
|
void |
setParserConfiguration(XMLParserConfiguration config)
Set the parser configuration.
|
void |
setProperties(Map<String,Object> properties)
Set the parser properties.
|
void |
setProperty(String name,
Object value)
Set a parser property.
|
static void |
setSAXParserFactoryImplementation(String factoryClassName)
Set the SAXParser factory implementation.
|
void |
setSchema(URL schemaURL)
Set the optional Schema URL to use for Schema validation.
|
void |
setSchemaValidator(boolean hasSchemaValidator)
Set if this SAX Parser uses a Schema Validator to validate XML files (true by default).
|
void |
setValidating(boolean isValidating)
Set if the parser is validating.
|
void |
setXIncludeAware(boolean xIncludeAware)
Set if the parser is XInclude-aware.
|
void |
showExceptions(boolean isShowingExceptions)
Set if the parser shows the exceptions encountered during the parsing.
|
void |
showWarnings(boolean isShowingWarnings)
Set if the parser shows the warnings encountered during the parsing.
|
Object |
stoppedParsingAdditionalContent()
Return the optional additional content returned while the parsing has been stopped before the end of the XML Stream).
|
protected ResolverSAXHandler handler
protected EntityListResolver resolver
protected boolean hasDefaultLSResolver
protected URL dtd
protected String publicID
protected boolean isValidating
protected boolean isShowingExceptions
protected boolean isShowingWarnings
protected boolean isSchemaAware
public static final String DEFAULT_ERROR_TITLE
protected String errorTitle
protected Throwable firstException
protected URL schemaURL
protected boolean isNameSpaceAware
protected boolean keepPrefixes
protected String encoding
setEncoding(String)
on the InputSource
.protected ParserExceptionListener exListener
public XMLSAXParser()
public XMLSAXParser(ResolverSAXHandler handler)
handler
- the SAX Handlerpublic XMLSAXParser(String errorTitle)
errorTitle
- error titlepublic XMLSAXParser(boolean isValidating)
isValidating
- true if the parser is validatingpublic XMLSAXParser(String errorTitle, boolean isValidating)
errorTitle
- error titleisValidating
- true if the parser is validatingpublic void setClassLoader(ClassLoader loader)
isRevertingOldReaderBehavior()
is false.loader
- the ClassLoaderpublic ClassLoader getClassLoader()
isRevertingOldReaderBehavior()
is false.public static void setDefaultLocale(Locale locale)
locale
- the default localepublic void setLocale(Locale locale)
locale
- the localepublic Locale getLocale()
public void setDefaultLSResourceResolver(boolean hasDefaultLSResolver)
hasDefaultLSResolver
- true if a default LSResourceResolver must be used even if no specific resolver has been setEntityListResolver
public boolean hasDefaultLSResourceResolver()
setDefaultLSResourceResolver(boolean)
public static void setDefaultAllowNestableConnections(boolean allow)
allow
- true if nestable connections are allowed by defaultpublic void setInputSourceCaching(short cachingType)
setInputSourceCaching
in interface InputSourceCaching
cachingType
- the caching typepublic short getInputSourceCaching()
getInputSourceCaching
in interface InputSourceCaching
public static void revertOldReaderBehavior(boolean revert)
Reader reader = new InputStreamReader(url.openStream());As you can see, prior to 1.2.7, the default platform Charset was used (no Charset was specified to decode the file), which could lead to decoding problems in the XML files.
revert
- true to revert to the previous reader behavior prior to 1.2.7public static void setSAXParserFactoryImplementation(String factoryClassName)
factoryClassName
- the factory class namepublic static void resetSAXParserFactoryImplementation()
public static void setDefaultAllowInternalReaders(boolean allowInternalReaders)
allowInternalReaders
- true if internal readers creation are allowed by defaultpublic static SAXParserFactory getStaticParserFactory()
public static SAXParserFactory getStaticParserFactory(ClassLoader loader)
loader
- the class loaderpublic void setAllowInternalReaders(boolean allowInternalReaders)
allowInternalReaders
- true if internal readers creation are allowed in this parser instance.public boolean isAllowingInternalReaders()
public static boolean isRevertingOldReaderBehavior()
Reader reader = new InputStreamReader(url.openStream());As you can see, prior to 1.2.7, the default platform Charset was used (no Charset was specified to decode the file), which could lead to decoding problems in the XML files.
public static boolean isDefaultAllowingNestableConnections()
public void setAllowNestableConnections(boolean allow)
allow
- true if nestable connections are allowedpublic boolean isAllowingNestableConnections()
isDefaultAllowingNestableConnections()
.public void setSchemaValidator(boolean hasSchemaValidator)
hasSchemaValidator
- true if this SAX Parser uses a Schema Validator to validate XML filespublic boolean hasSchemaValidator()
public void setEntityListResolver(EntityListResolver resolver)
resolver
- the resolverpublic void setEncoding(String encoding)
Charset.forName(encoding)
method.encoding
- the encodingpublic String getEncoding()
public Charset getCharset()
setEncoding(java.lang.String)
method has been set.public void setExceptionListener(ParserExceptionListener listener)
listener
- the listenerpublic ParserExceptionListener getExceptionListener()
public void setErrorTitle(String errorTitle)
errorTitle
- the parser error dialog titlepublic void setParserConfiguration(XMLParserConfiguration config)
config
- the parser configurationpublic void setFeatures(Map<String,Boolean> features)
features
- the featurespublic void setFeature(String name, boolean value)
name
- the feature namevalue
- the feature valuepublic Map<String,Boolean> getFeatures()
public void setProperty(String name, Object value)
name
- the property namevalue
- the property valuepublic void setProperties(Map<String,Object> properties)
properties
- the propertiespublic Map<String,Object> getProperties()
public void allowSchemaValidation(boolean isSchemaAware)
isSchemaAware
- true if the parser allow a Schema validationpublic void setValidating(boolean isValidating)
isValidating
- true if the parser is validatingpublic boolean isValidating()
public void setDefaultBaseDirectory(URL baseDir)
baseDir
- the default base directorypublic URL getDefaultBaseDirectory()
public void setSchema(URL schemaURL)
allowSchemaValidation(boolean)
property to trueallowSchemaValidation(boolean)
property to true. Note that you should only specify the Schema if you don't set an
EntityResolver. If the XML file use several namespaces (and several Schemas), you should only use an EntityResolver.schemaURL
- the URL of the Schemapublic void showExceptions(boolean isShowingExceptions)
isShowingExceptions
- true if the parser shows the exceptions encountered during the parsingpublic void showWarnings(boolean isShowingWarnings)
isShowingWarnings
- true if the parser shows the warnings encountered during the parsingpublic boolean isShowingExceptions()
public boolean isShowingWarnings()
public void setNameSpaceAware(boolean isNameSpaceAware)
isNameSpaceAware
- true if the parser is namespace-awarepublic boolean isNameSpaceAware()
public void setKeepPrefixes(boolean keepPrefixes)
keepPrefixes
- true if the parser must keep the prefixespublic boolean isKeepingPrefixes()
public void setConcatenateIncludes(boolean concatenateIncludes)
concatenateIncludes
- true if xInclude content must be concatenatedpublic boolean isConcatenatingIncludes()
public void setXIncludeAware(boolean xIncludeAware)
xIncludeAware
- true if the parser is XInclude-awarepublic boolean isXIncludeAware()
public boolean isXIncludeSupported()
isXIncludeAware()
is true and the underlying parser support XInclude. Note
that this value will only be valid after the parsing.public void setHandler(ResolverSAXHandler handler)
handler
- the SAX Handler used for the parsingpublic ResolverSAXHandler getHandler()
public void setHandlerDTD(URL dtd, String publicID)
dtd
- the DTD URLpublicID
- the DTD public IDpublic boolean hasStoppedParsing()
public Object stoppedParsingAdditionalContent()
StopParsingException
.public boolean hasException()
public Throwable getFirstException()
protected boolean configureParser(SAXParserFactory fac, InputSource source) throws ParserConfigurationException, SAXException, IOException
fac
- the parser factorysource
- the XML sourceParserConfigurationException
SAXException
IOException
protected boolean configureSchemaValidation(SAXParserFactory factory) throws ParserConfigurationException, SAXException
factory
- the SAX parser factoryParserConfigurationException
SAXException
public URL getParsedURL()
parse(java.io.InputStream)
.public void parse(InputStream stream)
stream
- the input Streampublic void parse(File file)
file
- the file to parsepublic void parse(File file, BufferedReaderFactory bufFac)
file
- the file to parsebufFac
- the BufferedReaderFactory to create readers (can be null)public void parse(ReaderProvider provider)
parse(java.io.Reader)
, the method allows to reuse the same provider for the Schema
validation phase and the parsing phase.provider
- the ReaderProviderpublic void parse(Reader reader)
parse(org.mdiutil.io.ReaderProvider)
method.reader
- the Reader to parsepublic void parse(URL url)
url
- the URL to parsepublic void parse(URL url, BufferedReaderFactory bufFac)
url
- the URL to parsebufFac
- the BufferedReaderFactory to create readers (can be null)protected void handleException(String message)
ParserExceptionListener
if onewas provided to this
parser.message
- the exception messageprotected void handleException(String title, Exception e)
ParserExceptionListener
if one was provided to this parser.title
- the exception titlee
- the Exceptionpublic void handleSAXExceptions(String title)
ParserExceptionListener
with an empty list of exceptions.title
- the titlegetExceptionListener()
protected void handleSAXExceptions(String title, List<ResolverSAXHandler.ExceptionResult> results)
title
- the titleresults
- the Exceptions results