KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > XSParser


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.xs;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24
25 import org.apache.ws.jaxme.xs.impl.XSLogicalParser;
26 import org.apache.ws.jaxme.xs.parser.XSContext;
27 import org.apache.ws.jaxme.xs.parser.XsSAXParser;
28 import org.apache.ws.jaxme.xs.parser.impl.XSContextImpl;
29 import org.apache.ws.jaxme.xs.xml.XsESchema;
30 import org.apache.ws.jaxme.xs.xml.XsObjectFactory;
31 import org.w3c.dom.Node JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.XMLReader JavaDoc;
35
36
37 /** <p>The XML schema parser.</p>
38  *
39  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
40  * @version $Id: XSParser.java,v 1.10 2005/04/27 19:42:55 jochen Exp $
41  */

42 public class XSParser {
43   /** <p>The XML Schema URI: <code>http://www.w3.org/2001/XMLSchema</code></p>
44    */

45   public static final String JavaDoc XML_SCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
46
47   private static final ThreadLocal JavaDoc parser = new ThreadLocal JavaDoc();
48
49   private XSContext data;
50   private boolean notValidating;
51   private List JavaDoc addedImports = new ArrayList JavaDoc();
52
53   /** <p>Creates a new instance of XSParser.</p>
54    */

55   public XSParser() {
56   }
57
58   /** <p>Sets whether the parser is validating.</p>
59    */

60   public void setValidating(boolean pValidating) {
61     notValidating = !pValidating;
62   }
63
64   /** <p>Returns whether the parser is validating.</p>
65    */

66   public boolean isValidating() {
67     return !notValidating;
68   }
69
70   /** <p>Adds a schema being imported by the parser. This feature
71    * is useful, if a schema silently assumes the presence of additional
72    * datatypes. For example, a WSDL definition will contain references
73    * to SOAP datatypes without explicit import.</p>
74    * @param pNamespace Matches the "xs:import" nodes "namespace" attribute.
75    * In particular it may be null, in which case the imported schema may
76    * not have a targetNamespace.
77    * @param pSchemaLocation Matches the "xs:import" nodes "schemaLocation"
78    * attribute. In particular it may be null.
79    * @see #addImport(String, Node)
80    */

81   public void addImport(String JavaDoc pNamespace, String JavaDoc pSchemaLocation) {
82     addedImports.add(new XSLogicalParser.AddedImport(pNamespace, pSchemaLocation));
83   }
84
85   /** <p>Adds a schema being imported by the parser. The schema is
86    * provided as a DOM node. This feature is useful, if a schema
87    * silently assumes the presence of additional datatypes. For
88    * example, a WSDL definition will contain references to SOAP
89    * datatypes without explicit import.</p>
90    * @param pNamespace Matches the "xs:import" nodes "namespace"
91    * attribute. In particular it may be null, in which case the
92    * imported schema may not have a targetNamespace.
93    * @param pSchemaLocation The imported schemas system ID, if known,
94    * or null. Knowing the system ID is important only, if you need
95    * to prevent recursive schematas being included more than once.
96    * @param pSchema A DOM node with the schema being imported.
97    * @see #addImport(String, String)
98    */

99   public void addImport(String JavaDoc pNamespace, String JavaDoc pSchemaLocation, Node JavaDoc pSchema) {
100     addedImports.add(new XSLogicalParser.AddedImport(pNamespace, pSchemaLocation, pSchema));
101   }
102
103   /** <p>Provides access to the parsers internal data. Use the
104    * {@link #getRunningInstance()} method to find the parser.</p>
105    */

106   public XSContext getContext() {
107     if (data == null) {
108       setData(new XSContextImpl());
109     }
110     return data;
111   }
112
113   protected void setData(XSContext pData) { data = pData; }
114
115   /** <p>Parses the given XML schema. and returns a syntactical
116    * representation.</p>
117    * @see #parse(InputSource)
118    */

119   public XsESchema parseSyntax(InputSource JavaDoc pSource) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
120     XSContext context = getContext();
121     parser.set(this);
122     try {
123       XsObjectFactory factory = context.getXsObjectFactory();
124       XsSAXParser xsSAXParser = factory.newXsSAXParser();
125       context.setCurrentContentHandler(xsSAXParser);
126       XMLReader JavaDoc xr = factory.newXMLReader(isValidating());
127       xr.setContentHandler(xsSAXParser);
128       xr.parse(pSource);
129       return (XsESchema) xsSAXParser.getBean();
130     } finally {
131       context.setCurrentContentHandler(null);
132       parser.set(null);
133     }
134   }
135
136   protected XSLogicalParser newXSLogicalParser() {
137     XSLogicalParser logicalParser = getContext().getXSObjectFactory().newXSLogicalParser();
138     logicalParser.setValidating(isValidating());
139     for (int i = 0; i < addedImports.size(); i++) {
140       XSLogicalParser.AddedImport addedImport = (XSLogicalParser.AddedImport) addedImports.get(i);
141       if (addedImport.getNode() == null) {
142         logicalParser.addImport(addedImport.getNamespace(), addedImport.getSchemaLocation());
143       } else {
144         logicalParser.addImport(addedImport.getNamespace(), addedImport.getSchemaLocation(), addedImport.getNode());
145       }
146     }
147     return logicalParser;
148   }
149
150   /** <p>Parses the given XML schema and returns a logical representation.</p>
151    */

152   public XSSchema parse(InputSource JavaDoc pSource) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
153     XSContext myData = getContext();
154     parser.set(this);
155     try {
156       return newXSLogicalParser().parse(pSource);
157     } finally {
158       myData.setCurrentContentHandler(null);
159       parser.set(null);
160     }
161   }
162
163   /** <p>Parses the given DOM node containing an an XML schema and returns
164    * a logical representation.</p>
165    * @param pNode A node containing a valid XML document. Must be either
166    * an instance of {@link org.w3c.dom.Document}, an instance of
167    * {@link org.w3c.dom.Element}, or an instance of
168    * {@link org.w3c.dom.DocumentFragment}. In the latter case, make
169    * sure, that the fragment contains a single root element.
170    */

171   public XSSchema parse(Node JavaDoc pNode) throws SAXException JavaDoc {
172     XSContext myData = getContext();
173     parser.set(this);
174     try {
175       return newXSLogicalParser().parse(pNode);
176     } finally {
177       myData.setCurrentContentHandler(null);
178       parser.set(null);
179     }
180   }
181
182   /** <p>Returns an instance of {@link XSContentHandler} for parsing a stream
183    * of SAX events.</p>
184    */

185   public XSContentHandler getXSContentHandler(String JavaDoc pSchemaLocation) throws SAXException JavaDoc {
186     parser.set(this);
187     return newXSLogicalParser().getXSContentHandler(pSchemaLocation);
188   }
189
190   /** <p>Provides access to the currently running instance of <code>XSParser</code>.</p>
191    */

192   public static XSParser getRunningInstance() {
193     return (XSParser) parser.get();
194   }
195 }
196
Popular Tags