KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > JAXBSchemaReader


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 JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
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 JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc sgFactoryChains = new ArrayList JavaDoc();
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 JavaDoc pChainClass) {
60         final String JavaDoc mName = "addSGFactoryChain";
61         if (pChainClass == null) {
62             throw new NullPointerException JavaDoc("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 JavaDoc {
74         final String JavaDoc mName = "getSGFactory";
75         log.finest(mName, "->");
76         SGFactoryChain chain = newSGFactoryChain(getGenerator());
77         log.finest(mName, "Created instance of " + chain.getClass().getName());
78         for (Iterator JavaDoc iter = sgFactoryChains.iterator(); iter.hasNext(); ) {
79             Class JavaDoc c = (Class JavaDoc) iter.next();
80             log.finest(mName, "Adding instance of " + c.getName());
81             Object JavaDoc o;
82             try {
83                 Constructor JavaDoc con = c.getConstructor(new Class JavaDoc[]{SGFactoryChain.class});
84                 o = con.newInstance(new Object JavaDoc[]{chain});
85             } catch (NoSuchMethodException JavaDoc e) {
86                 throw new SAXException JavaDoc("The SGFactoryChain class " + c.getName() +
87                 " has no constructor taking the backing chain as an argument.");
88             } catch (InvocationTargetException JavaDoc e) {
89                 Throwable JavaDoc t = e.getTargetException();
90                 String JavaDoc 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 JavaDoc) {
94                     throw new SAXException JavaDoc(msg, (Exception JavaDoc) t);
95                 } else {
96                     throw new SAXException JavaDoc(msg, e);
97                 }
98             } catch (Exception JavaDoc e) {
99                 throw new SAXException JavaDoc("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 JavaDoc pSource) throws Exception JavaDoc {
118         final String JavaDoc 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 JavaDoc 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 }
142
Popular Tags