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
  23  * questions.
  24  */
  25 
  26 package org.xml.sax.helpers;
  27 
  28 import java.io.IOException;
  29 
  30 import org.xml.sax.InputSource;
  31 import org.xml.sax.Locator;
  32 import org.xml.sax.Attributes;
  33 import org.xml.sax.EntityResolver;
  34 import org.xml.sax.DTDHandler;
  35 import org.xml.sax.ContentHandler;
  36 import org.xml.sax.ErrorHandler;
  37 import org.xml.sax.SAXException;
  38 import org.xml.sax.SAXParseException;
  39 
  40 
  41 /**
  42  * Default base class for SAX2 event handlers.
  43  *
  44  * <p>This class is available as a convenience base class for SAX2
  45  * applications: it provides default implementations for all of the
  46  * callbacks in the four core SAX2 handler classes:</p>
  47  *
  48  * <ul>
  49  * <li>{@link org.xml.sax.EntityResolver EntityResolver}</li>
  50  * <li>{@link org.xml.sax.DTDHandler DTDHandler}</li>
  51  * <li>{@link org.xml.sax.ContentHandler ContentHandler}</li>
  52  * <li>{@link org.xml.sax.ErrorHandler ErrorHandler}</li>
  53  * </ul>
  54  *
  55  * <p>Application writers can extend this class when they need to
  56  * implement only part of an interface; parser writers can
  57  * instantiate this class to provide default handlers when the
  58  * application has not supplied its own.</p>
  59  *
  60  * <p>This class replaces the deprecated SAX1
  61  * {@link org.xml.sax.HandlerBase HandlerBase} class.</p>
  62  *
  63  * @since 1.4, SAX 2.0
  64  * @author David Megginson,
  65  * @see org.xml.sax.EntityResolver
  66  * @see org.xml.sax.DTDHandler
  67  * @see org.xml.sax.ContentHandler
  68  * @see org.xml.sax.ErrorHandler
  69  */
  70 public class DefaultHandler
  71     implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler
  72 {
  73     /**
  74      * Constructs a {@code DefaultHandler}.
  75      */
  76     public DefaultHandler() {}
  77 
  78     ////////////////////////////////////////////////////////////////////
  79     // Default implementation of the EntityResolver interface.
  80     ////////////////////////////////////////////////////////////////////
  81 
  82     /**
  83      * Resolve an external entity.
  84      *
  85      * <p>Always return null, so that the parser will use the system
  86      * identifier provided in the XML document.  This method implements
  87      * the SAX default behaviour: application writers can override it
  88      * in a subclass to do special translations such as catalog lookups
  89      * or URI redirection.</p>
  90      *
  91      * @param publicId The public identifier, or null if none is
  92      *                 available.
  93      * @param systemId The system identifier provided in the XML
  94      *                 document.
  95      * @return The new input source, or null to require the
  96      *         default behaviour.
  97      * @throws java.io.IOException If there is an error setting
  98      *            up the new input source.
  99      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 100      *            wrapping another exception.
 101      * @see org.xml.sax.EntityResolver#resolveEntity
 102      */
 103     @Override
 104     public InputSource resolveEntity (String publicId, String systemId)
 105         throws IOException, SAXException
 106     {
 107         return null;
 108     }
 109 
 110 
 111 
 112     ////////////////////////////////////////////////////////////////////
 113     // Default implementation of DTDHandler interface.
 114     ////////////////////////////////////////////////////////////////////
 115 
 116 
 117     /**
 118      * Receive notification of a notation declaration.
 119      *
 120      * <p>By default, do nothing.  Application writers may override this
 121      * method in a subclass if they wish to keep track of the notations
 122      * declared in a document.</p>
 123      *
 124      * @param name The notation name.
 125      * @param publicId The notation public identifier, or null if not
 126      *                 available.
 127      * @param systemId The notation system identifier.
 128      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 129      *            wrapping another exception.
 130      * @see org.xml.sax.DTDHandler#notationDecl
 131      */
 132     @Override
 133     public void notationDecl (String name, String publicId, String systemId)
 134         throws SAXException
 135     {
 136         // no op
 137     }
 138 
 139 
 140     /**
 141      * Receive notification of an unparsed entity declaration.
 142      *
 143      * <p>By default, do nothing.  Application writers may override this
 144      * method in a subclass to keep track of the unparsed entities
 145      * declared in a document.</p>
 146      *
 147      * @param name The entity name.
 148      * @param publicId The entity public identifier, or null if not
 149      *                 available.
 150      * @param systemId The entity system identifier.
 151      * @param notationName The name of the associated notation.
 152      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 153      *            wrapping another exception.
 154      * @see org.xml.sax.DTDHandler#unparsedEntityDecl
 155      */
 156     @Override
 157     public void unparsedEntityDecl (String name, String publicId,
 158                                     String systemId, String notationName)
 159         throws SAXException
 160     {
 161         // no op
 162     }
 163 
 164 
 165 
 166     ////////////////////////////////////////////////////////////////////
 167     // Default implementation of ContentHandler interface.
 168     ////////////////////////////////////////////////////////////////////
 169 
 170 
 171     /**
 172      * Receive a Locator object for document events.
 173      *
 174      * <p>By default, do nothing.  Application writers may override this
 175      * method in a subclass if they wish to store the locator for use
 176      * with other document events.</p>
 177      *
 178      * @param locator A locator for all SAX document events.
 179      * @see org.xml.sax.ContentHandler#setDocumentLocator
 180      * @see org.xml.sax.Locator
 181      */
 182     @Override
 183     public void setDocumentLocator (Locator locator)
 184     {
 185         // no op
 186     }
 187 
 188 
 189     /**
 190      * Receive notification of the beginning of the document.
 191      *
 192      * <p>By default, do nothing.  Application writers may override this
 193      * method in a subclass to take specific actions at the beginning
 194      * of a document (such as allocating the root node of a tree or
 195      * creating an output file).</p>
 196      *
 197      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 198      *            wrapping another exception.
 199      * @see org.xml.sax.ContentHandler#startDocument
 200      */
 201     @Override
 202     public void startDocument ()
 203         throws SAXException
 204     {
 205         // no op
 206     }
 207 
 208 
 209     /**
 210      * Receive notification of the end of the document.
 211      *
 212      * <p>By default, do nothing.  Application writers may override this
 213      * method in a subclass to take specific actions at the end
 214      * of a document (such as finalising a tree or closing an output
 215      * file).</p>
 216      *
 217      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 218      *            wrapping another exception.
 219      * @see org.xml.sax.ContentHandler#endDocument
 220      */
 221     @Override
 222     public void endDocument ()
 223         throws SAXException
 224     {
 225         // no op
 226     }
 227 
 228 
 229     /**
 230      * Receive notification of the start of a Namespace mapping.
 231      *
 232      * <p>By default, do nothing.  Application writers may override this
 233      * method in a subclass to take specific actions at the start of
 234      * each Namespace prefix scope (such as storing the prefix mapping).</p>
 235      *
 236      * @param prefix The Namespace prefix being declared.
 237      * @param uri The Namespace URI mapped to the prefix.
 238      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 239      *            wrapping another exception.
 240      * @see org.xml.sax.ContentHandler#startPrefixMapping
 241      */
 242     @Override
 243     public void startPrefixMapping (String prefix, String uri)
 244         throws SAXException
 245     {
 246         // no op
 247     }
 248 
 249 
 250     /**
 251      * Receive notification of the end of a Namespace mapping.
 252      *
 253      * <p>By default, do nothing.  Application writers may override this
 254      * method in a subclass to take specific actions at the end of
 255      * each prefix mapping.</p>
 256      *
 257      * @param prefix The Namespace prefix being declared.
 258      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 259      *            wrapping another exception.
 260      * @see org.xml.sax.ContentHandler#endPrefixMapping
 261      */
 262     @Override
 263     public void endPrefixMapping (String prefix)
 264         throws SAXException
 265     {
 266         // no op
 267     }
 268 
 269 
 270     /**
 271      * Receive notification of the start of an element.
 272      *
 273      * <p>By default, do nothing.  Application writers may override this
 274      * method in a subclass to take specific actions at the start of
 275      * each element (such as allocating a new tree node or writing
 276      * output to a file).</p>
 277      *
 278      * @param uri The Namespace URI, or the empty string if the
 279      *        element has no Namespace URI or if Namespace
 280      *        processing is not being performed.
 281      * @param localName The local name (without prefix), or the
 282      *        empty string if Namespace processing is not being
 283      *        performed.
 284      * @param qName The qualified name (with prefix), or the
 285      *        empty string if qualified names are not available.
 286      * @param attributes The attributes attached to the element.  If
 287      *        there are no attributes, it shall be an empty
 288      *        Attributes object.
 289      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 290      *            wrapping another exception.
 291      * @see org.xml.sax.ContentHandler#startElement
 292      */
 293     @Override
 294     public void startElement (String uri, String localName,
 295                               String qName, Attributes attributes)
 296         throws SAXException
 297     {
 298         // no op
 299     }
 300 
 301 
 302     /**
 303      * Receive notification of the end of an element.
 304      *
 305      * <p>By default, do nothing.  Application writers may override this
 306      * method in a subclass to take specific actions at the end of
 307      * each element (such as finalising a tree node or writing
 308      * output to a file).</p>
 309      *
 310      * @param uri The Namespace URI, or the empty string if the
 311      *        element has no Namespace URI or if Namespace
 312      *        processing is not being performed.
 313      * @param localName The local name (without prefix), or the
 314      *        empty string if Namespace processing is not being
 315      *        performed.
 316      * @param qName The qualified name (with prefix), or the
 317      *        empty string if qualified names are not available.
 318      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 319      *            wrapping another exception.
 320      * @see org.xml.sax.ContentHandler#endElement
 321      */
 322     @Override
 323     public void endElement (String uri, String localName, String qName)
 324         throws SAXException
 325     {
 326         // no op
 327     }
 328 
 329 
 330     /**
 331      * Receive notification of character data inside an element.
 332      *
 333      * <p>By default, do nothing.  Application writers may override this
 334      * method to take specific actions for each chunk of character data
 335      * (such as adding the data to a node or buffer, or printing it to
 336      * a file).</p>
 337      *
 338      * @param ch The characters.
 339      * @param start The start position in the character array.
 340      * @param length The number of characters to use from the
 341      *               character array.
 342      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 343      *            wrapping another exception.
 344      * @see org.xml.sax.ContentHandler#characters
 345      */
 346     @Override
 347     public void characters (char ch[], int start, int length)
 348         throws SAXException
 349     {
 350         // no op
 351     }
 352 
 353 
 354     /**
 355      * Receive notification of ignorable whitespace in element content.
 356      *
 357      * <p>By default, do nothing.  Application writers may override this
 358      * method to take specific actions for each chunk of ignorable
 359      * whitespace (such as adding data to a node or buffer, or printing
 360      * it to a file).</p>
 361      *
 362      * @param ch The whitespace characters.
 363      * @param start The start position in the character array.
 364      * @param length The number of characters to use from the
 365      *               character array.
 366      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 367      *            wrapping another exception.
 368      * @see org.xml.sax.ContentHandler#ignorableWhitespace
 369      */
 370     @Override
 371     public void ignorableWhitespace (char ch[], int start, int length)
 372         throws SAXException
 373     {
 374         // no op
 375     }
 376 
 377 
 378     /**
 379      * Receive notification of a processing instruction.
 380      *
 381      * <p>By default, do nothing.  Application writers may override this
 382      * method in a subclass to take specific actions for each
 383      * processing instruction, such as setting status variables or
 384      * invoking other methods.</p>
 385      *
 386      * @param target The processing instruction target.
 387      * @param data The processing instruction data, or null if
 388      *             none is supplied.
 389      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 390      *            wrapping another exception.
 391      * @see org.xml.sax.ContentHandler#processingInstruction
 392      */
 393     @Override
 394     public void processingInstruction (String target, String data)
 395         throws SAXException
 396     {
 397         // no op
 398     }
 399 
 400 
 401     /**
 402      * Receive notification of a skipped entity.
 403      *
 404      * <p>By default, do nothing.  Application writers may override this
 405      * method in a subclass to take specific actions for each
 406      * processing instruction, such as setting status variables or
 407      * invoking other methods.</p>
 408      *
 409      * @param name The name of the skipped entity.
 410      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 411      *            wrapping another exception.
 412      * @see org.xml.sax.ContentHandler#processingInstruction
 413      */
 414     @Override
 415     public void skippedEntity (String name)
 416         throws SAXException
 417     {
 418         // no op
 419     }
 420 
 421 
 422 
 423     ////////////////////////////////////////////////////////////////////
 424     // Default implementation of the ErrorHandler interface.
 425     ////////////////////////////////////////////////////////////////////
 426 
 427 
 428     /**
 429      * Receive notification of a parser warning.
 430      *
 431      * <p>The default implementation does nothing.  Application writers
 432      * may override this method in a subclass to take specific actions
 433      * for each warning, such as inserting the message in a log file or
 434      * printing it to the console.</p>
 435      *
 436      * @param e The warning information encoded as an exception.
 437      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 438      *            wrapping another exception.
 439      * @see org.xml.sax.ErrorHandler#warning
 440      * @see org.xml.sax.SAXParseException
 441      */
 442     @Override
 443     public void warning (SAXParseException e)
 444         throws SAXException
 445     {
 446         // no op
 447     }
 448 
 449 
 450     /**
 451      * Receive notification of a recoverable parser error.
 452      *
 453      * <p>The default implementation does nothing.  Application writers
 454      * may override this method in a subclass to take specific actions
 455      * for each error, such as inserting the message in a log file or
 456      * printing it to the console.</p>
 457      *
 458      * @param e The error information encoded as an exception.
 459      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 460      *            wrapping another exception.
 461      * @see org.xml.sax.ErrorHandler#warning
 462      * @see org.xml.sax.SAXParseException
 463      */
 464     @Override
 465     public void error (SAXParseException e)
 466         throws SAXException
 467     {
 468         // no op
 469     }
 470 
 471 
 472     /**
 473      * Report a fatal XML parsing error.
 474      *
 475      * <p>The default implementation throws a SAXParseException.
 476      * Application writers may override this method in a subclass if
 477      * they need to take specific actions for each fatal error (such as
 478      * collecting all of the errors into a single report): in any case,
 479      * the application must stop all regular processing when this
 480      * method is invoked, since the document is no longer reliable, and
 481      * the parser may no longer report parsing events.</p>
 482      *
 483      * @param e The error information encoded as an exception.
 484      * @throws org.xml.sax.SAXException Any SAX exception, possibly
 485      *            wrapping another exception.
 486      * @see org.xml.sax.ErrorHandler#fatalError
 487      * @see org.xml.sax.SAXParseException
 488      */
 489     @Override
 490     public void fatalError (SAXParseException e)
 491         throws SAXException
 492     {
 493         throw e;
 494     }
 495 
 496 }
 497 
 498 // end of DefaultHandler.java