001: // dtx.DemoHandler -- public domain example, borrowed from the SAX package.
002: // just put into the dtx package to make it easier to run.
003:
004: // original notice:
005:
006: // SAX event handler for demos.
007: // No warranty; no copyright -- use this as you will.
008:
009: package dtx;
010:
011: import org.xml.sax.InputSource;
012: import org.xml.sax.Locator;
013: import org.xml.sax.AttributeList;
014: import org.xml.sax.EntityResolver;
015: import org.xml.sax.DTDHandler;
016: import org.xml.sax.DocumentHandler;
017: import org.xml.sax.ErrorHandler;
018: import org.xml.sax.SAXParseException;
019:
020: /**
021: * Event handler class for SAX demos.
022: *
023: * <p>This handler simply reports all of the events that it receives.
024: * It is useful for testing and comparing SAX implementations, and
025: * for teaching or learning about SAX. This is also a demonstration
026: * of how one class can implement all four handler interfaces.</p>
027: *
028: * @see org.xml.sax.EntityResolver
029: * @see org.xml.sax.DTDHandler
030: * @see org.xml.sax.DocumentHandler
031: * @see org.xml.sax.ErrorHandler
032: */
033: public class DemoHandler implements EntityResolver, DTDHandler,
034: DocumentHandler, ErrorHandler {
035:
036: //////////////////////////////////////////////////////////////////////
037: // Implementation of org.xml.sax.EntityResolver
038: //////////////////////////////////////////////////////////////////////
039:
040: /**
041: * Display requests for entity resolution.
042: *
043: * <p>The SAX parser will invoke this method to give the application
044: * a chance to resolve entities. This implementation always
045: * returns null, so that the parser will resolve the entity
046: * itself.</p>
047: *
048: * @see org.xml.sax.EntityResolver#resolveEntity
049: */
050: public InputSource resolveEntity(String publicId, String systemId) {
051: System.out.print("Resolve entity:");
052: if (publicId != null) {
053: System.out.print(" publicId=\"" + publicId + '"');
054: }
055: System.out.println(" systemId=\"" + systemId + '"');
056:
057: return null;
058: }
059:
060: //////////////////////////////////////////////////////////////////////
061: // Implementation of org.xml.sax.DTDHandler
062: //////////////////////////////////////////////////////////////////////
063:
064: /**
065: * Display notation declarations as they are reported.
066: *
067: * @see org.xml.sax.DTDHandler#notationDecl
068: */
069: public void notationDecl(String name, String publicId,
070: String systemId) {
071: System.out.print("Notation declaration: " + name);
072: if (publicId != null) {
073: System.out.print(" publicId=\"" + publicId + '"');
074: }
075: if (systemId != null) {
076: System.out.print(" systemId=\"" + systemId + '"');
077: }
078: System.out.print('\n');
079: }
080:
081: /**
082: * Display unparsed entity declarations as they are reported.
083: *
084: * @see org.xml.sax.DTDHandler#unparsedEntityDecl
085: */
086: public void unparsedEntityDecl(String name, String publicId,
087: String systemId, String notationName) {
088: System.out.print("Unparsed Entity Declaration: " + name);
089: if (publicId != null) {
090: System.out.print(" publicId=\"" + publicId + '"');
091: }
092: if (systemId != null) {
093: System.out.print(" systemId=\"" + systemId + '"');
094: }
095: System.out.println(" notationName=\"" + notationName + '"');
096: }
097:
098: //////////////////////////////////////////////////////////////////////
099: // Implementation of org.xml.sax.DocumentHandler
100: //////////////////////////////////////////////////////////////////////
101:
102: /**
103: * Print a message when the parser provides a locator.
104: *
105: * <p>Not all SAX parsers will provide a locator object.</p>
106: *
107: * @see org.xml.sax.DocumentHandler#setDocumentLocator
108: */
109: public void setDocumentLocator(Locator locator) {
110: System.out.println("Document locator supplied.");
111: }
112:
113: /**
114: * Print a message at the start of the document.
115: *
116: * @see org.xml.sax.DocumentHandler#startDocument
117: */
118: public void startDocument() {
119: System.out.println("Start document");
120: }
121:
122: /**
123: * Print a message for the end of the document.
124: *
125: * @see org.xml.sax.DocumentHandler#endDocument
126: */
127: public void endDocument() {
128: System.out.println("End document");
129: }
130:
131: /**
132: * Print a message for the start of an element.
133: *
134: * <p>Display all attributes on separate lines, indented.</p>
135: *
136: * @see org.xml.sax.DocumentHandler#startElement
137: */
138: public void startElement(String name, AttributeList attributes) {
139: System.out.println("Start element: " + name);
140: for (int i = 0; i < attributes.getLength(); i++) {
141: System.out.println(" Attribute: " + attributes.getName(i)
142: + ' ' + attributes.getType(i) + " \""
143: + attributes.getValue(i) + '"');
144: }
145: }
146:
147: /**
148: * Print a message for the end of an element.
149: *
150: * @see org.xml.sax.DocumentHandler#endElement
151: */
152: public void endElement(String name) {
153: System.out.println("End element: " + name);
154: }
155:
156: /**
157: * Print a message for character data.
158: *
159: * @see org.xml.sax.DocumentHandler#characters
160: */
161: public void characters(char ch[], int start, int length) {
162: System.out.print("Characters: ");
163: display(ch, start, length);
164: }
165:
166: /**
167: * Print a message for ignorable whitespace.
168: *
169: * @see org.xml.sax.DocumentHandler#ignorableWhitespace
170: */
171: public void ignorableWhitespace(char ch[], int start, int length) {
172: System.out.print("Ignorable Whitespace: ");
173: display(ch, start, length);
174: }
175:
176: /**
177: * Print a message for a processing instruction.
178: *
179: * @see org.xml.sax.DocumentHandler#processingInstruction
180: */
181: public void processingInstruction(String target, String data) {
182: System.out.println("Processing instruction: " + target + ' '
183: + data);
184: }
185:
186: //////////////////////////////////////////////////////////////////////
187: // Implementation of org.xml.sax.ErrorHandler
188: //////////////////////////////////////////////////////////////////////
189:
190: /**
191: * Report all warnings, and continue parsing.
192: *
193: * @see org.xml.sax.ErrorHandler#warning
194: */
195: public void warning(SAXParseException exception) {
196: System.out.println("Warning: " + exception.getMessage() + " ("
197: + exception.getSystemId() + ':'
198: + exception.getLineNumber() + ','
199: + exception.getColumnNumber() + ')');
200: }
201:
202: /**
203: * Report all recoverable errors, and try to continue parsing.
204: *
205: * @see org.xml.sax.ErrorHandler#error
206: */
207: public void error(SAXParseException exception) {
208: System.out.println("Recoverable Error: "
209: + exception.getMessage() + " ("
210: + exception.getSystemId() + ':'
211: + exception.getLineNumber() + ','
212: + exception.getColumnNumber() + ')');
213: }
214:
215: /**
216: * Report all fatal errors, and try to continue parsing.
217: *
218: * <p>Note: results are no longer reliable once a fatal error has
219: * been reported.</p>
220: *
221: * @see org.xml.sax.ErrorHandler#fatalError
222: */
223: public void fatalError(SAXParseException exception) {
224: System.out.println("Fatal Error: " + exception.getMessage()
225: + " (" + exception.getSystemId() + ':'
226: + exception.getLineNumber() + ','
227: + exception.getColumnNumber() + ')');
228: }
229:
230: //////////////////////////////////////////////////////////////////////
231: // Utility routines.
232: //////////////////////////////////////////////////////////////////////
233:
234: /**
235: * Display text, escaping some characters.
236: */
237: private static void display(char ch[], int start, int length) {
238: for (int i = start; i < start + length; i++) {
239: switch (ch[i]) {
240: case '\n':
241: System.out.print("\\n");
242: break;
243: case '\t':
244: System.out.print("\\t");
245: break;
246: default:
247: System.out.print(ch[i]);
248: break;
249: }
250: }
251: System.out.print("\n");
252: }
253:
254: }
|