KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package com.sun.org.apache.xerces.internal.impl.xs.traversers;
58
59 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeFacetException;
60 import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory;
61 import com.sun.org.apache.xerces.internal.impl.dv.XSFacets;
62 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
63 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
64 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
65 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
66 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeGroupDecl;
67 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeUseImpl;
68 import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl;
69 import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;
70 import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl;
71 import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;
72 import com.sun.org.apache.xerces.internal.impl.xs.XSWildcardDecl;
73 import com.sun.org.apache.xerces.internal.xs.XSAttributeUse;
74 import com.sun.org.apache.xerces.internal.xs.XSConstants;
75 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
76 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
77 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
78 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
79 import com.sun.org.apache.xerces.internal.util.DOMUtil;
80 import com.sun.org.apache.xerces.internal.xni.QName;
81 import org.w3c.dom.Element JavaDoc;
82
83 /**
84  * A complex type definition schema component traverser.
85  *
86  * <complexType
87  * abstract = boolean : false
88  * block = (#all | List of (extension | restriction))
89  * final = (#all | List of (extension | restriction))
90  * id = ID
91  * mixed = boolean : false
92  * name = NCName
93  * {any attributes with non-schema namespace . . .}>
94  * Content: (annotation?, (simpleContent | complexContent |
95  * ((group | all | choice | sequence)?,
96  * ((attribute | attributeGroup)*, anyAttribute?))))
97  * </complexType>
98  * @version $Id: XSDComplexTypeTraverser.java,v 1.41 2003/11/11 20:15:00 sandygao Exp $
99  */

100
101 class XSDComplexTypeTraverser extends XSDAbstractParticleTraverser {
102
103     // size of stack to hold globals:
104
private final static int GLOBAL_NUM = 11;
105
106     // globals for building XSComplexTypeDecls
107
private String JavaDoc fName = null;
108     private String JavaDoc fTargetNamespace = null;
109     private short fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
110     private short fFinal = XSConstants.DERIVATION_NONE;
111     private short fBlock = XSConstants.DERIVATION_NONE;
112     private short fContentType = XSComplexTypeDecl.CONTENTTYPE_EMPTY;
113     private XSTypeDefinition fBaseType = null;
114     private XSAttributeGroupDecl fAttrGrp = null;
115     private XSSimpleType fXSSimpleType = null;
116     private XSParticleDecl fParticle = null;
117     private boolean fIsAbstract = false;
118     private XSComplexTypeDecl fComplexTypeDecl = null;
119     private XSAnnotationImpl [] fAnnotations = null;
120
121     private XSParticleDecl fEmptyParticle = null;
122
123     // our own little stack to retain state when getGlobalDecls is called:
124
private Object JavaDoc [] fGlobalStore = null;
125     private int fGlobalStorePos = 0;
126
127     XSDComplexTypeTraverser (XSDHandler handler,
128                              XSAttributeChecker gAttrCheck) {
129         super(handler, gAttrCheck);
130     }
131
132
133     private static final boolean DEBUG=false;
134
135     private SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
136
137     private class ComplexTypeRecoverableError extends Exception JavaDoc {
138
139         Object JavaDoc[] errorSubstText=null;
140         Element JavaDoc errorElem = null;
141         ComplexTypeRecoverableError() {
142             super();
143         }
144         ComplexTypeRecoverableError(String JavaDoc msgKey, Object JavaDoc[] args, Element JavaDoc e) {
145             super(msgKey);
146             errorSubstText=args;
147             errorElem = e;
148         }
149
150     }
151
152     /**
153      * Traverse local complexType declarations
154      *
155      * @param Element
156      * @param XSDocumentInfo
157      * @param SchemaGrammar
158      * @return XSComplexTypeDecl
159      */

160     XSComplexTypeDecl traverseLocal(Element JavaDoc complexTypeNode,
161                                     XSDocumentInfo schemaDoc,
162                                     SchemaGrammar grammar) {
163
164
165         Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, false,
166                                                            schemaDoc);
167         String JavaDoc complexTypeName = genAnonTypeName(complexTypeNode);
168         contentBackup();
169         XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode,
170                                                           complexTypeName, attrValues, schemaDoc, grammar);
171         contentRestore();
172         // need to add the type to the grammar for later constraint checking
173
grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode));
174         type.setIsAnonymous();
175         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
176
177         return type;
178     }
179
180     /**
181      * Traverse global complexType declarations
182      *
183      * @param Element
184      * @param XSDocumentInfo
185      * @param SchemaGrammar
186      * @return XSComplexTypeDecXSComplexTypeDecl
187      */

188     XSComplexTypeDecl traverseGlobal (Element JavaDoc complexTypeNode,
189                                       XSDocumentInfo schemaDoc,
190                                       SchemaGrammar grammar) {
191
192         Object JavaDoc[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, true,
193                                                            schemaDoc);
194         String JavaDoc complexTypeName = (String JavaDoc) attrValues[XSAttributeChecker.ATTIDX_NAME];
195         contentBackup();
196         XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode,
197                                                           complexTypeName, attrValues, schemaDoc, grammar);
198         contentRestore();
199         if (complexTypeName == null) {
200             reportSchemaError("s4s-att-must-appear", new Object JavaDoc[]{SchemaSymbols.ELT_COMPLEXTYPE, SchemaSymbols.ATT_NAME}, complexTypeNode);
201         } else {
202             grammar.addGlobalTypeDecl(type);
203         }
204         // need to add the type to the grammar for later constraint checking
205
grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode));
206         fAttrChecker.returnAttrArray(attrValues, schemaDoc);
207
208         return type;
209     }
210
211
212     private XSComplexTypeDecl traverseComplexTypeDecl(Element JavaDoc complexTypeDecl,
213                                                       String JavaDoc complexTypeName,
214                                                       Object JavaDoc[] attrValues,
215                                                       XSDocumentInfo schemaDoc,
216                                                       SchemaGrammar grammar) {
217
218         fComplexTypeDecl = new XSComplexTypeDecl();
219         fAttrGrp = new XSAttributeGroupDecl();
220         Boolean JavaDoc abstractAtt = (Boolean JavaDoc) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT];
221         XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK];
222         Boolean JavaDoc mixedAtt = (Boolean JavaDoc) attrValues[XSAttributeChecker.ATTIDX_MIXED];
223         XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL];
224
225         fName = complexTypeName;
226         fComplexTypeDecl.setName(fName);
227         fTargetNamespace = schemaDoc.fTargetNamespace;
228
229         fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue();
230         fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue();
231         //discard valid Block/Final 'Default' values that are invalid for Block/Final
232
fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
233         fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
234
235         if (abstractAtt != null && abstractAtt.booleanValue())
236             fIsAbstract = true;
237
238
239         Element JavaDoc child = null;
240
241         try {
242             // ---------------------------------------------------------------
243
// First, handle any ANNOTATION declaration and get next child
244
// ---------------------------------------------------------------
245
child = DOMUtil.getFirstChildElement(complexTypeDecl);
246
247             if (child != null) {
248                 // traverse annotation if any
249
if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
250                     addAnnotation(traverseAnnotationDecl(child, attrValues, false, schemaDoc));
251                     child = DOMUtil.getNextSiblingElement(child);
252                 }
253                 if (child !=null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
254                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
255                            new Object JavaDoc[]{fName,SchemaSymbols.ELT_ANNOTATION},
256                            child);
257                 }
258             }
259             // ---------------------------------------------------------------
260
// Process the content of the complex type definition
261
// ---------------------------------------------------------------
262
if (child==null) {
263                 //
264
// EMPTY complexType with complexContent
265
//
266

267                 // set the base to the anyType
268
fBaseType = SchemaGrammar.fAnyType;
269                 processComplexContent(child, mixedAtt.booleanValue(), false,
270                                       schemaDoc, grammar);
271             }
272             else if (DOMUtil.getLocalName(child).equals
273                      (SchemaSymbols.ELT_SIMPLECONTENT)) {
274                 //
275
// SIMPLE CONTENT
276
//
277
traverseSimpleContent(child, schemaDoc, grammar);
278                 Element JavaDoc elemTmp = DOMUtil.getNextSiblingElement(child);
279                 if (elemTmp != null) {
280                     String JavaDoc siblingName = DOMUtil.getLocalName(elemTmp);
281                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
282                                                           new Object JavaDoc[]{fName,siblingName},
283                                                           elemTmp);
284                 }
285             }
286             else if (DOMUtil.getLocalName(child).equals
287                      (SchemaSymbols.ELT_COMPLEXCONTENT)) {
288                 traverseComplexContent(child, mixedAtt.booleanValue(),
289                                        schemaDoc, grammar);
290                 Element JavaDoc elemTmp = DOMUtil.getNextSiblingElement(child);
291                 if (elemTmp != null) {
292                     String JavaDoc siblingName = DOMUtil.getLocalName(elemTmp);
293                     throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
294                                                           new Object JavaDoc[]{fName,siblingName},
295                                                           elemTmp);
296                 }
297             }
298             else {
299                 //
300
// We must have ....
301
// GROUP, ALL, SEQUENCE or CHOICE, followed by optional attributes
302
// Note that it's possible that only attributes are specified.
303
//
304

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

465             if (DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
466                 addAnnotation(traverseAnnotationDecl(simpleContent, derivationTypeAttrValues, false, schemaDoc));
467                 simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
468             }
469
470             if (simpleContent !=null &&
471                 DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)){
472                 fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
473                 fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
474                 throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
475                        new Object JavaDoc[]{fName,SchemaSymbols.ELT_ANNOTATION},
476                        simpleContent);
477             }
478         }
479
480         // -----------------------------------------------------------------------
481
// Process a RESTRICTION
482
// -----------------------------------------------------------------------
483
if (fDerivedBy == XSConstants.DERIVATION_RESTRICTION) {
484
485             // -----------------------------------------------------------------------
486
// There may be a simple type definition in the restriction element
487
// The data type validator will be based on it, if specified
488
// -----------------------------------------------------------------------
489
if (simpleContent !=null &&
490             DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_SIMPLETYPE )) {
491
492                 XSSimpleType dv = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(
493                       simpleContent, schemaDoc, grammar);
494                 if (dv == null) {
495                     fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
496                     fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
497                     throw new ComplexTypeRecoverableError();
498                 }
499                 //check that this datatype validator is validly derived from the base
500
//according to derivation-ok-restriction 5.1.2.1
501

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

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

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

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

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

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

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

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