5da34b97511e6fb66e2bbc652bfc6b947ee46571
[jalview.git] / unused / xml / sax / demo / ByteStreamDemo.java
1 package org.xml.sax.demo;
2 // SAX demonstration for parsing from a raw byte stream.
3 // No warranty; no copyright -- use this as you will.
4 // $Id: ByteStreamDemo.java,v 1.4 1998/05/01 20:38:19 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.FileInputStream;
13
14
15
16 /**
17   * Demonstrate parsing from a byte stream.
18   *
19   * <p>Usage: java -Dorg.xml.sax.parser=<var>CLASSNAME</var> ByteStreamDemo
20   * <var>FILE</var></p>
21   *
22   * <p>The SAX parser will open a byte stream to the file name
23   * provided.  Note the use of the InputStreamAdapter class to allow a
24   * Java InputStream to serve as a SAX ByteStream.</p>
25   *
26   * @see DemoHandler
27   */
28 public class ByteStreamDemo {
29
30
31   /**
32     * Main entry point.
33     */
34   public static void main (String args[])
35     throws Exception
36   {
37     Parser parser;
38     InputSource source;
39     DemoHandler handler;
40     FileInputStream input;
41
42                                 // First, check the command-line usage.
43     if (args.length != 1) {
44       System.err.println("Usage: java -Dorg.xml.sax.parser=<classname> " +
45                          "SystemIdDemo <document>");
46       System.exit(2);
47     }
48
49                                 // Allocate a SAX Parser object, using
50                                 // the class name provided in the
51                                 // org.xml.sax.parser property.
52     parser = ParserFactory.makeParser();
53
54                                 // Allocate an event handler, and register
55                                 // it with the SAX parser for all four
56                                 // types of events (we could use a 
57                                 // separate object for each).
58     handler = new DemoHandler();
59     parser.setEntityResolver(handler);
60     parser.setDTDHandler(handler);
61     parser.setDocumentHandler(handler);
62     parser.setErrorHandler(handler);
63
64                                 // Create a Java FileInputStream from
65                                 // the file: note that SAX cannot
66                                 // use this directly.
67     input = new FileInputStream(args[0]);
68
69                                 // Create the input source.
70     source = new InputSource(input);
71     source.setSystemId(args[0]);
72
73                                 // Parse the document from the
74                                 // ByteStream.
75     parser.parse(source);
76   }
77
78 }