View Javadoc

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