1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.creator.compiler;
22
23 import java.io.StringReader;
24 import java.util.Hashtable;
25
26 import org.xml.sax.EntityResolver;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.SAXNotSupportedException;
30
31 import autohit.common.AutohitLogInjectorWrapper;
32
33 /**
34 * Implement our own resolver to handle XML activities.
35 * For the most part, this is used to provide DTDs.
36 * <p>
37 * The default constructor assumes you do not want to log. If you use the other constructor,
38 * supply a valid CreatorLog and a log entry will be made for every resolve.
39 * <p>
40 * After construction, you need to register URIs to resolve.
41 * If the resolver encounters any URIs that are not registered,
42 * it will throw a SAXNotSupportedException exception.
43 *
44 * In this version, we assume that are resources are Strings.
45 * This should be easy to extend in the future.
46 *
47 * @author Erich P. Gatejen
48 * @version 1.1
49 * <i>Version History</i>
50 * <code>EPG - Initial - 11Apr03</code>
51 *
52 */
53 public class XmlCompilerResolver implements EntityResolver {
54
55 /**
56 * Contains the registered entities. Leave this a Hashtable,
57 * since it should be syncronized.
58 */
59 private Hashtable registeredEntities;
60
61 private AutohitLogInjectorWrapper log;
62
63 // --- PUBLIC METHODS ----------------------------------------------------
64
65 /**
66 * Default constructor.
67 */
68 XmlCompilerResolver() {
69 log = null;
70 }
71
72 /**
73 * Constructor. This one sets a logger.
74 *
75 * @param cLog A reference to a valid, working CreatorLog.
76 * @see autohit.creator.CreatorLog
77 */
78 XmlCompilerResolver(AutohitLogInjectorWrapper cLog) {
79 log = cLog;
80 }
81
82 /**
83 * Register a resource as an entity. The entity is the system URI and *not* the public name It will store the resource in the hashtable keyed on the entity..
84 *
85 * @param uri A string containing the textual entity to trap and resolve.
86 * @param text A string containing actual resource.
87 */
88 public void register(String uri, String text) {
89 if (registeredEntities == null) {
90 registeredEntities = new Hashtable();
91 }
92 registeredEntities.put(uri, text);
93 }
94
95 /**
96 * overrides the resolver.
97 *
98 * @param name not implimented.
99 * @param uri Passed to this from the parser. We will trap
100 * the uri.
101 * @return an input source to be used by the XML parser.
102 */
103 public InputSource resolveEntity(
104 java.lang.String name,
105 java.lang.String uri)
106 throws SAXException, java.io.IOException {
107
108 InputSource tis;
109
110 if (log != null)
111 log.debug(
112 "Resolve Entity: public=[" + name + "] system=[" + uri + "]");
113
114 // strip the "//" crap we've added via the InputSource
115 uri = uri.substring(2, uri.length());
116
117 // Is the entity registered?
118 if (registeredEntities.containsKey(uri)) {
119
120 tis =
121 new InputSource(
122 new StringReader((String) registeredEntities.get(uri)));
123
124 } else {
125 if (log != null)
126 log.debug(" ERROR: but it is not registered!");
127 throw new SAXNotSupportedException(
128 "URI [" + uri + "] not registered");
129 }
130
131 return tis;
132 }
133 }
|