< prev index next >

src/java.xml/share/classes/org/xml/sax/helpers/XMLReaderAdapter.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  59  * @author David Megginson
  60  * @see org.xml.sax.Parser
  61  * @see org.xml.sax.XMLReader
  62  */
  63 @SuppressWarnings("deprecation")
  64 public class XMLReaderAdapter implements Parser, ContentHandler
  65 {
  66 
  67 
  68     ////////////////////////////////////////////////////////////////////
  69     // Constructor.
  70     ////////////////////////////////////////////////////////////////////
  71 
  72 
  73     /**
  74      * Create a new adapter.
  75      *
  76      * <p>Use the "org.xml.sax.driver" property to locate the SAX2
  77      * driver to embed.</p>
  78      *
  79      * @exception org.xml.sax.SAXException If the embedded driver
  80      *            cannot be instantiated or if the
  81      *            org.xml.sax.driver property is not specified.
  82      */
  83     public XMLReaderAdapter ()
  84       throws SAXException
  85     {
  86         setup(XMLReaderFactory.createXMLReader());
  87     }
  88 
  89 
  90     /**
  91      * Create a new adapter.
  92      *
  93      * <p>Create a new adapter, wrapped around a SAX2 XMLReader.
  94      * The adapter will make the XMLReader act like a SAX1
  95      * Parser.</p>
  96      *
  97      * @param xmlReader The SAX2 XMLReader to wrap.
  98      * @exception java.lang.NullPointerException If the argument is null.
  99      */
 100     public XMLReaderAdapter (XMLReader xmlReader)
 101     {
 102         setup(xmlReader);
 103     }
 104 
 105 
 106 
 107     /**
 108      * Internal setup.
 109      *
 110      * @param xmlReader The embedded XMLReader.
 111      */
 112     private void setup (XMLReader xmlReader)
 113     {
 114         if (xmlReader == null) {
 115             throw new NullPointerException("XMLReader must not be null");
 116         }
 117         this.xmlReader = xmlReader;
 118         qAtts = new AttributesAdapter();
 119     }
 120 
 121 
 122 
 123     ////////////////////////////////////////////////////////////////////
 124     // Implementation of org.xml.sax.Parser.
 125     ////////////////////////////////////////////////////////////////////
 126 
 127 
 128     /**
 129      * Set the locale for error reporting.
 130      *
 131      * <p>This is not supported in SAX2, and will always throw
 132      * an exception.</p>
 133      *
 134      * @param locale the locale for error reporting.
 135      * @see org.xml.sax.Parser#setLocale
 136      * @exception org.xml.sax.SAXException Thrown unless overridden.
 137      */
 138     public void setLocale (Locale locale)
 139         throws SAXException
 140     {
 141         throw new SAXNotSupportedException("setLocale not supported");
 142     }
 143 
 144 
 145     /**
 146      * Register the entity resolver.
 147      *
 148      * @param resolver The new resolver.
 149      * @see org.xml.sax.Parser#setEntityResolver
 150      */
 151     public void setEntityResolver (EntityResolver resolver)
 152     {
 153         xmlReader.setEntityResolver(resolver);
 154     }
 155 
 156 


 184     /**
 185      * Register the error event handler.
 186      *
 187      * @param handler The new error event handler.
 188      * @see org.xml.sax.Parser#setErrorHandler
 189      */
 190     public void setErrorHandler (ErrorHandler handler)
 191     {
 192         xmlReader.setErrorHandler(handler);
 193     }
 194 
 195 
 196     /**
 197      * Parse the document.
 198      *
 199      * <p>This method will throw an exception if the embedded
 200      * XMLReader does not support the
 201      * http://xml.org/sax/features/namespace-prefixes property.</p>
 202      *
 203      * @param systemId The absolute URL of the document.
 204      * @exception java.io.IOException If there is a problem reading
 205      *            the raw content of the document.
 206      * @exception org.xml.sax.SAXException If there is a problem
 207      *            processing the document.
 208      * @see #parse(org.xml.sax.InputSource)
 209      * @see org.xml.sax.Parser#parse(java.lang.String)
 210      */
 211     public void parse (String systemId)
 212         throws IOException, SAXException
 213     {
 214         parse(new InputSource(systemId));
 215     }
 216 
 217 
 218     /**
 219      * Parse the document.
 220      *
 221      * <p>This method will throw an exception if the embedded
 222      * XMLReader does not support the
 223      * http://xml.org/sax/features/namespace-prefixes property.</p>
 224      *
 225      * @param input An input source for the document.
 226      * @exception java.io.IOException If there is a problem reading
 227      *            the raw content of the document.
 228      * @exception org.xml.sax.SAXException If there is a problem
 229      *            processing the document.
 230      * @see #parse(java.lang.String)
 231      * @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)
 232      */
 233     public void parse (InputSource input)
 234         throws IOException, SAXException
 235     {
 236         setupXMLReader();
 237         xmlReader.parse(input);
 238     }
 239 
 240 
 241     /**
 242      * Set up the XML reader.
 243      */
 244     private void setupXMLReader ()
 245         throws SAXException
 246     {
 247         xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
 248         try {


 260     // Implementation of org.xml.sax.ContentHandler.
 261     ////////////////////////////////////////////////////////////////////
 262 
 263 
 264     /**
 265      * Set a document locator.
 266      *
 267      * @param locator The document locator.
 268      * @see org.xml.sax.ContentHandler#setDocumentLocator
 269      */
 270     public void setDocumentLocator (Locator locator)
 271     {
 272         if (documentHandler != null)
 273             documentHandler.setDocumentLocator(locator);
 274     }
 275 
 276 
 277     /**
 278      * Start document event.
 279      *
 280      * @exception org.xml.sax.SAXException The client may raise a
 281      *            processing exception.
 282      * @see org.xml.sax.ContentHandler#startDocument
 283      */
 284     public void startDocument ()
 285         throws SAXException
 286     {
 287         if (documentHandler != null)
 288             documentHandler.startDocument();
 289     }
 290 
 291 
 292     /**
 293      * End document event.
 294      *
 295      * @exception org.xml.sax.SAXException The client may raise a
 296      *            processing exception.
 297      * @see org.xml.sax.ContentHandler#endDocument
 298      */
 299     public void endDocument ()
 300         throws SAXException
 301     {
 302         if (documentHandler != null)
 303             documentHandler.endDocument();
 304     }
 305 
 306 
 307     /**
 308      * Adapt a SAX2 start prefix mapping event.
 309      *
 310      * @param prefix The prefix being mapped.
 311      * @param uri The Namespace URI being mapped to.
 312      * @see org.xml.sax.ContentHandler#startPrefixMapping
 313      */
 314     public void startPrefixMapping (String prefix, String uri)
 315     {


 317 
 318 
 319     /**
 320      * Adapt a SAX2 end prefix mapping event.
 321      *
 322      * @param prefix The prefix being mapped.
 323      * @see org.xml.sax.ContentHandler#endPrefixMapping
 324      */
 325     public void endPrefixMapping (String prefix)
 326     {
 327     }
 328 
 329 
 330     /**
 331      * Adapt a SAX2 start element event.
 332      *
 333      * @param uri The Namespace URI.
 334      * @param localName The Namespace local name.
 335      * @param qName The qualified (prefixed) name.
 336      * @param atts The SAX2 attributes.
 337      * @exception org.xml.sax.SAXException The client may raise a
 338      *            processing exception.
 339      * @see org.xml.sax.ContentHandler#endDocument
 340      */
 341     public void startElement (String uri, String localName,
 342                               String qName, Attributes atts)
 343         throws SAXException
 344     {
 345         if (documentHandler != null) {
 346             qAtts.setAttributes(atts);
 347             documentHandler.startElement(qName, qAtts);
 348         }
 349     }
 350 
 351 
 352     /**
 353      * Adapt a SAX2 end element event.
 354      *
 355      * @param uri The Namespace URI.
 356      * @param localName The Namespace local name.
 357      * @param qName The qualified (prefixed) name.
 358      * @exception org.xml.sax.SAXException The client may raise a
 359      *            processing exception.
 360      * @see org.xml.sax.ContentHandler#endElement
 361      */
 362     public void endElement (String uri, String localName,
 363                             String qName)
 364         throws SAXException
 365     {
 366         if (documentHandler != null)
 367             documentHandler.endElement(qName);
 368     }
 369 
 370 
 371     /**
 372      * Adapt a SAX2 characters event.
 373      *
 374      * @param ch An array of characters.
 375      * @param start The starting position in the array.
 376      * @param length The number of characters to use.
 377      * @exception org.xml.sax.SAXException The client may raise a
 378      *            processing exception.
 379      * @see org.xml.sax.ContentHandler#characters
 380      */
 381     public void characters (char ch[], int start, int length)
 382         throws SAXException
 383     {
 384         if (documentHandler != null)
 385             documentHandler.characters(ch, start, length);
 386     }
 387 
 388 
 389     /**
 390      * Adapt a SAX2 ignorable whitespace event.
 391      *
 392      * @param ch An array of characters.
 393      * @param start The starting position in the array.
 394      * @param length The number of characters to use.
 395      * @exception org.xml.sax.SAXException The client may raise a
 396      *            processing exception.
 397      * @see org.xml.sax.ContentHandler#ignorableWhitespace
 398      */
 399     public void ignorableWhitespace (char ch[], int start, int length)
 400         throws SAXException
 401     {
 402         if (documentHandler != null)
 403             documentHandler.ignorableWhitespace(ch, start, length);
 404     }
 405 
 406 
 407     /**
 408      * Adapt a SAX2 processing instruction event.
 409      *
 410      * @param target The processing instruction target.
 411      * @param data The remainder of the processing instruction
 412      * @exception org.xml.sax.SAXException The client may raise a
 413      *            processing exception.
 414      * @see org.xml.sax.ContentHandler#processingInstruction
 415      */
 416     public void processingInstruction (String target, String data)
 417         throws SAXException
 418     {
 419         if (documentHandler != null)
 420             documentHandler.processingInstruction(target, data);
 421     }
 422 
 423 
 424     /**
 425      * Adapt a SAX2 skipped entity event.
 426      *
 427      * @param name The name of the skipped entity.
 428      * @see org.xml.sax.ContentHandler#skippedEntity
 429      * @exception org.xml.sax.SAXException Throwable by subclasses.
 430      */
 431     public void skippedEntity (String name)
 432         throws SAXException
 433     {
 434     }
 435 
 436 
 437 
 438     ////////////////////////////////////////////////////////////////////
 439     // Internal state.
 440     ////////////////////////////////////////////////////////////////////
 441 
 442     XMLReader xmlReader;
 443     DocumentHandler documentHandler;
 444     AttributesAdapter qAtts;
 445 
 446 
 447 
 448     ////////////////////////////////////////////////////////////////////
 449     // Internal class.


   1 /*
   2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  59  * @author David Megginson
  60  * @see org.xml.sax.Parser
  61  * @see org.xml.sax.XMLReader
  62  */
  63 @SuppressWarnings("deprecation")
  64 public class XMLReaderAdapter implements Parser, ContentHandler
  65 {
  66 
  67 
  68     ////////////////////////////////////////////////////////////////////
  69     // Constructor.
  70     ////////////////////////////////////////////////////////////////////
  71 
  72 
  73     /**
  74      * Create a new adapter.
  75      *
  76      * <p>Use the "org.xml.sax.driver" property to locate the SAX2
  77      * driver to embed.</p>
  78      *
  79      * @throws org.xml.sax.SAXException If the embedded driver
  80      *            cannot be instantiated or if the
  81      *            org.xml.sax.driver property is not specified.
  82      */
  83     public XMLReaderAdapter ()
  84       throws SAXException
  85     {
  86         setup(XMLReaderFactory.createXMLReader());
  87     }
  88 
  89 
  90     /**
  91      * Create a new adapter.
  92      *
  93      * <p>Create a new adapter, wrapped around a SAX2 XMLReader.
  94      * The adapter will make the XMLReader act like a SAX1
  95      * Parser.</p>
  96      *
  97      * @param xmlReader The SAX2 XMLReader to wrap.
  98      * @throws java.lang.NullPointerException If the argument is null.
  99      */
 100     public XMLReaderAdapter (XMLReader xmlReader)
 101     {
 102         setup(xmlReader);
 103     }
 104 
 105 
 106 
 107     /**
 108      * Internal setup.
 109      *
 110      * @param xmlReader The embedded XMLReader.
 111      */
 112     private void setup (XMLReader xmlReader)
 113     {
 114         if (xmlReader == null) {
 115             throw new NullPointerException("XMLReader must not be null");
 116         }
 117         this.xmlReader = xmlReader;
 118         qAtts = new AttributesAdapter();
 119     }
 120 
 121 
 122 
 123     ////////////////////////////////////////////////////////////////////
 124     // Implementation of org.xml.sax.Parser.
 125     ////////////////////////////////////////////////////////////////////
 126 
 127 
 128     /**
 129      * Set the locale for error reporting.
 130      *
 131      * <p>This is not supported in SAX2, and will always throw
 132      * an exception.</p>
 133      *
 134      * @param locale the locale for error reporting.
 135      * @see org.xml.sax.Parser#setLocale
 136      * @throws org.xml.sax.SAXException Thrown unless overridden.
 137      */
 138     public void setLocale (Locale locale)
 139         throws SAXException
 140     {
 141         throw new SAXNotSupportedException("setLocale not supported");
 142     }
 143 
 144 
 145     /**
 146      * Register the entity resolver.
 147      *
 148      * @param resolver The new resolver.
 149      * @see org.xml.sax.Parser#setEntityResolver
 150      */
 151     public void setEntityResolver (EntityResolver resolver)
 152     {
 153         xmlReader.setEntityResolver(resolver);
 154     }
 155 
 156 


 184     /**
 185      * Register the error event handler.
 186      *
 187      * @param handler The new error event handler.
 188      * @see org.xml.sax.Parser#setErrorHandler
 189      */
 190     public void setErrorHandler (ErrorHandler handler)
 191     {
 192         xmlReader.setErrorHandler(handler);
 193     }
 194 
 195 
 196     /**
 197      * Parse the document.
 198      *
 199      * <p>This method will throw an exception if the embedded
 200      * XMLReader does not support the
 201      * http://xml.org/sax/features/namespace-prefixes property.</p>
 202      *
 203      * @param systemId The absolute URL of the document.
 204      * @throws java.io.IOException If there is a problem reading
 205      *            the raw content of the document.
 206      * @throws org.xml.sax.SAXException If there is a problem
 207      *            processing the document.
 208      * @see #parse(org.xml.sax.InputSource)
 209      * @see org.xml.sax.Parser#parse(java.lang.String)
 210      */
 211     public void parse (String systemId)
 212         throws IOException, SAXException
 213     {
 214         parse(new InputSource(systemId));
 215     }
 216 
 217 
 218     /**
 219      * Parse the document.
 220      *
 221      * <p>This method will throw an exception if the embedded
 222      * XMLReader does not support the
 223      * http://xml.org/sax/features/namespace-prefixes property.</p>
 224      *
 225      * @param input An input source for the document.
 226      * @throws java.io.IOException If there is a problem reading
 227      *            the raw content of the document.
 228      * @throws org.xml.sax.SAXException If there is a problem
 229      *            processing the document.
 230      * @see #parse(java.lang.String)
 231      * @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)
 232      */
 233     public void parse (InputSource input)
 234         throws IOException, SAXException
 235     {
 236         setupXMLReader();
 237         xmlReader.parse(input);
 238     }
 239 
 240 
 241     /**
 242      * Set up the XML reader.
 243      */
 244     private void setupXMLReader ()
 245         throws SAXException
 246     {
 247         xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
 248         try {


 260     // Implementation of org.xml.sax.ContentHandler.
 261     ////////////////////////////////////////////////////////////////////
 262 
 263 
 264     /**
 265      * Set a document locator.
 266      *
 267      * @param locator The document locator.
 268      * @see org.xml.sax.ContentHandler#setDocumentLocator
 269      */
 270     public void setDocumentLocator (Locator locator)
 271     {
 272         if (documentHandler != null)
 273             documentHandler.setDocumentLocator(locator);
 274     }
 275 
 276 
 277     /**
 278      * Start document event.
 279      *
 280      * @throws org.xml.sax.SAXException The client may raise a
 281      *            processing exception.
 282      * @see org.xml.sax.ContentHandler#startDocument
 283      */
 284     public void startDocument ()
 285         throws SAXException
 286     {
 287         if (documentHandler != null)
 288             documentHandler.startDocument();
 289     }
 290 
 291 
 292     /**
 293      * End document event.
 294      *
 295      * @throws org.xml.sax.SAXException The client may raise a
 296      *            processing exception.
 297      * @see org.xml.sax.ContentHandler#endDocument
 298      */
 299     public void endDocument ()
 300         throws SAXException
 301     {
 302         if (documentHandler != null)
 303             documentHandler.endDocument();
 304     }
 305 
 306 
 307     /**
 308      * Adapt a SAX2 start prefix mapping event.
 309      *
 310      * @param prefix The prefix being mapped.
 311      * @param uri The Namespace URI being mapped to.
 312      * @see org.xml.sax.ContentHandler#startPrefixMapping
 313      */
 314     public void startPrefixMapping (String prefix, String uri)
 315     {


 317 
 318 
 319     /**
 320      * Adapt a SAX2 end prefix mapping event.
 321      *
 322      * @param prefix The prefix being mapped.
 323      * @see org.xml.sax.ContentHandler#endPrefixMapping
 324      */
 325     public void endPrefixMapping (String prefix)
 326     {
 327     }
 328 
 329 
 330     /**
 331      * Adapt a SAX2 start element event.
 332      *
 333      * @param uri The Namespace URI.
 334      * @param localName The Namespace local name.
 335      * @param qName The qualified (prefixed) name.
 336      * @param atts The SAX2 attributes.
 337      * @throws org.xml.sax.SAXException The client may raise a
 338      *            processing exception.
 339      * @see org.xml.sax.ContentHandler#endDocument
 340      */
 341     public void startElement (String uri, String localName,
 342                               String qName, Attributes atts)
 343         throws SAXException
 344     {
 345         if (documentHandler != null) {
 346             qAtts.setAttributes(atts);
 347             documentHandler.startElement(qName, qAtts);
 348         }
 349     }
 350 
 351 
 352     /**
 353      * Adapt a SAX2 end element event.
 354      *
 355      * @param uri The Namespace URI.
 356      * @param localName The Namespace local name.
 357      * @param qName The qualified (prefixed) name.
 358      * @throws org.xml.sax.SAXException The client may raise a
 359      *            processing exception.
 360      * @see org.xml.sax.ContentHandler#endElement
 361      */
 362     public void endElement (String uri, String localName,
 363                             String qName)
 364         throws SAXException
 365     {
 366         if (documentHandler != null)
 367             documentHandler.endElement(qName);
 368     }
 369 
 370 
 371     /**
 372      * Adapt a SAX2 characters event.
 373      *
 374      * @param ch An array of characters.
 375      * @param start The starting position in the array.
 376      * @param length The number of characters to use.
 377      * @throws org.xml.sax.SAXException The client may raise a
 378      *            processing exception.
 379      * @see org.xml.sax.ContentHandler#characters
 380      */
 381     public void characters (char ch[], int start, int length)
 382         throws SAXException
 383     {
 384         if (documentHandler != null)
 385             documentHandler.characters(ch, start, length);
 386     }
 387 
 388 
 389     /**
 390      * Adapt a SAX2 ignorable whitespace event.
 391      *
 392      * @param ch An array of characters.
 393      * @param start The starting position in the array.
 394      * @param length The number of characters to use.
 395      * @throws org.xml.sax.SAXException The client may raise a
 396      *            processing exception.
 397      * @see org.xml.sax.ContentHandler#ignorableWhitespace
 398      */
 399     public void ignorableWhitespace (char ch[], int start, int length)
 400         throws SAXException
 401     {
 402         if (documentHandler != null)
 403             documentHandler.ignorableWhitespace(ch, start, length);
 404     }
 405 
 406 
 407     /**
 408      * Adapt a SAX2 processing instruction event.
 409      *
 410      * @param target The processing instruction target.
 411      * @param data The remainder of the processing instruction
 412      * @throws org.xml.sax.SAXException The client may raise a
 413      *            processing exception.
 414      * @see org.xml.sax.ContentHandler#processingInstruction
 415      */
 416     public void processingInstruction (String target, String data)
 417         throws SAXException
 418     {
 419         if (documentHandler != null)
 420             documentHandler.processingInstruction(target, data);
 421     }
 422 
 423 
 424     /**
 425      * Adapt a SAX2 skipped entity event.
 426      *
 427      * @param name The name of the skipped entity.
 428      * @see org.xml.sax.ContentHandler#skippedEntity
 429      * @throws org.xml.sax.SAXException Throwable by subclasses.
 430      */
 431     public void skippedEntity (String name)
 432         throws SAXException
 433     {
 434     }
 435 
 436 
 437 
 438     ////////////////////////////////////////////////////////////////////
 439     // Internal state.
 440     ////////////////////////////////////////////////////////////////////
 441 
 442     XMLReader xmlReader;
 443     DocumentHandler documentHandler;
 444     AttributesAdapter qAtts;
 445 
 446 
 447 
 448     ////////////////////////////////////////////////////////////////////
 449     // Internal class.


< prev index next >