Merge branch 'feature/JAL-3143ensemblJSON' into Jalview-BH/JAL-3026-JAL-3063-JAXB
[jalview.git] / unused / xml / sax / demo / EntityDemo.java
1 package org.xml.sax.demo;
2 // SAX demonstration for custom entity resolution.
3 // No warranty; no copyright -- use this as you will.
4 // $Id: EntityDemo.java,v 1.2 1998/05/01 20:52:16 david Exp $
5
6 import org.xml.sax.InputSource;
7 import org.xml.sax.Parser;
8 import org.xml.sax.SAXException;
9
10 import org.xml.sax.helpers.ParserFactory;
11
12 import java.io.StringReader;
13 import java.net.URL;
14
15
16 /**
17   * Demonstrate custom entity resolution.
18   *
19   * <p>Usage: java -Dorg.xml.sax.parser=<var>classname</var> EntityDemo
20   * <var>systemId</var></p>
21   *
22   * <p>If you create an XML document which references an 
23   * external text entity with the public identifier
24   * "-//megginson//TEXT Sample Entity//EN", this application will
25   * substitute the string "Entity resolution works!" for the
26   * entity's contents:</p>
27   *
28   * <pre>
29   * &lt;!DOCTYPE doc [
30   *   &lt;!ENTITY ent 
31   *     PUBLIC "-//megginson//TEXT Sample Entity//EN" "ent.xml">
32   * ]>
33   * &lt;doc>
34   * &lt;para>&ent;&lt;/para>
35   * &lt;/doc>
36   * </pre>
37   *
38   * <p>The SAX parser will open a connection to the URI itself.</p>
39   *
40   * @see DemoHandler
41   */
42 public class EntityDemo extends DemoHandler {
43
44                                 // This is the Reader that will be
45                                 // substituted for the entity contents.
46   StringReader reader =
47     new StringReader("Entity resolution works!");
48
49   /**
50     * Main entry point.
51     */
52   public static void main (String args[])
53     throws Exception
54   {
55     Parser parser;
56     EntityDemo handler;
57
58                                 // Check the command-line usage.
59     if (args.length != 1) {
60       System.err.println("Usage: java -Dorg.xml.sax.parser=<classname> " +
61                          "EntityDemo <document>");
62       System.exit(2);
63     }
64
65                                 // Make the parser, using the value
66                                 // provided in the org.xml.sax.parser property.
67     parser = ParserFactory.makeParser();
68
69                                 // Create an event handler, and register
70                                 // it with the SAX parser.
71     handler = new EntityDemo();
72     parser.setEntityResolver(handler);
73     parser.setDTDHandler(handler);
74     parser.setDocumentHandler(handler);
75     parser.setErrorHandler(handler);
76
77                                 // Parse the document.
78     parser.parse(makeAbsoluteURL(args[0]));
79   }
80
81
82   /**
83     * Override resolveEntity().
84     *
85     * <p>If the public identifier is "-//megginson//TEXT Sample
86     * //Entity//EN", instruct the parser to read the entity's
87     * contents from the StringReader rather than from the 
88     * system identifier.</p>
89     *
90     * <p>The public identifier is safer than the system identifier,
91     * since the parser may have resolved the system identifier to
92     * an absolute URL.</p>
93     *
94     * @see org.xml.sax.EntityResolver#resolveEntity
95     */
96   @Override
97         public InputSource resolveEntity (String publicId, String systemId)
98   {
99     if (publicId != null &&
100         publicId.equals("-//megginson//TEXT Sample Entity//EN")) {
101       return new InputSource(reader);
102     } else {
103       return null;
104     }
105   }
106
107
108   /**
109     * If a URL is relative, make it absolute against the current directory.
110     */
111   private static String makeAbsoluteURL (String url)
112     throws java.net.MalformedURLException
113   {
114     URL baseURL;
115
116     String currentDirectory = System.getProperty("user.dir");
117     String fileSep = System.getProperty("file.separator");
118     String file = currentDirectory.replace(fileSep.charAt(0), '/') + '/';
119
120     if (file.charAt(0) != '/') {
121       file = "/" + file;
122     }
123     baseURL = new URL("file", null, file);
124
125     return new URL(baseURL, url).toString();
126   }
127
128 }