KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > traversers > XSDComplexTypeTraverser


1 /*
2  * Copyright 2001-2005 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 package org.apache.xerces.impl.xs.traversers;
17
18 import org.apache.xerces.impl.dv.InvalidDatatypeFacetException;
19 import org.apache.xerces.impl.dv.SchemaDVFactory;
20 import org.apache.xerces.impl.dv.XSFacets;
21 import org.apache.xerces.impl.dv.XSSimpleType;
22 import org.apache.xerces.impl.xs.SchemaGrammar;
23 import org.apache.xerces.impl.xs.SchemaSymbols;
24 import org.apache.xerces.impl.xs.XSAnnotationImpl;
25 import org.apache.xerces.impl.xs.XSAttributeGroupDecl;
26 import org.apache.xerces.impl.xs.XSAttributeUseImpl;
27 import org.apache.xerces.impl.xs.XSComplexTypeDecl;
28 import org.apache.xerces.impl.xs.XSConstraints;
29 import org.apache.xerces.impl.xs.XSModelGroupImpl;
30 import org.apache.xerces.impl.xs.XSParticleDecl;
31 import org.apache.xerces.impl.xs.XSWildcardDecl;
32 import org.apache.xerces.impl.xs.util.XInt;
33 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
34 import org.apache.xerces.util.DOMUtil;
35 import org.apache.xerces.xni.QName;
36 import org.apache.xerces.xs.XSAttributeUse;
37 import org.apache.xerces.xs.XSConstants;
38 import org.apache.xerces.xs.XSObjectList;
39 import org.apache.xerces.xs.XSTypeDefinition;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * A complex type definition schema component traverser.
44  *
45  * <complexType
46  * abstract = boolean : false
47  * block = (#all | List of (extension | restriction))
48  * final = (#all | List of (extension | restriction))
49  * id = ID
50  * mixed = boolean : false
51  * name = NCName
52  * {any attributes with non-schema namespace . . .}>
53  * Content: (annotation?, (simpleContent | complexContent |
54  * ((group | all | choice | sequence)?,
55  * ((attribute | attributeGroup)*, anyAttribute?))))
56  * </complexType>
57  *
58  * @xerces.internal
59  *
60  * @version $Id: XSDComplexTypeTraverser.java,v 1.49 2005/04/19 18:49:47 mrglavas Exp $
61  */

62
63 class XSDComplexTypeTraverser extends XSDAbstractParticleTraverser {
64     
65     // size of stack to hold globals:
66
private final static int GLOBAL_NUM = 11;
67     
68     // globals for building XSComplexTypeDecls
69
private String JavaDoc fName = null;
70     private String JavaDoc fTargetNamespace = null;
71     private short fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
72     private short fFinal = XSConstants.DERIVATION_NONE;
73     private short fBlock = XSConstants.DERIVATION_NONE;
74     private short fContentType = XSComplexTypeDecl.CONTENTTYPE_EMPTY;
75     private XSTypeDefinition fBaseType = null;
76     private XSAttributeGroupDecl fAttrGrp = null;
77     private XSSimpleType fXSSimpleType = null;
78     private XSParticleDecl fParticle = null;
79     private boolean fIsAbstract = false;
80     private XSComplexTypeDecl fComplexTypeDecl = null;
81     private XSAnnotationImpl [] fAnnotations = null;
82     
83     private XSParticleDecl fEmptyParticle = null;
84     
85     // our own little stack to retain state when getGlobalDecls is called:
86
private Object JavaDoc [] fGlobalStore = null;
87     private int fGlobalStorePos = 0;
88     
89     XSDComplexTypeTraverser (XSDHandler handler,
90             XSAttributeChecker gAttrCheck) {
91         super(handler, gAttrCheck);
92     }
93     
94     
95     private static final boolean DEBUG=false;
96     
97     private SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
98     
99     private class ComplexTypeRecoverableError extends Exception JavaDoc {
100         
101         private static final long serialVersionUID = 3762247556666831417L;
102         
103         Object JavaDoc[] errorSubstText=null;
104         Element JavaDoc errorElem = null;
105         ComplexTypeRecoverableError() {
106             super();
107         }
108         ComplexTypeRecoverableError(String JavaDoc msgKey, Object JavaDoc[] args, Element JavaDoc e) {
109             super(msgKey);
110             errorSubstText=args;
111             errorElem = e;
112         }
113         
114     }
115     
116     /**
117      * Traverse local complexType declarations
118      *
119      * @param Element
120      * @param XSDocumentInfo
121      * @param SchemaGrammar
122      * @return XSComplexTypeDecl
123      */

124     XSComplexTypeDecl traverseLocal(Element JavaDoc complexTypeNode,
125             XSDocumentInfo schemaDoc,
126             SchemaGrammar grammar) {
127         
128         
129         Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, false,
130                 schemaDoc);
131         String JavaDoc complexTypeName = genAnonTypeName(complexTypeNode);
132         contentBackup();
133         XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode,
134                 complexTypeName, attrValues, schemaDoc, grammar);
135         contentRestore();
136         // need to add the type to the grammar for later constraint checking
137
grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode));
138         type.setIsAnonymous();
139         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
140         
141         return type;
142     }
143     
144     /**
145      * Traverse global complexType declarations
146      *
147      * @param Element
148      * @param XSDocumentInfo
149      * @param SchemaGrammar
150      * @return XSComplexTypeDecXSComplexTypeDecl
151      */

152     XSComplexTypeDecl traverseGlobal (Element JavaDoc complexTypeNode,
153             XSDocumentInfo schemaDoc,
154             SchemaGrammar grammar) {
155         
156         Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, true,
157                 schemaDoc);
158         String JavaDoc complexTypeName = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
159         contentBackup();
160         XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode,
161                 complexTypeName, attrValues, schemaDoc, grammar);
162         contentRestore();
163         if (complexTypeName == null) {
164             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{SchemaSymbols.ELT_COMPLEXTYPE, SchemaSymbols.ATT_NAME}, complexTypeNode);
165         } else {
166             grammar.addGlobalTypeDecl(type);
167         }
168         // need to add the type to the grammar for later constraint checking
169
grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode));
170         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
171         
172         return type;
173     }
174     
175     
176     private XSComplexTypeDecl traverseComplexTypeDecl(Element JavaDoc complexTypeDecl,
177             String JavaDoc complexTypeName,
178             Object JavaDoc[] attrValues,
179             XSDocumentInfo schemaDoc,
180             SchemaGrammar grammar) {
181         
182         fComplexTypeDecl = new XSComplexTypeDecl();
183         fAttrGrp = new XSAttributeGroupDecl();
184         Boolean JavaDoc abstractAtt = (Boolean JavaDoc) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT];
185         XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK];
186         Boolean JavaDoc mixedAtt = (Boolean JavaDoc) attrValues[XSAttributeChecker.ATTIDX_MIXED];
187         XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL];
188         
189         fName = complexTypeName;
190         fComplexTypeDecl.setName(fName);
191         fTargetNamespace = schemaDoc.fTargetNamespace;
192         
193         fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue();
194         fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue();
195         //discard valid Block/Final 'Default' values that are invalid for Block/Final
196
fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
197         fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
198         
199         fIsAbstract = (abstractAtt != null && abstractAtt.booleanValue());
200         
201         Element JavaDoc child = null;
202         
203         try {
204             // ---------------------------------------------------------------
205
// First, handle any ANNOTATION declaration and get next child
206
// ---------------------------------------------------------------
207
child = DOMUtil.getFirstChildElement(complexTypeDecl);
208             if(child != null) {
209                 if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
210                     addAnnotation(traverseAnnotationDecl(child, attrValues, false, schemaDoc));
211                     child = DOMUtil.getNextSiblingElement(child);
212                 }
213                 else {
214                     String JavaDoc text = DOMUtil.getSyntheticAnnotation(complexTypeDecl);
215                     if (text != null) {
216                         addAnnotation(traverseSyntheticAnnotation(complexTypeDecl, text, attrValues, false, schemaDoc));
217                     }
218                 }
219                 if (child !=null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
220                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
221                             new Object JavaDoc[]{fName,SchemaSymbols.ELT_ANNOTATION},
222                             child);
223                 }
224             }
225             else {
226                 String JavaDoc text = DOMUtil.getSyntheticAnnotation(complexTypeDecl);
227                 if (text != null) {
228                     addAnnotation(traverseSyntheticAnnotation(complexTypeDecl, text, attrValues, false, schemaDoc));
229                 }
230             }
231             // ---------------------------------------------------------------
232
// Process the content of the complex type definition
233
// ---------------------------------------------------------------
234
if (child==null) {
235                 //
236
// EMPTY complexType with complexContent
237
//
238

239                 // set the base to the anyType
240
fBaseType = SchemaGrammar.fAnyType;
241                 processComplexContent(child, mixedAtt.booleanValue(), false,
242                         schemaDoc, grammar);
243             }
244             else if (DOMUtil.getLocalName(child).equals
245                     (SchemaSymbols.ELT_SIMPLECONTENT)) {
246                 //
247
// SIMPLE CONTENT
248
//
249
traverseSimpleContent(child, schemaDoc, grammar);
250                 Element JavaDoc elemTmp = DOMUtil.getNextSiblingElement(child);
251                 if (elemTmp != null) {
252                     String JavaDoc siblingName = DOMUtil.getLocalName(elemTmp);
253                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
254                             new Object JavaDoc[]{fName,siblingName},
255                             elemTmp);
256                 }
257             }
258             else if (DOMUtil.getLocalName(child).equals
259                     (SchemaSymbols.ELT_COMPLEXCONTENT)) {
260                 traverseComplexContent(child, mixedAtt.booleanValue(),
261                         schemaDoc, grammar);
262                 Element JavaDoc elemTmp = DOMUtil.getNextSiblingElement(child);
263                 if (elemTmp != null) {
264                     String JavaDoc siblingName = DOMUtil.getLocalName(elemTmp);
265                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
266                             new Object JavaDoc[]{fName,siblingName},
267                             elemTmp);
268                 }
269             }
270             else {
271                 //
272
// We must have ....
273
// GROUP, ALL, SEQUENCE or CHOICE, followed by optional attributes
274
// Note that it's possible that only attributes are specified.
275
//
276

277                 // set the base to the anyType
278
fBaseType = SchemaGrammar.fAnyType;
279                 processComplexContent(child, mixedAtt.booleanValue(), false,
280                         schemaDoc, grammar);
281             }
282             
283         }
284         catch (ComplexTypeRecoverableError e) {
285             handleComplexTypeError(e.getMessage(), e.errorSubstText,
286                     e.errorElem);
287         }
288         
289         if (DEBUG) {
290             System.out.println(fName);
291         }
292         fComplexTypeDecl.setValues(fName, fTargetNamespace, fBaseType,
293                 fDerivedBy, fFinal, fBlock, fContentType, fIsAbstract,
294                 fAttrGrp, fXSSimpleType, fParticle, new XSObjectListImpl(fAnnotations,
295                         fAnnotations == null? 0 : fAnnotations.length));
296         return fComplexTypeDecl;
297     }
298     
299     
300     private void traverseSimpleContent(Element JavaDoc simpleContentElement,
301             XSDocumentInfo schemaDoc,
302             SchemaGrammar grammar)
303     throws ComplexTypeRecoverableError {
304         
305         
306         Object JavaDoc[] simpleContentAttrValues = fAttrChecker.checkAttributes(simpleContentElement, false,
307                 schemaDoc);
308         
309         // -----------------------------------------------------------------------
310
// Set content type
311
// -----------------------------------------------------------------------
312
fContentType = XSComplexTypeDecl.CONTENTTYPE_SIMPLE;
313         fParticle = null;
314         
315         Element JavaDoc simpleContent = DOMUtil.getFirstChildElement(simpleContentElement);
316         if (simpleContent != null && DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
317             addAnnotation(traverseAnnotationDecl(simpleContent, simpleContentAttrValues, false, schemaDoc));
318             simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
319         }
320         else {
321             String JavaDoc text = DOMUtil.getSyntheticAnnotation(simpleContentElement);
322             if (text != null) {
323                 addAnnotation(traverseSyntheticAnnotation(simpleContentElement, text, simpleContentAttrValues, false, schemaDoc));
324             }
325         }
326         
327         // If there are no children, return
328
if (simpleContent==null) {
329             fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
330             throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.2",
331                     new Object JavaDoc[]{fName,SchemaSymbols.ELT_SIMPLECONTENT},
332                     simpleContentElement);
333         }
334         
335         // -----------------------------------------------------------------------
336
// The content should be either "restriction" or "extension"
337
// -----------------------------------------------------------------------
338
String JavaDoc simpleContentName = DOMUtil.getLocalName(simpleContent);
339         if (simpleContentName.equals(SchemaSymbols.ELT_RESTRICTION))
340             fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
341         else if (simpleContentName.equals(SchemaSymbols.ELT_EXTENSION))
342             fDerivedBy = XSConstants.DERIVATION_EXTENSION;
343         else {
344             fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
345             throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
346                     new Object JavaDoc[]{fName,simpleContentName},
347                     simpleContent);
348         }
349         Element JavaDoc elemTmp = DOMUtil.getNextSiblingElement(simpleContent);
350         if (elemTmp != null) {
351             fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
352             String JavaDoc siblingName = DOMUtil.getLocalName(elemTmp);
353             throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
354                     new Object JavaDoc[]{fName,siblingName},
355                     elemTmp);
356         }
357         
358         Object JavaDoc [] derivationTypeAttrValues = fAttrChecker.checkAttributes(simpleContent, false,
359                 schemaDoc);
360         QName baseTypeName = (QName) derivationTypeAttrValues[XSAttributeChecker.ATTIDX_BASE];
361         
362         
363         // -----------------------------------------------------------------------
364
// Need a base type.
365
// -----------------------------------------------------------------------
366
if (baseTypeName==null) {
367             fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
368             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
369             throw new ComplexTypeRecoverableError("s4s-att-must-appear",
370                     new Object JavaDoc[]{simpleContentName, "base"}, simpleContent);
371         }
372         
373         XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc,
374                 XSDHandler.TYPEDECL_TYPE, baseTypeName,
375                 simpleContent);
376         if (type==null) {
377             fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
378             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
379             throw new ComplexTypeRecoverableError();
380         }
381         
382         fBaseType = type;
383         
384         XSSimpleType baseValidator = null;
385         XSComplexTypeDecl baseComplexType = null;
386         int baseFinalSet = 0;
387         
388         // If the base type is complex, it must have simpleContent
389
if ((type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE)) {
390             
391             baseComplexType = (XSComplexTypeDecl)type;
392             baseFinalSet = baseComplexType.getFinal();
393             // base is a CT with simple content (both restriction and extension are OK)
394
if (baseComplexType.getContentType() == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) {
395                 baseValidator = (XSSimpleType)baseComplexType.getSimpleType();
396             }
397             // base is a CT with mixed/emptiable content (only restriction is OK)
398
else if (fDerivedBy == XSConstants.DERIVATION_RESTRICTION &&
399                     baseComplexType.getContentType() == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
400                     ((XSParticleDecl)baseComplexType.getParticle()).emptiable()) {
401             }
402             else {
403                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
404                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
405                 throw new ComplexTypeRecoverableError("src-ct.2.1",
406                         new Object JavaDoc[]{fName, baseComplexType.getName()}, simpleContent);
407             }
408         }
409         else {
410             baseValidator = (XSSimpleType)type;
411             // base is a ST (only extension is OK)
412
if (fDerivedBy == XSConstants.DERIVATION_RESTRICTION) {
413                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
414                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
415                 throw new ComplexTypeRecoverableError("src-ct.2.1",
416                         new Object JavaDoc[]{fName, baseValidator.getName()}, simpleContent);
417             }
418             baseFinalSet=baseValidator.getFinal();
419         }
420         
421         // -----------------------------------------------------------------------
422
// Check that the base permits the derivation
423
// -----------------------------------------------------------------------
424
if ((baseFinalSet & fDerivedBy)!=0) {
425             fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
426             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
427             String JavaDoc errorKey = (fDerivedBy==XSConstants.DERIVATION_EXTENSION) ?
428                     "cos-ct-extends.1.1" : "derivation-ok-restriction.1";
429             throw new ComplexTypeRecoverableError(errorKey,
430                     new Object JavaDoc[]{fName, fBaseType.getName()}, simpleContent);
431         }
432         
433         // -----------------------------------------------------------------------
434
// Skip over any potential annotations
435
// -----------------------------------------------------------------------
436
Element JavaDoc scElement = simpleContent;
437         simpleContent = DOMUtil.getFirstChildElement(simpleContent);
438         if (simpleContent != null) {
439             // traverse annotation if any
440

441             if (DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
442                 addAnnotation(traverseAnnotationDecl(simpleContent, derivationTypeAttrValues, false, schemaDoc));
443                 simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
444             }
445             else {
446                 String JavaDoc text = DOMUtil.getSyntheticAnnotation(scElement);
447                 if (text != null) {
448                     addAnnotation(traverseSyntheticAnnotation(scElement, text, derivationTypeAttrValues, false, schemaDoc));
449                 }
450             }
451             
452             if (simpleContent !=null &&
453                     DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)){
454                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
455                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
456                 throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
457                         new Object JavaDoc[]{fName,SchemaSymbols.ELT_ANNOTATION},
458                         simpleContent);
459             }
460         }
461         else {
462             String JavaDoc text = DOMUtil.getSyntheticAnnotation(scElement);
463             if (text != null) {
464                 addAnnotation(traverseSyntheticAnnotation(scElement, text, derivationTypeAttrValues, false, schemaDoc));
465             }
466         }
467         
468         // -----------------------------------------------------------------------
469
// Process a RESTRICTION
470
// -----------------------------------------------------------------------
471
if (fDerivedBy == XSConstants.DERIVATION_RESTRICTION) {
472             
473             // -----------------------------------------------------------------------
474
// There may be a simple type definition in the restriction element
475
// The data type validator will be based on it, if specified
476
// -----------------------------------------------------------------------
477
if (simpleContent !=null &&
478                     DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_SIMPLETYPE )) {
479                 
480                 XSSimpleType dv = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(
481                         simpleContent, schemaDoc, grammar);
482                 if (dv == null) {
483                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
484                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
485                     throw new ComplexTypeRecoverableError();
486                 }
487                 //check that this datatype validator is validly derived from the base
488
//according to derivation-ok-restriction 5.1.2.1
489

490                 if (baseValidator != null &&
491                         !XSConstraints.checkSimpleDerivationOk(dv, baseValidator,
492                                 baseValidator.getFinal())) {
493                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
494                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
495                     throw new ComplexTypeRecoverableError("derivation-ok-restriction.5.2.2.1",
496                             new Object JavaDoc[]{fName, dv.getName(), baseValidator.getName()},
497                             simpleContent);
498                 }
499                 baseValidator = dv;
500                 simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
501             }
502             
503             // this only happens when restricting a mixed/emptiable CT
504
// but there is no <simpleType>, which is required
505
if (baseValidator == null) {
506                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
507                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
508                 throw new ComplexTypeRecoverableError("src-ct.2.2",
509                         new Object JavaDoc[]{fName}, simpleContent);
510             }
511             
512             // -----------------------------------------------------------------------
513
// Traverse any facets
514
// -----------------------------------------------------------------------
515
Element JavaDoc attrNode = null;
516             XSFacets facetData = null;
517             short presentFacets = 0 ;
518             short fixedFacets = 0 ;
519             
520             if (simpleContent!=null) {
521                 FacetInfo fi = traverseFacets(simpleContent, baseValidator, schemaDoc);
522                 attrNode = fi.nodeAfterFacets;
523                 facetData = fi.facetdata;
524                 presentFacets = fi.fPresentFacets;
525                 fixedFacets = fi.fFixedFacets;
526             }
527             
528             fXSSimpleType = schemaFactory.createTypeRestriction(null,schemaDoc.fTargetNamespace,(short)0,baseValidator,null);
529             try{
530                 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
531                 fXSSimpleType.applyFacets(facetData, presentFacets, fixedFacets, fValidationState);
532             }catch(InvalidDatatypeFacetException ex){
533                 reportSchemaError(ex.getKey(), ex.getArgs(), simpleContent);
534             }
535             
536             // -----------------------------------------------------------------------
537
// Traverse any attributes
538
// -----------------------------------------------------------------------
539
if (attrNode != null) {
540                 if (!isAttrOrAttrGroup(attrNode)) {
541                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
542                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
543                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
544                             new Object JavaDoc[]{fName,DOMUtil.getLocalName(attrNode)},
545                             attrNode);
546                 }
547                 Element JavaDoc node=traverseAttrsAndAttrGrps(attrNode,fAttrGrp,
548                         schemaDoc,grammar,fComplexTypeDecl);
549                 if (node!=null) {
550                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
551                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
552                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
553                             new Object JavaDoc[]{fName,DOMUtil.getLocalName(node)},
554                             node);
555                 }
556             }
557             
558             try {
559                 mergeAttributes(baseComplexType.getAttrGrp(), fAttrGrp, fName, false, simpleContentElement);
560             } catch (ComplexTypeRecoverableError e) {
561                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
562                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
563                 throw e;
564             }
565             // Prohibited uses must be removed after merge for RESTRICTION
566
fAttrGrp.removeProhibitedAttrs();
567             
568             Object JavaDoc[] errArgs=fAttrGrp.validRestrictionOf(fName, baseComplexType.getAttrGrp());
569             if (errArgs != null) {
570                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
571                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
572                 throw new ComplexTypeRecoverableError((String JavaDoc)errArgs[errArgs.length-1],
573                         errArgs, attrNode);
574             }
575             
576         }
577         // -----------------------------------------------------------------------
578
// Process a EXTENSION
579
// -----------------------------------------------------------------------
580
else {
581             fXSSimpleType = baseValidator;
582             if (simpleContent != null) {
583                 // -----------------------------------------------------------------------
584
// Traverse any attributes
585
// -----------------------------------------------------------------------
586
Element JavaDoc attrNode = simpleContent;
587                 if (!isAttrOrAttrGroup(attrNode)) {
588                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
589                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
590                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
591                             new Object JavaDoc[]{fName,DOMUtil.getLocalName(attrNode)},
592                             attrNode);
593                 }
594                 Element JavaDoc node=traverseAttrsAndAttrGrps(attrNode,fAttrGrp,
595                         schemaDoc,grammar,fComplexTypeDecl);
596                 
597                 if (node!=null) {
598                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
599                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
600                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
601                             new Object JavaDoc[]{fName,DOMUtil.getLocalName(node)},
602                             node);
603                 }
604                 // Remove prohibited uses. Should be done prior to any merge.
605
fAttrGrp.removeProhibitedAttrs();
606             }
607             
608             if (baseComplexType != null) {
609                 try {
610                     mergeAttributes(baseComplexType.getAttrGrp(), fAttrGrp, fName, true, simpleContentElement);
611                 } catch (ComplexTypeRecoverableError e) {
612                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
613                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
614                     throw e;
615                 }
616             }
617         }
618         // and finally, since we've nothing more to traverse, we can
619
// return the attributes (and thereby reset the namespace support)
620
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
621         fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
622     }
623     
624     private void traverseComplexContent(Element JavaDoc complexContentElement,
625             boolean mixedOnType, XSDocumentInfo schemaDoc,
626             SchemaGrammar grammar)
627     throws ComplexTypeRecoverableError {
628         
629         
630         Object JavaDoc[] complexContentAttrValues = fAttrChecker.checkAttributes(complexContentElement, false,
631                 schemaDoc);
632         
633         
634         // -----------------------------------------------------------------------
635
// Determine if this is mixed content
636
// -----------------------------------------------------------------------
637
boolean mixedContent = mixedOnType;
638         Boolean JavaDoc mixedAtt = (Boolean JavaDoc) complexContentAttrValues[XSAttributeChecker.ATTIDX_MIXED];
639         if (mixedAtt != null) {
640             mixedContent = mixedAtt.booleanValue();
641         }
642         
643         
644         // -----------------------------------------------------------------------
645
// Since the type must have complex content, set the simple type validators
646
// to null
647
// -----------------------------------------------------------------------
648
fXSSimpleType = null;
649         
650         Element JavaDoc complexContent = DOMUtil.getFirstChildElement(complexContentElement);
651         if (complexContent != null && DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
652             addAnnotation(traverseAnnotationDecl(complexContent, complexContentAttrValues, false, schemaDoc));
653             complexContent = DOMUtil.getNextSiblingElement(complexContent);
654         }
655         else {
656             String JavaDoc text = DOMUtil.getSyntheticAnnotation(complexContentElement);
657             if (text != null) {
658                 addAnnotation(traverseSyntheticAnnotation(complexContentElement, text, complexContentAttrValues, false, schemaDoc));
659             }
660         }
661         
662         // If there are no children, return
663
if (complexContent==null) {
664             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
665             throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.2",
666                     new Object JavaDoc[]{fName,SchemaSymbols.ELT_COMPLEXCONTENT},
667                     complexContentElement);
668         }
669         
670         // -----------------------------------------------------------------------
671
// The content should be either "restriction" or "extension"
672
// -----------------------------------------------------------------------
673
String JavaDoc complexContentName = DOMUtil.getLocalName(complexContent);
674         if (complexContentName.equals(SchemaSymbols.ELT_RESTRICTION))
675             fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
676         else if (complexContentName.equals(SchemaSymbols.ELT_EXTENSION))
677             fDerivedBy = XSConstants.DERIVATION_EXTENSION;
678         else {
679             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
680             throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
681                     new Object JavaDoc[]{fName, complexContentName}, complexContent);
682         }
683         Element JavaDoc elemTmp = DOMUtil.getNextSiblingElement(complexContent);
684         if (elemTmp != null) {
685             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
686             String JavaDoc siblingName = DOMUtil.getLocalName(elemTmp);
687             throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
688                     new Object JavaDoc[]{fName, siblingName}, elemTmp);
689         }
690         
691         Object JavaDoc[] derivationTypeAttrValues = fAttrChecker.checkAttributes(complexContent, false,
692                 schemaDoc);
693         QName baseTypeName = (QName) derivationTypeAttrValues[XSAttributeChecker.ATTIDX_BASE];
694         
695         
696         // -----------------------------------------------------------------------
697
// Need a base type. Check that it's a complex type
698
// -----------------------------------------------------------------------
699
if (baseTypeName==null) {
700             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
701             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
702             throw new ComplexTypeRecoverableError("s4s-att-must-appear",
703                     new Object JavaDoc[]{complexContentName, "base"}, complexContent);
704         }
705         
706         XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc,
707                 XSDHandler.TYPEDECL_TYPE,
708                 baseTypeName,
709                 complexContent);
710         
711         if (type==null) {
712             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
713             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
714             throw new ComplexTypeRecoverableError();
715         }
716         
717         if (! (type instanceof XSComplexTypeDecl)) {
718             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
719             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
720             throw new ComplexTypeRecoverableError("src-ct.1",
721                     new Object JavaDoc[]{fName, type.getName()}, complexContent);
722         }
723         XSComplexTypeDecl baseType = (XSComplexTypeDecl)type;
724         fBaseType = baseType;
725         
726         // -----------------------------------------------------------------------
727
// Check that the base permits the derivation
728
// -----------------------------------------------------------------------
729
if ((baseType.getFinal() & fDerivedBy)!=0) {
730             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
731             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
732             String JavaDoc errorKey = (fDerivedBy==XSConstants.DERIVATION_EXTENSION) ?
733                     "cos-ct-extends.1.1" : "derivation-ok-restriction.1";
734             throw new ComplexTypeRecoverableError(errorKey,
735                     new Object JavaDoc[]{fName, fBaseType.getName()}, complexContent);
736         }
737         
738         // -----------------------------------------------------------------------
739
// Skip over any potential annotations
740
// -----------------------------------------------------------------------
741
complexContent = DOMUtil.getFirstChildElement(complexContent);
742         
743         if (complexContent != null) {
744             // traverse annotation if any
745
if (DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
746                 addAnnotation(traverseAnnotationDecl(complexContent, derivationTypeAttrValues, false, schemaDoc));
747                 complexContent = DOMUtil.getNextSiblingElement(complexContent);
748             }
749             else {
750                 String JavaDoc text = DOMUtil.getSyntheticAnnotation(complexContent);
751                 if (text != null) {
752                     addAnnotation(traverseSyntheticAnnotation(complexContent, text, derivationTypeAttrValues, false, schemaDoc));
753                 }
754             }
755             if (complexContent !=null &&
756                     DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)){
757                 fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
758                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
759                 throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
760                         new Object JavaDoc[]{fName,SchemaSymbols.ELT_ANNOTATION}, complexContent);
761             }
762         }
763         else {
764             String JavaDoc text = DOMUtil.getSyntheticAnnotation(complexContent);
765             if (text != null) {
766                 addAnnotation(traverseSyntheticAnnotation(complexContent, text, derivationTypeAttrValues, false, schemaDoc));
767             }
768         }
769         // -----------------------------------------------------------------------
770
// Process the content. Note: should I try to catch any complexType errors
771
// here in order to return the attr array?
772
// -----------------------------------------------------------------------
773
try {
774             processComplexContent(complexContent, mixedContent, true, schemaDoc,
775                     grammar);
776         } catch (ComplexTypeRecoverableError e) {
777             fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
778             fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
779             throw e;
780         }
781         
782         // -----------------------------------------------------------------------
783
// Compose the final content and attribute uses
784
// -----------------------------------------------------------------------
785
XSParticleDecl baseContent = (XSParticleDecl)baseType.getParticle();
786         if (fDerivedBy==XSConstants.DERIVATION_RESTRICTION) {
787             
788             // This is an RESTRICTION
789

790             // N.B. derivation-ok-restriction.5.3 is checked under schema
791
// full checking. That's because we need to wait until locals are
792
// traversed so that occurrence information is correct.
793

794             
795             if (fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
796                     baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_MIXED) {
797                 fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
798                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
799                 throw new ComplexTypeRecoverableError("derivation-ok-restriction.5.4.1.2",
800                         new Object JavaDoc[]{fName, baseType.getName()},
801                         complexContent);
802             }
803             
804             try {
805                 mergeAttributes(baseType.getAttrGrp(), fAttrGrp, fName, false, complexContent);
806             } catch (ComplexTypeRecoverableError e) {
807                 fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
808                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
809                 throw e;
810             }
811             // Remove prohibited uses. Must be done after merge for RESTRICTION.
812
fAttrGrp.removeProhibitedAttrs();
813             
814             if (baseType != SchemaGrammar.fAnyType) {
815                 Object JavaDoc[] errArgs = fAttrGrp.validRestrictionOf(fName, baseType.getAttrGrp());
816                 if (errArgs != null) {
817                     fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
818                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
819                     throw new ComplexTypeRecoverableError((String JavaDoc)errArgs[errArgs.length-1],
820                             errArgs, complexContent);
821                 }
822             }
823         }
824         else {
825             
826             // This is an EXTENSION
827

828             // Create the particle
829
if (fParticle == null) {
830                 fContentType = baseType.getContentType();
831                 fXSSimpleType = (XSSimpleType)baseType.getSimpleType();
832                 fParticle = baseContent;
833             }
834             else if (baseType.getContentType() == XSComplexTypeDecl.CONTENTTYPE_EMPTY) {
835             }
836             else {
837                 //
838
// Check if the contentType of the base is consistent with the new type
839
// cos-ct-extends.1.4.3.2
840
if (fContentType == XSComplexTypeDecl.CONTENTTYPE_ELEMENT &&
841                         baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_ELEMENT) {
842                     fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
843                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
844                     throw new ComplexTypeRecoverableError("cos-ct-extends.1.4.3.2.2.1.a",
845                             new Object JavaDoc[]{fName}, complexContent);
846                 }
847                 else if (fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
848                         baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_MIXED) {
849                     fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
850                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
851                     throw new ComplexTypeRecoverableError("cos-ct-extends.1.4.3.2.2.1.b",
852                             new Object JavaDoc[]{fName}, complexContent);
853                 }
854                 
855                 // if the content of either type is an "all" model group, error.
856
if (fParticle.fType == XSParticleDecl.PARTICLE_MODELGROUP &&
857                         ((XSModelGroupImpl)fParticle.fValue).fCompositor == XSModelGroupImpl.MODELGROUP_ALL ||
858                         ((XSParticleDecl)baseType.getParticle()).fType == XSParticleDecl.PARTICLE_MODELGROUP &&
859                         ((XSModelGroupImpl)(((XSParticleDecl)baseType.getParticle())).fValue).fCompositor == XSModelGroupImpl.MODELGROUP_ALL) {
860                     fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
861                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
862                     throw new ComplexTypeRecoverableError("cos-all-limited.1.2",
863                             new Object JavaDoc[]{}, complexContent);
864                 }
865                 // the "sequence" model group to contain both particles
866
XSModelGroupImpl group = new XSModelGroupImpl();
867                 group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
868                 group.fParticleCount = 2;
869                 group.fParticles = new XSParticleDecl[2];
870                 group.fParticles[0] = (XSParticleDecl)baseType.getParticle();
871                 group.fParticles[1] = fParticle;
872                 // the particle to contain the above sequence
873
XSParticleDecl particle = new XSParticleDecl();
874                 particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
875                 particle.fValue = group;
876                 
877                 fParticle = particle;
878             }
879             
880             // Remove prohibited uses. Must be done before merge for EXTENSION.
881
fAttrGrp.removeProhibitedAttrs();
882             try {
883                 mergeAttributes(baseType.getAttrGrp(), fAttrGrp, fName, true, complexContent);
884             } catch (ComplexTypeRecoverableError e) {
885                 fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
886                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
887                 throw e;
888             }
889             
890         }
891         
892         // and *finally* we can legitimately return the attributes!
893
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
894         fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
895         
896     } // end of traverseComplexContent
897

898     
899     // This method merges attribute uses from the base, into the derived set.
900
// The first duplicate attribute, if any, is returned.
901
// LM: may want to merge with attributeGroup processing.
902
private void mergeAttributes(XSAttributeGroupDecl fromAttrGrp,
903             XSAttributeGroupDecl toAttrGrp,
904             String JavaDoc typeName,
905             boolean extension,
906             Element JavaDoc elem)
907     throws ComplexTypeRecoverableError {
908         
909         XSObjectList attrUseS = fromAttrGrp.getAttributeUses();
910         XSAttributeUseImpl duplicateAttrUse = null, oneAttrUse = null;
911         int attrCount = attrUseS.getLength();
912         for (int i=0; i<attrCount; i++) {
913             oneAttrUse = (XSAttributeUseImpl)attrUseS.item(i);
914             XSAttributeUse existingAttrUse = toAttrGrp.getAttributeUse(oneAttrUse.fAttrDecl.getNamespace(),
915                     oneAttrUse.fAttrDecl.getName());
916             if (existingAttrUse == null) {
917                 
918                 String JavaDoc idName = toAttrGrp.addAttributeUse(oneAttrUse);
919                 if (idName != null) {
920                     throw new ComplexTypeRecoverableError("ct-props-correct.5",
921                             new Object JavaDoc[]{typeName, idName, oneAttrUse.fAttrDecl.getName()},
922                             elem);
923                 }
924             }
925             else {
926                 if (extension) {
927                     throw new ComplexTypeRecoverableError("ct-props-correct.4",
928                             new Object JavaDoc[]{typeName, oneAttrUse.fAttrDecl.getName()},
929                             elem);
930                 }
931             }
932         }
933         // For extension, the wildcard must be formed by doing a union of the wildcards
934
if (extension) {
935             if (toAttrGrp.fAttributeWC==null) {
936                 toAttrGrp.fAttributeWC = fromAttrGrp.fAttributeWC;
937             }
938             else if (fromAttrGrp.fAttributeWC != null) {
939                 toAttrGrp.fAttributeWC = toAttrGrp.fAttributeWC.performUnionWith(fromAttrGrp.fAttributeWC, toAttrGrp.fAttributeWC.fProcessContents);
940             }
941             
942         }
943     }
944     
945     private void processComplexContent(Element JavaDoc complexContentChild,
946             boolean isMixed, boolean isDerivation,
947             XSDocumentInfo schemaDoc, SchemaGrammar grammar)
948     throws ComplexTypeRecoverableError {
949         
950         Element JavaDoc attrNode = null;
951         XSParticleDecl particle = null;
952         
953         // whether there is a particle with empty model group
954
boolean emptyParticle = false;
955         if (complexContentChild != null) {
956             // -------------------------------------------------------------
957
// GROUP, ALL, SEQUENCE or CHOICE, followed by attributes, if specified.
958
// Note that it's possible that only attributes are specified.
959
// -------------------------------------------------------------
960

961             
962             String JavaDoc childName = DOMUtil.getLocalName(complexContentChild);
963             
964             if (childName.equals(SchemaSymbols.ELT_GROUP)) {
965                 
966                 particle = fSchemaHandler.fGroupTraverser.traverseLocal(complexContentChild,
967                         schemaDoc, grammar);
968                 attrNode = DOMUtil.getNextSiblingElement(complexContentChild);
969             }
970             else if (childName.equals(SchemaSymbols.ELT_SEQUENCE)) {
971                 particle = traverseSequence(complexContentChild,schemaDoc,grammar,
972                         NOT_ALL_CONTEXT,fComplexTypeDecl);
973                 if (particle != null) {
974                     XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue;
975                     if (group.fParticleCount == 0)
976                         emptyParticle = true;
977                 }
978                 attrNode = DOMUtil.getNextSiblingElement(complexContentChild);
979             }
980             else if (childName.equals(SchemaSymbols.ELT_CHOICE)) {
981                 particle = traverseChoice(complexContentChild,schemaDoc,grammar,
982                         NOT_ALL_CONTEXT,fComplexTypeDecl);
983                 if (particle != null && particle.fMinOccurs == 0) {
984                     XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue;
985                     if (group.fParticleCount == 0)
986                         emptyParticle = true;
987                 }
988                 attrNode = DOMUtil.getNextSiblingElement(complexContentChild);
989             }
990             else if (childName.equals(SchemaSymbols.ELT_ALL)) {
991                 particle = traverseAll(complexContentChild,schemaDoc,grammar,
992                         PROCESSING_ALL_GP,fComplexTypeDecl);
993                 if (particle != null) {
994                     XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue;
995                     if (group.fParticleCount == 0)
996                         emptyParticle = true;
997                 }
998                 attrNode = DOMUtil.getNextSiblingElement(complexContentChild);
999             }
1000            else {
1001                // Should be attributes here - will check below...
1002
attrNode = complexContentChild;
1003            }
1004        }
1005        
1006        // if the particle is empty because there is no non-annotation chidren,
1007
// we need to make the particle itself null (so that the effective
1008
// content is empty).
1009
if (emptyParticle) {
1010            // get the first child
1011
Element JavaDoc child = DOMUtil.getFirstChildElement(complexContentChild);
1012            // if it's annotation, get the next one
1013
if (child != null) {
1014                if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
1015                    child = DOMUtil.getNextSiblingElement(child);
1016                }
1017            }
1018            // if there is no (non-annotation) children, mark particle empty
1019
if (child == null)
1020                particle = null;
1021            // child != null means we might have seen an element with
1022
// minOccurs == maxOccurs == 0
1023
}
1024        
1025        if (particle == null && isMixed) {
1026            if (fEmptyParticle == null) {
1027                XSModelGroupImpl group = new XSModelGroupImpl();
1028                group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
1029                group.fParticleCount = 0;
1030                group.fParticles = null;
1031                fEmptyParticle = new XSParticleDecl();
1032                fEmptyParticle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
1033                fEmptyParticle.fValue = group;
1034            }
1035            particle = fEmptyParticle;
1036        }
1037        fParticle = particle;
1038        
1039        // -----------------------------------------------------------------------
1040
// Set the content type
1041
// -----------------------------------------------------------------------
1042
if (fParticle == null)
1043            fContentType = XSComplexTypeDecl.CONTENTTYPE_EMPTY;
1044        else if (isMixed)
1045            fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
1046        else
1047            fContentType = XSComplexTypeDecl.CONTENTTYPE_ELEMENT;
1048        
1049        
1050        // -------------------------------------------------------------
1051
// Now, process attributes
1052
// -------------------------------------------------------------
1053
if (attrNode != null) {
1054            if (!isAttrOrAttrGroup(attrNode)) {
1055                throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
1056                        new Object JavaDoc[]{fName,DOMUtil.getLocalName(attrNode)},
1057                        attrNode);
1058            }
1059            Element JavaDoc node =
1060                traverseAttrsAndAttrGrps(attrNode,fAttrGrp,schemaDoc,grammar,fComplexTypeDecl);
1061            if (node!=null) {
1062                throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
1063                        new Object JavaDoc[]{fName,DOMUtil.getLocalName(node)},
1064                        node);
1065            }
1066            // Only remove prohibited attribute uses if this isn't a derived type
1067
// Derivation-specific code worries about this elsewhere
1068
if (!isDerivation) {
1069                fAttrGrp.removeProhibitedAttrs();
1070            }
1071        }
1072        
1073        
1074        
1075    } // end processComplexContent
1076

1077    
1078    private boolean isAttrOrAttrGroup(Element JavaDoc e) {
1079        String JavaDoc elementName = DOMUtil.getLocalName(e);
1080        
1081        if (elementName.equals(SchemaSymbols.ELT_ATTRIBUTE) ||
1082                elementName.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP) ||
1083                elementName.equals(SchemaSymbols.ELT_ANYATTRIBUTE))
1084            return true;
1085        else
1086            return false;
1087    }
1088    
1089    private void traverseSimpleContentDecl(Element JavaDoc simpleContentDecl) {
1090    }
1091    
1092    private void traverseComplexContentDecl(Element JavaDoc complexContentDecl,
1093            boolean mixedOnComplexTypeDecl) {
1094    }
1095    
1096    /*
1097     * Generate a name for an anonymous type
1098     */

1099    private String JavaDoc genAnonTypeName(Element JavaDoc complexTypeDecl) {
1100        
1101        // Generate a unique name for the anonymous type by concatenating together the
1102
// names of parent nodes
1103
// The name is quite good for debugging/error purposes, but we may want to
1104
// revisit how this is done for performance reasons (LM).
1105
StringBuffer JavaDoc typeName = new StringBuffer JavaDoc("#AnonType_");
1106        Element JavaDoc node = DOMUtil.getParent(complexTypeDecl);
1107        while (node != null && (node != DOMUtil.getRoot(DOMUtil.getDocument(node)))) {
1108            typeName.append(node.getAttribute(SchemaSymbols.ATT_NAME));
1109            node = DOMUtil.getParent(node);
1110        }
1111        return typeName.toString();
1112    }
1113    
1114    
1115    private void handleComplexTypeError(String JavaDoc messageId,Object JavaDoc[] args,
1116            Element JavaDoc e) {
1117        
1118        if (messageId!=null) {
1119            reportSchemaError(messageId, args, e);
1120        }
1121        
1122        //
1123
// Mock up the typeInfo structure so that there won't be problems during
1124
// validation
1125
//
1126
fBaseType = SchemaGrammar.fAnyType;
1127        fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
1128        fParticle = getErrorContent();
1129        // REVISIT: do we need to remove all attribute uses already added into
1130
// the attribute group? maybe it's ok to leave them there. -SG
1131
fAttrGrp.fAttributeWC = getErrorWildcard();
1132        
1133        return;
1134        
1135    }
1136    
1137    private XSParticleDecl getErrorContent() {
1138        XSParticleDecl particle = new XSParticleDecl();
1139        particle.fType = XSParticleDecl.PARTICLE_WILDCARD;
1140        particle.fValue = getErrorWildcard();
1141        particle.fMinOccurs = 0;
1142        particle.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
1143        XSModelGroupImpl group = new XSModelGroupImpl();
1144        group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
1145        group.fParticleCount = 1;
1146        group.fParticles = new XSParticleDecl[1];
1147        group.fParticles[0] = particle;
1148        XSParticleDecl errorContent = new XSParticleDecl();
1149        errorContent.fType = XSParticleDecl.PARTICLE_MODELGROUP;
1150        errorContent.fValue = group;
1151        
1152        return errorContent;
1153    }
1154    
1155    private XSWildcardDecl getErrorWildcard() {
1156        XSWildcardDecl errorWildcard = new XSWildcardDecl();
1157        errorWildcard.fProcessContents = XSWildcardDecl.PC_SKIP;
1158        return errorWildcard;
1159    }
1160    
1161    private void contentBackup() {
1162        if(fGlobalStore == null) {
1163            fGlobalStore = new Object JavaDoc [GLOBAL_NUM];
1164            fGlobalStorePos = 0;
1165        }
1166        if(fGlobalStorePos == fGlobalStore.length) {
1167            Object JavaDoc [] newArray = new Object JavaDoc[fGlobalStorePos+GLOBAL_NUM];
1168            System.arraycopy(fGlobalStore, 0, newArray, 0, fGlobalStorePos);
1169            fGlobalStore = newArray;
1170        }
1171        fGlobalStore[fGlobalStorePos++] = fComplexTypeDecl;
1172        fGlobalStore[fGlobalStorePos++] = fIsAbstract?Boolean.TRUE:Boolean.FALSE;
1173        fGlobalStore[fGlobalStorePos++] = fName ;
1174        fGlobalStore[fGlobalStorePos++] = fTargetNamespace;
1175        // let's save ourselves a couple of objects...
1176
fGlobalStore[fGlobalStorePos++] = new Integer JavaDoc((fDerivedBy << 16) + fFinal);
1177        fGlobalStore[fGlobalStorePos++] = new Integer JavaDoc((fBlock << 16) + fContentType);
1178        fGlobalStore[fGlobalStorePos++] = fBaseType;
1179        fGlobalStore[fGlobalStorePos++] = fAttrGrp;
1180        fGlobalStore[fGlobalStorePos++] = fParticle;
1181        fGlobalStore[fGlobalStorePos++] = fXSSimpleType;
1182        fGlobalStore[fGlobalStorePos++] = fAnnotations;
1183    }
1184    
1185    private void contentRestore() {
1186        fAnnotations = (XSAnnotationImpl [])fGlobalStore[--fGlobalStorePos];
1187        fXSSimpleType = (XSSimpleType)fGlobalStore[--fGlobalStorePos];
1188        fParticle = (XSParticleDecl)fGlobalStore[--fGlobalStorePos];
1189        fAttrGrp = (XSAttributeGroupDecl)fGlobalStore[--fGlobalStorePos];
1190        fBaseType = (XSTypeDefinition)fGlobalStore[--fGlobalStorePos];
1191        int i = ((Integer JavaDoc)(fGlobalStore[--fGlobalStorePos])).intValue();
1192        fBlock = (short)(i >> 16);
1193        fContentType = (short)i;
1194        i = ((Integer JavaDoc)(fGlobalStore[--fGlobalStorePos])).intValue();
1195        fDerivedBy = (short)(i >> 16);
1196        fFinal = (short)i;
1197        fTargetNamespace = (String JavaDoc)fGlobalStore[--fGlobalStorePos];
1198        fName = (String JavaDoc)fGlobalStore[--fGlobalStorePos];
1199        fIsAbstract = ((Boolean JavaDoc)fGlobalStore[--fGlobalStorePos]).booleanValue();
1200        fComplexTypeDecl = (XSComplexTypeDecl)fGlobalStore[--fGlobalStorePos];
1201    }
1202    
1203    private void addAnnotation(XSAnnotationImpl annotation) {
1204        if(annotation == null)
1205            return;
1206        // it isn't very likely that there will be more than one annotation
1207
// in a complexType decl. This saves us fromhaving to push/pop
1208
// one more object from the fGlobalStore, and that's bound
1209
// to be a savings for most applications
1210
if(fAnnotations == null) {
1211            fAnnotations = new XSAnnotationImpl[1];
1212        } else {
1213            XSAnnotationImpl [] tempArray = new XSAnnotationImpl[fAnnotations.length + 1];
1214            System.arraycopy(fAnnotations, 0, tempArray, 0, fAnnotations.length);
1215            fAnnotations = tempArray;
1216        }
1217        fAnnotations[fAnnotations.length-1] = annotation;
1218    }
1219}
1220
Popular Tags