Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/helpers/XMLReaderAdapter.java
48576 views
/*1* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425// XMLReaderAdapter.java - adapt an SAX2 XMLReader to a SAX1 Parser26// http://www.saxproject.org27// Written by David Megginson28// NO WARRANTY! This class is in the public domain.29// $Id: XMLReaderAdapter.java,v 1.3 2004/11/03 22:53:09 jsuttor Exp $3031package org.xml.sax.helpers;3233import java.io.IOException;34import java.util.Locale;3536import org.xml.sax.Parser; // deprecated37import org.xml.sax.Locator;38import org.xml.sax.InputSource;39import org.xml.sax.AttributeList; // deprecated40import org.xml.sax.EntityResolver;41import org.xml.sax.DTDHandler;42import org.xml.sax.DocumentHandler; // deprecated43import org.xml.sax.ErrorHandler;44import org.xml.sax.SAXException;4546import org.xml.sax.XMLReader;47import org.xml.sax.Attributes;48import org.xml.sax.ContentHandler;49import org.xml.sax.SAXNotSupportedException;505152/**53* Adapt a SAX2 XMLReader as a SAX1 Parser.54*55* <blockquote>56* <em>This module, both source code and documentation, is in the57* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>58* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>59* for further information.60* </blockquote>61*62* <p>This class wraps a SAX2 {@link org.xml.sax.XMLReader XMLReader}63* and makes it act as a SAX1 {@link org.xml.sax.Parser Parser}. The XMLReader64* must support a true value for the65* http://xml.org/sax/features/namespace-prefixes property or parsing will fail66* with a {@link org.xml.sax.SAXException SAXException}; if the XMLReader67* supports a false value for the http://xml.org/sax/features/namespaces68* property, that will also be used to improve efficiency.</p>69*70* @since SAX 2.071* @author David Megginson72* @see org.xml.sax.Parser73* @see org.xml.sax.XMLReader74*/75public class XMLReaderAdapter implements Parser, ContentHandler76{777879////////////////////////////////////////////////////////////////////80// Constructor.81////////////////////////////////////////////////////////////////////828384/**85* Create a new adapter.86*87* <p>Use the "org.xml.sax.driver" property to locate the SAX288* driver to embed.</p>89*90* @exception org.xml.sax.SAXException If the embedded driver91* cannot be instantiated or if the92* org.xml.sax.driver property is not specified.93*/94public XMLReaderAdapter ()95throws SAXException96{97setup(XMLReaderFactory.createXMLReader());98}99100101/**102* Create a new adapter.103*104* <p>Create a new adapter, wrapped around a SAX2 XMLReader.105* The adapter will make the XMLReader act like a SAX1106* Parser.</p>107*108* @param xmlReader The SAX2 XMLReader to wrap.109* @exception java.lang.NullPointerException If the argument is null.110*/111public XMLReaderAdapter (XMLReader xmlReader)112{113setup(xmlReader);114}115116117118/**119* Internal setup.120*121* @param xmlReader The embedded XMLReader.122*/123private void setup (XMLReader xmlReader)124{125if (xmlReader == null) {126throw new NullPointerException("XMLReader must not be null");127}128this.xmlReader = xmlReader;129qAtts = new AttributesAdapter();130}131132133134////////////////////////////////////////////////////////////////////135// Implementation of org.xml.sax.Parser.136////////////////////////////////////////////////////////////////////137138139/**140* Set the locale for error reporting.141*142* <p>This is not supported in SAX2, and will always throw143* an exception.</p>144*145* @param locale the locale for error reporting.146* @see org.xml.sax.Parser#setLocale147* @exception org.xml.sax.SAXException Thrown unless overridden.148*/149public void setLocale (Locale locale)150throws SAXException151{152throw new SAXNotSupportedException("setLocale not supported");153}154155156/**157* Register the entity resolver.158*159* @param resolver The new resolver.160* @see org.xml.sax.Parser#setEntityResolver161*/162public void setEntityResolver (EntityResolver resolver)163{164xmlReader.setEntityResolver(resolver);165}166167168/**169* Register the DTD event handler.170*171* @param handler The new DTD event handler.172* @see org.xml.sax.Parser#setDTDHandler173*/174public void setDTDHandler (DTDHandler handler)175{176xmlReader.setDTDHandler(handler);177}178179180/**181* Register the SAX1 document event handler.182*183* <p>Note that the SAX1 document handler has no Namespace184* support.</p>185*186* @param handler The new SAX1 document event handler.187* @see org.xml.sax.Parser#setDocumentHandler188*/189public void setDocumentHandler (DocumentHandler handler)190{191documentHandler = handler;192}193194195/**196* Register the error event handler.197*198* @param handler The new error event handler.199* @see org.xml.sax.Parser#setErrorHandler200*/201public void setErrorHandler (ErrorHandler handler)202{203xmlReader.setErrorHandler(handler);204}205206207/**208* Parse the document.209*210* <p>This method will throw an exception if the embedded211* XMLReader does not support the212* http://xml.org/sax/features/namespace-prefixes property.</p>213*214* @param systemId The absolute URL of the document.215* @exception java.io.IOException If there is a problem reading216* the raw content of the document.217* @exception org.xml.sax.SAXException If there is a problem218* processing the document.219* @see #parse(org.xml.sax.InputSource)220* @see org.xml.sax.Parser#parse(java.lang.String)221*/222public void parse (String systemId)223throws IOException, SAXException224{225parse(new InputSource(systemId));226}227228229/**230* Parse the document.231*232* <p>This method will throw an exception if the embedded233* XMLReader does not support the234* http://xml.org/sax/features/namespace-prefixes property.</p>235*236* @param input An input source for the document.237* @exception java.io.IOException If there is a problem reading238* the raw content of the document.239* @exception org.xml.sax.SAXException If there is a problem240* processing the document.241* @see #parse(java.lang.String)242* @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)243*/244public void parse (InputSource input)245throws IOException, SAXException246{247setupXMLReader();248xmlReader.parse(input);249}250251252/**253* Set up the XML reader.254*/255private void setupXMLReader ()256throws SAXException257{258xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);259try {260xmlReader.setFeature("http://xml.org/sax/features/namespaces",261false);262} catch (SAXException e) {263// NO OP: it's just extra information, and we can ignore it264}265xmlReader.setContentHandler(this);266}267268269270////////////////////////////////////////////////////////////////////271// Implementation of org.xml.sax.ContentHandler.272////////////////////////////////////////////////////////////////////273274275/**276* Set a document locator.277*278* @param locator The document locator.279* @see org.xml.sax.ContentHandler#setDocumentLocator280*/281public void setDocumentLocator (Locator locator)282{283if (documentHandler != null)284documentHandler.setDocumentLocator(locator);285}286287288/**289* Start document event.290*291* @exception org.xml.sax.SAXException The client may raise a292* processing exception.293* @see org.xml.sax.ContentHandler#startDocument294*/295public void startDocument ()296throws SAXException297{298if (documentHandler != null)299documentHandler.startDocument();300}301302303/**304* End document event.305*306* @exception org.xml.sax.SAXException The client may raise a307* processing exception.308* @see org.xml.sax.ContentHandler#endDocument309*/310public void endDocument ()311throws SAXException312{313if (documentHandler != null)314documentHandler.endDocument();315}316317318/**319* Adapt a SAX2 start prefix mapping event.320*321* @param prefix The prefix being mapped.322* @param uri The Namespace URI being mapped to.323* @see org.xml.sax.ContentHandler#startPrefixMapping324*/325public void startPrefixMapping (String prefix, String uri)326{327}328329330/**331* Adapt a SAX2 end prefix mapping event.332*333* @param prefix The prefix being mapped.334* @see org.xml.sax.ContentHandler#endPrefixMapping335*/336public void endPrefixMapping (String prefix)337{338}339340341/**342* Adapt a SAX2 start element event.343*344* @param uri The Namespace URI.345* @param localName The Namespace local name.346* @param qName The qualified (prefixed) name.347* @param atts The SAX2 attributes.348* @exception org.xml.sax.SAXException The client may raise a349* processing exception.350* @see org.xml.sax.ContentHandler#endDocument351*/352public void startElement (String uri, String localName,353String qName, Attributes atts)354throws SAXException355{356if (documentHandler != null) {357qAtts.setAttributes(atts);358documentHandler.startElement(qName, qAtts);359}360}361362363/**364* Adapt a SAX2 end element event.365*366* @param uri The Namespace URI.367* @param localName The Namespace local name.368* @param qName The qualified (prefixed) name.369* @exception org.xml.sax.SAXException The client may raise a370* processing exception.371* @see org.xml.sax.ContentHandler#endElement372*/373public void endElement (String uri, String localName,374String qName)375throws SAXException376{377if (documentHandler != null)378documentHandler.endElement(qName);379}380381382/**383* Adapt a SAX2 characters event.384*385* @param ch An array of characters.386* @param start The starting position in the array.387* @param length The number of characters to use.388* @exception org.xml.sax.SAXException The client may raise a389* processing exception.390* @see org.xml.sax.ContentHandler#characters391*/392public void characters (char ch[], int start, int length)393throws SAXException394{395if (documentHandler != null)396documentHandler.characters(ch, start, length);397}398399400/**401* Adapt a SAX2 ignorable whitespace event.402*403* @param ch An array of characters.404* @param start The starting position in the array.405* @param length The number of characters to use.406* @exception org.xml.sax.SAXException The client may raise a407* processing exception.408* @see org.xml.sax.ContentHandler#ignorableWhitespace409*/410public void ignorableWhitespace (char ch[], int start, int length)411throws SAXException412{413if (documentHandler != null)414documentHandler.ignorableWhitespace(ch, start, length);415}416417418/**419* Adapt a SAX2 processing instruction event.420*421* @param target The processing instruction target.422* @param data The remainder of the processing instruction423* @exception org.xml.sax.SAXException The client may raise a424* processing exception.425* @see org.xml.sax.ContentHandler#processingInstruction426*/427public void processingInstruction (String target, String data)428throws SAXException429{430if (documentHandler != null)431documentHandler.processingInstruction(target, data);432}433434435/**436* Adapt a SAX2 skipped entity event.437*438* @param name The name of the skipped entity.439* @see org.xml.sax.ContentHandler#skippedEntity440* @exception org.xml.sax.SAXException Throwable by subclasses.441*/442public void skippedEntity (String name)443throws SAXException444{445}446447448449////////////////////////////////////////////////////////////////////450// Internal state.451////////////////////////////////////////////////////////////////////452453XMLReader xmlReader;454DocumentHandler documentHandler;455AttributesAdapter qAtts;456457458459////////////////////////////////////////////////////////////////////460// Internal class.461////////////////////////////////////////////////////////////////////462463464/**465* Internal class to wrap a SAX2 Attributes object for SAX1.466*/467final class AttributesAdapter implements AttributeList468{469AttributesAdapter ()470{471}472473474/**475* Set the embedded Attributes object.476*477* @param The embedded SAX2 Attributes.478*/479void setAttributes (Attributes attributes)480{481this.attributes = attributes;482}483484485/**486* Return the number of attributes.487*488* @return The length of the attribute list.489* @see org.xml.sax.AttributeList#getLength490*/491public int getLength ()492{493return attributes.getLength();494}495496497/**498* Return the qualified (prefixed) name of an attribute by position.499*500* @return The qualified name.501* @see org.xml.sax.AttributeList#getName502*/503public String getName (int i)504{505return attributes.getQName(i);506}507508509/**510* Return the type of an attribute by position.511*512* @return The type.513* @see org.xml.sax.AttributeList#getType(int)514*/515public String getType (int i)516{517return attributes.getType(i);518}519520521/**522* Return the value of an attribute by position.523*524* @return The value.525* @see org.xml.sax.AttributeList#getValue(int)526*/527public String getValue (int i)528{529return attributes.getValue(i);530}531532533/**534* Return the type of an attribute by qualified (prefixed) name.535*536* @return The type.537* @see org.xml.sax.AttributeList#getType(java.lang.String)538*/539public String getType (String qName)540{541return attributes.getType(qName);542}543544545/**546* Return the value of an attribute by qualified (prefixed) name.547*548* @return The value.549* @see org.xml.sax.AttributeList#getValue(java.lang.String)550*/551public String getValue (String qName)552{553return attributes.getValue(qName);554}555556private Attributes attributes;557}558559}560561// end of XMLReaderAdapter.java562563564