KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.ws.jaxme.xs.XSAnnotation;
20 import org.apache.ws.jaxme.xs.XSAttributable;
21 import org.apache.ws.jaxme.xs.XSComplexType;
22 import org.apache.ws.jaxme.xs.XSGroup;
23 import org.apache.ws.jaxme.xs.XSModelGroup;
24 import org.apache.ws.jaxme.xs.XSObject;
25 import org.apache.ws.jaxme.xs.XSObjectFactory;
26 import org.apache.ws.jaxme.xs.XSParticle;
27 import org.apache.ws.jaxme.xs.XSSchema;
28 import org.apache.ws.jaxme.xs.XSSimpleContentType;
29 import org.apache.ws.jaxme.xs.XSSimpleType;
30 import org.apache.ws.jaxme.xs.XSType;
31 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
32 import org.apache.ws.jaxme.xs.types.XSAnyType;
33 import org.apache.ws.jaxme.xs.xml.*;
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.Locator JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38
39 /**
40  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
41  * @author <a HREF="mailto:iasandcb@tmax.co.kr">Ias</a>
42  */

43 public class XSTypeImpl extends XSOpenAttrsImpl implements XSType {
44   public abstract class XSComplexTypeImpl implements XSComplexType {
45     protected final XSType owner;
46     private final XsTComplexType myComplexType;
47     protected XSType extendedType, restrictedType;
48
49     public XSComplexTypeImpl(XSType pOwner, XsTComplexType pType) {
50       owner = pOwner;
51       myComplexType = pType;
52     }
53
54     protected XsTComplexType getXsTComplexType() { return myComplexType; }
55     protected XSType getOwner() { return owner; }
56
57     public boolean isSequence() { return false; }
58     public boolean isChoice() { return false; }
59     public boolean isAll() { return false; }
60     public boolean hasSimpleContent() { return false; }
61     public XSSimpleContentType getSimpleContent() {
62       throw new IllegalStateException JavaDoc("This complex type doesn't have simple content.");
63     }
64     public boolean hasComplexContent() { return false; }
65     public boolean isEmpty() { return XsComplexContentType.EMPTY.equals(getComplexContentType()); }
66     public boolean isElementOnly() { return XsComplexContentType.ELEMENT_ONLY.equals(getComplexContentType()); }
67     public boolean isMixed() { return XsComplexContentType.MIXED.equals(getComplexContentType()); }
68
69     public void validate() throws SAXException {
70     }
71
72
73     public boolean isExtension() { return extendedType != null; }
74     public XSType getExtendedType() {
75       if (extendedType == null) {
76         throw new IllegalStateException JavaDoc("This type is no extension.");
77       }
78       return extendedType;
79     }
80
81     public boolean isRestriction() { return restrictedType != null; }
82     public XSType getRestrictedType() {
83       if (restrictedType == null) {
84         throw new IllegalStateException JavaDoc("This type is no restriction.");
85       }
86       return restrictedType;
87     }
88   }
89
90   public class XSSimpleContentImpl extends XSComplexTypeImpl {
91     private final XsESimpleContent simpleContent;
92     private final XSSimpleContentType simpleContentType;
93     private final XSAttributable[] attributes;
94
95     public boolean isElementOnly() { return false; }
96     public boolean isEmpty() { return false; }
97     public boolean isMixed() { return false; }
98
99     public XSSimpleContentImpl(XSType pOwner, XsTComplexType pType, XsESimpleContent pSimpleContent)
100         throws SAXException {
101       super(pOwner, pType);
102       simpleContent = pSimpleContent;
103       XsTSimpleExtensionType extension = simpleContent.getExtension();
104       if (extension == null) {
105         XsTSimpleRestrictionType restriction = simpleContent.getRestriction();
106         if (restriction == null) {
107           throw new LocSAXException("Invalid 'simpleContent', neither of the 'extension' or 'restriction' child elements are present.",
108                                        simpleContent.getLocator());
109         }
110         XsQName restrictedTypesName = restriction.getBase();
111         if (restrictedTypesName == null) {
112           throw new LocSAXException("Invalid 'restriction': Missing 'base' attribute.", restriction.getLocator());
113         }
114         restrictedType = getXSSchema().getType(restrictedTypesName);
115         if (restrictedType == null) {
116           throw new LocSAXException("Invalid 'restriction': The base type " + restrictedTypesName + " is unknown.",
117                                      restriction.getLocator());
118         }
119         restrictedType.validate();
120         extendedType = null;
121         XSObjectFactory factory = pOwner.getXSSchema().getXSObjectFactory();
122         XSType contentType = factory.newXSType(pOwner, restriction);
123         simpleContentType = factory.newXSSimpleContentType(pOwner, contentType, restriction);
124         attributes = XSAttributeGroupImpl.getAttributes(XSTypeImpl.this, restriction);
125       } else {
126         XsQName extendedTypesName = extension.getBase();
127         if (extendedTypesName == null) {
128           throw new LocSAXException("Invalid 'extension': Missing 'base' attribute.",
129                                        extension.getLocator());
130         }
131         extendedType = getXSSchema().getType(extendedTypesName);
132         if (extendedType == null) {
133           throw new LocSAXException("Invalid 'extension': Unknown 'base' type " + extendedTypesName,
134                                        extension.getLocator());
135         }
136         extendedType.validate();
137         restrictedType = null;
138         XSAttributable[] inheritedAttributes;
139         if (extendedType.isSimple()) {
140           simpleContentType = getOwner().getXSSchema().getXSObjectFactory().newXSSimpleContentType(pOwner,
141                                                                                      extendedType, extension);
142           inheritedAttributes = new XSAttributable[0];
143         } else {
144           XSComplexType myComplexType = extendedType.getComplexType();
145           if (!myComplexType.hasSimpleContent()) {
146             throw new LocSAXException("Invalid 'extension': The base type " + extendedTypesName +
147                                          " is neither a simple type nor a complex type with simple content.",
148                                          extension.getLocator());
149           }
150           simpleContentType = myComplexType.getSimpleContent();
151           inheritedAttributes = myComplexType.getAttributes();
152         }
153         XSAttributable[] myAttributes = XSAttributeGroupImpl.getAttributes(XSTypeImpl.this, extension);
154         attributes = new XSAttributable[inheritedAttributes.length + myAttributes.length];
155         System.arraycopy(inheritedAttributes, 0, attributes, 0, inheritedAttributes.length);
156         System.arraycopy(myAttributes, 0, attributes, inheritedAttributes.length, myAttributes.length);
157       }
158     }
159
160     public boolean hasSimpleContent() { return true; }
161     public XSSimpleContentType getSimpleContent() { return simpleContentType; }
162     public XSAttributable[] getAttributes() { return attributes; }
163     public XSParticle getParticle() {
164       throw new IllegalStateException JavaDoc("This complex type doesn't have a model group particle.");
165     }
166     public XsComplexContentType getComplexContentType() {
167         throw new IllegalStateException JavaDoc("This complex type (" + this.getClass().getName() +
168                                          ") doesn't have complex content.");
169     }
170   }
171
172   public class XSComplexContentImpl extends XSComplexTypeImpl {
173     private final XsEComplexContent complexContent;
174     private final XSParticle complexContentParticle;
175     private final XsComplexContentType complexContentType;
176     private final XSAttributable[] attributes;
177
178     protected XsEComplexContent getComplexContent() {
179       return complexContent;
180     }
181
182     protected XSGroup getGroupByParticle(XsTTypeDefParticle pParticle) throws SAXException {
183       XSGroup result;
184       XSType myOwner = getOwner();
185       XSObjectFactory factory = myOwner.getXSSchema().getXSObjectFactory();
186       if (pParticle == null) {
187         return null;
188       } else if (pParticle instanceof XsEChoice) {
189         XsEChoice choice = (XsEChoice) pParticle;
190         result = factory.newXSGroup(myOwner, choice);
191       } else if (pParticle instanceof XsESequence) {
192         XsESequence sequence = (XsESequence) pParticle;
193         result = factory.newXSGroup(myOwner, sequence);
194       } else if (pParticle instanceof XsTAll) {
195         XsTAll all = (XsTAll) pParticle;
196         result = factory.newXSGroup(myOwner, all);
197       } else if (pParticle instanceof XsTGroupRef) {
198         XsTGroupRef groupRef = (XsTGroupRef) pParticle;
199         result = factory.newXSGroup(myOwner, groupRef);
200       } else {
201         throw new IllegalStateException JavaDoc("Unknown TypeDefParticle type: " + pParticle.getClass().getName());
202       }
203       result.validate();
204       return result;
205     }
206
207     protected XsComplexContentType getContentTypeByParticle(XsTTypeDefParticle pParticle, XSGroup pGroup)
208         throws SAXException {
209       if (pParticle == null) {
210         return XsComplexContentType.EMPTY;
211       } else if (pParticle instanceof XsEChoice) {
212         if (pGroup.getParticles().length == 0) {
213           XsEChoice choice = (XsEChoice) pParticle;
214           if (choice.getMinOccurs() == 0) {
215             return XsComplexContentType.EMPTY;
216           } else {
217             throw new LocSAXException("Invalid choice: Neither child elements, nor 'minOccurs'=0", choice.getLocator());
218           }
219         } else {
220         }
221       } else if (pParticle instanceof XsESequence) {
222         if (pGroup.getParticles().length == 0) {
223           return XsComplexContentType.EMPTY;
224         }
225       } else if (pParticle instanceof XsTAll) {
226         if (pGroup.getParticles().length == 0) {
227           return XsComplexContentType.EMPTY;
228         }
229       } else if (pParticle instanceof XsTGroupRef) {
230       } else {
231         throw new IllegalStateException JavaDoc("Unknown TypeDefParticle type: " + pParticle.getClass().getName());
232       }
233       boolean isMixed;
234       if (complexContent.isMixed() == null) {
235         isMixed = ((XsTComplexType) getXsObject()).isMixed();
236       } else {
237         isMixed = complexContent.isMixed().booleanValue();
238       }
239       return isMixed ? XsComplexContentType.MIXED : XsComplexContentType.ELEMENT_ONLY;
240     }
241
242     private class ExtensionGroup implements XSGroup {
243       private final XSObject parent;
244       private final XSParticle[] particles;
245       private ExtensionGroup(XSObject pParent, XSParticle[] pParticles) {
246         parent = pParent;
247         particles = pParticles;
248       }
249       public boolean isTopLevelObject() { return false; }
250       public boolean isGlobal() { return false; }
251       public XsQName getName() { return null; }
252       public XSSchema getXSSchema() { return XSTypeImpl.this.getXSSchema(); }
253       public Locator JavaDoc getLocator() { return getComplexContent().getLocator(); }
254       public XSObjectFactory getXSObjectFactory() { return getXSObjectFactory(); }
255       public void validate() throws SAXException {}
256       public XSModelGroup.Compositor getCompositor() { return XSModelGroup.SEQUENCE; }
257       public boolean isSequence() { return true; }
258       public boolean isChoice() { return false; }
259       public boolean isAll() { return false; }
260       public XSParticle[] getParticles() { return particles; }
261       public XSAnnotation[] getAnnotations() { return new XSAnnotation[0]; }
262       public void setGlobal(boolean pGlobal) {
263         if (pGlobal) {
264           throw new IllegalStateException JavaDoc("An extensions model group cannot be made global.");
265         }
266       }
267       public XSObject getParentObject() { return parent; }
268       public Attributes JavaDoc getOpenAttributes() { return null; }
269     }
270
271     public XSComplexContentImpl(XSType pOwner, XsTComplexType pType, XsEComplexContent pComplexContent)
272         throws SAXException {
273       super(pOwner, pType);
274       complexContent = pComplexContent;
275       XsTExtensionType extension = complexContent.getExtension();
276       if (extension == null) {
277         XsTComplexRestrictionType restriction = complexContent.getRestriction();
278         if (restriction == null) {
279           // TODO: Restriction of the ur-type
280
throw new LocSAXException("Neither of extension or restriction, aka restriction of the ur-type: Not implemented",
281                                        complexContent.getLocator());
282         }
283         XsQName base = restriction.getBase();
284         if (base == null) {
285           throw new LocSAXException("Invalid 'restriction': Missing 'base' attribute", getLocator());
286         }
287         XSType type = getXSSchema().getType(base);
288         if (type == null) {
289           throw new LocSAXException("Invalid 'restriction': Unknown base type " + base, getLocator());
290         }
291         if (type.isSimple()) {
292           throw new LocSAXException("Invalid 'restriction': The base type " + getName() + " is simple.", getLocator());
293         }
294         XsTTypeDefParticle particle = restriction.getTypeDefParticle();
295
296         XSGroup group = getGroupByParticle(particle);
297         if (group == null) {
298           // TODO: Restriction of the ur-type
299
complexContentParticle = null;
300         } else {
301           complexContentParticle = new XSParticleImpl(group);
302         }
303         complexContentType = getContentTypeByParticle(particle, group);
304         attributes = XSAttributeGroupImpl.getAttributes(XSTypeImpl.this, restriction);
305         restrictedType = type;
306       } else {
307         XsQName base = extension.getBase();
308         if (base == null) {
309           throw new LocSAXException("Invalid 'extension': Missing 'base' attribute", getLocator());
310         }
311         XSType type = getXSSchema().getType(base);
312         if (type == null) {
313           throw new LocSAXException("Invalid 'extension': Unknown base type " + base, getLocator());
314         }
315         if (type.isSimple()) {
316           throw new LocSAXException("Invalid 'extension': The base type " + base + " is simple.", getLocator());
317         }
318         XSComplexType extendedComplexType = type.getComplexType();
319         if (extendedComplexType.hasSimpleContent()) {
320           throw new LocSAXException("Invalid 'extension': The base type " + base + " has simple content.",
321                                        getLocator());
322         }
323         XsTTypeDefParticle particle = extension.getTypeDefParticle();
324         XSGroup group = getGroupByParticle(particle);
325         XsComplexContentType groupType = getContentTypeByParticle(particle, group);
326
327         if (XsComplexContentType.EMPTY.equals(groupType)) {
328             if (type == XSAnyType.getInstance()) {
329                 complexContentType = null;
330                 complexContentParticle = null;
331             } else {
332                 complexContentType = extendedComplexType.getComplexContentType();
333                 complexContentParticle = extendedComplexType.getParticle();
334             }
335         } else if (extendedComplexType.isEmpty()) {
336           complexContentType = groupType;
337           complexContentParticle = new XSParticleImpl(group);
338         } else {
339           XSGroup sequenceGroup = new ExtensionGroup(pOwner, new XSParticle[]{extendedComplexType.getParticle(), new XSParticleImpl(group)});
340           complexContentParticle = new XSParticleImpl(sequenceGroup);
341           complexContentType = groupType;
342         }
343
344         XSAttributable[] inheritedAttributes = extendedComplexType.getAttributes();
345         XSAttributable[] myAttributes = XSAttributeGroupImpl.getAttributes(XSTypeImpl.this, extension);
346         attributes = new XSAttributable[inheritedAttributes.length + myAttributes.length];
347         System.arraycopy(inheritedAttributes, 0, attributes, 0, inheritedAttributes.length);
348         System.arraycopy(myAttributes, 0, attributes, inheritedAttributes.length, myAttributes.length);
349         extendedType = type;
350       }
351     }
352
353     public boolean hasComplexContent() { return true; }
354
355     public XsComplexContentType getComplexContentType() {
356       return complexContentType;
357     }
358
359     public XSParticle getParticle() {
360       return complexContentParticle;
361     }
362
363     public XSAttributable[] getAttributes() { return attributes; }
364   }
365
366
367   public abstract class XSBasicComplexTypeImpl extends XSComplexTypeImpl {
368     private final XSAttributable[] attributes;
369     private XSParticle particle;
370     private XsComplexContentType contentType;
371
372     public XSBasicComplexTypeImpl(XSType pOwner, XsTComplexType pType) throws SAXException {
373       super(pOwner, pType);
374       attributes = XSAttributeGroupImpl.getAttributes(XSTypeImpl.this, pType);
375     }
376
377     public void setParticle(XsComplexContentType pType, XSParticle pParticle) {
378       contentType = pType;
379       particle = pParticle;
380     }
381     public XSParticle getParticle() { return particle; }
382     public XSAttributable[] getAttributes() { return attributes; }
383     public XsComplexContentType getComplexContentType() { return contentType; }
384   }
385
386   public class XSSequenceComplexTypeImpl extends XSBasicComplexTypeImpl {
387     private final XsESequence sequence;
388
389     public XSSequenceComplexTypeImpl(XSType pOwner, XsTComplexType pType) throws SAXException {
390       super(pOwner, pType);
391       sequence = null;
392       setParticle(pType.isMixed() ? XsComplexContentType.MIXED : XsComplexContentType.EMPTY, null);
393     }
394     public XSSequenceComplexTypeImpl(XSType pOwner, XsTComplexType pType, XsESequence pSequence) throws SAXException {
395       super(pOwner, pType);
396       sequence = pSequence;
397       XSGroup group = pOwner.getXSSchema().getXSObjectFactory().newXSGroup(pOwner, sequence);
398       group.validate();
399       XSParticleImpl particle = new XSParticleImpl(group);
400       particle.setMaxOccurs(pSequence.getMaxOccurs());
401       particle.setMinOccurs(pSequence.getMinOccurs());
402       if (group.getParticles().length == 0) {
403         setParticle(XsComplexContentType.EMPTY, particle);
404       } else {
405         setParticle(pType.isMixed() ? XsComplexContentType.MIXED : XsComplexContentType.ELEMENT_ONLY, particle);
406       }
407     }
408
409     public boolean isSequence() { return true; }
410   }
411
412   public class XSChoiceComplexTypeImpl extends XSBasicComplexTypeImpl {
413     private final XsEChoice choice;
414
415     public XSChoiceComplexTypeImpl(XSType pOwner, XsTComplexType pType, XsEChoice pChoice) throws SAXException {
416       super(pOwner, pType);
417       choice = pChoice;
418       XSGroup group = pOwner.getXSSchema().getXSObjectFactory().newXSGroup(pOwner, choice);
419       group.validate();
420       XSParticleImpl particle = new XSParticleImpl(group);
421       particle.setMaxOccurs(pChoice.getMaxOccurs());
422       particle.setMinOccurs(pChoice.getMinOccurs());
423       if (group.getParticles().length == 0) {
424         throw new LocSAXException("The complex type must not have an empty element group, as it is a choice.",
425                                      choice.getLocator());
426       } else {
427         setParticle(pType.isMixed() ? XsComplexContentType.MIXED : XsComplexContentType.ELEMENT_ONLY, particle);
428       }
429     }
430
431     public boolean isChoice() { return true; }
432   }
433
434   public class XSAllComplexTypeImpl extends XSBasicComplexTypeImpl {
435     private final XsTAll all;
436
437     public XSAllComplexTypeImpl(XSType pOwner, XsTComplexType pType, XsTAll pAll) throws SAXException {
438       super(pOwner, pType);
439       all = pAll;
440       XSGroup group = pOwner.getXSSchema().getXSObjectFactory().newXSGroup(pOwner, all);
441       group.validate();
442       XSParticleImpl particle = new XSParticleImpl(group);
443       particle.setMaxOccurs(pAll.getMaxOccurs());
444       particle.setMinOccurs(pAll.getMinOccurs());
445       if (group.getParticles().length == 0) {
446         setParticle(XsComplexContentType.EMPTY, particle);
447       } else {
448         setParticle(pType.isMixed() ? XsComplexContentType.MIXED : XsComplexContentType.ELEMENT_ONLY, particle);
449       }
450     }
451
452     public boolean isAll() { return true; }
453   }
454
455   public class XSGroupComplexTypeImpl extends XSBasicComplexTypeImpl {
456     private final XSGroup group;
457
458     public XSGroupComplexTypeImpl(XSType pOwner, XsTComplexType pType, XSGroup pGroup) throws SAXException {
459       super(pOwner, pType);
460       group = pGroup;
461       XSParticleImpl particle = new XSParticleImpl(group);
462       if (group.getParticles().length == 0) {
463         setParticle(XsComplexContentType.EMPTY, particle);
464       } else {
465         setParticle(pType.isMixed() ? XsComplexContentType.MIXED : XsComplexContentType.ELEMENT_ONLY, particle);
466       }
467     }
468
469     public boolean isAll() { return group.isAll(); }
470     public boolean isChoice() { return group.isChoice(); }
471     public boolean isSequence() { return group.isSequence(); }
472   }
473
474   private final boolean isSimple;
475   private final XsQName name;
476   private boolean isGlobal;
477   private XSSimpleType simpleType;
478   private XSComplexType complexType;
479   private boolean isValidated;
480   private final XsEAnnotation xsAnnotation;
481   private XSAnnotation[] annotations;
482
483   protected boolean isValidated() { return isValidated; }
484
485   protected XSTypeImpl(XSObject pParent, XsETopLevelSimpleType pSimpleType)
486        throws SAXException {
487     super(pParent, pSimpleType);
488     isSimple = true;
489     XsNCName myName = pSimpleType.getName();
490     if (myName == null) {
491       throw new LocSAXException("Invalid simple type: Missing 'name' attribute.",
492                                    pSimpleType.getLocator());
493     }
494     XsESchema schema = pSimpleType.getXsESchema();
495     this.name = new XsQName(schema.getTargetNamespace(), myName.toString(), schema.getTargetNamespacePrefix());
496     xsAnnotation = pSimpleType.getAnnotation();
497   }
498
499   protected XSTypeImpl(XSObject pParent, XsTLocalSimpleType pSimpleType) {
500     super(pParent, pSimpleType);
501     isSimple = true;
502     name = null;
503     xsAnnotation = pSimpleType == null ? null : pSimpleType.getAnnotation();
504   }
505
506   protected XSTypeImpl(XSObject pParent, XsTComplexType pComplexType)
507       throws SAXException {
508     super(pParent, pComplexType);
509     isSimple = false;
510     XsNCName myName = pComplexType.getName();
511     if (myName == null) {
512       throw new LocSAXException("Invalid complex type: Missing 'name' attribute.",
513                                    pComplexType.getLocator());
514     }
515     XsESchema schema = pComplexType.getXsESchema();
516     this.name = new XsQName(schema.getTargetNamespace(), myName.toString(), schema.getTargetNamespacePrefix());
517     xsAnnotation = pComplexType.getAnnotation();
518   }
519
520   protected XSTypeImpl(XSObject pParent, XsTLocalComplexType pComplexType) {
521     super(pParent, pComplexType);
522     isSimple = false;
523     name = null;
524     xsAnnotation = pComplexType.getAnnotation();
525   }
526
527   protected XSTypeImpl(XSObject pParent, XsTSimpleRestrictionType pRestriction)
528       throws SAXException {
529     super(pParent, pRestriction);
530     XsQName myName = pRestriction.getBase();
531     if (myName == null) {
532       throw new LocSAXException("Invalid 'restriction': Missing 'base' attribute.",
533                                    pRestriction.getLocator());
534     }
535     XSType type = getXSSchema().getType(myName);
536     if (type == null) {
537       throw new LocSAXException("Invalid 'restriction': Unknown 'base' type " + myName,
538                                    pRestriction.getLocator());
539     }
540     type.validate();
541     if (type.isSimple()) {
542       throw new LocSAXException("The 'base' type " + myName + " of 'simpleContent/restriction' is simple." +
543                                    " It ought to be a complex type with simple content: ",
544                                    pRestriction.getLocator());
545     }
546     XSComplexType myComplexType = type.getComplexType();
547     if (!myComplexType.hasSimpleContent()) {
548       throw new LocSAXException("The 'base' type " + myName + " of 'simpleContent/restriction' is complex," +
549                                    " but doesn't have simple content: ", pRestriction.getLocator());
550     }
551     XSObjectFactory factory = pParent.getXSSchema().getXSObjectFactory();
552     if (myComplexType.isExtension()) {
553       XSType extendedType = myComplexType.getSimpleContent().getType();
554       extendedType.validate();
555       XSSimpleType extendedSimpleType = extendedType.getSimpleType();
556     
557       XSSimpleType mySimpleType;
558       if (extendedSimpleType.isAtomic()) {
559         mySimpleType = factory.newXSAtomicType(this, extendedType, pRestriction);
560       } else if (extendedSimpleType.isList()) {
561         mySimpleType = factory.newXSListType(this, extendedType, pRestriction);
562       } else if (extendedSimpleType.isUnion()) {
563         mySimpleType = factory.newXSUnionType(this, extendedType, pRestriction);
564       } else {
565         throw new LocSAXException("Unknown restriction type: " + extendedType,
566                                   pRestriction.getLocator());
567       }
568
569       simpleType = mySimpleType;
570       //was: setSimpleType( extendedType.getSimpleType() );
571
} else {
572       XsTLocalSimpleType localSimpleType = pRestriction.getSimpleType();
573       XSType restrictedType;
574       if (localSimpleType != null) {
575         restrictedType = factory.newXSType(this, localSimpleType);
576       } else {
577         restrictedType = myComplexType.getSimpleContent().getType();
578       }
579       restrictedType.validate();
580       XSSimpleType restrictedSimpleType = restrictedType.getSimpleType();
581       if (restrictedSimpleType.isAtomic()) {
582         simpleType = factory.newXSAtomicType(this, restrictedType, pRestriction);
583       } else if (restrictedSimpleType.isList()) {
584         simpleType = factory.newXSListType(this, restrictedType, pRestriction);
585       } else if (restrictedSimpleType.isUnion()) {
586         simpleType = factory.newXSUnionType(this, restrictedType, pRestriction);
587       }
588     }
589     this.name = null;
590     isSimple = true;
591     xsAnnotation = pRestriction.getAnnotation();
592   }
593
594   public XsQName getName() {
595     return name;
596   }
597
598   public boolean isSimple() {
599     return isSimple;
600   }
601
602   public boolean isGlobal() {
603     return isGlobal;
604   }
605
606   public void setGlobal(boolean pGlobal) {
607     isGlobal = pGlobal;
608   }
609
610   public XSAnnotation[] getAnnotations() {
611     return annotations;
612   }
613
614   public void validate() throws SAXException {
615     if (isValidated()) {
616       return;
617     } else {
618       isValidated = true;
619     }
620
621     if (xsAnnotation == null) {
622       annotations = new XSAnnotation[0];
623     } else {
624       XSAnnotation result = getXSSchema().getXSObjectFactory().newXSAnnotation(this, xsAnnotation);
625       result.validate();
626       annotations = new XSAnnotation[]{result};
627     }
628
629     if (isSimple()) {
630       XSSimpleType mySimpleType;
631       Object JavaDoc baseObject = getXsObject();
632       if (baseObject instanceof XsTSimpleType) {
633         XsTSimpleType myXsTSimpleType = (XsTSimpleType) baseObject;
634         XsEList list = myXsTSimpleType.getList();
635         if (list == null) {
636             XsEUnion union = myXsTSimpleType.getUnion();
637             if (union == null) {
638                 XsERestriction restriction = myXsTSimpleType.getRestriction();
639                 if (restriction == null) {
640                     throw new LocSAXException("Either of the 'list', 'union', or 'restriction' child elements must be set.",
641                             myXsTSimpleType.getLocator());
642                 }
643                 XsQName myName = restriction.getBase();
644                 XSType restrictedType;
645                 if (myName == null) {
646                     XsTLocalSimpleType baseType = restriction.getSimpleType();
647                     if (baseType == null) {
648                         throw new LocSAXException("Neither the 'base' attribute nor an inner 'simpleType' element are present",
649                                 restriction.getLocator());
650                     } else {
651                         restrictedType = getXSSchema().getXSObjectFactory().newXSType(this, baseType);
652                     }
653                 } else {
654                     restrictedType = getXSSchema().getType(myName);
655                     if (restrictedType == null) {
656                         throw new LocSAXException("Unknown base type: " + myName,
657                                 restriction.getLocator());
658                     }
659                 }
660                 restrictedType.validate();
661                 if (!restrictedType.isSimple()) {
662                     throw new LocSAXException("The restricted type " + myName + " is complex.",
663                             restriction.getLocator());
664                 }
665                 XSSimpleType baseType = restrictedType.getSimpleType();
666                 if (baseType.isAtomic()) {
667                     mySimpleType = getXSSchema().getXSObjectFactory().newXSAtomicType(this, restrictedType, restriction);
668                 } else if (baseType.isList()) {
669                     mySimpleType = getXSSchema().getXSObjectFactory().newXSListType(this, restrictedType, restriction);
670                 } else if (baseType.isUnion()) {
671                     mySimpleType = getXSSchema().getXSObjectFactory().newXSUnionType(this, restrictedType, restriction);
672                 } else {
673                     throw new LocSAXException("Unknown restriction type: " + baseType, restriction.getLocator());
674                 }
675             } else {
676                 mySimpleType = getXSSchema().getXSObjectFactory().newXSUnionType(this, union);
677             }
678         } else {
679             mySimpleType = getXSSchema().getXSObjectFactory().newXSListType(this, list);
680         }
681       }
682       else {
683         mySimpleType = getSimpleType();
684       }
685       
686       this.simpleType = mySimpleType;
687     } else {
688       XSComplexTypeImpl myComplexType;
689       XsTComplexType myXsTComplexType = (XsTComplexType) getXsObject();
690       XsESimpleContent simpleContent = myXsTComplexType.getSimpleContent();
691       if (simpleContent == null) {
692         XsEComplexContent complexContent = myXsTComplexType.getComplexContent();
693         if (complexContent == null) {
694           XsTTypeDefParticle particle = myXsTComplexType.getTypeDefParticle();
695           if (particle == null) {
696             myComplexType = new XSSequenceComplexTypeImpl(this, myXsTComplexType);
697           } else if (particle instanceof XsESequence) {
698             myComplexType = new XSSequenceComplexTypeImpl(this, myXsTComplexType, (XsESequence) particle);
699           } else if (particle instanceof XsEChoice) {
700             myComplexType = new XSChoiceComplexTypeImpl(this, myXsTComplexType, (XsEChoice) particle);
701           } else if (particle instanceof XsTAll) {
702             myComplexType = new XSAllComplexTypeImpl(this, myXsTComplexType, (XsTAll) particle);
703           } else if (particle instanceof XsTGroupRef) {
704             XsTGroupRef groupRef = (XsTGroupRef) particle;
705             XsQName myName = groupRef.getRef();
706             if (myName == null) {
707               throw new LocSAXException("Missing 'ref' attribute", groupRef.getLocator());
708             }
709             XSGroup group = getXSSchema().getGroup(myName);
710             if (group == null) {
711               throw new LocSAXException("Unknown group: " + myName, getLocator());
712             }
713             group.validate();
714             myComplexType = new XSGroupComplexTypeImpl(this, myXsTComplexType, group);
715           } else {
716             throw new IllegalStateException JavaDoc("Invalid particle: " + particle.getClass().getName());
717           }
718         } else {
719           XSComplexContentImpl complexContentImpl = new XSComplexContentImpl(this, myXsTComplexType, complexContent);
720           myComplexType = complexContentImpl;
721         }
722       } else {
723         XSSimpleContentImpl simpleContentImpl = new XSSimpleContentImpl(this, myXsTComplexType, simpleContent);
724         myComplexType = simpleContentImpl;
725       }
726       this.complexType = myComplexType;
727       myComplexType.validate();
728     }
729   }
730
731   public XSSimpleType getSimpleType() throws SAXException {
732     validate();
733     XSSimpleType result = simpleType;
734     if (result == null) {
735       throw new IllegalStateException JavaDoc("This is a complex type.");
736     }
737     return result;
738   }
739
740   public XSComplexType getComplexType() throws SAXException {
741     validate();
742     XSComplexType result = complexType;
743     if (result == null) {
744       if (getName() == null) {
745         throw new IllegalStateException JavaDoc("This is a simple type.");
746       } else {
747         throw new IllegalStateException JavaDoc("The type " + getName() + " is simple.");
748       }
749     }
750     return result;
751   }
752
753   public boolean isBuiltin() {
754     return false;
755   }
756
757   public XsSchemaHeader getSchemaHeader() {
758     return getXsObject().getXsESchema();
759   }
760 }
761
Popular Tags