1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.apache.jdo.impl.model.jdo.xml;
25
26 import java.io.*;
27 import java.security.AccessController;
28 import java.security.PrivilegedAction;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParserFactory;
31
32 import org.apache.jdo.model.jdo.JDOModel;
33 import org.apache.jdo.util.I18NHelper;
34 import org.xml.sax.*;
35 import org.xml.sax.helpers.*;
36
37 /***
38 * The class reads XML documents according to specified DTD and
39 * translates all related events into JDOHandler events.
40 * <p>Usage sample:
41 * <pre>
42 * JDOParser parser = new JDOParser(...);
43 * parser.parse(new InputSource("..."));
44 * </pre>
45 * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p>
46 */
47 public class JDOParser
48 implements ContentHandler {
49
50 /*** I18N support. */
51 private static final I18NHelper msg = I18NHelper.getInstance(
52 "org.apache.jdo.impl.model.jdo.Bundle", JDOParser.class.getClassLoader());
53
54 private StringBuffer buffer;
55
56 private JDOHandler handler;
57
58 private java.util.Stack context;
59
60 public JDOParser(final JDOHandler handler)
61 {
62 this.handler = handler;
63 buffer = new StringBuffer(111);
64 context = new java.util.Stack();
65 }
66
67 public void setDocumentLocator(Locator locator)
68 {
69 }
70
71 public void startDocument() throws SAXException
72 {
73 }
74
75 public void endDocument() throws SAXException
76 {
77 }
78
79 public void startElement(String ns, String name, String qname, Attributes attrs)
80 throws SAXException
81 {
82 dispatch(true);
83 context.push(new Object[] {qname, new org.xml.sax.helpers.AttributesImpl(attrs)});
84
85 if ("package".equals(name)) {
86 handler.start_package(attrs);
87 } else if ("jdo".equals(name)) {
88 handler.start_jdo(attrs);
89 } else if ("class".equals(name)) {
90 handler.start_class(attrs);
91 } else if ("map".equals(name)) {
92 handler.start_map(attrs);
93 } else if ("field".equals(name)) {
94 handler.start_field(attrs);
95 } else if ("collection".equals(name)) {
96 handler.start_collection(attrs);
97 } else if ("extension".equals(name)) {
98 handler.start_extension(attrs);
99 } else if ("array".equals(name)) {
100 handler.start_array(attrs);
101 }
102 }
103
104 public void endElement(String ns, String name, String qname)
105 throws SAXException
106 {
107 dispatch(false);
108 context.pop();
109 if ("package".equals(name)) {
110 handler.end_package();
111 } else if ("jdo".equals(name)) {
112 handler.end_jdo();
113 } else if ("class".equals(name)) {
114 handler.end_class();
115 } else if ("map".equals(name)) {
116 handler.end_map();
117 } else if ("field".equals(name)) {
118 handler.end_field();
119 } else if ("collection".equals(name)) {
120 handler.end_collection();
121 } else if ("extension".equals(name)) {
122 handler.end_extension();
123 } else if ("array".equals(name)) {
124 handler.end_array();
125 }
126 }
127
128 public void characters(char[] chars, int start, int len)
129 throws SAXException
130 {
131 buffer.append(chars, start, len);
132 }
133
134 public void ignorableWhitespace(char[] chars, int start, int len)
135 throws SAXException
136 {
137 }
138
139 public void processingInstruction(String target, String data)
140 throws SAXException
141 {
142 }
143
144 public void startPrefixMapping(final String prefix, final String uri)
145 throws SAXException
146 {
147 }
148
149 public void endPrefixMapping(final String prefix)
150 throws SAXException
151 {
152 }
153
154 public void skippedEntity(String name)
155 throws SAXException
156 {
157 }
158
159 private void dispatch(final boolean fireOnlyIfMixed)
160 throws SAXException
161 {
162 if (fireOnlyIfMixed && buffer.length() == 0)
163 return;
164
165 Object[] ctx = (Object[]) context.peek();
166 String here = (String) ctx[0];
167 Attributes attrs = (Attributes) ctx[1];
168 buffer.delete(0, buffer.length());
169 }
170
171 /***
172 * The recognizer entry method taking an InputSource.
173 * @param input InputSource to be parsed.
174 * @throws java.io.IOException on I/O error.
175 * @throws SAXException propagated exception thrown by a DocumentHandler.
176 * @throws javax.xml.parsers.ParserConfigurationException a parser
177 * satisfining requested configuration can not be created.
178 * @throws javax.xml.parsers.FactoryConfigurationError if the implementation
179 * can not be instantiated.
180 */
181 public void parse(final InputSource input)
182 throws SAXException, ParserConfigurationException, IOException
183 {
184 parse(input, this);
185 }
186
187 /***
188 * The recognizer entry method taking a URL.
189 * @param url URL source to be parsed.
190 * @throws java.io.IOException on I/O error.
191 * @throws SAXException propagated exception thrown by a DocumentHandler.
192 * @throws javax.xml.parsers.ParserConfigurationException a parser
193 * satisfining requested configuration can not be created.
194 * @throws javax.xml.parsers.FactoryConfigurationError if the implementation
195 * can not be instantiated.
196 */
197 public void parse(final java.net.URL url)
198 throws SAXException, ParserConfigurationException, IOException
199 {
200 parse(new InputSource(url.toExternalForm()), this);
201 }
202
203 /***
204 * The recognizer entry method taking an Inputsource.
205 * @param input InputSource to be parsed.
206 * @throws java.io.IOException on I/O error.
207 * @throws SAXException propagated exception thrown by a DocumentHandler.
208 * @throws javax.xml.parsers.ParserConfigurationException a parser
209 * satisfining requested configuration can not be created.
210 * @throws javax.xml.parsers.FactoryConfigurationError if the implementation
211 * can not be instantiated.
212 */
213 public static void parse(final InputSource input, final JDOHandler handler)
214 throws SAXException, ParserConfigurationException, IOException
215 {
216 parse(input, new JDOParser(handler));
217 }
218
219 /***
220 * The recognizer entry method taking a URL.
221 * @param url URL source to be parsed.
222 * @throws java.io.IOException on I/O error.
223 * @throws SAXException propagated exception thrown by a DocumentHandler.
224 * @throws javax.xml.parsers.ParserConfigurationException a parser
225 * satisfining requested configuration can not be created.
226 * @throws javax.xml.parsers.FactoryConfigurationError if the implementation
227 * can not be instantiated.
228 */
229 public static void parse(final java.net.URL url, final JDOHandler handler)
230 throws SAXException, ParserConfigurationException, IOException
231 {
232 parse(new InputSource(url.toExternalForm()), handler);
233 }
234
235 private static void parse(final InputSource input, final JDOParser recognizer)
236 throws SAXException, ParserConfigurationException, IOException
237 {
238 SAXParserFactory factory = SAXParserFactory.newInstance();
239 factory.setValidating(true);
240 factory.setNamespaceAware(true);
241 XMLReader parser = factory.newSAXParser().getXMLReader();
242 parser.setEntityResolver(new JDOEntityResolver());
243 parser.setContentHandler(recognizer);
244 parser.setErrorHandler(recognizer.getDefaultErrorHandler());
245 parser.parse(input);
246 }
247
248 private ErrorHandler getDefaultErrorHandler()
249 {
250 return new ErrorHandler() {
251 public void error(SAXParseException ex)
252 throws SAXException {
253 if (context.isEmpty())
254 System.err.println("Missing DOCTYPE.");
255 throw ex;
256 }
257
258 public void fatalError(SAXParseException ex)
259 throws SAXException {
260 throw ex;
261 }
262
263 public void warning(SAXParseException ex)
264 throws SAXException {
265
266 }
267 };
268 }
269
270 /***
271 * Implementation of EntityResolver interface to check the jdo.dtd location
272 **/
273 private static class JDOEntityResolver
274 implements EntityResolver
275 {
276 private static final String RECOGNIZED_PUBLIC_ID =
277 "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN"; //NOI18N
278 private static final String RECOGNIZED_SYSTEM_ID =
279 "file:/javax/jdo/jdo.dtd";
280
281 public InputSource resolveEntity(String publicId, String systemId)
282 throws SAXException, IOException
283 {
284
285 if (((publicId != null) && RECOGNIZED_PUBLIC_ID.equals(publicId)) ||
286 ((publicId == null) && (systemId != null) &&
287 RECOGNIZED_SYSTEM_ID.equals(systemId))) {
288
289
290
291
292 InputStream stream = (InputStream) AccessController.doPrivileged (
293 new PrivilegedAction () {
294 public Object run () {
295 return getClass().getClassLoader().
296 getResourceAsStream("javax/jdo/jdo.dtd");
297 }
298 }
299 );
300 if (stream == null) {
301 throw new RuntimeException(
302 msg.msg("EXC_MissingJDODTD",
303 publicId, systemId));
304 }
305 return new InputSource(new InputStreamReader(stream));
306 }
307 return null;
308 }
309 }
310 }
311
312