public class ParserAdapter extends Object implements XMLReader, DocumentHandler
This module, both source code and documentation, is in the Public Domain, and comes with NO WARRANTY.
This class wraps a SAX1 Parser
and makes it act as a SAX2 XMLReader
,
with feature, property, and Namespace support. Note
that it is not possible to report skippedEntity
events, since SAX1 does not make that information available.
This adapter does not test for duplicate Namespace-qualified attribute names.
XMLReaderAdapter
,
XMLReader
,
Parser
Modifier and Type | Class and Description |
---|---|
(package private) class |
ParserAdapter.AttributeListAdapter
Adapt a SAX1 AttributeList as a SAX2 Attributes object.
|
Modifier and Type | Field and Description |
---|---|
private ParserAdapter.AttributeListAdapter |
attAdapter |
private AttributesImpl |
atts |
(package private) ContentHandler |
contentHandler |
(package private) DTDHandler |
dtdHandler |
(package private) EntityResolver |
entityResolver |
(package private) ErrorHandler |
errorHandler |
private static String |
EXTERNAL_GENERAL |
private static String |
EXTERNAL_PARAMETER |
private static String |
FEATURES |
(package private) Locator |
locator |
private String[] |
nameParts |
private static String |
NAMESPACE_PREFIXES |
private boolean |
namespaces |
private static String |
NAMESPACES |
private NamespaceSupport |
nsSupport |
private Parser |
parser |
private boolean |
parsing |
private boolean |
prefixes |
private static String |
VALIDATION |
Constructor and Description |
---|
ParserAdapter()
Construct a new parser adapter.
|
ParserAdapter(Parser parser)
Construct a new parser adapter.
|
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length)
Adapt a SAX1 characters event.
|
private void |
checkNotParsing(String type,
String name)
Throw an exception if we are parsing.
|
void |
endDocument()
Adapt a SAX1 end document event.
|
void |
endElement(String qName)
Adapt a SAX1 end element event.
|
ContentHandler |
getContentHandler()
Return the current content handler.
|
DTDHandler |
getDTDHandler()
Return the current DTD handler.
|
EntityResolver |
getEntityResolver()
Return the current entity resolver.
|
ErrorHandler |
getErrorHandler()
Return the current error handler.
|
boolean |
getFeature(String name)
Check a parser feature.
|
Object |
getProperty(String name)
Get a parser property.
|
void |
ignorableWhitespace(char[] ch,
int start,
int length)
Adapt a SAX1 ignorable whitespace event.
|
void |
parse(InputSource input)
Parse an XML document.
|
void |
parse(String systemId)
Parse an XML document.
|
void |
processingInstruction(String target,
String data)
Adapt a SAX1 processing instruction event.
|
private String[] |
processName(String qName,
boolean isAttribute)
Process a qualified (prefixed) name.
|
(package private) void |
reportError(String message)
Report a non-fatal error.
|
void |
setContentHandler(ContentHandler handler)
Set the content handler.
|
void |
setDocumentLocator(Locator locator)
Adapt a SAX1 document locator event.
|
void |
setDTDHandler(DTDHandler handler)
Set the DTD handler.
|
void |
setEntityResolver(EntityResolver resolver)
Set the entity resolver.
|
void |
setErrorHandler(ErrorHandler handler)
Set the error handler.
|
void |
setFeature(String name,
boolean state)
Set a feature for the parser.
|
void |
setProperty(String name,
Object value)
Set a parser property.
|
private void |
setup(Parser parser)
Internal setup method.
|
private void |
setupParser()
Initialize the parser before each run.
|
void |
startDocument()
Adapt a SAX1 start document event.
|
void |
startElement(String qName,
AttributeList qAtts)
Adapt a SAX1 startElement event.
|
private static final String FEATURES
private static final String NAMESPACES
private static final String NAMESPACE_PREFIXES
private static final String VALIDATION
private static final String EXTERNAL_GENERAL
private static final String EXTERNAL_PARAMETER
private NamespaceSupport nsSupport
private ParserAdapter.AttributeListAdapter attAdapter
private boolean parsing
private AttributesImpl atts
private boolean namespaces
private boolean prefixes
EntityResolver entityResolver
DTDHandler dtdHandler
ContentHandler contentHandler
ErrorHandler errorHandler
public ParserAdapter() throws SAXException
Use the "org.xml.sax.parser" property to locate the embedded SAX1 driver.
SAXException
- If the embedded driver
cannot be instantiated or if the
org.xml.sax.parser property is not specified.public ParserAdapter(Parser parser)
Note that the embedded parser cannot be changed once the adapter is created; to embed a different parser, allocate a new ParserAdapter.
parser
- The SAX1 parser to embed.NullPointerException
- If the parser parameter
is null.private void setup(Parser parser)
parser
- The embedded parser.NullPointerException
- If the parser parameter
is null.public void setFeature(String name, boolean state) throws SAXNotRecognizedException, SAXNotSupportedException
The only features supported are namespaces and namespace-prefixes.
setFeature
in interface XMLReader
name
- The feature name, as a complete URI.state
- The requested feature state.SAXNotRecognizedException
- If the feature
name is not known.SAXNotSupportedException
- If the feature
state is not supported.XMLReader.setFeature(java.lang.String, boolean)
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException
The only features supported are namespaces and namespace-prefixes.
getFeature
in interface XMLReader
name
- The feature name, as a complete URI.SAXNotRecognizedException
- If the feature
name is not known.SAXNotSupportedException
- If querying the
feature state is not supported.XMLReader.setFeature(java.lang.String, boolean)
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
No special properties are currently supported.
setProperty
in interface XMLReader
name
- The property name.value
- The property value.SAXNotRecognizedException
- If the feature
name is not known.SAXNotSupportedException
- If the feature
state is not supported.XMLReader.setProperty(java.lang.String, java.lang.Object)
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException
No special properties are currently supported.
getProperty
in interface XMLReader
name
- The property name.SAXNotRecognizedException
- If the feature
name is not known.SAXNotSupportedException
- If the feature
state is not supported.XMLReader.getProperty(java.lang.String)
public void setEntityResolver(EntityResolver resolver)
setEntityResolver
in interface XMLReader
resolver
- The new entity resolver.NullPointerException
- If the entity resolver
parameter is null.XMLReader.setEntityResolver(org.xml.sax.EntityResolver)
public EntityResolver getEntityResolver()
getEntityResolver
in interface XMLReader
XMLReader.getEntityResolver()
public void setDTDHandler(DTDHandler handler)
setDTDHandler
in interface XMLReader
resolver
- The new DTD handler.NullPointerException
- If the DTD handler
parameter is null.XMLReader.setEntityResolver(org.xml.sax.EntityResolver)
public DTDHandler getDTDHandler()
getDTDHandler
in interface XMLReader
XMLReader.getEntityResolver()
public void setContentHandler(ContentHandler handler)
setContentHandler
in interface XMLReader
resolver
- The new content handler.NullPointerException
- If the content handler
parameter is null.XMLReader.setEntityResolver(org.xml.sax.EntityResolver)
public ContentHandler getContentHandler()
getContentHandler
in interface XMLReader
XMLReader.getEntityResolver()
public void setErrorHandler(ErrorHandler handler)
setErrorHandler
in interface XMLReader
resolver
- The new error handler.NullPointerException
- If the error handler
parameter is null.XMLReader.setEntityResolver(org.xml.sax.EntityResolver)
public ErrorHandler getErrorHandler()
getErrorHandler
in interface XMLReader
XMLReader.getEntityResolver()
public void parse(String systemId) throws IOException, SAXException
parse
in interface XMLReader
systemId
- The absolute URL of the document.IOException
- If there is a problem reading
the raw content of the document.SAXException
- If there is a problem
processing the document.parse(org.xml.sax.InputSource)
,
Parser.parse(java.lang.String)
public void parse(InputSource input) throws IOException, SAXException
parse
in interface XMLReader
input
- An input source for the document.IOException
- If there is a problem reading
the raw content of the document.SAXException
- If there is a problem
processing the document.parse(java.lang.String)
,
Parser.parse(org.xml.sax.InputSource)
public void setDocumentLocator(Locator locator)
setDocumentLocator
in interface DocumentHandler
locator
- A document locator.ContentHandler.setDocumentLocator(org.xml.sax.Locator)
public void startDocument() throws SAXException
startDocument
in interface DocumentHandler
SAXException
- The client may raise a
processing exception.DocumentHandler.startDocument()
public void endDocument() throws SAXException
endDocument
in interface DocumentHandler
SAXException
- The client may raise a
processing exception.DocumentHandler.endDocument()
public void startElement(String qName, AttributeList qAtts) throws SAXException
If necessary, perform Namespace processing.
startElement
in interface DocumentHandler
qName
- The qualified (prefixed) name.qAtts
- The XML 1.0 attribute list (with qnames).SAXException
- Any SAX exception, possibly
wrapping another exception.DocumentHandler.endElement(java.lang.String)
,
AttributeList
public void endElement(String qName) throws SAXException
endElement
in interface DocumentHandler
qName
- The qualified (prefixed) name.SAXException
- The client may raise a
processing exception.DocumentHandler.endElement(java.lang.String)
public void characters(char[] ch, int start, int length) throws SAXException
characters
in interface DocumentHandler
ch
- An array of characters.start
- The starting position in the array.length
- The number of characters to use.SAXException
- The client may raise a
processing exception.DocumentHandler.characters(char[], int, int)
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
ignorableWhitespace
in interface DocumentHandler
ch
- An array of characters.start
- The starting position in the array.length
- The number of characters to use.SAXException
- The client may raise a
processing exception.DocumentHandler.ignorableWhitespace(char[], int, int)
public void processingInstruction(String target, String data) throws SAXException
processingInstruction
in interface DocumentHandler
target
- The processing instruction target.data
- The remainder of the processing instructionSAXException
- The client may raise a
processing exception.DocumentHandler.processingInstruction(java.lang.String, java.lang.String)
private void setupParser()
private String[] processName(String qName, boolean isAttribute) throws SAXException
If the name has an undeclared prefix, use only the qname and make an ErrorHandler.error callback in case the app is interested.
qName
- The qualified (prefixed) name.isAttribute
- true if this is an attribute name.SAXException
- The client may throw
an exception if there is an error callback.void reportError(String message) throws SAXException
message
- The error message.SAXException
- The client may throw
an exception.private void checkNotParsing(String type, String name) throws SAXNotSupportedException
Use this method to detect illegal feature or property changes.
type
- The type of thing (feature or property).name
- The feature or property name.SAXNotSupportedException
- If a
document is currently being parsed.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses