2
* The Apache Software License, Version 1.1
5
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
48
* ====================================================================
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/>.
2
* Copyright 2001-2005 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
57
16
package org.apache.xerces.impl.xs.traversers;
70
29
import org.apache.xerces.impl.xs.XSModelGroupImpl;
71
30
import org.apache.xerces.impl.xs.XSParticleDecl;
72
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;
73
36
import org.apache.xerces.xs.XSAttributeUse;
74
37
import org.apache.xerces.xs.XSConstants;
75
38
import org.apache.xerces.xs.XSObjectList;
76
39
import org.apache.xerces.xs.XSTypeDefinition;
77
import org.apache.xerces.impl.xs.util.XInt;
78
import org.apache.xerces.impl.xs.util.XSObjectListImpl;
79
import org.apache.xerces.util.DOMUtil;
80
import org.apache.xerces.xni.QName;
81
40
import org.w3c.dom.Element;
95
54
* ((group | all | choice | sequence)?,
96
55
* ((attribute | attributeGroup)*, anyAttribute?))))
98
* @version $Id: XSDComplexTypeTraverser.java,v 1.41 2003/11/11 20:15:00 sandygao Exp $
60
* @version $Id: XSDComplexTypeTraverser.java 383214 2006-03-04 22:20:48Z nddelima $
101
63
class XSDComplexTypeTraverser extends XSDAbstractParticleTraverser {
103
65
// size of stack to hold globals:
104
66
private final static int GLOBAL_NUM = 11;
106
68
// globals for building XSComplexTypeDecls
107
69
private String fName = null;
108
70
private String fTargetNamespace = null;
117
79
private boolean fIsAbstract = false;
118
80
private XSComplexTypeDecl fComplexTypeDecl = null;
119
81
private XSAnnotationImpl [] fAnnotations = null;
121
83
private XSParticleDecl fEmptyParticle = null;
123
85
// our own little stack to retain state when getGlobalDecls is called:
124
86
private Object [] fGlobalStore = null;
125
87
private int fGlobalStorePos = 0;
127
89
XSDComplexTypeTraverser (XSDHandler handler,
128
XSAttributeChecker gAttrCheck) {
90
XSAttributeChecker gAttrCheck) {
129
91
super(handler, gAttrCheck);
133
95
private static final boolean DEBUG=false;
135
97
private SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
137
99
private class ComplexTypeRecoverableError extends Exception {
101
private static final long serialVersionUID = 6802729912091130335L;
139
103
Object[] errorSubstText=null;
140
104
Element errorElem = null;
141
105
ComplexTypeRecoverableError() {
158
122
* @return XSComplexTypeDecl
160
124
XSComplexTypeDecl traverseLocal(Element complexTypeNode,
161
XSDocumentInfo schemaDoc,
162
SchemaGrammar grammar) {
125
XSDocumentInfo schemaDoc,
126
SchemaGrammar grammar) {
165
129
Object[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, false,
167
131
String complexTypeName = genAnonTypeName(complexTypeNode);
169
133
XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode,
170
complexTypeName, attrValues, schemaDoc, grammar);
134
complexTypeName, attrValues, schemaDoc, grammar);
171
135
contentRestore();
172
136
// need to add the type to the grammar for later constraint checking
173
137
grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode));
174
138
type.setIsAnonymous();
175
139
fAttrChecker.returnAttrArray(attrValues, schemaDoc);
181
145
* Traverse global complexType declarations
186
150
* @return XSComplexTypeDecXSComplexTypeDecl
188
152
XSComplexTypeDecl traverseGlobal (Element complexTypeNode,
189
XSDocumentInfo schemaDoc,
190
SchemaGrammar grammar) {
153
XSDocumentInfo schemaDoc,
154
SchemaGrammar grammar) {
192
156
Object[] attrValues = fAttrChecker.checkAttributes(complexTypeNode, true,
194
158
String complexTypeName = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
196
160
XSComplexTypeDecl type = traverseComplexTypeDecl (complexTypeNode,
197
complexTypeName, attrValues, schemaDoc, grammar);
161
complexTypeName, attrValues, schemaDoc, grammar);
198
162
contentRestore();
199
163
if (complexTypeName == null) {
200
164
reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_COMPLEXTYPE, SchemaSymbols.ATT_NAME}, complexTypeNode);
204
168
// need to add the type to the grammar for later constraint checking
205
169
grammar.addComplexTypeDecl(type, fSchemaHandler.element2Locator(complexTypeNode));
206
170
fAttrChecker.returnAttrArray(attrValues, schemaDoc);
212
176
private XSComplexTypeDecl traverseComplexTypeDecl(Element complexTypeDecl,
213
String complexTypeName,
215
XSDocumentInfo schemaDoc,
216
SchemaGrammar grammar) {
177
String complexTypeName,
179
XSDocumentInfo schemaDoc,
180
SchemaGrammar grammar) {
218
182
fComplexTypeDecl = new XSComplexTypeDecl();
219
183
fAttrGrp = new XSAttributeGroupDecl();
220
184
Boolean abstractAtt = (Boolean) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT];
221
185
XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK];
222
186
Boolean mixedAtt = (Boolean) attrValues[XSAttributeChecker.ATTIDX_MIXED];
223
187
XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL];
225
189
fName = complexTypeName;
226
190
fComplexTypeDecl.setName(fName);
227
191
fTargetNamespace = schemaDoc.fTargetNamespace;
229
193
fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue();
230
194
fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue();
231
195
//discard valid Block/Final 'Default' values that are invalid for Block/Final
232
196
fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
233
197
fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
235
if (abstractAtt != null && abstractAtt.booleanValue())
199
fIsAbstract = (abstractAtt != null && abstractAtt.booleanValue());
239
202
Element child = null;
242
205
// ---------------------------------------------------------------
243
206
// First, handle any ANNOTATION declaration and get next child
244
207
// ---------------------------------------------------------------
245
208
child = DOMUtil.getFirstChildElement(complexTypeDecl);
248
// traverse annotation if any
249
210
if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
250
211
addAnnotation(traverseAnnotationDecl(child, attrValues, false, schemaDoc));
251
212
child = DOMUtil.getNextSiblingElement(child);
215
String text = DOMUtil.getSyntheticAnnotation(complexTypeDecl);
217
addAnnotation(traverseSyntheticAnnotation(complexTypeDecl, text, attrValues, false, schemaDoc));
253
220
if (child !=null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
254
221
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
255
new Object[]{fName,SchemaSymbols.ELT_ANNOTATION},
222
new Object[]{fName,SchemaSymbols.ELT_ANNOTATION},
227
String text = DOMUtil.getSyntheticAnnotation(complexTypeDecl);
229
addAnnotation(traverseSyntheticAnnotation(complexTypeDecl, text, attrValues, false, schemaDoc));
259
232
// ---------------------------------------------------------------
279
252
if (elemTmp != null) {
280
253
String siblingName = DOMUtil.getLocalName(elemTmp);
281
254
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
282
new Object[]{fName,siblingName},
255
new Object[]{fName,siblingName},
286
259
else if (DOMUtil.getLocalName(child).equals
287
(SchemaSymbols.ELT_COMPLEXCONTENT)) {
260
(SchemaSymbols.ELT_COMPLEXCONTENT)) {
288
261
traverseComplexContent(child, mixedAtt.booleanValue(),
290
263
Element elemTmp = DOMUtil.getNextSiblingElement(child);
291
264
if (elemTmp != null) {
292
265
String siblingName = DOMUtil.getLocalName(elemTmp);
293
266
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
294
new Object[]{fName,siblingName},
267
new Object[]{fName,siblingName},
301
274
// GROUP, ALL, SEQUENCE or CHOICE, followed by optional attributes
302
275
// Note that it's possible that only attributes are specified.
305
278
// set the base to the anyType
306
279
fBaseType = SchemaGrammar.fAnyType;
307
280
processComplexContent(child, mixedAtt.booleanValue(), false,
312
285
catch (ComplexTypeRecoverableError e) {
313
286
handleComplexTypeError(e.getMessage(), e.errorSubstText,
318
291
System.out.println(fName);
320
293
fComplexTypeDecl.setValues(fName, fTargetNamespace, fBaseType,
321
294
fDerivedBy, fFinal, fBlock, fContentType, fIsAbstract,
322
295
fAttrGrp, fXSSimpleType, fParticle, new XSObjectListImpl(fAnnotations,
323
fAnnotations == null? 0 : fAnnotations.length));
296
fAnnotations == null? 0 : fAnnotations.length));
324
297
return fComplexTypeDecl;
328
301
private void traverseSimpleContent(Element simpleContentElement,
329
XSDocumentInfo schemaDoc,
330
SchemaGrammar grammar)
302
XSDocumentInfo schemaDoc,
303
SchemaGrammar grammar)
331
304
throws ComplexTypeRecoverableError {
334
307
Object[] simpleContentAttrValues = fAttrChecker.checkAttributes(simpleContentElement, false,
337
310
// -----------------------------------------------------------------------
338
311
// Set content type
339
312
// -----------------------------------------------------------------------
340
313
fContentType = XSComplexTypeDecl.CONTENTTYPE_SIMPLE;
341
314
fParticle = null;
343
316
Element 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);
317
if (simpleContent != null && DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
318
addAnnotation(traverseAnnotationDecl(simpleContent, simpleContentAttrValues, false, schemaDoc));
319
simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
322
String text = DOMUtil.getSyntheticAnnotation(simpleContentElement);
324
addAnnotation(traverseSyntheticAnnotation(simpleContentElement, text, simpleContentAttrValues, false, schemaDoc));
352
328
// If there are no children, return
353
329
if (simpleContent==null) {
354
330
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
355
331
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.2",
356
new Object[]{fName,SchemaSymbols.ELT_SIMPLECONTENT},
357
simpleContentElement);
332
new Object[]{fName,SchemaSymbols.ELT_SIMPLECONTENT},
333
simpleContentElement);
360
336
// -----------------------------------------------------------------------
361
337
// The content should be either "restriction" or "extension"
362
338
// -----------------------------------------------------------------------
369
345
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
370
346
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
371
new Object[]{fName,simpleContentName},
347
new Object[]{fName,simpleContentName},
374
350
Element elemTmp = DOMUtil.getNextSiblingElement(simpleContent);
375
351
if (elemTmp != null) {
376
352
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
377
353
String siblingName = DOMUtil.getLocalName(elemTmp);
378
354
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
379
new Object[]{fName,siblingName},
355
new Object[]{fName,siblingName},
383
359
Object [] derivationTypeAttrValues = fAttrChecker.checkAttributes(simpleContent, false,
385
361
QName baseTypeName = (QName) derivationTypeAttrValues[XSAttributeChecker.ATTIDX_BASE];
388
364
// -----------------------------------------------------------------------
389
365
// Need a base type.
390
366
// -----------------------------------------------------------------------
392
368
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
393
369
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
394
370
throw new ComplexTypeRecoverableError("s4s-att-must-appear",
395
new Object[]{simpleContentName, "base"}, simpleContent);
371
new Object[]{simpleContentName, "base"}, simpleContent);
398
374
XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc,
399
XSDHandler.TYPEDECL_TYPE, baseTypeName,
375
XSDHandler.TYPEDECL_TYPE, baseTypeName,
401
377
if (type==null) {
402
378
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
403
379
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
404
380
throw new ComplexTypeRecoverableError();
407
383
fBaseType = type;
409
385
XSSimpleType baseValidator = null;
410
386
XSComplexTypeDecl baseComplexType = null;
411
387
int baseFinalSet = 0;
413
389
// If the base type is complex, it must have simpleContent
414
390
if ((type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE)) {
416
392
baseComplexType = (XSComplexTypeDecl)type;
417
393
baseFinalSet = baseComplexType.getFinal();
418
394
// base is a CT with simple content (both restriction and extension are OK)
422
398
// base is a CT with mixed/emptiable content (only restriction is OK)
423
399
else if (fDerivedBy == XSConstants.DERIVATION_RESTRICTION &&
424
baseComplexType.getContentType() == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
425
((XSParticleDecl)baseComplexType.getParticle()).emptiable()) {
400
baseComplexType.getContentType() == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
401
((XSParticleDecl)baseComplexType.getParticle()).emptiable()) {
428
404
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
429
405
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
430
406
throw new ComplexTypeRecoverableError("src-ct.2.1",
431
new Object[]{fName, baseComplexType.getName()}, simpleContent);
407
new Object[]{fName, baseComplexType.getName()}, simpleContent);
438
414
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
439
415
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
440
416
throw new ComplexTypeRecoverableError("src-ct.2.1",
441
new Object[]{fName, baseValidator.getName()}, simpleContent);
417
new Object[]{fName, baseValidator.getName()}, simpleContent);
443
419
baseFinalSet=baseValidator.getFinal();
446
422
// -----------------------------------------------------------------------
447
423
// Check that the base permits the derivation
448
424
// -----------------------------------------------------------------------
450
426
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
451
427
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
452
428
String errorKey = (fDerivedBy==XSConstants.DERIVATION_EXTENSION) ?
453
"cos-ct-extends.1.1" : "derivation-ok-restriction.1";
429
"cos-ct-extends.1.1" : "derivation-ok-restriction.1";
454
430
throw new ComplexTypeRecoverableError(errorKey,
455
new Object[]{fName, fBaseType.getName()}, simpleContent);
431
new Object[]{fName, fBaseType.getName()}, simpleContent);
458
434
// -----------------------------------------------------------------------
459
435
// Skip over any potential annotations
460
436
// -----------------------------------------------------------------------
437
Element scElement = simpleContent;
461
438
simpleContent = DOMUtil.getFirstChildElement(simpleContent);
462
439
if (simpleContent != null) {
463
440
// traverse annotation if any
465
442
if (DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
466
443
addAnnotation(traverseAnnotationDecl(simpleContent, derivationTypeAttrValues, false, schemaDoc));
467
444
simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
447
String text = DOMUtil.getSyntheticAnnotation(scElement);
449
addAnnotation(traverseSyntheticAnnotation(scElement, text, derivationTypeAttrValues, false, schemaDoc));
470
453
if (simpleContent !=null &&
471
DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)){
454
DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_ANNOTATION)){
472
455
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
473
456
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
474
457
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
475
new Object[]{fName,SchemaSymbols.ELT_ANNOTATION},
458
new Object[]{fName,SchemaSymbols.ELT_ANNOTATION},
463
String text = DOMUtil.getSyntheticAnnotation(scElement);
465
addAnnotation(traverseSyntheticAnnotation(scElement, text, derivationTypeAttrValues, false, schemaDoc));
480
469
// -----------------------------------------------------------------------
481
470
// Process a RESTRICTION
482
471
// -----------------------------------------------------------------------
483
472
if (fDerivedBy == XSConstants.DERIVATION_RESTRICTION) {
485
474
// -----------------------------------------------------------------------
486
475
// There may be a simple type definition in the restriction element
487
476
// The data type validator will be based on it, if specified
488
477
// -----------------------------------------------------------------------
489
478
if (simpleContent !=null &&
490
DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_SIMPLETYPE )) {
479
DOMUtil.getLocalName(simpleContent).equals(SchemaSymbols.ELT_SIMPLETYPE )) {
492
481
XSSimpleType dv = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(
493
simpleContent, schemaDoc, grammar);
482
simpleContent, schemaDoc, grammar);
494
483
if (dv == null) {
495
484
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
496
485
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
499
488
//check that this datatype validator is validly derived from the base
500
489
//according to derivation-ok-restriction 5.1.2.1
502
491
if (baseValidator != null &&
503
!XSConstraints.checkSimpleDerivationOk(dv, baseValidator,
504
baseValidator.getFinal())) {
492
!XSConstraints.checkSimpleDerivationOk(dv, baseValidator,
493
baseValidator.getFinal())) {
505
494
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
506
495
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
507
496
throw new ComplexTypeRecoverableError("derivation-ok-restriction.5.2.2.1",
508
new Object[]{fName, dv.getName(), baseValidator.getName()},
497
new Object[]{fName, dv.getName(), baseValidator.getName()},
511
500
baseValidator = dv;
512
501
simpleContent = DOMUtil.getNextSiblingElement(simpleContent);
515
504
// this only happens when restricting a mixed/emptiable CT
516
505
// but there is no <simpleType>, which is required
517
506
if (baseValidator == null) {
518
507
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
519
508
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
520
509
throw new ComplexTypeRecoverableError("src-ct.2.2",
521
new Object[]{fName}, simpleContent);
510
new Object[]{fName}, simpleContent);
524
513
// -----------------------------------------------------------------------
525
514
// Traverse any facets
526
515
// -----------------------------------------------------------------------
553
542
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
554
543
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
555
544
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
556
new Object[]{fName,DOMUtil.getLocalName(attrNode)},
545
new Object[]{fName,DOMUtil.getLocalName(attrNode)},
559
548
Element node=traverseAttrsAndAttrGrps(attrNode,fAttrGrp,
560
schemaDoc,grammar,fComplexTypeDecl);
549
schemaDoc,grammar,fComplexTypeDecl);
561
550
if (node!=null) {
562
551
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
563
552
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
564
553
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
565
new Object[]{fName,DOMUtil.getLocalName(node)},
554
new Object[]{fName,DOMUtil.getLocalName(node)},
571
560
mergeAttributes(baseComplexType.getAttrGrp(), fAttrGrp, fName, false, simpleContentElement);
572
561
} catch (ComplexTypeRecoverableError e) {
600
589
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
601
590
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
602
591
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
603
new Object[]{fName,DOMUtil.getLocalName(attrNode)},
592
new Object[]{fName,DOMUtil.getLocalName(attrNode)},
606
595
Element node=traverseAttrsAndAttrGrps(attrNode,fAttrGrp,
607
schemaDoc,grammar,fComplexTypeDecl);
596
schemaDoc,grammar,fComplexTypeDecl);
609
598
if (node!=null) {
610
599
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
611
600
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
612
601
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
613
new Object[]{fName,DOMUtil.getLocalName(node)},
602
new Object[]{fName,DOMUtil.getLocalName(node)},
616
605
// Remove prohibited uses. Should be done prior to any merge.
617
606
fAttrGrp.removeProhibitedAttrs();
620
609
if (baseComplexType != null) {
622
611
mergeAttributes(baseComplexType.getAttrGrp(), fAttrGrp, fName, true, simpleContentElement);
632
621
fAttrChecker.returnAttrArray(simpleContentAttrValues, schemaDoc);
633
622
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
636
625
private void traverseComplexContent(Element complexContentElement,
637
boolean mixedOnType, XSDocumentInfo schemaDoc,
638
SchemaGrammar grammar)
626
boolean mixedOnType, XSDocumentInfo schemaDoc,
627
SchemaGrammar grammar)
639
628
throws ComplexTypeRecoverableError {
642
631
Object[] complexContentAttrValues = fAttrChecker.checkAttributes(complexContentElement, false,
646
635
// -----------------------------------------------------------------------
647
636
// Determine if this is mixed content
648
637
// -----------------------------------------------------------------------
651
640
if (mixedAtt != null) {
652
641
mixedContent = mixedAtt.booleanValue();
656
645
// -----------------------------------------------------------------------
657
646
// Since the type must have complex content, set the simple type validators
659
648
// -----------------------------------------------------------------------
660
649
fXSSimpleType = null;
662
651
Element 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);
652
if (complexContent != null && DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
653
addAnnotation(traverseAnnotationDecl(complexContent, complexContentAttrValues, false, schemaDoc));
654
complexContent = DOMUtil.getNextSiblingElement(complexContent);
657
String text = DOMUtil.getSyntheticAnnotation(complexContentElement);
659
addAnnotation(traverseSyntheticAnnotation(complexContentElement, text, complexContentAttrValues, false, schemaDoc));
671
663
// If there are no children, return
672
664
if (complexContent==null) {
673
665
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
674
666
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.2",
675
new Object[]{fName,SchemaSymbols.ELT_COMPLEXCONTENT},
676
complexContentElement);
667
new Object[]{fName,SchemaSymbols.ELT_COMPLEXCONTENT},
668
complexContentElement);
679
671
// -----------------------------------------------------------------------
680
672
// The content should be either "restriction" or "extension"
681
673
// -----------------------------------------------------------------------
688
680
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
689
681
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
690
new Object[]{fName, complexContentName}, complexContent);
682
new Object[]{fName, complexContentName}, complexContent);
692
684
Element elemTmp = DOMUtil.getNextSiblingElement(complexContent);
693
685
if (elemTmp != null) {
694
686
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
695
687
String siblingName = DOMUtil.getLocalName(elemTmp);
696
688
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
697
new Object[]{fName, siblingName}, elemTmp);
689
new Object[]{fName, siblingName}, elemTmp);
700
692
Object[] derivationTypeAttrValues = fAttrChecker.checkAttributes(complexContent, false,
702
694
QName baseTypeName = (QName) derivationTypeAttrValues[XSAttributeChecker.ATTIDX_BASE];
705
697
// -----------------------------------------------------------------------
706
698
// Need a base type. Check that it's a complex type
707
699
// -----------------------------------------------------------------------
709
701
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
710
702
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
711
703
throw new ComplexTypeRecoverableError("s4s-att-must-appear",
712
new Object[]{complexContentName, "base"}, complexContent);
704
new Object[]{complexContentName, "base"}, complexContent);
715
707
XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc,
716
XSDHandler.TYPEDECL_TYPE,
708
XSDHandler.TYPEDECL_TYPE,
720
712
if (type==null) {
721
713
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
722
714
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
723
715
throw new ComplexTypeRecoverableError();
726
718
if (! (type instanceof XSComplexTypeDecl)) {
727
719
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
728
720
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
729
721
throw new ComplexTypeRecoverableError("src-ct.1",
730
new Object[]{fName, type.getName()}, complexContent);
722
new Object[]{fName, type.getName()}, complexContent);
732
724
XSComplexTypeDecl baseType = (XSComplexTypeDecl)type;
733
725
fBaseType = baseType;
735
727
// -----------------------------------------------------------------------
736
728
// Check that the base permits the derivation
737
729
// -----------------------------------------------------------------------
739
731
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
740
732
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
741
733
String errorKey = (fDerivedBy==XSConstants.DERIVATION_EXTENSION) ?
742
"cos-ct-extends.1.1" : "derivation-ok-restriction.1";
734
"cos-ct-extends.1.1" : "derivation-ok-restriction.1";
743
735
throw new ComplexTypeRecoverableError(errorKey,
744
new Object[]{fName, fBaseType.getName()}, complexContent);
736
new Object[]{fName, fBaseType.getName()}, complexContent);
747
739
// -----------------------------------------------------------------------
748
740
// Skip over any potential annotations
749
741
// -----------------------------------------------------------------------
750
742
complexContent = DOMUtil.getFirstChildElement(complexContent);
752
744
if (complexContent != null) {
753
745
// traverse annotation if any
754
746
if (DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)) {
755
747
addAnnotation(traverseAnnotationDecl(complexContent, derivationTypeAttrValues, false, schemaDoc));
756
748
complexContent = DOMUtil.getNextSiblingElement(complexContent);
751
String text = DOMUtil.getSyntheticAnnotation(complexContent);
753
addAnnotation(traverseSyntheticAnnotation(complexContent, text, derivationTypeAttrValues, false, schemaDoc));
758
756
if (complexContent !=null &&
759
DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)){
757
DOMUtil.getLocalName(complexContent).equals(SchemaSymbols.ELT_ANNOTATION)){
760
758
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
761
759
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
762
760
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
763
new Object[]{fName,SchemaSymbols.ELT_ANNOTATION}, complexContent);
761
new Object[]{fName,SchemaSymbols.ELT_ANNOTATION}, complexContent);
765
String text = DOMUtil.getSyntheticAnnotation(complexContent);
767
addAnnotation(traverseSyntheticAnnotation(complexContent, text, derivationTypeAttrValues, false, schemaDoc));
766
770
// -----------------------------------------------------------------------
769
773
// -----------------------------------------------------------------------
771
775
processComplexContent(complexContent, mixedContent, true, schemaDoc,
773
777
} catch (ComplexTypeRecoverableError e) {
774
778
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
775
779
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
779
783
// -----------------------------------------------------------------------
780
784
// Compose the final content and attribute uses
781
785
// -----------------------------------------------------------------------
782
786
XSParticleDecl baseContent = (XSParticleDecl)baseType.getParticle();
783
787
if (fDerivedBy==XSConstants.DERIVATION_RESTRICTION) {
785
789
// This is an RESTRICTION
787
791
// N.B. derivation-ok-restriction.5.3 is checked under schema
788
792
// full checking. That's because we need to wait until locals are
789
793
// traversed so that occurrence information is correct.
792
796
if (fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
793
baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_MIXED) {
797
baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_MIXED) {
794
798
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
795
799
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
796
800
throw new ComplexTypeRecoverableError("derivation-ok-restriction.5.4.1.2",
797
new Object[]{fName, baseType.getName()},
801
new Object[]{fName, baseType.getName()},
802
806
mergeAttributes(baseType.getAttrGrp(), fAttrGrp, fName, false, complexContent);
803
807
} catch (ComplexTypeRecoverableError e) {
808
812
// Remove prohibited uses. Must be done after merge for RESTRICTION.
809
813
fAttrGrp.removeProhibitedAttrs();
811
815
if (baseType != SchemaGrammar.fAnyType) {
812
816
Object[] errArgs = fAttrGrp.validRestrictionOf(fName, baseType.getAttrGrp());
813
817
if (errArgs != null) {
814
818
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
815
819
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
816
820
throw new ComplexTypeRecoverableError((String)errArgs[errArgs.length-1],
817
errArgs, complexContent);
821
errArgs, complexContent);
823
827
// This is an EXTENSION
825
829
// Create the particle
826
830
if (fParticle == null) {
827
831
fContentType = baseType.getContentType();
835
839
// Check if the contentType of the base is consistent with the new type
836
840
// cos-ct-extends.1.4.3.2
837
841
if (fContentType == XSComplexTypeDecl.CONTENTTYPE_ELEMENT &&
838
baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_ELEMENT) {
842
baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_ELEMENT) {
839
843
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
840
844
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
841
845
throw new ComplexTypeRecoverableError("cos-ct-extends.1.4.3.2.2.1.a",
842
846
new Object[]{fName}, complexContent);
844
848
else if (fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED &&
845
baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_MIXED) {
849
baseType.getContentType() != XSComplexTypeDecl.CONTENTTYPE_MIXED) {
846
850
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
847
851
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
848
852
throw new ComplexTypeRecoverableError("cos-ct-extends.1.4.3.2.2.1.b",
849
853
new Object[]{fName}, complexContent);
852
856
// if the content of either type is an "all" model group, error.
853
857
if (fParticle.fType == XSParticleDecl.PARTICLE_MODELGROUP &&
854
858
((XSModelGroupImpl)fParticle.fValue).fCompositor == XSModelGroupImpl.MODELGROUP_ALL ||
866
870
group.fParticles = new XSParticleDecl[2];
867
871
group.fParticles[0] = (XSParticleDecl)baseType.getParticle();
868
872
group.fParticles[1] = fParticle;
873
group.fAnnotations = XSObjectListImpl.EMPTY_LIST;
869
874
// the particle to contain the above sequence
870
875
XSParticleDecl particle = new XSParticleDecl();
871
876
particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
872
877
particle.fValue = group;
878
particle.fAnnotations = fParticle.getAnnotations();
874
880
fParticle = particle;
877
883
// Remove prohibited uses. Must be done before merge for EXTENSION.
878
884
fAttrGrp.removeProhibitedAttrs();
880
mergeAttributes(baseType.getAttrGrp(), fAttrGrp, fName, true, complexContent);
886
mergeAttributes(baseType.getAttrGrp(), fAttrGrp, fName, true, complexContent);
881
887
} catch (ComplexTypeRecoverableError e) {
882
888
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
883
889
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
889
895
// and *finally* we can legitimately return the attributes!
890
896
fAttrChecker.returnAttrArray(complexContentAttrValues, schemaDoc);
891
897
fAttrChecker.returnAttrArray(derivationTypeAttrValues, schemaDoc);
893
899
} // end of traverseComplexContent
896
902
// This method merges attribute uses from the base, into the derived set.
897
903
// The first duplicate attribute, if any, is returned.
898
904
// LM: may want to merge with attributeGroup processing.
899
905
private void mergeAttributes(XSAttributeGroupDecl fromAttrGrp,
900
XSAttributeGroupDecl toAttrGrp,
906
XSAttributeGroupDecl toAttrGrp,
904
910
throws ComplexTypeRecoverableError {
906
912
XSObjectList attrUseS = fromAttrGrp.getAttributeUses();
907
913
XSAttributeUseImpl duplicateAttrUse = null, oneAttrUse = null;
908
914
int attrCount = attrUseS.getLength();
909
915
for (int i=0; i<attrCount; i++) {
910
916
oneAttrUse = (XSAttributeUseImpl)attrUseS.item(i);
911
917
XSAttributeUse existingAttrUse = toAttrGrp.getAttributeUse(oneAttrUse.fAttrDecl.getNamespace(),
912
oneAttrUse.fAttrDecl.getName());
918
oneAttrUse.fAttrDecl.getName());
913
919
if (existingAttrUse == null) {
915
921
String idName = toAttrGrp.addAttributeUse(oneAttrUse);
916
922
if (idName != null) {
917
923
throw new ComplexTypeRecoverableError("ct-props-correct.5",
918
new Object[]{typeName, idName, oneAttrUse.fAttrDecl.getName()},
924
new Object[]{typeName, idName, oneAttrUse.fAttrDecl.getName()},
924
930
throw new ComplexTypeRecoverableError("ct-props-correct.4",
925
new Object[]{typeName, oneAttrUse.fAttrDecl.getName()},
931
new Object[]{typeName, oneAttrUse.fAttrDecl.getName()},
935
941
else if (fromAttrGrp.fAttributeWC != null) {
936
942
toAttrGrp.fAttributeWC = toAttrGrp.fAttributeWC.performUnionWith(fromAttrGrp.fAttributeWC, toAttrGrp.fAttributeWC.fProcessContents);
942
948
private void processComplexContent(Element complexContentChild,
943
boolean isMixed, boolean isDerivation,
944
XSDocumentInfo schemaDoc, SchemaGrammar grammar)
949
boolean isMixed, boolean isDerivation,
950
XSDocumentInfo schemaDoc, SchemaGrammar grammar)
945
951
throws ComplexTypeRecoverableError {
947
953
Element attrNode = null;
948
954
XSParticleDecl particle = null;
950
956
// whether there is a particle with empty model group
951
957
boolean emptyParticle = false;
952
958
if (complexContentChild != null) {
954
960
// GROUP, ALL, SEQUENCE or CHOICE, followed by attributes, if specified.
955
961
// Note that it's possible that only attributes are specified.
956
962
// -------------------------------------------------------------
959
965
String childName = DOMUtil.getLocalName(complexContentChild);
961
967
if (childName.equals(SchemaSymbols.ELT_GROUP)) {
963
969
particle = fSchemaHandler.fGroupTraverser.traverseLocal(complexContentChild,
965
971
attrNode = DOMUtil.getNextSiblingElement(complexContentChild);
967
973
else if (childName.equals(SchemaSymbols.ELT_SEQUENCE)) {
968
974
particle = traverseSequence(complexContentChild,schemaDoc,grammar,
969
NOT_ALL_CONTEXT,fComplexTypeDecl);
975
NOT_ALL_CONTEXT,fComplexTypeDecl);
970
976
if (particle != null) {
971
977
XSModelGroupImpl group = (XSModelGroupImpl)particle.fValue;
972
978
if (group.fParticleCount == 0)
1018
1024
// child != null means we might have seen an element with
1019
1025
// minOccurs == maxOccurs == 0
1022
1028
if (particle == null && isMixed) {
1023
1029
if (fEmptyParticle == null) {
1024
1030
XSModelGroupImpl group = new XSModelGroupImpl();
1025
1031
group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
1026
1032
group.fParticleCount = 0;
1027
1033
group.fParticles = null;
1034
group.fAnnotations = XSObjectListImpl.EMPTY_LIST;
1028
1035
fEmptyParticle = new XSParticleDecl();
1029
1036
fEmptyParticle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
1030
1037
fEmptyParticle.fValue = group;
1038
fEmptyParticle.fAnnotations = XSObjectListImpl.EMPTY_LIST;
1032
1040
particle = fEmptyParticle;
1034
1042
fParticle = particle;
1036
1044
// -----------------------------------------------------------------------
1037
1045
// Set the content type
1038
1046
// -----------------------------------------------------------------------
1042
1050
fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
1044
1052
fContentType = XSComplexTypeDecl.CONTENTTYPE_ELEMENT;
1047
1055
// -------------------------------------------------------------
1048
1056
// Now, process attributes
1049
1057
// -------------------------------------------------------------
1050
1058
if (attrNode != null) {
1051
1059
if (!isAttrOrAttrGroup(attrNode)) {
1052
1060
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
1053
new Object[]{fName,DOMUtil.getLocalName(attrNode)},
1061
new Object[]{fName,DOMUtil.getLocalName(attrNode)},
1057
traverseAttrsAndAttrGrps(attrNode,fAttrGrp,schemaDoc,grammar,fComplexTypeDecl);
1065
traverseAttrsAndAttrGrps(attrNode,fAttrGrp,schemaDoc,grammar,fComplexTypeDecl);
1058
1066
if (node!=null) {
1059
1067
throw new ComplexTypeRecoverableError("s4s-elt-invalid-content.1",
1060
new Object[]{fName,DOMUtil.getLocalName(node)},
1068
new Object[]{fName,DOMUtil.getLocalName(node)},
1063
1071
// Only remove prohibited attribute uses if this isn't a derived type
1064
1072
// Derivation-specific code worries about this elsewhere
1066
1074
fAttrGrp.removeProhibitedAttrs();
1072
1080
} // end processComplexContent
1075
1083
private boolean isAttrOrAttrGroup(Element e) {
1076
1084
String elementName = DOMUtil.getLocalName(e);
1078
1086
if (elementName.equals(SchemaSymbols.ELT_ATTRIBUTE) ||
1079
elementName.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP) ||
1080
elementName.equals(SchemaSymbols.ELT_ANYATTRIBUTE))
1087
elementName.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP) ||
1088
elementName.equals(SchemaSymbols.ELT_ANYATTRIBUTE))
1086
1094
private void traverseSimpleContentDecl(Element simpleContentDecl) {
1089
1097
private void traverseComplexContentDecl(Element complexContentDecl,
1090
boolean mixedOnComplexTypeDecl) {
1098
boolean mixedOnComplexTypeDecl) {
1094
1102
* Generate a name for an anonymous type
1096
1104
private String genAnonTypeName(Element complexTypeDecl) {
1098
1106
// Generate a unique name for the anonymous type by concatenating together the
1099
1107
// names of parent nodes
1100
1108
// The name is quite good for debugging/error purposes, but we may want to
1101
1109
// revisit how this is done for performance reasons (LM).
1110
StringBuffer typeName = new StringBuffer("#AnonType_");
1103
1111
Element node = DOMUtil.getParent(complexTypeDecl);
1104
typeName="#AnonType_";
1105
1112
while (node != null && (node != DOMUtil.getRoot(DOMUtil.getDocument(node)))) {
1106
typeName = typeName+node.getAttribute(SchemaSymbols.ATT_NAME);
1113
typeName.append(node.getAttribute(SchemaSymbols.ATT_NAME));
1107
1114
node = DOMUtil.getParent(node);
1116
return typeName.toString();
1113
1120
private void handleComplexTypeError(String messageId,Object[] args,
1116
1123
if (messageId!=null) {
1117
1124
reportSchemaError(messageId, args, e);
1121
1128
// Mock up the typeInfo structure so that there won't be problems during