KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > impl > XSSchemaImpl


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.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ListIterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27
28 import org.apache.ws.jaxme.xs.XSAnnotation;
29 import org.apache.ws.jaxme.xs.XSAttribute;
30 import org.apache.ws.jaxme.xs.XSAttributeGroup;
31 import org.apache.ws.jaxme.xs.XSElement;
32 import org.apache.ws.jaxme.xs.XSGroup;
33 import org.apache.ws.jaxme.xs.XSIdentityConstraint;
34 import org.apache.ws.jaxme.xs.XSKeyRef;
35 import org.apache.ws.jaxme.xs.XSNotation;
36 import org.apache.ws.jaxme.xs.XSObject;
37 import org.apache.ws.jaxme.xs.XSObjectFactory;
38 import org.apache.ws.jaxme.xs.XSSchema;
39 import org.apache.ws.jaxme.xs.XSType;
40 import org.apache.ws.jaxme.xs.parser.XSContext;
41 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
42 import org.apache.ws.jaxme.xs.types.*;
43 import org.apache.ws.jaxme.xs.xml.XsAnyURI;
44 import org.apache.ws.jaxme.xs.xml.XsESchema;
45 import org.apache.ws.jaxme.xs.xml.XsQName;
46 import org.xml.sax.Attributes JavaDoc;
47 import org.xml.sax.Locator JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49
50
51 /** <p>Implementation of an XML Schema, as defined by the
52  * {@link XSSchema} interface.</p>
53  *
54  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
55  */

56 public class XSSchemaImpl implements XSSchema {
57   private static final XSType[] BUILTIN_TYPES = new XSType[]{
58     XSAnySimpleType.getInstance(),
59     XSAnyURI.getInstance(),
60     XSBase64Binary.getInstance(),
61     XSBoolean.getInstance(),
62     XSByte.getInstance(),
63     XSDate.getInstance(),
64     XSDateTime.getInstance(),
65     XSDecimal.getInstance(),
66     XSDouble.getInstance(),
67     XSDuration.getInstance(),
68     XSEntities.getInstance(),
69     XSEntity.getInstance(),
70     XSFloat.getInstance(),
71     XSGDay.getInstance(),
72     XSGMonth.getInstance(),
73     XSGMonthDay.getInstance(),
74     XSGYear.getInstance(),
75     XSGYearMonth.getInstance(),
76     XSHexBinary.getInstance(),
77     XSID.getInstance(),
78     XSIDREF.getInstance(),
79     XSIDREFs.getInstance(),
80     XSInt.getInstance(),
81     XSInteger.getInstance(),
82     XSLanguage.getInstance(),
83     XSLong.getInstance(),
84     XSName.getInstance(),
85     XSNCName.getInstance(),
86     XSNegativeInteger.getInstance(),
87     XSNMToken.getInstance(),
88     XSNMTokens.getInstance(),
89     XSNonNegativeInteger.getInstance(),
90     XSNonPositiveInteger.getInstance(),
91     XSNormalizedString.getInstance(),
92     org.apache.ws.jaxme.xs.types.XSNotation.getInstance(),
93     XSPositiveInteger.getInstance(),
94     XSQName.getInstance(),
95     XSShort.getInstance(),
96     XSString.getInstance(),
97     XSTime.getInstance(),
98     XSToken.getInstance(),
99     XSUnsignedByte.getInstance(),
100     XSUnsignedInt.getInstance(),
101     XSUnsignedLong.getInstance(),
102     XSUnsignedShort.getInstance(),
103     XSAnyType.getInstance()
104   };
105
106   private final XSContext context;
107   private final XsESchema syntaxSchema;
108   private final List JavaDoc childs = new ArrayList JavaDoc(1);
109   private final Map JavaDoc types = new HashMap JavaDoc(1);
110   private final Map JavaDoc builtinTypes = new HashMap JavaDoc(1);
111   private final Map JavaDoc groups = new HashMap JavaDoc(1);
112   private final Map JavaDoc attributeGroups = new HashMap JavaDoc(1);
113   private final Map JavaDoc attributes = new HashMap JavaDoc(1);
114   private final Map JavaDoc elements = new HashMap JavaDoc(1);
115   private final Attributes JavaDoc openAttrs;
116
117   private final Map JavaDoc identityConstraintsMap = new HashMap JavaDoc(1);
118   private final Map JavaDoc keyRefsMap = new HashMap JavaDoc(1);
119
120   private final Map JavaDoc immutableIdentityConstraintsMap
121     = Collections.unmodifiableMap( identityConstraintsMap );
122   private final Map JavaDoc immutableKeyRefsMap = Collections.unmodifiableMap(
123     keyRefsMap
124   );
125
126   private boolean isValidated;
127
128   /** <p>Creates a new logical schema by loading data,
129    * which represents the given syntactical schema
130    * <code>pSchema</code> and uses the given context
131    * <code>pContext</code> for acquiring additional
132    * information.</p>
133    */

134   public XSSchemaImpl(XSContext pContext, XsESchema pSchema) {
135     context = pContext;
136     syntaxSchema = pSchema;
137     for (int i = 0; i < BUILTIN_TYPES.length; i++) {
138       builtinTypes.put(BUILTIN_TYPES[i].getName(), BUILTIN_TYPES[i]);
139     }
140     openAttrs = pSchema.getOpenAttributes();
141   }
142
143   public XSContext getContext() { return context; }
144   public boolean isTopLevelObject() { return true; }
145   public XSObject getParentObject() { return null; }
146   public XSObjectFactory getXSObjectFactory() { return context.getXSObjectFactory(); }
147   public Locator JavaDoc getLocator() { return syntaxSchema.getLocator(); }
148   public XSSchema getXSSchema() { return this; }
149   protected XsESchema getXsESchema() { return syntaxSchema; }
150
151   /** <p>Adds a new child to the array returned by {@link #getChilds()}.</p>
152    */

153   protected void addChild(Object JavaDoc pChild) {
154     childs.add(pChild);
155   }
156
157   /** <p>Replaces the existing child <code>pOldChild</code> with
158    * the replacement object <code>pNewChild</code>. This method
159    * is used from within the various <code>redefine()</code> methods.</p>
160    */

161   protected void replace(Object JavaDoc pOldChild, Object JavaDoc pNewChild) {
162     for (ListIterator JavaDoc iter = childs.listIterator(); iter.hasNext(); ) {
163       Object JavaDoc o = iter.next();
164       if (o.equals(pOldChild)) {
165         iter.set(pNewChild);
166         return;
167       }
168     }
169     throw new NoSuchElementException JavaDoc();
170   }
171
172   public XSAnnotation[] getAnnotations() {
173     List JavaDoc result = new ArrayList JavaDoc();
174     for (Iterator JavaDoc iter = childs.iterator(); iter.hasNext(); ) {
175       Object JavaDoc o = iter.next();
176       if (o instanceof XSAnnotation) {
177         result.add(o);
178       }
179     }
180     return (XSAnnotation[]) result.toArray(new XSAnnotation[result.size()]);
181   }
182
183   public XSType[] getTypes() {
184     List JavaDoc result = new ArrayList JavaDoc();
185     for (Iterator JavaDoc iter = childs.iterator(); iter.hasNext(); ) {
186       Object JavaDoc o = iter.next();
187       if (o instanceof XSType) {
188         result.add(o);
189       }
190     }
191     return (XSType[]) result.toArray(new XSType[result.size()]);
192   }
193
194   public XSType[] getBuiltinTypes() {
195     return BUILTIN_TYPES;
196   }
197
198   public XSType getType(XsQName pName) {
199     XSType result = (XSType) types.get(pName);
200     if (result == null) {
201       result = (XSType) builtinTypes.get(pName);
202     }
203     return result;
204   }
205
206   public XSGroup[] getGroups() {
207     List JavaDoc result = new ArrayList JavaDoc();
208     for (Iterator JavaDoc iter = childs.iterator(); iter.hasNext(); ) {
209       Object JavaDoc o = iter.next();
210       if (o instanceof XSGroup) {
211         result.add(o);
212       }
213     }
214     return (XSGroup[]) result.toArray(new XSGroup[result.size()]);
215   }
216
217   public XSGroup getGroup(XsQName pName) {
218     return (XSGroup) groups.get(pName);
219   }
220
221   public XSAttributeGroup[] getAttributeGroups() {
222     List JavaDoc result = new ArrayList JavaDoc();
223     for (Iterator JavaDoc iter = childs.iterator(); iter.hasNext(); ) {
224       Object JavaDoc o = iter.next();
225       if (o instanceof XSAttributeGroup) {
226         result.add(o);
227       }
228     }
229     return (XSAttributeGroup[]) result.toArray(new XSAttributeGroup[result.size()]);
230   }
231
232   public XSAttributeGroup getAttributeGroup(XsQName pName) {
233     return (XSAttributeGroup) attributeGroups.get(pName);
234   }
235
236   public XSElement[] getElements() {
237     List JavaDoc result = new ArrayList JavaDoc();
238     for (Iterator JavaDoc iter = childs.iterator(); iter.hasNext(); ) {
239       Object JavaDoc o = iter.next();
240       if (o instanceof XSElement) {
241         result.add(o);
242       }
243     }
244     return (XSElement[]) result.toArray(new XSElement[result.size()]);
245   }
246
247   public XSElement getElement(XsQName pName) {
248     return (XSElement) elements.get(pName);
249   }
250
251   public Map JavaDoc getIdentityConstraints() {
252     return immutableIdentityConstraintsMap;
253   }
254
255   public Map JavaDoc getKeyRefs() {
256     return immutableKeyRefsMap;
257   }
258
259   public void add( XSIdentityConstraint ic ) throws SAXException {
260     String JavaDoc name = ic.getName();
261
262     if ( name == null ) {
263       throw new LocSAXException(
264         "An identity constraint must have a 'name' attribute.",
265         ic.getLocator()
266       );
267     } else if ( identityConstraintsMap.put( name, ic ) != null ) {
268       throw new LocSAXException(
269         "No two identity constraints may share the same name.",
270         ic.getLocator()
271       );
272     }
273
274     identityConstraintsMap.put( name, ic );
275   }
276
277   public void add( XSKeyRef rf ) throws SAXException {
278     String JavaDoc name = rf.getName();
279
280     if ( name == null ) {
281       throw new LocSAXException(
282         "A key ref must have a 'name' attribute.",
283         rf.getLocator()
284       );
285     } else if ( keyRefsMap.put( name, rf ) != null ) {
286       throw new LocSAXException(
287         "No two key refs may share the same name.",
288         rf.getLocator()
289       );
290     }
291
292     keyRefsMap.put( name, rf );
293   }
294
295
296
297   public XSAttribute[] getAttributes() {
298     List JavaDoc result = new ArrayList JavaDoc();
299     for (Iterator JavaDoc iter = childs.iterator(); iter.hasNext(); ) {
300       Object JavaDoc o = iter.next();
301       if (o instanceof XSAttribute) {
302         result.add(o);
303       }
304     }
305     return (XSAttribute[]) result.toArray(new XSAttribute[result.size()]);
306   }
307
308   public XSAttribute getAttribute(XsQName pQName) {
309     return (XSAttribute) attributes.get(pQName);
310   }
311
312   public void add(XSAnnotation pAnnotation) {
313     addChild(pAnnotation);
314   }
315
316   public void add(XSType pType) throws SAXException {
317     XsQName name = pType.getName();
318     if (name == null) {
319       throw new LocSAXException("A global type must have a 'name' attribute.", pType.getLocator());
320     }
321     if (types.containsKey(name)) {
322       throw new LocSAXException("A global type " + name + " is already defined.", pType.getLocator());
323     }
324     types.put(name, pType);
325     pType.setGlobal(true);
326     addChild(pType);
327   }
328
329   public void redefine(XSType pType) throws SAXException {
330     XsQName name = pType.getName();
331     if (name == null) {
332       throw new LocSAXException("A global type must have a 'name' attribute.", pType.getLocator());
333     }
334     Object JavaDoc oldType = types.get(name);
335     if (oldType == null) {
336       throw new LocSAXException("The global type " + name + " cannot be redefined, as it doesn't yet exist.",
337                                    pType.getLocator());
338     }
339     types.put(name, pType);
340     pType.setGlobal(true);
341     replace(oldType, pType);
342   }
343
344   public void add(XSGroup pGroup) throws SAXException {
345     XsQName name = pGroup.getName();
346     if (name == null) {
347       throw new LocSAXException("A global group must have a 'name' attribute.", pGroup.getLocator());
348     }
349     if (groups.containsKey(name)) {
350       throw new LocSAXException("A global group " + name + " is already defined.", pGroup.getLocator());
351     }
352     groups.put(name, pGroup);
353     addChild(pGroup);
354     pGroup.setGlobal(true);
355   }
356
357   public void redefine(XSGroup pGroup) throws SAXException {
358     XsQName name = pGroup.getName();
359     if (name == null) {
360       throw new LocSAXException("A global group must have a 'name' attribute.", pGroup.getLocator());
361     }
362     Object JavaDoc oldGroup = groups.get(name);
363     if (oldGroup == null) {
364       throw new LocSAXException("The global group " + name + " cannot be redefined, as it doesn't yet exist.",
365                                    pGroup.getLocator());
366     }
367     groups.put(name, pGroup);
368     replace(oldGroup, pGroup);
369   }
370
371   public void add(XSAttributeGroup pGroup) throws SAXException {
372     XsQName name = pGroup.getName();
373     if (name == null) {
374       throw new LocSAXException("A global attribute group must have a 'name' attribute.", pGroup.getLocator());
375     }
376     if (attributeGroups.containsKey(name)) {
377       throw new LocSAXException("A global attribute group " + name + " is already defined.", pGroup.getLocator());
378     }
379     attributeGroups.put(name, pGroup);
380     addChild(pGroup);
381   }
382
383   public void redefine(XSAttributeGroup pGroup) throws SAXException {
384     XsQName name = pGroup.getName();
385     if (name == null) {
386       throw new LocSAXException("A global attribute group must have a 'name' attribute.", pGroup.getLocator());
387     }
388     Object JavaDoc oldGroup = attributeGroups.get(name);
389     if (!attributeGroups.containsKey(name)) {
390       throw new LocSAXException("The global attribute group " + name + " cannot be redefined, as it doesn't yet exist.",
391                                    pGroup.getLocator());
392     }
393     attributeGroups.put(name, pGroup);
394     replace(oldGroup, pGroup);
395   }
396
397   public void add(XSAttribute pAttribute) throws SAXException {
398     XsQName name = pAttribute.getName();
399     if (name == null) {
400       throw new LocSAXException("A global attribute must have a 'name' attribute.", pAttribute.getLocator());
401     }
402     if (attributes.containsKey(name)) {
403       throw new LocSAXException("A global attribute " + name + " is already defined.", pAttribute.getLocator());
404     }
405     attributes.put(name, pAttribute);
406     addChild(pAttribute);
407   }
408
409   public void add(XSElement pElement) throws SAXException {
410     XsQName name = pElement.getName();
411     if (name == null) {
412       throw new LocSAXException("A global element must have a 'name' attribute.", pElement.getLocator());
413     }
414     if (elements.containsKey(name)) {
415       throw new LocSAXException("A global element " + name + " is already defined.", pElement.getLocator());
416     }
417     elements.put(name, pElement);
418     addChild(pElement);
419   }
420
421   public void add(XSNotation pNotation) {
422     addChild(pNotation);
423   }
424
425   public Object JavaDoc[] getChilds() {
426     return childs.toArray();
427   }
428
429   protected void validate(Object JavaDoc pChild) throws SAXException {
430     if (pChild instanceof XSObject) {
431       ((XSObject) pChild).validate();
432     } else {
433       throw new IllegalStateException JavaDoc("Unable to validate the child " + pChild +
434                                        ", perhaps you should overwrite the method " +
435                                        getClass().getName() + ".validate(Object).");
436     }
437   }
438
439   protected boolean isValidated() {
440     return isValidated;
441   }
442
443   public void validate() throws SAXException {
444     if (isValidated()) {
445       return;
446     } else {
447       isValidated = true;
448     }
449
450     Object JavaDoc[] myChilds = getChilds();
451     for (int i = 0; i < myChilds.length; i++) {
452       validate(myChilds[i]);
453     }
454   }
455
456     public Attributes JavaDoc getOpenAttributes() {
457         return openAttrs;
458     }
459
460     public XsAnyURI getTargetNamespace() {
461         return syntaxSchema.getTargetNamespace();
462     }
463 }
464
Popular Tags