/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// ParserAdapter.java - adapt a SAX1 Parser to a SAX2 XMLReader.
// Written by David Megginson
// NO WARRANTY! This class is in the public domain.
// $Id: ParserAdapter.java,v 1.3 2004/11/03 22:53:09 jsuttor Exp $
/**
* Adapt a SAX1 Parser as a SAX2 XMLReader.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class wraps a SAX1 {@link org.xml.sax.Parser Parser}
* and makes it act as a SAX2 {@link org.xml.sax.XMLReader XMLReader},
* with feature, property, and Namespace support. Note
* that it is not possible to report {@link org.xml.sax.ContentHandler#skippedEntity
* skippedEntity} events, since SAX1 does not make that information available.</p>
*
* <p>This adapter does not test for duplicate Namespace-qualified
* attribute names.</p>
*
* @since SAX 2.0
* @author David Megginson
* @version 2.0.1 (sax2r2)
* @see org.xml.sax.helpers.XMLReaderAdapter
* @see org.xml.sax.XMLReader
* @see org.xml.sax.Parser
*/
{
////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////
/**
* Construct a new parser adapter.
*
* <p>Use the "org.xml.sax.parser" property to locate the
* embedded SAX1 driver.</p>
*
* @exception SAXException If the embedded driver
* cannot be instantiated or if the
* org.xml.sax.parser property is not specified.
*/
public ParserAdapter ()
throws SAXException
{
super();
try {
} catch (ClassNotFoundException e1) {
throw new
SAXException("Cannot find SAX1 driver class " +
} catch (IllegalAccessException e2) {
throw new
SAXException("SAX1 driver class " +
driver +
" found but cannot be loaded", e2);
} catch (InstantiationException e3) {
throw new
SAXException("SAX1 driver class " +
driver +
" loaded but cannot be instantiated", e3);
} catch (ClassCastException e4) {
throw new
SAXException("SAX1 driver class " +
driver +
" does not implement org.xml.sax.Parser");
} catch (NullPointerException e5) {
throw new
SAXException("System property org.xml.sax.parser not specified");
}
}
/**
* Construct a new parser adapter.
*
* <p>Note that the embedded parser cannot be changed once the
* adapter is created; to embed a different parser, allocate
* a new ParserAdapter.</p>
*
* @param parser The SAX1 parser to embed.
* @exception java.lang.NullPointerException If the parser parameter
* is null.
*/
{
super();
}
/**
* Internal setup method.
*
* @param parser The embedded parser.
* @exception java.lang.NullPointerException If the parser parameter
* is null.
*/
{
throw new
NullPointerException("Parser argument must not be null");
}
atts = new AttributesImpl();
nsSupport = new NamespaceSupport();
attAdapter = new AttributeListAdapter();
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.XMLReader.
////////////////////////////////////////////////////////////////////
//
// Internal constants for the sake of convenience.
//
/**
* Set a feature flag for the parser.
*
* <p>The only features recognized are namespaces and
* namespace-prefixes.</p>
*
* @param name The feature name, as a complete URI.
* @param value The requested feature value.
* @exception SAXNotRecognizedException If the feature
* can't be assigned or retrieved.
* @exception SAXNotSupportedException If the feature
* can't be assigned that value.
* @see org.xml.sax.XMLReader#setFeature
*/
{
namespaces = value;
if (!namespaces && !prefixes) {
prefixes = true;
}
if (!prefixes && !namespaces) {
namespaces = true;
}
} else {
}
}
/**
* Check a parser feature flag.
*
* <p>The only features recognized are namespaces and
* namespace-prefixes.</p>
*
* @param name The feature name, as a complete URI.
* @return The current feature value.
* @exception SAXNotRecognizedException If the feature
* value can't be assigned or retrieved.
* @exception SAXNotSupportedException If the
* feature is not currently readable.
* @see org.xml.sax.XMLReader#setFeature
*/
{
return namespaces;
return prefixes;
return uris;
} else {
}
}
/**
* Set a parser property.
*
* <p>No properties are currently recognized.</p>
*
* @param name The property name.
* @param value The property value.
* @exception SAXNotRecognizedException If the property
* value can't be assigned or retrieved.
* @exception SAXNotSupportedException If the property
* can't be assigned that value.
* @see org.xml.sax.XMLReader#setProperty
*/
{
}
/**
* Get a parser property.
*
* <p>No properties are currently recognized.</p>
*
* @param name The property name.
* @return The property value.
* @exception SAXNotRecognizedException If the property
* value can't be assigned or retrieved.
* @exception SAXNotSupportedException If the property
* value is not currently readable.
* @see org.xml.sax.XMLReader#getProperty
*/
{
}
/**
* Set the entity resolver.
*
* @param resolver The new entity resolver.
* @see org.xml.sax.XMLReader#setEntityResolver
*/
{
}
/**
* Return the current entity resolver.
*
* @return The current entity resolver, or null if none was supplied.
* @see org.xml.sax.XMLReader#getEntityResolver
*/
{
return entityResolver;
}
/**
* Set the DTD handler.
*
* @param handler the new DTD handler
* @see org.xml.sax.XMLReader#setEntityResolver
*/
{
}
/**
* Return the current DTD handler.
*
* @return the current DTD handler, or null if none was supplied
* @see org.xml.sax.XMLReader#getEntityResolver
*/
{
return dtdHandler;
}
/**
* Set the content handler.
*
* @param handler the new content handler
* @see org.xml.sax.XMLReader#setEntityResolver
*/
{
}
/**
* Return the current content handler.
*
* @return The current content handler, or null if none was supplied.
* @see org.xml.sax.XMLReader#getEntityResolver
*/
{
return contentHandler;
}
/**
* Set the error handler.
*
* @param handler The new error handler.
* @see org.xml.sax.XMLReader#setEntityResolver
*/
{
}
/**
* Return the current error handler.
*
* @return The current error handler, or null if none was supplied.
* @see org.xml.sax.XMLReader#getEntityResolver
*/
{
return errorHandler;
}
/**
* Parse an XML document.
*
* @param systemId The absolute URL of the document.
* @exception java.io.IOException If there is a problem reading
* the raw content of the document.
* @exception SAXException If there is a problem
* processing the document.
* @see #parse(org.xml.sax.InputSource)
* @see org.xml.sax.Parser#parse(java.lang.String)
*/
throws IOException, SAXException
{
}
/**
* Parse an XML document.
*
* @param input An input source for the document.
* @exception java.io.IOException If there is a problem reading
* the raw content of the document.
* @exception SAXException If there is a problem
* processing the document.
* @see #parse(java.lang.String)
*/
throws IOException, SAXException
{
if (parsing) {
throw new SAXException("Parser is already in use");
}
setupParser();
parsing = true;
try {
} finally {
parsing = false;
}
parsing = false;
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.DocumentHandler.
////////////////////////////////////////////////////////////////////
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 document locator event.
*
* @param locator A document locator.
* @see org.xml.sax.ContentHandler#setDocumentLocator
*/
{
if (contentHandler != null) {
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 start document event.
*
* @exception SAXException The client may raise a
* processing exception.
* @see org.xml.sax.DocumentHandler#startDocument
*/
public void startDocument ()
throws SAXException
{
if (contentHandler != null) {
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 end document event.
*
* @exception SAXException The client may raise a
* processing exception.
* @see org.xml.sax.DocumentHandler#endDocument
*/
public void endDocument ()
throws SAXException
{
if (contentHandler != null) {
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 startElement event.
*
* <p>If necessary, perform Namespace processing.</p>
*
* @param qName The qualified (prefixed) name.
* @param qAtts The XML attribute list (with qnames).
* @exception SAXException The client may raise a
* processing exception.
*/
throws SAXException
{
// These are exceptions from the
// first pass; they should be
// ignored if there's a second pass,
// but reported otherwise.
// If we're not doing Namespace
// processing, dispatch this quickly.
if (!namespaces) {
if (contentHandler != null) {
}
return;
}
// OK, we're doing Namespace processing.
// First pass: handle NS decls
for (int i = 0; i < length; i++) {
continue;
// Could be a declaration...
// xmlns=...
prefix = "";
} else if (n != 5) {
// XML namespaces spec doesn't discuss "xmlnsf:oo"
// (and similarly named) attributes ... at most, warn
continue;
} else // xmlns:foo=...
continue;
}
if (contentHandler != null)
}
// Second pass: copy all relevant
// attributes into the SAX2 AttributeList
// using updated prefix bindings
for (int i = 0; i < length; i++) {
// Declaration?
prefix = "";
} else if (n != 5) {
// XML namespaces spec doesn't discuss "xmlnsf:oo"
// (and similarly named) attributes ... ignore
} else {
}
// Yes, decl: report or prune
if (prefixes) {
if (uris)
// note funky case: localname can be null
// when declaring the default prefix, and
// yet the uri isn't null.
else
}
continue;
}
}
// Not a declaration -- report
try {
} catch (SAXException e) {
if (exceptions == null)
exceptions = new Vector();
exceptions.addElement(e);
}
}
// now handle the deferred exception reports
(exceptions.elementAt(i)));
}
// OK, finally report the event.
if (contentHandler != null) {
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 end element event.
*
* @param qName The qualified (prefixed) name.
* @exception SAXException The client may raise a
* processing exception.
* @see org.xml.sax.DocumentHandler#endElement
*/
throws SAXException
{
// If we're not doing Namespace
// processing, dispatch this quickly.
if (!namespaces) {
if (contentHandler != null) {
}
return;
}
// Split the name.
if (contentHandler != null) {
while (prefixes.hasMoreElements()) {
}
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 characters event.
*
* @param ch An array of characters.
* @param start The starting position in the array.
* @param length The number of characters to use.
* @exception SAXException The client may raise a
* processing exception.
* @see org.xml.sax.DocumentHandler#characters
*/
throws SAXException
{
if (contentHandler != null) {
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 ignorable whitespace event.
*
* @param ch An array of characters.
* @param start The starting position in the array.
* @param length The number of characters to use.
* @exception SAXException The client may raise a
* processing exception.
* @see org.xml.sax.DocumentHandler#ignorableWhitespace
*/
throws SAXException
{
if (contentHandler != null) {
}
}
/**
* Adapter implementation method; do not call.
* Adapt a SAX1 processing instruction event.
*
* @param target The processing instruction target.
* @param data The remainder of the processing instruction
* @exception SAXException The client may raise a
* processing exception.
* @see org.xml.sax.DocumentHandler#processingInstruction
*/
throws SAXException
{
if (contentHandler != null) {
}
}
////////////////////////////////////////////////////////////////////
// Internal utility methods.
////////////////////////////////////////////////////////////////////
/**
* Initialize the parser before each run.
*/
private void setupParser ()
{
// catch an illegal "nonsense" state.
if (!prefixes && !namespaces)
throw new IllegalStateException ();
if (uris)
nsSupport.setNamespaceDeclUris (true);
if (entityResolver != null) {
}
if (dtdHandler != null) {
}
if (errorHandler != null) {
}
parser.setDocumentHandler(this);
}
/**
* Process a qualified (prefixed) name.
*
* <p>If the name has an undeclared prefix, use only the qname
* and make an ErrorHandler.error callback in case the app is
* interested.</p>
*
* @param qName The qualified (prefixed) name.
* @param isAttribute true if this is an attribute name.
* @return The name split into three parts.
* @exception SAXException The client may throw
* an exception if there is an error callback.
*/
boolean useException)
throws SAXException
{
if (useException)
}
return parts;
}
/**
* Report a non-fatal error.
*
* @param message The error message.
* @exception SAXException The client may throw
* an exception.
*/
throws SAXException
{
if (errorHandler != null)
}
/**
* Construct an exception for the current context.
*
* @param message The error message.
*/
{
} else {
}
}
/**
* Throw an exception if we are parsing.
*
* <p>Use this method to detect illegal feature or
* property changes.</p>
*
* @param type The type of thing (feature or property).
* @param name The feature or property name.
* @exception SAXNotSupportedException If a
* document is currently being parsed.
*/
throws SAXNotSupportedException
{
if (parsing) {
throw new SAXNotSupportedException("Cannot change " +
type + ' ' +
name + " while parsing");
}
}
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
private boolean parsing = false;
// Features
private boolean namespaces = true;
private boolean prefixes = false;
private boolean uris = false;
// Properties
// Handlers
////////////////////////////////////////////////////////////////////
// Inner class to wrap an AttributeList when not doing NS proc.
////////////////////////////////////////////////////////////////////
/**
* Adapt a SAX1 AttributeList as a SAX2 Attributes object.
*
* <p>This class is in the Public Domain, and comes with NO
* WARRANTY of any kind.</p>
*
* <p>This wrapper class is used only when Namespace support
* is disabled -- it provides pretty much a direct mapping
* from SAX1 to SAX2, except that names and types are
* interned whenever requested.</p>
*/
{
/**
* Construct a new adapter.
*/
{
}
/**
* Set the embedded AttributeList.
*
* <p>This method must be invoked before any of the others
* can be used.</p>
*
* @param The SAX1 attribute list (with qnames).
*/
{
}
/**
* Return the length of the attribute list.
*
* @return The number of attributes in the list.
* @see org.xml.sax.Attributes#getLength
*/
public int getLength ()
{
}
/**
* Return the Namespace URI of the specified attribute.
*
* @param The attribute's index.
* @return Always the empty string.
* @see org.xml.sax.Attributes#getURI
*/
{
return "";
}
/**
* Return the local name of the specified attribute.
*
* @param The attribute's index.
* @return Always the empty string.
* @see org.xml.sax.Attributes#getLocalName
*/
{
return "";
}
/**
* Return the qualified (prefixed) name of the specified attribute.
*
* @param The attribute's index.
* @return The attribute's qualified name, internalized.
*/
{
}
/**
* Return the type of the specified attribute.
*
* @param The attribute's index.
* @return The attribute's type as an internalized string.
*/
{
}
/**
* Return the value of the specified attribute.
*
* @param The attribute's index.
* @return The attribute's value.
*/
{
}
/**
* Look up an attribute index by Namespace name.
*
* @param uri The Namespace URI or the empty string.
* @param localName The local name.
* @return The attributes index, or -1 if none was found.
* @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String)
*/
{
return -1;
}
/**
* Look up an attribute index by qualified (prefixed) name.
*
* @param qName The qualified name.
* @return The attributes index, or -1 if none was found.
* @see org.xml.sax.Attributes#getIndex(java.lang.String)
*/
{
for (int i = 0; i < max; i++) {
return i;
}
}
return -1;
}
/**
* Look up the type of an attribute by Namespace name.
*
* @param uri The Namespace URI
* @param localName The local name.
* @return The attribute's type as an internalized string.
*/
{
return null;
}
/**
* Look up the type of an attribute by qualified (prefixed) name.
*
* @param qName The qualified name.
* @return The attribute's type as an internalized string.
*/
{
}
/**
* Look up the value of an attribute by Namespace name.
*
* @param uri The Namespace URI
* @param localName The local name.
* @return The attribute's value.
*/
{
return null;
}
/**
* Look up the value of an attribute by qualified (prefixed) name.
*
* @param qName The qualified name.
* @return The attribute's value.
*/
{
}
}
}
// end of ParserAdapter.java