1 package org.xml.sax.demo;
3 // SAX event handler for demos.
4 // No warranty; no copyright -- use this as you will.
5 // $Id: DemoHandler.java,v 1.3 1998/05/01 20:45:16 david Exp $
7 import org.xml.sax.HandlerBase;
8 import org.xml.sax.InputSource;
9 import org.xml.sax.Locator;
10 import org.xml.sax.AttributeList;
11 import org.xml.sax.EntityResolver;
12 import org.xml.sax.DTDHandler;
13 import org.xml.sax.DocumentHandler;
14 import org.xml.sax.ErrorHandler;
15 import org.xml.sax.SAXParseException;
19 * Event handler class for SAX demos.
21 * <p>This handler simply reports all of the events that it receives.
22 * It is useful for testing and comparing SAX implementations, and
23 * for teaching or learning about SAX. This is also a demonstration
24 * of how one class can implement all four handler interfaces.</p>
26 * @see org.xml.sax.EntityResolver
27 * @see org.xml.sax.DTDHandler
28 * @see org.xml.sax.DocumentHandler
29 * @see org.xml.sax.ErrorHandler
31 public class DemoHandler
32 implements EntityResolver, DTDHandler, DocumentHandler, ErrorHandler
37 //////////////////////////////////////////////////////////////////////
38 // Implementation of org.xml.sax.EntityResolver
39 //////////////////////////////////////////////////////////////////////
43 * Display requests for entity resolution.
45 * <p>The SAX parser will invoke this method to give the application
46 * a chance to resolve entities. This implementation always
47 * returns null, so that the parser will resolve the entity
50 * @see org.xml.sax.EntityResolver#resolveEntity
53 public InputSource resolveEntity (String publicId, String systemId)
55 System.out.print("Resolve entity:");
56 if (publicId != null) {
57 System.out.print(" publicId=\"" + publicId + '"');
59 System.out.println(" systemId=\"" + systemId + '"');
66 //////////////////////////////////////////////////////////////////////
67 // Implementation of org.xml.sax.DTDHandler
68 //////////////////////////////////////////////////////////////////////
72 * Display notation declarations as they are reported.
74 * @see org.xml.sax.DTDHandler#notationDecl
77 public void notationDecl (String name, String publicId, String systemId)
79 System.out.print("Notation declaration: " + name);
80 if (publicId != null) {
81 System.out.print(" publicId=\"" + publicId + '"');
83 if (systemId != null) {
84 System.out.print(" systemId=\"" + systemId + '"');
86 System.out.print('\n');
91 * Display unparsed entity declarations as they are reported.
93 * @see org.xml.sax.DTDHandler#unparsedEntityDecl
96 public void unparsedEntityDecl (String name,
101 System.out.print("Unparsed Entity Declaration: " + name);
102 if (publicId != null) {
103 System.out.print(" publicId=\"" + publicId + '"');
105 if (systemId != null) {
106 System.out.print(" systemId=\"" + systemId + '"');
108 System.out.println(" notationName=\"" + notationName + '"');
113 //////////////////////////////////////////////////////////////////////
114 // Implementation of org.xml.sax.DocumentHandler
115 //////////////////////////////////////////////////////////////////////
119 * Print a message when the parser provides a locator.
121 * <p>Not all SAX parsers will provide a locator object.</p>
123 * @see org.xml.sax.DocumentHandler#setDocumentLocator
126 public void setDocumentLocator (Locator locator)
128 System.out.println("Document locator supplied.");
133 * Print a message at the start of the document.
135 * @see org.xml.sax.DocumentHandler#startDocument
138 public void startDocument ()
140 System.out.println("Start document");
145 * Print a message for the end of the document.
147 * @see org.xml.sax.DocumentHandler#endDocument
150 public void endDocument ()
152 System.out.println("End document");
157 * Print a message for the start of an element.
159 * <p>Display all attributes on separate lines, indented.</p>
161 * @see org.xml.sax.DocumentHandler#startElement
164 public void startElement (String name, AttributeList attributes)
166 System.out.println("Start element: " + name);
167 for (int i = 0; i < attributes.getLength(); i++) {
168 System.out.println(" Attribute: " +
169 attributes.getName(i) +
171 attributes.getType(i) +
173 attributes.getValue(i) +
180 * Print a message for the end of an element.
182 * @see org.xml.sax.DocumentHandler#endElement
185 public void endElement (String name)
187 System.out.println("End element: " + name);
192 * Print a message for character data.
194 * @see org.xml.sax.DocumentHandler#characters
197 public void characters (char ch[], int start, int length)
199 System.out.print("Characters: ");
200 display(ch, start, length);
205 * Print a message for ignorable whitespace.
207 * @see org.xml.sax.DocumentHandler#ignorableWhitespace
210 public void ignorableWhitespace (char ch[], int start, int length)
212 System.out.print("Ignorable Whitespace: ");
213 display(ch, start, length);
218 * Print a message for a processing instruction.
220 * @see org.xml.sax.DocumentHandler#processingInstruction
223 public void processingInstruction (String target, String data)
225 System.out.println("Processing instruction: " + target + ' ' + data);
230 //////////////////////////////////////////////////////////////////////
231 // Implementation of org.xml.sax.ErrorHandler
232 //////////////////////////////////////////////////////////////////////
236 * Report all warnings, and continue parsing.
238 * @see org.xml.sax.ErrorHandler#warning
241 public void warning (SAXParseException exception)
243 System.out.println("Warning: " +
244 exception.getMessage() +
246 exception.getSystemId() +
248 exception.getLineNumber() +
250 exception.getColumnNumber() +
256 * Report all recoverable errors, and try to continue parsing.
258 * @see org.xml.sax.ErrorHandler#error
261 public void error (SAXParseException exception)
263 System.out.println("Recoverable Error: " +
264 exception.getMessage() +
266 exception.getSystemId() +
268 exception.getLineNumber() +
270 exception.getColumnNumber() +
276 * Report all fatal errors, and try to continue parsing.
278 * <p>Note: results are no longer reliable once a fatal error has
281 * @see org.xml.sax.ErrorHandler#fatalError
284 public void fatalError (SAXParseException exception)
286 System.out.println("Fatal Error: " +
287 exception.getMessage() +
289 exception.getSystemId() +
291 exception.getLineNumber() +
293 exception.getColumnNumber() +
299 //////////////////////////////////////////////////////////////////////
301 //////////////////////////////////////////////////////////////////////
305 * Display text, escaping some characters.
307 private static void display (char ch[], int start, int length)
309 for (int i = start; i < start + length; i++) {
312 System.out.print("\\n");
315 System.out.print("\\t");
318 System.out.print(ch[i]);
322 System.out.print("\n");