-// XMLReaderAdapter.java - adapt an SAX2 XMLReader to a SAX1 Parser\r
-// Written by David Megginson, sax@megginson.com\r
-// NO WARRANTY! This class is in the public domain.\r
-\r
-// $Id: XMLReaderAdapter.java,v 1.1 2000/10/02 02:43:20 sboag Exp $\r
-\r
-package org.xml.sax.helpers;\r
-\r
-import java.io.IOException;\r
-import java.util.Locale;\r
-\r
-import org.xml.sax.Parser; // deprecated\r
-import org.xml.sax.Locator;\r
-import org.xml.sax.InputSource;\r
-import org.xml.sax.AttributeList; // deprecated\r
-import org.xml.sax.EntityResolver;\r
-import org.xml.sax.DTDHandler;\r
-import org.xml.sax.DocumentHandler; // deprecated\r
-import org.xml.sax.ErrorHandler;\r
-import org.xml.sax.SAXException;\r
-\r
-import org.xml.sax.XMLReader;\r
-import org.xml.sax.Attributes;\r
-import org.xml.sax.ContentHandler;\r
-import org.xml.sax.SAXNotSupportedException;\r
-\r
-\r
-/**\r
- * Adapt a SAX2 XMLReader as a SAX1 Parser.\r
- *\r
- * <blockquote>\r
- * <em>This module, both source code and documentation, is in the\r
- * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>\r
- * </blockquote>\r
- *\r
- * <p>This class wraps a SAX2 {@link org.xml.sax.XMLReader XMLReader}\r
- * and makes it act as a SAX1 {@link org.xml.sax.Parser Parser}. The XMLReader \r
- * must support a true value for the \r
- * http://xml.org/sax/features/namespace-prefixes property or parsing will fail\r
- * with a {@link org.xml.sax.SAXException SAXException}; if the XMLReader \r
- * supports a false value for the http://xml.org/sax/features/namespaces \r
- * property, that will also be used to improve efficiency.</p>\r
- *\r
- * @since SAX 2.0\r
- * @author David Megginson, \r
- * <a href="mailto:sax@megginson.com">sax@megginson.com</a>\r
- * @version 2.0\r
- * @see org.xml.sax.Parser\r
- * @see org.xml.sax.XMLReader\r
- */\r
-public class XMLReaderAdapter implements Parser, ContentHandler\r
-{\r
-\r
-\f\r
- ////////////////////////////////////////////////////////////////////\r
- // Constructor.\r
- ////////////////////////////////////////////////////////////////////\r
-\r
-\r
- /**\r
- * Create a new adapter.\r
- *\r
- * <p>Use the "org.xml.sax.driver" property to locate the SAX2\r
- * driver to embed.</p>\r
- *\r
- * @exception org.xml.sax.SAXException If the embedded driver\r
- * cannot be instantiated or if the\r
- * org.xml.sax.driver property is not specified.\r
- */\r
- public XMLReaderAdapter ()\r
- throws SAXException\r
- {\r
- setup(XMLReaderFactory.createXMLReader());\r
- }\r
-\r
-\r
- /**\r
- * Create a new adapter.\r
- *\r
- * <p>Create a new adapter, wrapped around a SAX2 XMLReader.\r
- * The adapter will make the XMLReader act like a SAX1\r
- * Parser.</p>\r
- *\r
- * @param xmlReader The SAX2 XMLReader to wrap.\r
- * @exception java.lang.NullPointerException If the argument is null.\r
- */\r
- public XMLReaderAdapter (XMLReader xmlReader)\r
- {\r
- setup(xmlReader);\r
- }\r
-\r
-\r
-\r
- /**\r
- * Internal setup.\r
- *\r
- * @param xmlReader The embedded XMLReader.\r
- */\r
- private void setup (XMLReader xmlReader)\r
- {\r
- if (xmlReader == null) {\r
- throw new NullPointerException("XMLReader must not be null");\r
- }\r
- this.xmlReader = xmlReader;\r
- qAtts = new AttributesAdapter();\r
- }\r
-\r
-\r
-\f\r
- ////////////////////////////////////////////////////////////////////\r
- // Implementation of org.xml.sax.Parser.\r
- ////////////////////////////////////////////////////////////////////\r
-\r
-\r
- /**\r
- * Set the locale for error reporting.\r
- *\r
- * <p>This is not supported in SAX2, and will always throw\r
- * an exception.</p>\r
- *\r
- * @param The locale for error reporting.\r
- * @see org.xml.sax.Parser#setLocale\r
- */\r
- public void setLocale (Locale locale)\r
- throws SAXException\r
- {\r
- throw new SAXNotSupportedException("setLocale not supported");\r
- }\r
-\r
-\r
- /**\r
- * Register the entity resolver.\r
- *\r
- * @param resolver The new resolver.\r
- * @see org.xml.sax.Parser#setEntityResolver\r
- */\r
- public void setEntityResolver (EntityResolver resolver)\r
- {\r
- xmlReader.setEntityResolver(resolver);\r
- }\r
-\r
-\r
- /**\r
- * Register the DTD event handler.\r
- *\r
- * @param handler The new DTD event handler.\r
- * @see org.xml.sax.Parser#setDTDHandler\r
- */\r
- public void setDTDHandler (DTDHandler handler)\r
- {\r
- xmlReader.setDTDHandler(handler);\r
- }\r
-\r
-\r
- /**\r
- * Register the SAX1 document event handler.\r
- *\r
- * <p>Note that the SAX1 document handler has no Namespace\r
- * support.</p>\r
- *\r
- * @param handler The new SAX1 document event handler.\r
- * @see org.xml.sax.Parser#setDocumentHandler\r
- */\r
- public void setDocumentHandler (DocumentHandler handler)\r
- {\r
- documentHandler = handler;\r
- }\r
-\r
-\r
- /**\r
- * Register the error event handler.\r
- *\r
- * @param handler The new error event handler.\r
- * @see org.xml.sax.Parser#setErrorHandler\r
- */\r
- public void setErrorHandler (ErrorHandler handler)\r
- {\r
- xmlReader.setErrorHandler(handler);\r
- }\r
-\r
-\r
- /**\r
- * Parse the document.\r
- *\r
- * <p>This method will throw an exception if the embedded\r
- * XMLReader does not support the \r
- * http://xml.org/sax/features/namespace-prefixes property.</p>\r
- *\r
- * @param systemId The absolute URL of the document.\r
- * @exception java.io.IOException If there is a problem reading\r
- * the raw content of the document.\r
- * @exception org.xml.sax.SAXException If there is a problem\r
- * processing the document.\r
- * @see #parse(org.xml.sax.InputSource)\r
- * @see org.xml.sax.Parser#parse(java.lang.String)\r
- */\r
- public void parse (String systemId)\r
- throws IOException, SAXException\r
- {\r
- parse(new InputSource(systemId));\r
- }\r
-\r
-\r
- /**\r
- * Parse the document.\r
- *\r
- * <p>This method will throw an exception if the embedded\r
- * XMLReader does not support the \r
- * http://xml.org/sax/features/namespace-prefixes property.</p>\r
- *\r
- * @param input An input source for the document.\r
- * @exception java.io.IOException If there is a problem reading\r
- * the raw content of the document.\r
- * @exception org.xml.sax.SAXException If there is a problem\r
- * processing the document.\r
- * @see #parse(java.lang.String)\r
- * @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)\r
- */\r
- public void parse (InputSource input)\r
- throws IOException, SAXException\r
- {\r
- setupXMLReader();\r
- xmlReader.parse(input);\r
- }\r
-\r
-\r
- /**\r
- * Set up the XML reader.\r
- */\r
- private void setupXMLReader ()\r
- throws SAXException\r
- {\r
- xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);\r
- try {\r
- xmlReader.setFeature("http://xml.org/sax/features/namespaces",\r
- false);\r
- } catch (SAXException e) {\r
- // NO OP: it's just extra information, and we can ignore it\r
- }\r
- xmlReader.setContentHandler(this);\r
- }\r
-\r
-\r
-\f\r
- ////////////////////////////////////////////////////////////////////\r
- // Implementation of org.xml.sax.ContentHandler.\r
- ////////////////////////////////////////////////////////////////////\r
-\r
-\r
- /**\r
- * Set a document locator.\r
- *\r
- * @param locator The document locator.\r
- * @see org.xml.sax.ContentHandler#setDocumentLocator\r
- */\r
- public void setDocumentLocator (Locator locator)\r
- {\r
- documentHandler.setDocumentLocator(locator);\r
- }\r
-\r
-\r
- /**\r
- * Start document event.\r
- *\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#startDocument\r
- */\r
- public void startDocument ()\r
- throws SAXException\r
- {\r
- documentHandler.startDocument();\r
- }\r
-\r
-\r
- /**\r
- * End document event.\r
- *\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#endDocument\r
- */\r
- public void endDocument ()\r
- throws SAXException\r
- {\r
- documentHandler.endDocument();\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 start prefix mapping event.\r
- *\r
- * @param prefix The prefix being mapped.\r
- * @param uri The Namespace URI being mapped to.\r
- * @see org.xml.sax.ContentHandler#startPrefixMapping\r
- */\r
- public void startPrefixMapping (String prefix, String uri)\r
- {\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 end prefix mapping event.\r
- *\r
- * @param prefix The prefix being mapped.\r
- * @see org.xml.sax.ContentHandler#endPrefixMapping\r
- */\r
- public void endPrefixMapping (String prefix)\r
- {\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 start element event.\r
- *\r
- * @param uri The Namespace URI.\r
- * @param localName The Namespace local name.\r
- * @param qName The qualified (prefixed) name.\r
- * @param atts The SAX2 attributes.\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#endDocument\r
- */\r
- public void startElement (String uri, String localName,\r
- String qName, Attributes atts)\r
- throws SAXException\r
- {\r
- qAtts.setAttributes(atts);\r
- documentHandler.startElement(qName, qAtts);\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 end element event.\r
- *\r
- * @param uri The Namespace URI.\r
- * @param localName The Namespace local name.\r
- * @param qName The qualified (prefixed) name.\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#endElement\r
- */\r
- public void endElement (String uri, String localName,\r
- String qName)\r
- throws SAXException\r
- {\r
- documentHandler.endElement(qName);\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 characters event.\r
- *\r
- * @param ch An array of characters.\r
- * @param start The starting position in the array.\r
- * @param length The number of characters to use.\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#characters\r
- */\r
- public void characters (char ch[], int start, int length)\r
- throws SAXException\r
- {\r
- documentHandler.characters(ch, start, length);\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 ignorable whitespace event.\r
- *\r
- * @param ch An array of characters.\r
- * @param start The starting position in the array.\r
- * @param length The number of characters to use.\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#ignorableWhitespace\r
- */\r
- public void ignorableWhitespace (char ch[], int start, int length)\r
- throws SAXException\r
- {\r
- documentHandler.ignorableWhitespace(ch, start, length);\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 processing instruction event.\r
- *\r
- * @param target The processing instruction target.\r
- * @param data The remainder of the processing instruction\r
- * @exception org.xml.sax.SAXException The client may raise a\r
- * processing exception.\r
- * @see org.xml.sax.ContentHandler#processingInstruction\r
- */\r
- public void processingInstruction (String target, String data)\r
- throws SAXException\r
- {\r
- documentHandler.processingInstruction(target, data);\r
- }\r
-\r
-\r
- /**\r
- * Adapt a SAX2 skipped entity event.\r
- *\r
- * @param name The name of the skipped entity.\r
- * @see org.xml.sax.ContentHandler#skippedEntity\r
- */\r
- public void skippedEntity (String name)\r
- throws SAXException\r
- {\r
- }\r
-\r
-\r
-\f\r
- ////////////////////////////////////////////////////////////////////\r
- // Internal state.\r
- ////////////////////////////////////////////////////////////////////\r
-\r
- XMLReader xmlReader;\r
- DocumentHandler documentHandler;\r
- AttributesAdapter qAtts;\r
-\r
-\r
-\f\r
- ////////////////////////////////////////////////////////////////////\r
- // Internal class.\r
- ////////////////////////////////////////////////////////////////////\r
-\r
-\r
- /**\r
- * Internal class to wrap a SAX2 Attributes object for SAX1.\r
- */\r
- final class AttributesAdapter implements AttributeList\r
- {\r
- AttributesAdapter ()\r
- {\r
- }\r
-\r
-\r
- /**\r
- * Set the embedded Attributes object.\r
- *\r
- * @param The embedded SAX2 Attributes.\r
- */ \r
- void setAttributes (Attributes attributes)\r
- {\r
- this.attributes = attributes;\r
- }\r
-\r
-\r
- /**\r
- * Return the number of attributes.\r
- *\r
- * @return The length of the attribute list.\r
- * @see org.xml.sax.AttributeList#getLength\r
- */\r
- public int getLength ()\r
- {\r
- return attributes.getLength();\r
- }\r
-\r
-\r
- /**\r
- * Return the qualified (prefixed) name of an attribute by position.\r
- *\r
- * @return The qualified name.\r
- * @see org.xml.sax.AttributeList#getName\r
- */\r
- public String getName (int i)\r
- {\r
- return attributes.getQName(i);\r
- }\r
-\r
-\r
- /**\r
- * Return the type of an attribute by position.\r
- *\r
- * @return The type.\r
- * @see org.xml.sax.AttributeList#getType(int)\r
- */\r
- public String getType (int i)\r
- {\r
- return attributes.getType(i);\r
- }\r
-\r
-\r
- /**\r
- * Return the value of an attribute by position.\r
- *\r
- * @return The value.\r
- * @see org.xml.sax.AttributeList#getValue(int)\r
- */\r
- public String getValue (int i)\r
- {\r
- return attributes.getValue(i);\r
- }\r
-\r
-\r
- /**\r
- * Return the type of an attribute by qualified (prefixed) name.\r
- *\r
- * @return The type.\r
- * @see org.xml.sax.AttributeList#getType(java.lang.String)\r
- */\r
- public String getType (String qName)\r
- {\r
- return attributes.getType(qName);\r
- }\r
-\r
-\r
- /**\r
- * Return the value of an attribute by qualified (prefixed) name.\r
- *\r
- * @return The value.\r
- * @see org.xml.sax.AttributeList#getValue(java.lang.String)\r
- */\r
- public String getValue (String qName)\r
- {\r
- return attributes.getValue(qName);\r
- }\r
-\r
- private Attributes attributes;\r
- }\r
-\r
-}\r
-\r
-// end of XMLReaderAdapter.java\r