2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.xerces.impl.xs;
20
import java.lang.ref.SoftReference;
21
import java.util.Vector;
23
import org.apache.xerces.impl.Constants;
24
import org.apache.xerces.impl.dv.SchemaDVFactory;
25
import org.apache.xerces.impl.dv.ValidatedInfo;
26
import org.apache.xerces.impl.dv.XSSimpleType;
27
import org.apache.xerces.impl.xs.identity.IdentityConstraint;
28
import org.apache.xerces.impl.xs.util.SimpleLocator;
29
import org.apache.xerces.impl.xs.util.StringListImpl;
30
import org.apache.xerces.impl.xs.util.XSNamedMap4Types;
31
import org.apache.xerces.impl.xs.util.XSNamedMapImpl;
32
import org.apache.xerces.impl.xs.util.XSObjectListImpl;
33
import org.apache.xerces.parsers.DOMParser;
34
import org.apache.xerces.parsers.SAXParser;
35
import org.apache.xerces.parsers.XML11Configuration;
36
import org.apache.xerces.util.SymbolHash;
37
import org.apache.xerces.util.SymbolTable;
38
import org.apache.xerces.xni.NamespaceContext;
39
import org.apache.xerces.xni.grammars.XMLGrammarDescription;
40
import org.apache.xerces.xni.grammars.XSGrammar;
41
import org.apache.xerces.xs.StringList;
42
import org.apache.xerces.xs.XSAnnotation;
43
import org.apache.xerces.xs.XSAttributeDeclaration;
44
import org.apache.xerces.xs.XSAttributeGroupDefinition;
45
import org.apache.xerces.xs.XSConstants;
46
import org.apache.xerces.xs.XSElementDeclaration;
47
import org.apache.xerces.xs.XSModel;
48
import org.apache.xerces.xs.XSModelGroupDefinition;
49
import org.apache.xerces.xs.XSNamedMap;
50
import org.apache.xerces.xs.XSNamespaceItem;
51
import org.apache.xerces.xs.XSNotationDeclaration;
52
import org.apache.xerces.xs.XSObjectList;
53
import org.apache.xerces.xs.XSParticle;
54
import org.apache.xerces.xs.XSTypeDefinition;
55
import org.apache.xerces.xs.XSWildcard;
56
import org.xml.sax.SAXException;
59
* This class is to hold all schema component declaration that are declared
60
* within one namespace.
62
* The Grammar class this class extends contains what little
63
* commonality there is between XML Schema and DTD grammars. It's
64
* useful to distinguish grammar objects from other kinds of object
65
* when they exist in pools or caches.
69
* @author Sandy Gao, IBM
70
* @author Elena Litani, IBM
72
* @version $Id: SchemaGrammar.java,v 1.2 2009/12/10 03:18:27 matthewoliver Exp $
75
public class SchemaGrammar implements XSGrammar, XSNamespaceItem {
77
// the target namespace of grammar
78
String fTargetNamespace;
80
// global decls: map from decl name to decl object
81
SymbolHash fGlobalAttrDecls;
82
SymbolHash fGlobalAttrGrpDecls;
83
SymbolHash fGlobalElemDecls;
84
SymbolHash fGlobalGroupDecls;
85
SymbolHash fGlobalNotationDecls;
86
SymbolHash fGlobalIDConstraintDecls;
87
SymbolHash fGlobalTypeDecls;
89
// the XMLGrammarDescription member
90
XSDDescription fGrammarDescription = null;
92
// annotations associated with the "root" schema of this targetNamespace
93
XSAnnotationImpl [] fAnnotations = null;
95
// number of annotations declared
98
// symbol table for constructing parsers (annotation support)
99
private SymbolTable fSymbolTable = null;
100
// parsers for annotation support
101
private SoftReference fSAXParser = null;
102
private SoftReference fDOMParser = null;
108
// needed to make BuiltinSchemaGrammar work.
109
protected SchemaGrammar() {}
112
* Default constructor.
114
* @param targetNamespace
115
* @param grammarDesc the XMLGrammarDescription corresponding to this objec
116
* at the least a systemId should always be known.
117
* @param symbolTable needed for annotation support
119
public SchemaGrammar(String targetNamespace, XSDDescription grammarDesc,
120
SymbolTable symbolTable) {
121
fTargetNamespace = targetNamespace;
122
fGrammarDescription = grammarDesc;
123
fSymbolTable = symbolTable;
125
// REVISIT: do we know the numbers of the following global decls
126
// when creating this grammar? If so, we can pass the numbers in,
127
// and use that number to initialize the following hashtables.
128
fGlobalAttrDecls = new SymbolHash();
129
fGlobalAttrGrpDecls = new SymbolHash();
130
fGlobalElemDecls = new SymbolHash();
131
fGlobalGroupDecls = new SymbolHash();
132
fGlobalNotationDecls = new SymbolHash();
133
fGlobalIDConstraintDecls = new SymbolHash();
135
// if we are parsing S4S, put built-in types in first
136
// they might get overwritten by the types from S4S, but that's
137
// considered what the application wants to do.
138
if (fTargetNamespace == SchemaSymbols.URI_SCHEMAFORSCHEMA)
139
fGlobalTypeDecls = SG_SchemaNS.fGlobalTypeDecls.makeClone();
141
fGlobalTypeDecls = new SymbolHash();
142
} // <init>(String, XSDDescription)
144
// number of built-in XSTypes we need to create for base and full
146
private static final int BASICSET_COUNT = 29;
147
private static final int FULLSET_COUNT = 46;
149
private static final int GRAMMAR_XS = 1;
150
private static final int GRAMMAR_XSI = 2;
152
// this class makes sure the static, built-in schema grammars
154
public static class BuiltinSchemaGrammar extends SchemaGrammar {
156
* Special constructor to create the grammars for the schema namespaces
160
public BuiltinSchemaGrammar(int grammar) {
161
SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
163
if (grammar == GRAMMAR_XS) {
165
fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
167
// grammar description
168
fGrammarDescription = new XSDDescription();
169
fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
170
fGrammarDescription.setNamespace(SchemaSymbols.URI_SCHEMAFORSCHEMA);
172
// no global decls other than types
173
fGlobalAttrDecls = new SymbolHash(1);
174
fGlobalAttrGrpDecls = new SymbolHash(1);
175
fGlobalElemDecls = new SymbolHash(1);
176
fGlobalGroupDecls = new SymbolHash(1);
177
fGlobalNotationDecls = new SymbolHash(1);
178
fGlobalIDConstraintDecls = new SymbolHash(1);
180
// get all built-in types
181
fGlobalTypeDecls = schemaFactory.getBuiltInTypes();
183
fGlobalTypeDecls.put(fAnyType.getName(), fAnyType);
185
else if (grammar == GRAMMAR_XSI) {
187
fTargetNamespace = SchemaSymbols.URI_XSI;
188
// grammar description
189
fGrammarDescription = new XSDDescription();
190
fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
191
fGrammarDescription.setNamespace(SchemaSymbols.URI_XSI);
193
// no global decls other than attributes
194
fGlobalAttrGrpDecls = new SymbolHash(1);
195
fGlobalElemDecls = new SymbolHash(1);
196
fGlobalGroupDecls = new SymbolHash(1);
197
fGlobalNotationDecls = new SymbolHash(1);
198
fGlobalIDConstraintDecls = new SymbolHash(1);
199
fGlobalTypeDecls = new SymbolHash(1);
201
// 4 attributes, so initialize the size as 4*2 = 8
202
fGlobalAttrDecls = new SymbolHash(8);
205
XSSimpleType type = null;
206
short scope = XSConstants.SCOPE_GLOBAL;
209
name = SchemaSymbols.XSI_TYPE;
210
tns = SchemaSymbols.URI_XSI;
211
type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_QNAME);
212
fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
215
name = SchemaSymbols.XSI_NIL;
216
tns = SchemaSymbols.URI_XSI;
217
type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_BOOLEAN);
218
fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
220
XSSimpleType anyURI = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_ANYURI);
222
// xsi:schemaLocation
223
name = SchemaSymbols.XSI_SCHEMALOCATION;
224
tns = SchemaSymbols.URI_XSI;
225
type = schemaFactory.createTypeList(null, SchemaSymbols.URI_XSI, (short)0, anyURI, null);
226
fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
228
// xsi:noNamespaceSchemaLocation
229
name = SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION;
230
tns = SchemaSymbols.URI_XSI;
232
fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
236
// return the XMLGrammarDescription corresponding to this
238
public XMLGrammarDescription getGrammarDescription() {
239
return fGrammarDescription.makeClone();
240
} // getGrammarDescription(): XMLGrammarDescription
242
// override these methods solely so that these
243
// objects cannot be modified once they're created.
244
public void setImportedGrammars(Vector importedGrammars) {
247
public void addGlobalAttributeDecl(XSAttributeDecl decl) {
250
public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
253
public void addGlobalElementDecl(XSElementDecl decl) {
256
public void addGlobalGroupDecl(XSGroupDecl decl) {
259
public void addGlobalNotationDecl(XSNotationDecl decl) {
262
public void addGlobalTypeDecl(XSTypeDefinition decl) {
265
public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
268
public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
271
public synchronized void addDocument(Object document, String location) {
275
// annotation support
276
synchronized DOMParser getDOMParser() {
279
synchronized SAXParser getSAXParser() {
285
* <p>A partial schema for schemas for validating annotations.</p>
289
* @author Michael Glavassevich, IBM
291
public static final class Schema4Annotations extends SchemaGrammar {
294
* Special constructor to create a schema
295
* capable of validating annotations.
297
public Schema4Annotations() {
300
fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
302
// grammar description
303
fGrammarDescription = new XSDDescription();
304
fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
305
fGrammarDescription.setNamespace(SchemaSymbols.URI_SCHEMAFORSCHEMA);
307
// no global decls other than types and
308
// element declarations for <annotation>, <documentation> and <appinfo>.
309
fGlobalAttrDecls = new SymbolHash(1);
310
fGlobalAttrGrpDecls = new SymbolHash(1);
311
fGlobalElemDecls = new SymbolHash(6);
312
fGlobalGroupDecls = new SymbolHash(1);
313
fGlobalNotationDecls = new SymbolHash(1);
314
fGlobalIDConstraintDecls = new SymbolHash(1);
316
// get all built-in types
317
fGlobalTypeDecls = SG_SchemaNS.fGlobalTypeDecls;
319
// create element declarations for <annotation>, <documentation> and <appinfo>
320
XSElementDecl annotationDecl = createAnnotationElementDecl(SchemaSymbols.ELT_ANNOTATION);
321
XSElementDecl documentationDecl = createAnnotationElementDecl(SchemaSymbols.ELT_DOCUMENTATION);
322
XSElementDecl appinfoDecl = createAnnotationElementDecl(SchemaSymbols.ELT_APPINFO);
324
// add global element declarations
325
fGlobalElemDecls.put(annotationDecl.fName, annotationDecl);
326
fGlobalElemDecls.put(documentationDecl.fName, documentationDecl);
327
fGlobalElemDecls.put(appinfoDecl.fName, appinfoDecl);
329
// create complex type declarations for <annotation>, <documentation> and <appinfo>
330
XSComplexTypeDecl annotationType = new XSComplexTypeDecl();
331
XSComplexTypeDecl documentationType = new XSComplexTypeDecl();
332
XSComplexTypeDecl appinfoType = new XSComplexTypeDecl();
334
// set the types on their element declarations
335
annotationDecl.fType = annotationType;
336
documentationDecl.fType = documentationType;
337
appinfoDecl.fType = appinfoType;
339
// create attribute groups for <annotation>, <documentation> and <appinfo>
340
XSAttributeGroupDecl annotationAttrs = new XSAttributeGroupDecl();
341
XSAttributeGroupDecl documentationAttrs = new XSAttributeGroupDecl();
342
XSAttributeGroupDecl appinfoAttrs = new XSAttributeGroupDecl();
344
// fill in attribute groups
346
// create and fill attribute uses for <annotation>, <documentation> and <appinfo>
347
XSAttributeUseImpl annotationIDAttr = new XSAttributeUseImpl();
348
annotationIDAttr.fAttrDecl = new XSAttributeDecl();
349
annotationIDAttr.fAttrDecl.setValues(SchemaSymbols.ATT_ID, null, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_ID),
350
XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, annotationType, null);
351
annotationIDAttr.fUse = SchemaSymbols.USE_OPTIONAL;
352
annotationIDAttr.fConstraintType = XSConstants.VC_NONE;
354
XSAttributeUseImpl documentationSourceAttr = new XSAttributeUseImpl();
355
documentationSourceAttr.fAttrDecl = new XSAttributeDecl();
356
documentationSourceAttr.fAttrDecl.setValues(SchemaSymbols.ATT_SOURCE, null, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_ANYURI),
357
XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, documentationType, null);
358
documentationSourceAttr.fUse = SchemaSymbols.USE_OPTIONAL;
359
documentationSourceAttr.fConstraintType = XSConstants.VC_NONE;
361
XSAttributeUseImpl documentationLangAttr = new XSAttributeUseImpl();
362
documentationLangAttr.fAttrDecl = new XSAttributeDecl();
363
documentationLangAttr.fAttrDecl.setValues("lang".intern(), NamespaceContext.XML_URI, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_LANGUAGE),
364
XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, documentationType, null);
365
documentationLangAttr.fUse = SchemaSymbols.USE_OPTIONAL;
366
documentationLangAttr.fConstraintType = XSConstants.VC_NONE;
368
XSAttributeUseImpl appinfoSourceAttr = new XSAttributeUseImpl();
369
appinfoSourceAttr.fAttrDecl = new XSAttributeDecl();
370
appinfoSourceAttr.fAttrDecl.setValues(SchemaSymbols.ATT_SOURCE, null, (XSSimpleType) fGlobalTypeDecls.get(SchemaSymbols.ATTVAL_ANYURI),
371
XSConstants.VC_NONE, XSConstants.SCOPE_LOCAL, null, appinfoType, null);
372
appinfoSourceAttr.fUse = SchemaSymbols.USE_OPTIONAL;
373
appinfoSourceAttr.fConstraintType = XSConstants.VC_NONE;
375
// create lax attribute wildcard for <annotation>, <documentation> and <appinfo>
376
XSWildcardDecl otherAttrs = new XSWildcardDecl();
377
otherAttrs.fNamespaceList = new String [] {fTargetNamespace, null};
378
otherAttrs.fType = XSWildcard.NSCONSTRAINT_NOT;
379
otherAttrs.fProcessContents = XSWildcard.PC_LAX;
381
// add attribute uses and wildcards to attribute groups for <annotation>, <documentation> and <appinfo>
382
annotationAttrs.addAttributeUse(annotationIDAttr);
383
annotationAttrs.fAttributeWC = otherAttrs;
385
documentationAttrs.addAttributeUse(documentationSourceAttr);
386
documentationAttrs.addAttributeUse(documentationLangAttr);
387
documentationAttrs.fAttributeWC = otherAttrs;
389
appinfoAttrs.addAttributeUse(appinfoSourceAttr);
390
appinfoAttrs.fAttributeWC = otherAttrs;
393
// create particles for <annotation>
394
XSParticleDecl annotationParticle = createUnboundedModelGroupParticle();
396
XSModelGroupImpl annotationChoice = new XSModelGroupImpl();
397
annotationChoice.fCompositor = XSModelGroupImpl.MODELGROUP_CHOICE;
398
annotationChoice.fParticleCount = 2;
399
annotationChoice.fParticles = new XSParticleDecl[2];
400
annotationChoice.fParticles[0] = createChoiceElementParticle(appinfoDecl);
401
annotationChoice.fParticles[1] = createChoiceElementParticle(documentationDecl);
402
annotationParticle.fValue = annotationChoice;
405
// create wildcard particle for <documentation> and <appinfo>
406
XSParticleDecl anyWCSequenceParticle = createUnboundedAnyWildcardSequenceParticle();
408
// fill complex types
409
annotationType.setValues("#AnonType_" + SchemaSymbols.ELT_ANNOTATION, fTargetNamespace, SchemaGrammar.fAnyType,
410
XSConstants.DERIVATION_RESTRICTION, XSConstants.DERIVATION_NONE, (short) (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION),
411
XSComplexTypeDecl.CONTENTTYPE_ELEMENT, false, annotationAttrs, null, annotationParticle, new XSObjectListImpl(null, 0));
412
annotationType.setName("#AnonType_" + SchemaSymbols.ELT_ANNOTATION);
413
annotationType.setIsAnonymous();
415
documentationType.setValues("#AnonType_" + SchemaSymbols.ELT_DOCUMENTATION, fTargetNamespace, SchemaGrammar.fAnyType,
416
XSConstants.DERIVATION_RESTRICTION, XSConstants.DERIVATION_NONE, (short) (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION),
417
XSComplexTypeDecl.CONTENTTYPE_MIXED, false, documentationAttrs, null, anyWCSequenceParticle, new XSObjectListImpl(null, 0));
418
documentationType.setName("#AnonType_" + SchemaSymbols.ELT_DOCUMENTATION);
419
documentationType.setIsAnonymous();
421
appinfoType.setValues("#AnonType_" + SchemaSymbols.ELT_APPINFO, fTargetNamespace, SchemaGrammar.fAnyType,
422
XSConstants.DERIVATION_RESTRICTION, XSConstants.DERIVATION_NONE, (short) (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION),
423
XSComplexTypeDecl.CONTENTTYPE_MIXED, false, appinfoAttrs, null, anyWCSequenceParticle, new XSObjectListImpl(null, 0));
424
appinfoType.setName("#AnonType_" + SchemaSymbols.ELT_APPINFO);
425
appinfoType.setIsAnonymous();
429
// return the XMLGrammarDescription corresponding to this
431
public XMLGrammarDescription getGrammarDescription() {
432
return fGrammarDescription.makeClone();
433
} // getGrammarDescription(): XMLGrammarDescription
435
// override these methods solely so that these
436
// objects cannot be modified once they're created.
437
public void setImportedGrammars(Vector importedGrammars) {
440
public void addGlobalAttributeDecl(XSAttributeDecl decl) {
443
public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
446
public void addGlobalElementDecl(XSElementDecl decl) {
449
public void addGlobalGroupDecl(XSGroupDecl decl) {
452
public void addGlobalNotationDecl(XSNotationDecl decl) {
455
public void addGlobalTypeDecl(XSTypeDefinition decl) {
458
public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
461
public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
464
public synchronized void addDocument(Object document, String location) {
468
// annotation support
469
synchronized DOMParser getDOMParser() {
472
synchronized SAXParser getSAXParser() {
477
// private helper methods
480
private XSElementDecl createAnnotationElementDecl(String localName) {
481
XSElementDecl eDecl = new XSElementDecl();
482
eDecl.fName = localName;
483
eDecl.fTargetNamespace = fTargetNamespace;
485
eDecl.fBlock = (XSConstants.DERIVATION_EXTENSION |
486
XSConstants.DERIVATION_RESTRICTION | XSConstants.DERIVATION_SUBSTITUTION);
487
eDecl.setConstraintType(XSConstants.VC_NONE);
491
private XSParticleDecl createUnboundedModelGroupParticle() {
492
XSParticleDecl particle = new XSParticleDecl();
493
particle.fMinOccurs = 0;
494
particle.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
495
particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
499
private XSParticleDecl createChoiceElementParticle(XSElementDecl ref) {
500
XSParticleDecl particle = new XSParticleDecl();
501
particle.fMinOccurs = 1;
502
particle.fMaxOccurs = 1;
503
particle.fType = XSParticleDecl.PARTICLE_ELEMENT;
504
particle.fValue = ref;
508
private XSParticleDecl createUnboundedAnyWildcardSequenceParticle() {
509
XSParticleDecl particle = createUnboundedModelGroupParticle();
510
XSModelGroupImpl sequence = new XSModelGroupImpl();
511
sequence.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
512
sequence.fParticleCount = 1;
513
sequence.fParticles = new XSParticleDecl[1];
514
sequence.fParticles[0] = createAnyLaxWildcardParticle();
515
particle.fValue = sequence;
519
private XSParticleDecl createAnyLaxWildcardParticle() {
520
XSParticleDecl particle = new XSParticleDecl();
521
particle.fMinOccurs = 1;
522
particle.fMaxOccurs = 1;
523
particle.fType = XSParticleDecl.PARTICLE_WILDCARD;
525
XSWildcardDecl anyWC = new XSWildcardDecl();
526
anyWC.fNamespaceList = null;
527
anyWC.fType = XSWildcard.NSCONSTRAINT_ANY;
528
anyWC.fProcessContents = XSWildcard.PC_LAX;
530
particle.fValue = anyWC;
537
// return the XMLGrammarDescription corresponding to this
539
public XMLGrammarDescription getGrammarDescription() {
540
return fGrammarDescription;
541
} // getGrammarDescription(): XMLGrammarDescription
543
// DTDGrammar methods
544
public boolean isNamespaceAware () {
546
} // isNamespaceAware():boolean
548
Vector fImported = null;
550
public void setImportedGrammars(Vector importedGrammars) {
551
fImported = importedGrammars;
554
public Vector getImportedGrammars() {
559
* Returns this grammar's target namespace.
561
public final String getTargetNamespace() {
562
return fTargetNamespace;
563
} // getTargetNamespace():String
566
* register one global attribute
568
public void addGlobalAttributeDecl(XSAttributeDecl decl) {
569
fGlobalAttrDecls.put(decl.fName, decl);
573
* register one global attribute group
575
public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
576
fGlobalAttrGrpDecls.put(decl.fName, decl);
580
* register one global element
582
public void addGlobalElementDecl(XSElementDecl decl) {
583
fGlobalElemDecls.put(decl.fName, decl);
585
// if there is a substitution group affiliation, store in an array,
586
// for further constraint checking: UPA, PD, EDC
587
if (decl.fSubGroup != null) {
588
if (fSubGroupCount == fSubGroups.length)
589
fSubGroups = resize(fSubGroups, fSubGroupCount+INC_SIZE);
590
fSubGroups[fSubGroupCount++] = decl;
595
* register one global group
597
public void addGlobalGroupDecl(XSGroupDecl decl) {
598
fGlobalGroupDecls.put(decl.fName, decl);
602
* register one global notation
604
public void addGlobalNotationDecl(XSNotationDecl decl) {
605
fGlobalNotationDecls.put(decl.fName, decl);
609
* register one global type
611
public void addGlobalTypeDecl(XSTypeDefinition decl) {
612
fGlobalTypeDecls.put(decl.getName(), decl);
616
* register one identity constraint
618
public final void addIDConstraintDecl(XSElementDecl elmDecl, IdentityConstraint decl) {
619
elmDecl.addIDConstraint(decl);
620
fGlobalIDConstraintDecls.put(decl.getIdentityConstraintName(), decl);
624
* get one global attribute
626
public final XSAttributeDecl getGlobalAttributeDecl(String declName) {
627
return(XSAttributeDecl)fGlobalAttrDecls.get(declName);
631
* get one global attribute group
633
public final XSAttributeGroupDecl getGlobalAttributeGroupDecl(String declName) {
634
return(XSAttributeGroupDecl)fGlobalAttrGrpDecls.get(declName);
638
* get one global element
640
public final XSElementDecl getGlobalElementDecl(String declName) {
641
return(XSElementDecl)fGlobalElemDecls.get(declName);
645
* get one global group
647
public final XSGroupDecl getGlobalGroupDecl(String declName) {
648
return(XSGroupDecl)fGlobalGroupDecls.get(declName);
652
* get one global notation
654
public final XSNotationDecl getGlobalNotationDecl(String declName) {
655
return(XSNotationDecl)fGlobalNotationDecls.get(declName);
659
* get one global type
661
public final XSTypeDefinition getGlobalTypeDecl(String declName) {
662
return(XSTypeDefinition)fGlobalTypeDecls.get(declName);
666
* get one identity constraint
668
public final IdentityConstraint getIDConstraintDecl(String declName) {
669
return(IdentityConstraint)fGlobalIDConstraintDecls.get(declName);
673
* get one identity constraint
675
public final boolean hasIDConstraints() {
676
return fGlobalIDConstraintDecls.getLength() > 0;
679
// array to store complex type decls
680
private static final int INITIAL_SIZE = 16;
681
private static final int INC_SIZE = 16;
683
private int fCTCount = 0;
684
private XSComplexTypeDecl[] fComplexTypeDecls = new XSComplexTypeDecl[INITIAL_SIZE];
685
private SimpleLocator[] fCTLocators = new SimpleLocator[INITIAL_SIZE];
687
// an array to store groups being redefined by restriction
688
// even-numbered elements are the derived groups, odd-numbered ones their bases
689
private static final int REDEFINED_GROUP_INIT_SIZE = 2;
690
private int fRGCount = 0;
691
private XSGroupDecl[] fRedefinedGroupDecls = new XSGroupDecl[REDEFINED_GROUP_INIT_SIZE];
692
private SimpleLocator[] fRGLocators = new SimpleLocator[REDEFINED_GROUP_INIT_SIZE/2];
694
// a flag to indicate whether we have checked the 3 constraints on this
696
boolean fFullChecked = false;
699
* add one complex type decl: for later constraint checking
701
public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
702
if (fCTCount == fComplexTypeDecls.length) {
703
fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount+INC_SIZE);
704
fCTLocators = resize(fCTLocators, fCTCount+INC_SIZE);
706
fCTLocators[fCTCount] = locator;
707
fComplexTypeDecls[fCTCount++] = decl;
711
* add a group redefined by restriction: for later constraint checking
713
public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
714
if (fRGCount == fRedefinedGroupDecls.length) {
715
// double array size each time.
716
fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount << 1);
717
fRGLocators = resize(fRGLocators, fRGCount);
719
fRGLocators[fRGCount/2] = locator;
720
fRedefinedGroupDecls[fRGCount++] = derived;
721
fRedefinedGroupDecls[fRGCount++] = base;
725
* get all complex type decls: for later constraint checking
727
final XSComplexTypeDecl[] getUncheckedComplexTypeDecls() {
728
if (fCTCount < fComplexTypeDecls.length) {
729
fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
730
fCTLocators = resize(fCTLocators, fCTCount);
732
return fComplexTypeDecls;
736
* get the error locator of all complex type decls
738
final SimpleLocator[] getUncheckedCTLocators() {
739
if (fCTCount < fCTLocators.length) {
740
fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
741
fCTLocators = resize(fCTLocators, fCTCount);
747
* get all redefined groups: for later constraint checking
749
final XSGroupDecl[] getRedefinedGroupDecls() {
750
if (fRGCount < fRedefinedGroupDecls.length) {
751
fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
752
fRGLocators = resize(fRGLocators, fRGCount/2);
754
return fRedefinedGroupDecls;
758
* get the error locator of all redefined groups
760
final SimpleLocator[] getRGLocators() {
761
if (fRGCount < fRedefinedGroupDecls.length) {
762
fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
763
fRGLocators = resize(fRGLocators, fRGCount/2);
769
* after the first-round checking, some types don't need to be checked
770
* against UPA again. here we trim the array to the proper size.
772
final void setUncheckedTypeNum(int newSize) {
774
fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
775
fCTLocators = resize(fCTLocators, fCTCount);
778
// used to store all substitution group information declared in
780
private int fSubGroupCount = 0;
781
private XSElementDecl[] fSubGroups = new XSElementDecl[INITIAL_SIZE];
784
* get all substitution group information: for the 3 constraint checking
786
final XSElementDecl[] getSubstitutionGroups() {
787
if (fSubGroupCount < fSubGroups.length)
788
fSubGroups = resize(fSubGroups, fSubGroupCount);
792
// anyType and anySimpleType: because there are so many places where
793
// we need direct access to these two types
794
public final static XSComplexTypeDecl fAnyType = new XSAnyType();
795
private static class XSAnyType extends XSComplexTypeDecl {
796
public XSAnyType () {
797
fName = SchemaSymbols.ATTVAL_ANYTYPE;
798
super.fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
800
fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
801
fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
807
// overridden methods
808
public void setValues(String name, String targetNamespace,
809
XSTypeDefinition baseType, short derivedBy, short schemaFinal,
810
short block, short contentType,
811
boolean isAbstract, XSAttributeGroupDecl attrGrp,
812
XSSimpleType simpleType, XSParticleDecl particle) {
816
public void setName(String name){
820
public void setIsAbstractType() {
821
// null implementation
824
public void setContainsTypeID() {
825
// null implementation
828
public void setIsAnonymous() {
829
// null implementation
832
public void reset() {
833
// null implementation
836
public XSObjectList getAttributeUses() {
837
return new XSObjectListImpl(null, 0);
840
public XSAttributeGroupDecl getAttrGrp() {
841
XSWildcardDecl wildcard = new XSWildcardDecl();
842
wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
843
XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
844
attrGrp.fAttributeWC = wildcard;
848
public XSWildcard getAttributeWildcard() {
849
XSWildcardDecl wildcard = new XSWildcardDecl();
850
wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
854
public XSParticle getParticle() {
855
// the wildcard used in anyType (content and attribute)
856
// the spec will change strict to skip for anyType
857
XSWildcardDecl wildcard = new XSWildcardDecl();
858
wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
859
// the particle for the content wildcard
860
XSParticleDecl particleW = new XSParticleDecl();
861
particleW.fMinOccurs = 0;
862
particleW.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
863
particleW.fType = XSParticleDecl.PARTICLE_WILDCARD;
864
particleW.fValue = wildcard;
865
// the model group of a sequence of the above particle
866
XSModelGroupImpl group = new XSModelGroupImpl();
867
group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
868
group.fParticleCount = 1;
869
group.fParticles = new XSParticleDecl[1];
870
group.fParticles[0] = particleW;
871
// the content of anyType: particle of the above model group
872
XSParticleDecl particleG = new XSParticleDecl();
873
particleG.fType = XSParticleDecl.PARTICLE_MODELGROUP;
874
particleG.fValue = group;
879
public XSObjectList getAnnotations() {
883
private static class BuiltinAttrDecl extends XSAttributeDecl {
884
public BuiltinAttrDecl(String name, String tns,
885
XSSimpleType type, short scope) {
887
super.fTargetNamespace = tns;
892
public void setValues(String name, String targetNamespace,
893
XSSimpleType simpleType, short constraintType, short scope,
894
ValidatedInfo valInfo, XSComplexTypeDecl enclosingCT) {
898
public void reset () {
899
// also ignore this call.
901
public XSAnnotation getAnnotation() {
904
} // class BuiltinAttrDecl
906
// the grammars to hold components of the schema namespace
907
public final static BuiltinSchemaGrammar SG_SchemaNS = new BuiltinSchemaGrammar(GRAMMAR_XS);
909
public final static Schema4Annotations SG_Schema4Annotations = new Schema4Annotations();
911
public final static XSSimpleType fAnySimpleType = (XSSimpleType)SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_ANYSIMPLETYPE);
913
// the grammars to hold components of the schema-instance namespace
914
public final static BuiltinSchemaGrammar SG_XSI = new BuiltinSchemaGrammar(GRAMMAR_XSI);
916
static final XSComplexTypeDecl[] resize(XSComplexTypeDecl[] oldArray, int newSize) {
917
XSComplexTypeDecl[] newArray = new XSComplexTypeDecl[newSize];
918
System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
922
static final XSGroupDecl[] resize(XSGroupDecl[] oldArray, int newSize) {
923
XSGroupDecl[] newArray = new XSGroupDecl[newSize];
924
System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
928
static final XSElementDecl[] resize(XSElementDecl[] oldArray, int newSize) {
929
XSElementDecl[] newArray = new XSElementDecl[newSize];
930
System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
934
static final SimpleLocator[] resize(SimpleLocator[] oldArray, int newSize) {
935
SimpleLocator[] newArray = new SimpleLocator[newSize];
936
System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
940
// XSNamespaceItem methods
942
// the max index / the max value of XSObject type
943
private static final short MAX_COMP_IDX = XSTypeDefinition.SIMPLE_TYPE;
944
private static final boolean[] GLOBAL_COMP = {false, // null
948
false, // attribute use
949
true, // attribute group
951
false, // model group
958
false, // multi value facet
959
true, // complex type
963
// store a certain kind of components from all namespaces
964
private XSNamedMap[] fComponents = null;
966
// store the documents and their locations contributing to this namespace
967
// REVISIT: use StringList and XSObjectList for there fields.
968
private Vector fDocuments = null;
969
private Vector fLocations = null;
971
public synchronized void addDocument(Object document, String location) {
972
if (fDocuments == null) {
973
fDocuments = new Vector();
974
fLocations = new Vector();
976
fDocuments.addElement(document);
977
fLocations.addElement(location);
982
* @see <a href="http://www.w3.org/TR/xmlschema-1/#nsi-schema_namespace">[schema namespace]</a>
983
* @return The target namespace of this item.
985
public String getSchemaNamespace() {
986
return fTargetNamespace;
989
// annotation support
990
synchronized DOMParser getDOMParser() {
991
if (fDOMParser != null) {
992
DOMParser parser = (DOMParser) fDOMParser.get();
993
if (parser != null) {
997
// REVISIT: when schema handles XML 1.1, will need to
998
// revisit this (and the practice of not prepending an XML decl to the annotation string
999
XML11Configuration config = new XML11Configuration(fSymbolTable);
1000
// note that this should never produce errors or require
1001
// entity resolution, so just a barebones configuration with
1002
// a couple of feature set will do fine
1003
config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
1004
config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
1006
DOMParser parser = new DOMParser(config);
1008
parser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE, false);
1010
catch (SAXException exc) {}
1011
fDOMParser = new SoftReference(parser);
1015
synchronized SAXParser getSAXParser() {
1016
if (fSAXParser != null) {
1017
SAXParser parser = (SAXParser) fSAXParser.get();
1018
if (parser != null) {
1022
// REVISIT: when schema handles XML 1.1, will need to
1023
// revisit this (and the practice of not prepending an XML decl to the annotation string
1024
XML11Configuration config = new XML11Configuration(fSymbolTable);
1025
// note that this should never produce errors or require
1026
// entity resolution, so just a barebones configuration with
1027
// a couple of feature set will do fine
1028
config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
1029
config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
1030
SAXParser parser = new SAXParser(config);
1031
fSAXParser = new SoftReference(parser);
1036
* [schema components]: a list of top-level components, i.e. element
1037
* declarations, attribute declarations, etc.
1038
* @param objectType The type of the declaration, i.e.
1039
* <code>ELEMENT_DECLARATION</code>. Note that
1040
* <code>XSTypeDefinition.SIMPLE_TYPE</code> and
1041
* <code>XSTypeDefinition.COMPLEX_TYPE</code> can also be used as the
1042
* <code>objectType</code> to retrieve only complex types or simple
1043
* types, instead of all types.
1044
* @return A list of top-level definition of the specified type in
1045
* <code>objectType</code> or an empty <code>XSNamedMap</code> if no
1046
* such definitions exist.
1048
public synchronized XSNamedMap getComponents(short objectType) {
1049
if (objectType <= 0 || objectType > MAX_COMP_IDX ||
1050
!GLOBAL_COMP[objectType]) {
1051
return XSNamedMapImpl.EMPTY_MAP;
1054
if (fComponents == null)
1055
fComponents = new XSNamedMap[MAX_COMP_IDX+1];
1057
// get the hashtable for this type of components
1058
if (fComponents[objectType] == null) {
1059
SymbolHash table = null;
1060
switch (objectType) {
1061
case XSConstants.TYPE_DEFINITION:
1062
case XSTypeDefinition.COMPLEX_TYPE:
1063
case XSTypeDefinition.SIMPLE_TYPE:
1064
table = fGlobalTypeDecls;
1066
case XSConstants.ATTRIBUTE_DECLARATION:
1067
table = fGlobalAttrDecls;
1069
case XSConstants.ELEMENT_DECLARATION:
1070
table = fGlobalElemDecls;
1072
case XSConstants.ATTRIBUTE_GROUP:
1073
table = fGlobalAttrGrpDecls;
1075
case XSConstants.MODEL_GROUP_DEFINITION:
1076
table = fGlobalGroupDecls;
1078
case XSConstants.NOTATION_DECLARATION:
1079
table = fGlobalNotationDecls;
1083
// for complex/simple types, create a special implementation,
1084
// which take specific types out of the hash table
1085
if (objectType == XSTypeDefinition.COMPLEX_TYPE ||
1086
objectType == XSTypeDefinition.SIMPLE_TYPE) {
1087
fComponents[objectType] = new XSNamedMap4Types(fTargetNamespace, table, objectType);
1090
fComponents[objectType] = new XSNamedMapImpl(fTargetNamespace, table);
1094
return fComponents[objectType];
1098
* Convenience method. Returns a top-level simple or complex type
1100
* @param name The name of the definition.
1101
* @return An <code>XSTypeDefinition</code> or null if such definition
1104
public XSTypeDefinition getTypeDefinition(String name) {
1105
return getGlobalTypeDecl(name);
1109
* Convenience method. Returns a top-level attribute declaration.
1110
* @param name The name of the declaration.
1111
* @return A top-level attribute declaration or null if such declaration
1114
public XSAttributeDeclaration getAttributeDeclaration(String name) {
1115
return getGlobalAttributeDecl(name);
1119
* Convenience method. Returns a top-level element declaration.
1120
* @param name The name of the declaration.
1121
* @return A top-level element declaration or null if such declaration
1124
public XSElementDeclaration getElementDeclaration(String name) {
1125
return getGlobalElementDecl(name);
1129
* Convenience method. Returns a top-level attribute group definition.
1130
* @param name The name of the definition.
1131
* @return A top-level attribute group definition or null if such
1132
* definition does not exist.
1134
public XSAttributeGroupDefinition getAttributeGroup(String name) {
1135
return getGlobalAttributeGroupDecl(name);
1139
* Convenience method. Returns a top-level model group definition.
1141
* @param name The name of the definition.
1142
* @return A top-level model group definition definition or null if such
1143
* definition does not exist.
1145
public XSModelGroupDefinition getModelGroupDefinition(String name) {
1146
return getGlobalGroupDecl(name);
1150
* Convenience method. Returns a top-level notation declaration.
1152
* @param name The name of the declaration.
1153
* @return A top-level notation declaration or null if such declaration
1156
public XSNotationDeclaration getNotationDeclaration(String name) {
1157
return getGlobalNotationDecl(name);
1162
* [document location]
1163
* @see <a href="http://www.w3.org/TR/xmlschema-1/#sd-document_location">[document location]</a>
1164
* @return a list of document information item
1166
public StringList getDocumentLocations() {
1167
return new StringListImpl(fLocations);
1171
* Return an <code>XSModel</code> that represents components in this schema
1174
* @return an <code>XSModel</code> representing this schema grammar
1176
public XSModel toXSModel() {
1177
return new XSModelImpl(new SchemaGrammar[]{this});
1180
public XSModel toXSModel(XSGrammar[] grammars) {
1181
if (grammars == null || grammars.length == 0)
1184
int len = grammars.length;
1185
boolean hasSelf = false;
1186
for (int i = 0; i < len; i++) {
1187
if (grammars[i] == this) {
1193
SchemaGrammar[] gs = new SchemaGrammar[hasSelf ? len : len+1];
1194
for (int i = 0; i < len; i++)
1195
gs[i] = (SchemaGrammar)grammars[i];
1198
return new XSModelImpl(gs);
1202
* @see org.apache.xerces.xs.XSNamespaceItem#getAnnotations()
1204
public XSObjectList getAnnotations() {
1205
return new XSObjectListImpl(fAnnotations, fNumAnnotations);
1208
public void addAnnotation(XSAnnotationImpl annotation) {
1209
if(annotation == null)
1211
if(fAnnotations == null) {
1212
fAnnotations = new XSAnnotationImpl[2];
1213
} else if(fNumAnnotations == fAnnotations.length) {
1214
XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
1215
System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
1216
fAnnotations = newArray;
1218
fAnnotations[fNumAnnotations++] = annotation;
1221
} // class SchemaGrammar