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