update of javajs package; adds org.apache.harmony, org.xml.sax.helpers
[jalview.git] / src / org / xml / sax / demo / CharacterStreamDemo.java
1 package org.xml.sax.demo;
2 // SAX demonstration for parsing from a UTF-16 character stream.
3 // No warranty; no copyright -- use this as you will.
4 // $Id: CharacterStreamDemo.java,v 1.3 1998/05/01 20:44:33 david Exp $
5
6 import org.xml.sax.Parser;
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
9
10 import org.xml.sax.helpers.ParserFactory;
11
12 import java.io.StringReader;
13
14
15 /**
16   * Demonstrate parsing from a UTF-16 character stream.
17   *
18   * <p>Usage: java -Dorg.xml.sax.parser=<var>CLASSNAME</var>
19   * CharacterStreamDemo</p>
20   *
21   * <p>The SAX parser will read the document from a character
22   * stream connected to an internal string.</p>
23   *
24   * <p>Note that the Java implementation of SAX represents a
25   * character stream using a Reader.</p>
26   *
27   * @see DemoHandler
28   * @see org.xml.org.xml.sax.parser
29   * @see org.xml.sax.InputSource
30   * @see org.xml.sax.helpers.ParserFactory
31   * @see java.io.StringReader
32   */
33 public class CharacterStreamDemo {
34
35                                 // This is the document, in all its glory.
36                                 // In a read-world application, this
37                                 // could come from a database, a text
38                                 // field, or just about anywhere.
39   final static String doc = "<?xml version=\"1.0\"?>" +
40                             "<doc>\n" +
41                             "<title>Hello</title>\n" +
42                             "<para>Hello, world!</para>\n" +
43                             "</doc>\n";
44
45   /**
46     * Main entry point for an application.
47     */
48   public static void main (String args[])
49     throws Exception
50   {
51     DemoHandler handler;
52     InputSource source;
53     Parser parser;
54     StringReader reader;
55
56                                 // First, check the command-line
57                                 // arguments.
58     if (args.length != 0) {
59       System.err.println("Usage: java CharTest");
60       System.exit(2);
61     }
62
63                                 // Allocate a SAX Parser object, using
64                                 // the class name provided in the
65                                 // org.xml.sax.parser property (you could
66                                 // also specify a classname here).
67     parser = ParserFactory.makeParser();
68
69                                 // Allocate an event handler, and register
70                                 // it with the SAX parser for all four
71                                 // types of events (we could use a 
72                                 // separate object for each).
73     handler = new DemoHandler();
74     parser.setEntityResolver(handler);
75     parser.setDTDHandler(handler);
76     parser.setDocumentHandler(handler);
77     parser.setErrorHandler(handler);
78
79                                 // Create a Java-specific StringReader
80                                 // for the internal document.
81     reader = new StringReader(doc);
82
83                                 // Parse the document from the
84                                 // character stream.
85     parser.parse(new InputSource(reader));
86   }
87
88 }