66c6a0899bb850dfe5118325e9d8cf45727ab8b0
[jalview.git] / unused / xml / sax / demo / DemoHandler.java
1 package org.xml.sax.demo;
2
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 $
6
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;
16
17
18 /**
19   * Event handler class for SAX demos.
20   *
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>
25   *
26   * @see org.xml.sax.EntityResolver
27   * @see org.xml.sax.DTDHandler
28   * @see org.xml.sax.DocumentHandler
29   * @see org.xml.sax.ErrorHandler
30   */
31 public class DemoHandler
32   implements EntityResolver, DTDHandler, DocumentHandler, ErrorHandler
33 {
34
35
36 \f
37   //////////////////////////////////////////////////////////////////////
38   // Implementation of org.xml.sax.EntityResolver
39   //////////////////////////////////////////////////////////////////////
40
41
42   /**
43     * Display requests for entity resolution.
44     *
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
48     * itself.</p>
49     *
50     * @see org.xml.sax.EntityResolver#resolveEntity
51     */
52   @Override
53         public InputSource resolveEntity (String publicId, String systemId)
54   {
55     System.out.print("Resolve entity:");
56     if (publicId != null) {
57       System.out.print(" publicId=\"" + publicId + '"');
58     }
59     System.out.println(" systemId=\"" + systemId + '"');
60
61     return null;
62   }
63
64
65 \f
66   //////////////////////////////////////////////////////////////////////
67   // Implementation of org.xml.sax.DTDHandler
68   //////////////////////////////////////////////////////////////////////
69
70
71   /**
72     * Display notation declarations as they are reported.
73     *
74     * @see org.xml.sax.DTDHandler#notationDecl
75     */
76   @Override
77         public void notationDecl (String name, String publicId, String systemId)
78   {
79     System.out.print("Notation declaration: " + name);
80     if (publicId != null) {
81       System.out.print(" publicId=\"" + publicId + '"');
82     }
83     if (systemId != null) {
84       System.out.print(" systemId=\"" + systemId + '"');
85     }
86     System.out.print('\n');
87   }
88
89
90   /**
91     * Display unparsed entity declarations as they are reported.
92     *
93     * @see org.xml.sax.DTDHandler#unparsedEntityDecl
94     */
95   @Override
96         public void unparsedEntityDecl (String name,
97                                   String publicId,
98                                   String systemId,
99                                   String notationName)
100   {
101     System.out.print("Unparsed Entity Declaration: " + name);
102     if (publicId != null) {
103       System.out.print(" publicId=\"" + publicId + '"');
104     }
105     if (systemId != null) {
106       System.out.print(" systemId=\"" + systemId + '"');
107     }
108     System.out.println(" notationName=\"" + notationName + '"');
109   }
110
111
112 \f
113   //////////////////////////////////////////////////////////////////////
114   // Implementation of org.xml.sax.DocumentHandler
115   //////////////////////////////////////////////////////////////////////
116
117
118   /**
119     * Print a message when the parser provides a locator.
120     *
121     * <p>Not all SAX parsers will provide a locator object.</p>
122     *
123     * @see org.xml.sax.DocumentHandler#setDocumentLocator
124     */
125   @Override
126         public void setDocumentLocator (Locator locator)
127   {
128     System.out.println("Document locator supplied.");
129   }
130
131
132   /**
133     * Print a message at the start of the document.
134     *
135     * @see org.xml.sax.DocumentHandler#startDocument
136     */
137   @Override
138         public void startDocument ()
139   {
140     System.out.println("Start document");
141   }
142
143
144   /**
145     * Print a message for the end of the document.
146     *
147     * @see org.xml.sax.DocumentHandler#endDocument
148     */
149   @Override
150         public void endDocument ()
151   {
152     System.out.println("End document");
153   }
154
155
156   /**
157     * Print a message for the start of an element.
158     *
159     * <p>Display all attributes on separate lines, indented.</p>
160     *
161     * @see org.xml.sax.DocumentHandler#startElement
162     */
163   @Override
164         public void startElement (String name, AttributeList attributes)
165   {
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) +
170                          ' ' +
171                          attributes.getType(i) +
172                          " \"" +
173                          attributes.getValue(i) +
174                          '"');
175     }
176   }
177
178
179   /**
180     * Print a message for the end of an element.
181     *
182     * @see org.xml.sax.DocumentHandler#endElement
183     */
184   @Override
185         public void endElement (String name)
186   {
187     System.out.println("End element: " + name);
188   }
189
190
191   /**
192     * Print a message for character data.
193     *
194     * @see org.xml.sax.DocumentHandler#characters
195     */
196   @Override
197         public void characters (char ch[], int start, int length)
198   {
199     System.out.print("Characters: ");
200     display(ch, start, length);
201   }
202
203
204   /**
205     * Print a message for ignorable whitespace.
206     *
207     * @see org.xml.sax.DocumentHandler#ignorableWhitespace
208     */
209   @Override
210         public void ignorableWhitespace (char ch[], int start, int length)
211   {
212     System.out.print("Ignorable Whitespace: ");
213     display(ch, start, length);
214   }
215
216
217   /**
218     * Print a message for a processing instruction.
219     *
220     * @see org.xml.sax.DocumentHandler#processingInstruction
221     */
222   @Override
223         public void processingInstruction (String target, String data)
224   {
225     System.out.println("Processing instruction: " + target + ' ' + data);
226   }
227
228
229 \f
230   //////////////////////////////////////////////////////////////////////
231   // Implementation of org.xml.sax.ErrorHandler
232   //////////////////////////////////////////////////////////////////////
233
234
235   /**
236     * Report all warnings, and continue parsing.
237     *
238     * @see org.xml.sax.ErrorHandler#warning
239     */
240   @Override
241         public void warning (SAXParseException exception)
242   {
243     System.out.println("Warning: " +
244                        exception.getMessage() +
245                        " (" +
246                        exception.getSystemId() +
247                        ':' +
248                        exception.getLineNumber() +
249                        ',' +
250                        exception.getColumnNumber() +
251                        ')');
252   }
253
254
255   /**
256     * Report all recoverable errors, and try to continue parsing.
257     *
258     * @see org.xml.sax.ErrorHandler#error
259     */
260   @Override
261         public void error (SAXParseException exception)
262   {
263     System.out.println("Recoverable Error: " +
264                        exception.getMessage() +
265                        " (" +
266                        exception.getSystemId() +
267                        ':' +
268                        exception.getLineNumber() +
269                        ',' +
270                        exception.getColumnNumber() +
271                        ')');
272   }
273
274
275   /**
276     * Report all fatal errors, and try to continue parsing.
277     *
278     * <p>Note: results are no longer reliable once a fatal error has
279     * been reported.</p>
280     *
281     * @see org.xml.sax.ErrorHandler#fatalError
282     */
283   @Override
284         public void fatalError (SAXParseException exception)
285   {
286     System.out.println("Fatal Error: " +
287                        exception.getMessage() +
288                        " (" +
289                        exception.getSystemId() +
290                        ':' +
291                        exception.getLineNumber() +
292                        ',' +
293                        exception.getColumnNumber() +
294                        ')');
295   }
296
297
298 \f
299   //////////////////////////////////////////////////////////////////////
300   // Utility routines.
301   //////////////////////////////////////////////////////////////////////
302
303
304   /**
305     * Display text, escaping some characters.
306     */
307   private static void display (char ch[], int start, int length)
308   {
309     for (int i = start; i < start + length; i++) {
310       switch (ch[i]) {
311       case '\n':
312         System.out.print("\\n");
313         break;
314       case '\t':
315         System.out.print("\\t");
316         break;
317       default:
318         System.out.print(ch[i]);
319         break;
320       }
321     }
322     System.out.print("\n");
323   }
324
325 }