KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > xml > impl > XsESchemaImpl


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.xml.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.xml.XMLConstants JavaDoc;
23
24 import org.apache.ws.jaxme.xs.parser.XSContext;
25 import org.apache.ws.jaxme.xs.xml.*;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
28
29
30 /** <p>Implementation of xs:schema. Follows this specification:
31  * <pre>
32  * &lt;xs:element name="schema" id="schema"&gt;
33  * &lt;xs:annotation&gt;
34  * &lt;xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-schema"/&gt;
35  * &lt;/xs:annotation&gt;
36  * &lt;xs:complexType&gt;
37  * &lt;xs:complexContent&gt;
38  * &lt;xs:extension base="xs:openAttrs"&gt;
39  * &lt;xs:sequence&gt;
40  * &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
41  * &lt;xs:element ref="xs:include"/&gt;
42  * &lt;xs:element ref="xs:import"/&gt;
43  * &lt;xs:element ref="xs:redefine"/&gt;
44  * &lt;xs:element ref="xs:annotation"/&gt;
45  * &lt;/xs:choice&gt;
46  * &lt;xs:sequence minOccurs="0" maxOccurs="unbounded"&gt;
47  * &lt;xs:group ref="xs:schemaTop"/&gt;
48  * &lt;xs:element ref="xs:annotation" minOccurs="0" maxOccurs="unbounded"/&gt;
49  * &lt;/xs:sequence&gt;
50  * &lt;/xs:sequence&gt;
51  * &lt;xs:attribute name="targetNamespace" type="xs:anyURI"/&gt;
52  * &lt;xs:attribute name="version" type="xs:token"/&gt;
53  * &lt;xs:attribute name="finalDefault" type="xs:derivationSet" use="optional" default=""/&gt;
54  * &lt;xs:attribute name="blockDefault" type="xs:blockSet" use="optional" default=""/&gt;
55  * &lt;xs:attribute name="attributeFormDefault" type="xs:formChoice" use="optional" default="unqualified"/&gt;
56  * &lt;xs:attribute name="elementFormDefault" type="xs:formChoice" use="optional" default="unqualified"/&gt;
57  * &lt;xs:attribute name="id" type="xs:ID"/&gt;
58  * &lt;xs:attribute ref="xml:lang"/&gt;
59  * &lt;/xs:extension&gt;
60  * &lt;/xs:complexContent&gt;
61  * &lt;/xs:complexType&gt;
62  *
63  * &lt;xs:key name="element"&gt;
64  * &lt;xs:selector xpath="xs:element"/&gt;
65  * &lt;xs:field xpath="@name"/&gt;
66  * &lt;/xs:key&gt;
67  * &lt;xs:key name="attribute"&gt;
68  * &lt;xs:selector xpath="xs:attribute"/&gt;
69  * &lt;xs:field xpath="@name"/&gt;
70  * &lt;/xs:key&gt;
71  * &lt;xs:key name="type"&gt;
72  * &lt;xs:selector xpath="xs:complexType|xs:simpleType"/&gt;
73  * &lt;xs:field xpath="@name"/&gt;
74  * &lt;/xs:key&gt;
75  * &lt;xs:key name="group"&gt;
76  * &lt;xs:selector xpath="xs:group"/&gt;
77  * &lt;xs:field xpath="@name"/&gt;
78  * &lt;/xs:key&gt;
79  * &lt;xs:key name="attributeGroup"&gt;
80  * &lt;xs:selector xpath="xs:attributeGroup"/&gt;
81  * &lt;xs:field xpath="@name"/&gt;
82  * &lt;/xs:key&gt;
83  * &lt;xs:key name="notation"&gt;
84  * &lt;xs:selector xpath="xs:notation"/&gt;
85  * &lt;xs:field xpath="@name"/&gt;
86  * &lt;/xs:key&gt;
87  * &lt;xs:key name="identityConstraint"&gt;
88  * &lt;xs:selector xpath=".//xs:key|.//xs:unique|.//xs:keyref"/&gt;
89  * &lt;xs:field xpath="@name"/&gt;
90  * &lt;/xs:key&gt;
91  * &lt;/xs:element&gt;
92  *
93  * &lt;xs:group name="schemaTop"&gt;
94  * &lt;xs:annotation&gt;
95  * &lt;xs:documentation&gt;
96  * This group is for the
97  * elements which occur freely at the top level of schemas.
98  * All of their types are based on the "annotated" type by extension.
99  * &lt;/xs:documentation&gt;
100  * &lt;/xs:annotation&gt;
101  * &lt;xs:choice&gt;
102  * &lt;xs:group ref="xs:redefinable"/&gt;
103  * &lt;xs:element ref="xs:element"/&gt;
104  * &lt;xs:element ref="xs:attribute"/&gt;
105  * &lt;xs:element ref="xs:notation"/&gt;
106  * &lt;/xs:choice&gt;
107  * &lt;/xs:group&gt;
108  * </pre>
109  * </p>
110  *
111  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
112  */

113 public class XsESchemaImpl extends XsTOpenAttrsImpl implements XsESchema {
114   private final XSContext context;
115   private XsAnyURI targetNamespace;
116   private String JavaDoc targetNamespacePrefix;
117   private XsToken version;
118   private XsDerivationSet finalDefault = new XsDerivationSet("");
119   private XsBlockSet blockDefault = new XsBlockSet("#all");
120   private XsFormChoice attributeFormDefault = XsFormChoice.UNQUALIFIED;
121   private XsFormChoice elementFormDefault = XsFormChoice.UNQUALIFIED;
122   private XsID id;
123   private XmlLang language;
124   private List JavaDoc childs = new ArrayList JavaDoc();
125   boolean schemaTopSeen = false;
126
127   protected XsESchemaImpl(XSContext pContext) {
128     super(null);
129     context = pContext;
130   }
131
132   public XSContext getContext() { return context; }
133
134   public XsEInclude createInclude() {
135     if (schemaTopSeen) {
136       throw new IllegalStateException JavaDoc("xs:include is not allowed after xs:simpleType, xs:complexType, xs:group, xs:attributeGroup, xs:element. xs:attribute, or xs:notation have been seen.");
137     }
138     XsEInclude xsInclude = getObjectFactory().newXsEInclude(this);
139     childs.add(xsInclude);
140     return xsInclude;
141   }
142
143   public XsEImport createImport() {
144     if (schemaTopSeen) {
145       throw new IllegalStateException JavaDoc("xs:include is not allowed after xs:simpleType, xs:complexType, xs:group, xs:attributeGroup, xs:element. xs:attribute, or xs:notation have been seen.");
146     }
147     XsEImport xsImport = getObjectFactory().newXsEImport(this);
148     childs.add(xsImport);
149     return xsImport;
150   }
151
152   public XsERedefine createRedefine() {
153     if (schemaTopSeen) {
154       throw new IllegalStateException JavaDoc("xs:include is not allowed after xs:simpleType, xs:complexType, xs:group, xs:attributeGroup, xs:element. xs:attribute, or xs:notation have been seen.");
155     }
156     XsERedefine xsRedefine = getObjectFactory().newXsERedefine(this);
157     childs.add(xsRedefine);
158     return xsRedefine;
159   }
160
161   public XsEAnnotation createAnnotation() {
162     XsEAnnotation xsAnnotation = getObjectFactory().newXsEAnnotation(this);
163     childs.add(xsAnnotation);
164     return xsAnnotation;
165   }
166
167   public XsETopLevelSimpleType createSimpleType() {
168     schemaTopSeen = true;
169     XsETopLevelSimpleType xsSimpleType = getObjectFactory().newXsETopLevelSimpleType(this);
170     childs.add(xsSimpleType);
171     return xsSimpleType;
172   }
173
174   public XsTComplexType createComplexType() {
175     schemaTopSeen = true;
176     XsTComplexType xsComplexType = getObjectFactory().newXsTComplexType(this);
177     childs.add(xsComplexType);
178     return xsComplexType;
179   }
180
181   public XsTNamedGroup createGroup() {
182     schemaTopSeen = true;
183     XsTNamedGroup xsGroup = getObjectFactory().newXsTNamedGroup(this);
184     childs.add(xsGroup);
185     return xsGroup;
186   }
187
188   public XsTAttributeGroup createAttributeGroup() {
189     schemaTopSeen = true;
190     XsTAttributeGroup xsAttributeGroup = getObjectFactory().newXsTAttributeGroup(this);
191     childs.add(xsAttributeGroup);
192     return xsAttributeGroup;
193   }
194
195   public XsTTopLevelElement createElement() {
196     schemaTopSeen = true;
197     XsTTopLevelElement xsElement = getObjectFactory().newXsTTopLevelElement(this);
198     childs.add(xsElement);
199     return xsElement;
200   }
201
202   public XsTAttribute createAttribute() {
203     schemaTopSeen = true;
204     XsTAttribute xsAttribute = getObjectFactory().newXsTAttribute(this);
205     childs.add(xsAttribute);
206     return xsAttribute;
207   }
208
209   public XsENotation createNotation() {
210     schemaTopSeen = true;
211     XsENotation xsNotation = getObjectFactory().newXsENotation(this);
212     childs.add(xsNotation);
213     return xsNotation;
214   }
215
216   public XsFormChoice getAttributeFormDefault() { return attributeFormDefault; }
217   public void setAttributeFormDefault(XsFormChoice pChoice) { attributeFormDefault = pChoice; }
218   public XsBlockSet getBlockDefault() { return blockDefault; }
219   public void setBlockDefault(XsBlockSet pSet) { blockDefault = pSet; }
220   public XsFormChoice getElementFormDefault() { return elementFormDefault; }
221   public void setElementFormDefault(XsFormChoice pChoice) { elementFormDefault = pChoice; }
222   public XsDerivationSet getFinalDefault() { return finalDefault; }
223   public void setFinalDefault(XsDerivationSet pSet) { finalDefault = pSet; }
224   public XsID getId() { return id; }
225   public void setId(XsID pId) { id = pId; }
226   public XsAnyURI getTargetNamespace() { return targetNamespace; }
227   public String JavaDoc getTargetNamespacePrefix() { return targetNamespacePrefix; }
228   public void setTargetNamespace(XsAnyURI pAnyURI) {
229     targetNamespace = pAnyURI;
230     if (targetNamespace == null) {
231       targetNamespacePrefix = null;
232     } else {
233       NamespaceSupport JavaDoc nss = getNamespaceSupport();
234       targetNamespacePrefix = nss.getPrefix(targetNamespace.toString());
235     }
236   }
237   public XsToken getVersion() { return version; }
238   public void setVersion(XsToken pToken) { version = pToken; }
239   public XmlLang getLang() { return language; }
240   public void setLang(XmlLang pLanguage) { language = pLanguage; }
241
242   public boolean setAttribute(String JavaDoc pQName, String JavaDoc pNamespaceURI,
243                                 String JavaDoc pLocalName, String JavaDoc pValue) throws SAXException JavaDoc {
244     if (XMLConstants.XML_NS_URI.equals(pNamespaceURI) && "lang".equals(pLocalName)) {
245       setLang(new XmlLang(pValue));
246       return true;
247     }
248     return super.setAttribute(pQName, pNamespaceURI, pLocalName, pValue);
249   }
250
251   public Object JavaDoc[] getChilds() {
252     return childs.toArray();
253   }
254
255     /** <p>Creates a new {@link XsQName}, which is mutable and
256      * has the schemas target namespace as a namespace URI:
257      * If the target namespace changes, then the names URI
258      * does as well.</p>
259      */

260     public XsQName newXsQName(String JavaDoc pLocalName, String JavaDoc pPrefix) {
261         return new XsQName(getTargetNamespace(), pLocalName, pPrefix){
262             public String JavaDoc getNamespaceURI() {
263                 XsAnyURI uri = getTargetNamespace();
264                 if (uri != null) {
265                     return uri.toString();
266                 } else {
267                     return "";
268                 }
269             }
270             public String JavaDoc toString() {
271                 XsAnyURI uri = getTargetNamespace();
272                 if (uri == null) {
273                     return super.toString();
274                 } else {
275                     return "{" + uri + "}" + getLocalName() + " (<= " + super.toString() + ")";
276                 }
277             }
278         };
279     }
280 }
281
Popular Tags