Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/ws/jaxme/generator/sg/impl/JAXBSchemaReader.java


1   /*
2    * Copyright 2003,2004  The Apache Software Foundation
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15  
16   */
17  package org.apache.ws.jaxme.generator.sg.impl;
18  
19  import java.lang.reflect.Constructor;
20  import java.lang.reflect.InvocationTargetException;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.ws.jaxme.generator.Generator;
26  import org.apache.ws.jaxme.generator.impl.SchemaReaderImpl;
27  import org.apache.ws.jaxme.generator.sg.SGFactory;
28  import org.apache.ws.jaxme.generator.sg.SGFactoryChain;
29  import org.apache.ws.jaxme.generator.sg.SchemaSG;
30  import org.apache.ws.jaxme.logging.Logger;
31  import org.apache.ws.jaxme.logging.LoggerAccess;
32  import org.apache.ws.jaxme.xs.XSParser;
33  import org.apache.ws.jaxme.xs.XSSchema;
34  import org.apache.ws.jaxme.xs.util.DTDParser;
35  import org.apache.ws.jaxme.xs.xml.XsAnyURI;
36  import org.xml.sax.InputSource;
37  import org.xml.sax.SAXException;
38  
39  
40  /** The default, JAXB compliant, schema reader.
41   */
42  public class JAXBSchemaReader extends SchemaReaderImpl {
43      private static final Logger log = LoggerAccess.getLogger(JAXBSchemaReader.class);
44      private boolean isSupportingExtensions = false;
45      private List sgFactoryChains = new ArrayList();
46  
47      /** Returns whether vendor extensions are being supported.
48       */
49      public boolean isSupportingExtensions() {
50        return isSupportingExtensions;
51      }
52  
53      /** Sets whether vendor extensions are being supported.
54       */
55      public void setSupportingExtensions(boolean pSupportingExtensions) {
56        isSupportingExtensions = pSupportingExtensions;
57      }
58  
59      public void addSGFactoryChain(Class pChainClass) {
60        final String mName = "addSGFactoryChain";
61        if (pChainClass == null) {
62          throw new NullPointerException("The pChainClass argument must not be null.");
63        }
64        log.finest(mName, "->", pChainClass.getName());
65        sgFactoryChains.add(pChainClass);
66        log.finest(mName, "<-", Integer.toString(sgFactoryChains.size()));
67      }
68  
69      protected SGFactoryChain newSGFactoryChain(Generator pGenerator) {
70        return new JAXBSGFactory(pGenerator);
71      }
72  
73      public SGFactory getSGFactory() throws SAXException {
74        final String mName = "getSGFactory";
75        log.finest(mName, "->");
76        SGFactoryChain chain = newSGFactoryChain(getGenerator());
77        log.finest(mName, "Created instance of " + chain.getClass().getName());
78        for (Iterator iter = sgFactoryChains.iterator();  iter.hasNext();  ) {
79          Class c = (Class) iter.next();
80          log.finest(mName, "Adding instance of " + c.getName());
81          Object o;
82          try {
83            Constructor con = c.getConstructor(new Class[]{SGFactoryChain.class});
84            o = con.newInstance(new Object[]{chain});
85          } catch (NoSuchMethodException e) {
86            throw new SAXException("The SGFactoryChain class " + c.getName() +
87            " has no constructor taking the backing chain as an argument.");
88          } catch (InvocationTargetException e) {
89            Throwable t = e.getTargetException();
90            String msg = "Failed to invoke the constructor of class " + c.getName() +
91          " with an argument of type " + chain.getClass().getName() +
92          ": " + t.getClass().getName() + ", " + t.getMessage();
93            if (t instanceof Exception) {
94              throw new SAXException(msg, (Exception) t);
95            } else {
96              throw new SAXException(msg, e);
97            }
98          } catch (Exception e) {
99            throw new SAXException("Failed to invoke the constructor of class " + c.getName() +
100               " with an argument of type " + chain.getClass().getName() +
101             ": " + e.getClass().getName() + ", " + e.getMessage(), e);
102         }
103         chain = (SGFactoryChain) o;
104       }
105       SGFactory result = new SGFactoryImpl(chain);
106       result.init();
107       log.finest(mName, "<-", result);
108       return result;
109     }
110 
111     /** Resets the schema readers internal state, allowing
112      * to parse multiple schemas with a single instance.
113      */
114     protected void reset() {
115     }
116 
117     public SchemaSG parse(InputSource pSource) throws Exception {
118       final String mName = "parse";
119       reset();
120       log.finest(mName, "->", pSource.getSystemId());
121       SGFactory factory = getSGFactory();
122       XSSchema schema;
123         if (Boolean.valueOf(getGenerator().getProperty("jaxme.dtd.input")).booleanValue()) {
124             DTDParser parser = new DTDParser();
125             String targetNamespace = getGenerator().getProperty("jaxme.dtd.targetNamespace");
126             if (targetNamespace != null  &&  !"".equals(targetNamespace)) {
127               parser.setTargetNamespace(new XsAnyURI(targetNamespace));
128             }
129             schema = parser.parse(pSource);
130         } else {
131           XSParser parser = factory.newXSParser();
132           log.finest(mName, "Parser = " + parser + ", validating = " + getGenerator().isValidating());
133           parser.setValidating(getGenerator().isValidating());
134           schema = parser.parse(pSource);
135         }
136       log.finest(mName, "Schema = " + schema);
137       SchemaSG result = factory.getSchemaSG(schema);
138       log.finest(mName, "<-", result);
139       return result;
140     }
141 }