KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 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
58 package com.sun.org.apache.xerces.internal.impl.xs;
59
60 import java.util.Vector JavaDoc;
61
62 import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory;
63 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
64 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
65 import com.sun.org.apache.xerces.internal.impl.xs.identity.IdentityConstraint;
66 import com.sun.org.apache.xerces.internal.xs.StringList;
67 import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
68 import com.sun.org.apache.xerces.internal.xs.XSAttributeDeclaration;
69 import com.sun.org.apache.xerces.internal.xs.XSAttributeGroupDefinition;
70 import com.sun.org.apache.xerces.internal.xs.XSConstants;
71 import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
72 import com.sun.org.apache.xerces.internal.xs.XSModel;
73 import com.sun.org.apache.xerces.internal.xs.XSModelGroupDefinition;
74 import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
75 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
76 import com.sun.org.apache.xerces.internal.xs.XSNotationDeclaration;
77 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
78 import com.sun.org.apache.xerces.internal.xs.XSParticle;
79 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
80 import com.sun.org.apache.xerces.internal.xs.XSWildcard;
81 import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
82 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
83 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMap4Types;
84 import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl;
85 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
86 import com.sun.org.apache.xerces.internal.impl.Constants;
87 import com.sun.org.apache.xerces.internal.parsers.DOMParser;
88 import com.sun.org.apache.xerces.internal.parsers.SAXParser;
89 import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;
90 import com.sun.org.apache.xerces.internal.util.SymbolHash;
91 import com.sun.org.apache.xerces.internal.util.SymbolTable;
92 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
93 import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar;
94
95 /**
96  * This class is to hold all schema component declaration that are declared
97  * within one namespace.
98  *
99  * The Grammar class this class extends contains what little
100  * commonality there is between XML Schema and DTD grammars. It's
101  * useful to distinguish grammar objects from other kinds of object
102  * when they exist in pools or caches.
103  *
104  * @author Sandy Gao, IBM
105  * @author Elena Litani, IBM
106  *
107  * @version $Id: SchemaGrammar.java,v 1.35 2004/02/03 17:11:09 sandygao Exp $
108  */

109
110 public class SchemaGrammar implements XSGrammar, XSNamespaceItem {
111
112     // the target namespace of grammar
113
String JavaDoc fTargetNamespace;
114
115     // global decls: map from decl name to decl object
116
SymbolHash fGlobalAttrDecls;
117     SymbolHash fGlobalAttrGrpDecls;
118     SymbolHash fGlobalElemDecls;
119     SymbolHash fGlobalGroupDecls;
120     SymbolHash fGlobalNotationDecls;
121     SymbolHash fGlobalIDConstraintDecls;
122     SymbolHash fGlobalTypeDecls;
123
124     // the XMLGrammarDescription member
125
XSDDescription fGrammarDescription = null;
126
127     // annotations associated with the "root" schema of this targetNamespace
128
XSAnnotationImpl [] fAnnotations = null;
129
130     // number of annotations declared
131
int fNumAnnotations;
132
133     // symbol table for constructing parsers (annotation support)
134
private SymbolTable fSymbolTable = null;
135     // parsers for annotation support
136
private SAXParser fSAXParser = null;
137     private DOMParser fDOMParser = null;
138
139     //
140
// Constructors
141
//
142

143     // needed to make BuiltinSchemaGrammar work.
144
private SchemaGrammar() {}
145
146     /**
147      * Default constructor.
148      *
149      * @param targetNamespace
150      * @param grammarDesc the XMLGrammarDescription corresponding to this objec
151      * at the least a systemId should always be known.
152      * @param symbolTable needed for annotation support
153      */

154     public SchemaGrammar(String JavaDoc targetNamespace, XSDDescription grammarDesc,
155                 SymbolTable symbolTable) {
156         fTargetNamespace = targetNamespace;
157         fGrammarDescription = grammarDesc;
158         fSymbolTable = symbolTable;
159
160         // REVISIT: do we know the numbers of the following global decls
161
// when creating this grammar? If so, we can pass the numbers in,
162
// and use that number to initialize the following hashtables.
163
fGlobalAttrDecls = new SymbolHash();
164         fGlobalAttrGrpDecls = new SymbolHash();
165         fGlobalElemDecls = new SymbolHash();
166         fGlobalGroupDecls = new SymbolHash();
167         fGlobalNotationDecls = new SymbolHash();
168         fGlobalIDConstraintDecls = new SymbolHash();
169
170         // if we are parsing S4S, put built-in types in first
171
// they might get overwritten by the types from S4S, but that's
172
// considered what the application wants to do.
173
if (fTargetNamespace == SchemaSymbols.URI_SCHEMAFORSCHEMA)
174             fGlobalTypeDecls = SG_SchemaNS.fGlobalTypeDecls.makeClone();
175         else
176             fGlobalTypeDecls = new SymbolHash();
177     } // <init>(String, XSDDescription)
178

179     // number of built-in XSTypes we need to create for base and full
180
// datatype set
181
private static final int BASICSET_COUNT = 29;
182     private static final int FULLSET_COUNT = 46;
183
184     private static final int GRAMMAR_XS = 1;
185     private static final int GRAMMAR_XSI = 2;
186
187     // this class makes sure the static, built-in schema grammars
188
// are immutable.
189
public static class BuiltinSchemaGrammar extends SchemaGrammar {
190         /**
191          * Special constructor to create the grammars for the schema namespaces
192          *
193          * @param grammar
194          */

195         public BuiltinSchemaGrammar(int grammar) {
196             SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
197     
198             if (grammar == GRAMMAR_XS) {
199                 // target namespace
200
fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
201         
202                 // grammar description
203
fGrammarDescription = new XSDDescription();
204                 fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
205                 fGrammarDescription.setNamespace(SchemaSymbols.URI_SCHEMAFORSCHEMA);
206         
207                 // no global decls other than types
208
fGlobalAttrDecls = new SymbolHash(1);
209                 fGlobalAttrGrpDecls = new SymbolHash(1);
210                 fGlobalElemDecls = new SymbolHash(1);
211                 fGlobalGroupDecls = new SymbolHash(1);
212                 fGlobalNotationDecls = new SymbolHash(1);
213                 fGlobalIDConstraintDecls = new SymbolHash(1);
214         
215                 // get all built-in types
216
fGlobalTypeDecls = schemaFactory.getBuiltInTypes();
217                 // add anyType
218
fGlobalTypeDecls.put(fAnyType.getName(), fAnyType);
219             }
220             else if (grammar == GRAMMAR_XSI) {
221                 // target namespace
222
fTargetNamespace = SchemaSymbols.URI_XSI;
223                 // grammar description
224
fGrammarDescription = new XSDDescription();
225                 fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
226                 fGrammarDescription.setNamespace(SchemaSymbols.URI_XSI);
227         
228                 // no global decls other than attributes
229
fGlobalAttrGrpDecls = new SymbolHash(1);
230                 fGlobalElemDecls = new SymbolHash(1);
231                 fGlobalGroupDecls = new SymbolHash(1);
232                 fGlobalNotationDecls = new SymbolHash(1);
233                 fGlobalIDConstraintDecls = new SymbolHash(1);
234                 fGlobalTypeDecls = new SymbolHash(1);
235     
236                 // 4 attributes, so initialize the size as 4*2 = 8
237
fGlobalAttrDecls = new SymbolHash(8);
238                 String JavaDoc name = null;
239                 String JavaDoc tns = null;
240                 XSSimpleType type = null;
241                 short scope = XSConstants.SCOPE_GLOBAL;
242                 
243                 // xsi:type
244
name = SchemaSymbols.XSI_TYPE;
245                 tns = SchemaSymbols.URI_XSI;
246                 type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_QNAME);
247                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
248                 
249                 // xsi:nil
250
name = SchemaSymbols.XSI_NIL;
251                 tns = SchemaSymbols.URI_XSI;
252                 type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_BOOLEAN);
253                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
254                 
255                 XSSimpleType anyURI = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_ANYURI);
256     
257                 // xsi:schemaLocation
258
name = SchemaSymbols.XSI_SCHEMALOCATION;
259                 tns = SchemaSymbols.URI_XSI;
260                 type = schemaFactory.createTypeList(null, SchemaSymbols.URI_XSI, (short)0, anyURI, null);
261                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
262                 
263                 // xsi:noNamespaceSchemaLocation
264
name = SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION;
265                 tns = SchemaSymbols.URI_XSI;
266                 type = anyURI;
267                 fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
268             }
269         } // <init>(int)
270

271         // return the XMLGrammarDescription corresponding to this
272
// object
273
public XMLGrammarDescription getGrammarDescription() {
274             return fGrammarDescription.makeClone();
275         } // getGrammarDescription(): XMLGrammarDescription
276

277         // override these methods solely so that these
278
// objects cannot be modified once they're created.
279
public void setImportedGrammars(Vector JavaDoc importedGrammars) {
280             // ignore
281
}
282         public void addGlobalAttributeDecl(XSAttributeDecl decl) {
283             // ignore
284
}
285         public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
286             // ignore
287
}
288         public void addGlobalElementDecl(XSElementDecl decl) {
289             // ignore
290
}
291         public void addGlobalGroupDecl(XSGroupDecl decl) {
292             // ignore
293
}
294         public void addGlobalNotationDecl(XSNotationDecl decl) {
295             // ignore
296
}
297         public void addGlobalTypeDecl(XSTypeDefinition decl) {
298             // ignore
299
}
300         public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
301             // ignore
302
}
303         public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
304             // ignore
305
}
306         public synchronized void addDocument(Object JavaDoc document, String JavaDoc location) {
307             // ignore
308
}
309
310         // annotation support
311
synchronized DOMParser getDOMParser() {
312             return null;
313         }
314         synchronized SAXParser getSAXParser() {
315             return null;
316         }
317     }
318
319     // Grammar methods
320

321     // return the XMLGrammarDescription corresponding to this
322
// object
323
public XMLGrammarDescription getGrammarDescription() {
324         return fGrammarDescription;
325     } // getGrammarDescription(): XMLGrammarDescription
326

327     // DTDGrammar methods
328
public boolean isNamespaceAware () {
329         return true;
330     } // isNamespaceAware():boolean
331

332     Vector JavaDoc fImported = null;
333
334     public void setImportedGrammars(Vector JavaDoc importedGrammars) {
335         fImported = importedGrammars;
336     }
337
338     public Vector JavaDoc getImportedGrammars() {
339         return fImported;
340     }
341
342     /**
343      * Returns this grammar's target namespace.
344      */

345     public final String JavaDoc getTargetNamespace() {
346         return fTargetNamespace;
347     } // getTargetNamespace():String
348

349     /**
350      * register one global attribute
351      */

352     public void addGlobalAttributeDecl(XSAttributeDecl decl) {
353         fGlobalAttrDecls.put(decl.fName, decl);
354     }
355
356     /**
357      * register one global attribute group
358      */

359     public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
360         fGlobalAttrGrpDecls.put(decl.fName, decl);
361     }
362
363     /**
364      * register one global element
365      */

366     public void addGlobalElementDecl(XSElementDecl decl) {
367         fGlobalElemDecls.put(decl.fName, decl);
368
369         // if there is a substitution group affiliation, store in an array,
370
// for further constraint checking: UPA, PD, EDC
371
if (decl.fSubGroup != null) {
372             if (fSubGroupCount == fSubGroups.length)
373                 fSubGroups = resize(fSubGroups, fSubGroupCount+INC_SIZE);
374             fSubGroups[fSubGroupCount++] = decl;
375         }
376     }
377
378     /**
379      * register one global group
380      */

381     public void addGlobalGroupDecl(XSGroupDecl decl) {
382         fGlobalGroupDecls.put(decl.fName, decl);
383     }
384
385     /**
386      * register one global notation
387      */

388     public void addGlobalNotationDecl(XSNotationDecl decl) {
389         fGlobalNotationDecls.put(decl.fName, decl);
390     }
391
392     /**
393      * register one global type
394      */

395     public void addGlobalTypeDecl(XSTypeDefinition decl) {
396         fGlobalTypeDecls.put(decl.getName(), decl);
397     }
398
399     /**
400      * register one identity constraint
401      */

402     public final void addIDConstraintDecl(XSElementDecl elmDecl, IdentityConstraint decl) {
403         elmDecl.addIDConstraint(decl);
404         fGlobalIDConstraintDecls.put(decl.getIdentityConstraintName(), decl);
405     }
406
407     /**
408      * get one global attribute
409      */

410     public final XSAttributeDecl getGlobalAttributeDecl(String JavaDoc declName) {
411         return(XSAttributeDecl)fGlobalAttrDecls.get(declName);
412     }
413
414     /**
415      * get one global attribute group
416      */

417     public final XSAttributeGroupDecl getGlobalAttributeGroupDecl(String JavaDoc declName) {
418         return(XSAttributeGroupDecl)fGlobalAttrGrpDecls.get(declName);
419     }
420
421     /**
422      * get one global element
423      */

424     public final XSElementDecl getGlobalElementDecl(String JavaDoc declName) {
425         return(XSElementDecl)fGlobalElemDecls.get(declName);
426     }
427
428     /**
429      * get one global group
430      */

431     public final XSGroupDecl getGlobalGroupDecl(String JavaDoc declName) {
432         return(XSGroupDecl)fGlobalGroupDecls.get(declName);
433     }
434
435     /**
436      * get one global notation
437      */

438     public final XSNotationDecl getGlobalNotationDecl(String JavaDoc declName) {
439         return(XSNotationDecl)fGlobalNotationDecls.get(declName);
440     }
441
442     /**
443      * get one global type
444      */

445     public final XSTypeDefinition getGlobalTypeDecl(String JavaDoc declName) {
446         return(XSTypeDefinition)fGlobalTypeDecls.get(declName);
447     }
448
449     /**
450      * get one identity constraint
451      */

452     public final IdentityConstraint getIDConstraintDecl(String JavaDoc declName) {
453         return(IdentityConstraint)fGlobalIDConstraintDecls.get(declName);
454     }
455
456     /**
457      * get one identity constraint
458      */

459     public final boolean hasIDConstraints() {
460         return fGlobalIDConstraintDecls.getLength() > 0;
461     }
462
463     // array to store complex type decls
464
private static final int INITIAL_SIZE = 16;
465     private static final int INC_SIZE = 16;
466
467     private int fCTCount = 0;
468     private XSComplexTypeDecl[] fComplexTypeDecls = new XSComplexTypeDecl[INITIAL_SIZE];
469     private SimpleLocator[] fCTLocators = new SimpleLocator[INITIAL_SIZE];
470
471     // an array to store groups being redefined by restriction
472
// even-numbered elements are the derived groups, odd-numbered ones their bases
473
private static final int REDEFINED_GROUP_INIT_SIZE = 2;
474     private int fRGCount = 0;
475     private XSGroupDecl[] fRedefinedGroupDecls = new XSGroupDecl[REDEFINED_GROUP_INIT_SIZE];
476     private SimpleLocator[] fRGLocators = new SimpleLocator[REDEFINED_GROUP_INIT_SIZE/2];
477
478     // a flag to indicate whether we have checked the 3 constraints on this
479
// grammar.
480
boolean fFullChecked = false;
481
482     /**
483      * add one complex type decl: for later constraint checking
484      */

485     public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
486         if (fCTCount == fComplexTypeDecls.length) {
487             fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount+INC_SIZE);
488             fCTLocators = resize(fCTLocators, fCTCount+INC_SIZE);
489         }
490         fCTLocators[fCTCount] = locator;
491         fComplexTypeDecls[fCTCount++] = decl;
492     }
493
494     /**
495      * add a group redefined by restriction: for later constraint checking
496      */

497     public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
498         if (fRGCount == fRedefinedGroupDecls.length) {
499             // double array size each time.
500
fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount << 1);
501             fRGLocators = resize(fRGLocators, fRGCount);
502         }
503         fRGLocators[fRGCount/2] = locator;
504         fRedefinedGroupDecls[fRGCount++] = derived;
505         fRedefinedGroupDecls[fRGCount++] = base;
506     }
507
508     /**
509      * get all complex type decls: for later constraint checking
510      */

511     final XSComplexTypeDecl[] getUncheckedComplexTypeDecls() {
512         if (fCTCount < fComplexTypeDecls.length) {
513             fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
514             fCTLocators = resize(fCTLocators, fCTCount);
515         }
516         return fComplexTypeDecls;
517     }
518
519     /**
520      * get the error locator of all complex type decls
521      */

522     final SimpleLocator[] getUncheckedCTLocators() {
523         if (fCTCount < fCTLocators.length) {
524             fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
525             fCTLocators = resize(fCTLocators, fCTCount);
526         }
527         return fCTLocators;
528     }
529
530     /**
531      * get all redefined groups: for later constraint checking
532      */

533     final XSGroupDecl[] getRedefinedGroupDecls() {
534         if (fRGCount < fRedefinedGroupDecls.length) {
535             fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
536             fRGLocators = resize(fRGLocators, fRGCount/2);
537         }
538         return fRedefinedGroupDecls;
539     }
540
541     /**
542      * get the error locator of all redefined groups
543      */

544     final SimpleLocator[] getRGLocators() {
545         if (fRGCount < fRedefinedGroupDecls.length) {
546             fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
547             fRGLocators = resize(fRGLocators, fRGCount/2);
548         }
549         return fRGLocators;
550     }
551
552     /**
553      * after the first-round checking, some types don't need to be checked
554      * against UPA again. here we trim the array to the proper size.
555      */

556     final void setUncheckedTypeNum(int newSize) {
557         fCTCount = newSize;
558         fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
559         fCTLocators = resize(fCTLocators, fCTCount);
560     }
561
562     // used to store all substitution group information declared in
563
// this namespace
564
private int fSubGroupCount = 0;
565     private XSElementDecl[] fSubGroups = new XSElementDecl[INITIAL_SIZE];
566
567     /**
568      * get all substitution group information: for the 3 constraint checking
569      */

570     final XSElementDecl[] getSubstitutionGroups() {
571         if (fSubGroupCount < fSubGroups.length)
572             fSubGroups = resize(fSubGroups, fSubGroupCount);
573         return fSubGroups;
574     }
575
576     // anyType and anySimpleType: because there are so many places where
577
// we need direct access to these two types
578
public final static XSComplexTypeDecl fAnyType = new XSAnyType();
579     private static class XSAnyType extends XSComplexTypeDecl {
580         public XSAnyType () {
581             fName = SchemaSymbols.ATTVAL_ANYTYPE;
582             super.fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
583             fBaseType = this;
584             fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
585             fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
586
587             fParticle = null;
588             fAttrGrp = null;
589         }
590
591         // overridden methods
592
public void setValues(String JavaDoc name, String JavaDoc targetNamespace,
593                 XSTypeDefinition baseType, short derivedBy, short schemaFinal,
594                 short block, short contentType,
595                 boolean isAbstract, XSAttributeGroupDecl attrGrp,
596                 XSSimpleType simpleType, XSParticleDecl particle) {
597             // don't allow this.
598
}
599
600         public void setName(String JavaDoc name){
601             // don't allow this.
602
}
603
604         public void setIsAbstractType() {
605             // null implementation
606
}
607
608         public void setContainsTypeID() {
609             // null implementation
610
}
611
612         public void setIsAnonymous() {
613             // null implementation
614
}
615
616         public void reset() {
617             // null implementation
618
}
619
620         public XSObjectList getAttributeUses() {
621             return new XSObjectListImpl(null, 0);
622         }
623
624         public XSAttributeGroupDecl getAttrGrp() {
625             XSWildcardDecl wildcard = new XSWildcardDecl();
626             wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
627             XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
628             attrGrp.fAttributeWC = wildcard;
629             return attrGrp;
630         }
631
632         public XSWildcard getAttributeWildcard() {
633             XSWildcardDecl wildcard = new XSWildcardDecl();
634             wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
635             return wildcard;
636         }
637
638         public XSParticle getParticle() {
639             // the wildcard used in anyType (content and attribute)
640
// the spec will change strict to skip for anyType
641
XSWildcardDecl wildcard = new XSWildcardDecl();
642             wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
643             // the particle for the content wildcard
644
XSParticleDecl particleW = new XSParticleDecl();
645             particleW.fMinOccurs = 0;
646             particleW.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
647             particleW.fType = XSParticleDecl.PARTICLE_WILDCARD;
648             particleW.fValue = wildcard;
649             // the model group of a sequence of the above particle
650
XSModelGroupImpl group = new XSModelGroupImpl();
651             group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
652             group.fParticleCount = 1;
653             group.fParticles = new XSParticleDecl[1];
654             group.fParticles[0] = particleW;
655             // the content of anyType: particle of the above model group
656
XSParticleDecl particleG = new XSParticleDecl();
657             particleG.fType = XSParticleDecl.PARTICLE_MODELGROUP;
658             particleG.fValue = group;
659         
660             return particleG;
661         }
662
663         public XSObjectList getAnnotations() {
664             return null;
665         }
666     }
667     private static class BuiltinAttrDecl extends XSAttributeDecl {
668         public BuiltinAttrDecl(String JavaDoc name, String JavaDoc tns,
669                 XSSimpleType type, short scope) {
670             fName = name;
671             super.fTargetNamespace = tns;
672             fType = type;
673             fScope = scope;
674         }
675
676         public void setValues(String JavaDoc name, String JavaDoc targetNamespace,
677                 XSSimpleType simpleType, short constraintType, short scope,
678                 ValidatedInfo valInfo, XSComplexTypeDecl enclosingCT) {
679             // ignore this call.
680
}
681
682         public void reset () {
683             // also ignore this call.
684
}
685         public XSAnnotation getAnnotation() {
686             return null;
687         }
688     } // class BuiltinAttrDecl
689

690     // the grammars to hold components of the schema namespace
691
public final static BuiltinSchemaGrammar SG_SchemaNS = new BuiltinSchemaGrammar(GRAMMAR_XS);
692
693     public final static XSSimpleType fAnySimpleType = (XSSimpleType)SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_ANYSIMPLETYPE);
694
695     // the grammars to hold components of the schema-instance namespace
696
public final static BuiltinSchemaGrammar SG_XSI = new BuiltinSchemaGrammar(GRAMMAR_XSI);
697
698     static final XSComplexTypeDecl[] resize(XSComplexTypeDecl[] oldArray, int newSize) {
699         XSComplexTypeDecl[] newArray = new XSComplexTypeDecl[newSize];
700         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
701         return newArray;
702     }
703
704     static final XSGroupDecl[] resize(XSGroupDecl[] oldArray, int newSize) {
705         XSGroupDecl[] newArray = new XSGroupDecl[newSize];
706         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
707         return newArray;
708     }
709
710     static final XSElementDecl[] resize(XSElementDecl[] oldArray, int newSize) {
711         XSElementDecl[] newArray = new XSElementDecl[newSize];
712         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
713         return newArray;
714     }
715
716     static final SimpleLocator[] resize(SimpleLocator[] oldArray, int newSize) {
717         SimpleLocator[] newArray = new SimpleLocator[newSize];
718         System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
719         return newArray;
720     }
721
722     // XSNamespaceItem methods
723

724     // the max index / the max value of XSObject type
725
private static final short MAX_COMP_IDX = XSTypeDefinition.SIMPLE_TYPE;
726     private static final boolean[] GLOBAL_COMP = {false, // null
727
true, // attribute
728
true, // element
729
true, // type
730
false, // attribute use
731
true, // attribute group
732
true, // group
733
false, // model group
734
false, // particle
735
false, // wildcard
736
false, // idc
737
true, // notation
738
false, // annotation
739
true, // complex type
740
true // simple type
741
};
742                                                  
743     // store a certain kind of components from all namespaces
744
private XSNamedMap[] fComponents = null;
745
746     // store the documents and their locations contributing to this namespace
747
// REVISIT: use StringList and XSObjectList for there fields.
748
private Vector JavaDoc fDocuments = null;
749     private Vector JavaDoc fLocations = null;
750     
751     public synchronized void addDocument(Object JavaDoc document, String JavaDoc location) {
752         if (fDocuments == null) {
753             fDocuments = new Vector JavaDoc();
754             fLocations = new Vector JavaDoc();
755         }
756         fDocuments.addElement(document);
757         fLocations.addElement(location);
758     }
759     
760     /**
761      * [schema namespace]
762      * @see <a HREF="http://www.w3.org/TR/xmlschema-1/#nsi-schema_namespace">[schema namespace]</a>
763      * @return The target namespace of this item.
764      */

765     public String JavaDoc getSchemaNamespace() {
766         return fTargetNamespace;
767     }
768
769     // annotation support
770
synchronized DOMParser getDOMParser() {
771         if (fDOMParser != null) return fDOMParser;
772         // REVISIT: when schema handles XML 1.1, will need to
773
// revisit this (and the practice of not prepending an XML decl to the annotation string
774
IntegratedParserConfiguration config = new IntegratedParserConfiguration(fSymbolTable);
775         // note that this should never produce errors or require
776
// entity resolution, so just a barebones configuration with
777
// a couple of feature set will do fine
778
config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
779         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
780         fDOMParser = new DOMParser(config);
781         return fDOMParser;
782     }
783
784     synchronized SAXParser getSAXParser() {
785         if (fSAXParser != null) return fSAXParser;
786         // REVISIT: when schema handles XML 1.1, will need to
787
// revisit this (and the practice of not prepending an XML decl to the annotation string
788
IntegratedParserConfiguration config = new IntegratedParserConfiguration(fSymbolTable);
789         // note that this should never produce errors or require
790
// entity resolution, so just a barebones configuration with
791
// a couple of feature set will do fine
792
config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
793         config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
794         fSAXParser = new SAXParser(config);
795         return fSAXParser;
796     }
797
798     /**
799      * Returns a list of top-level components, i.e. element declarations,
800      * attribute declarations, etc.<p>
801      * Note that <code>XSTypeDefinition#SIMPLE_TYPE</code> and
802      * <code>XSTypeDefinition#COMPLEX_TYPE</code> can also be used as the
803      * <code>objectType</code> to retrieve only complex types or simple types,
804      * instead of all types.
805      * @param objectType The type of the declaration, i.e.
806      * ELEMENT_DECLARATION, ATTRIBUTE_DECLARATION, etc.
807      * @return A list of top-level definition of the specified type in
808      * <code>objectType</code> or <code>null</code>.
809      */

810     public synchronized XSNamedMap getComponents(short objectType) {
811         if (objectType <= 0 || objectType > MAX_COMP_IDX ||
812             !GLOBAL_COMP[objectType]) {
813             return null;
814         }
815         
816         if (fComponents == null)
817             fComponents = new XSNamedMap[MAX_COMP_IDX+1];
818
819         // get the hashtable for this type of components
820
if (fComponents[objectType] == null) {
821             SymbolHash table = null;
822             switch (objectType) {
823             case XSConstants.TYPE_DEFINITION:
824             case XSTypeDefinition.COMPLEX_TYPE:
825             case XSTypeDefinition.SIMPLE_TYPE:
826                 table = fGlobalTypeDecls;
827                 break;
828             case XSConstants.ATTRIBUTE_DECLARATION:
829                 table = fGlobalAttrDecls;
830                 break;
831             case XSConstants.ELEMENT_DECLARATION:
832                 table = fGlobalElemDecls;
833                 break;
834             case XSConstants.ATTRIBUTE_GROUP:
835                 table = fGlobalAttrGrpDecls;
836                 break;
837             case XSConstants.MODEL_GROUP_DEFINITION:
838                 table = fGlobalGroupDecls;
839                 break;
840             case XSConstants.NOTATION_DECLARATION:
841                 table = fGlobalNotationDecls;
842                 break;
843             }
844             
845             // for complex/simple types, create a special implementation,
846
// which take specific types out of the hash table
847
if (objectType == XSTypeDefinition.COMPLEX_TYPE ||
848                 objectType == XSTypeDefinition.SIMPLE_TYPE) {
849                 fComponents[objectType] = new XSNamedMap4Types(fTargetNamespace, table, objectType);
850             }
851             else {
852                 fComponents[objectType] = new XSNamedMapImpl(fTargetNamespace, table);
853             }
854         }
855         
856         return fComponents[objectType];
857     }
858
859     /**
860      * Convenience method. Returns a top-level simple or complex type
861      * definition.
862      * @param name The name of the definition.
863      * @return An <code>XSTypeDefinition</code> or null if such definition
864      * does not exist.
865      */

866     public XSTypeDefinition getTypeDefinition(String JavaDoc name) {
867         return getGlobalTypeDecl(name);
868     }
869
870     /**
871      * Convenience method. Returns a top-level attribute declaration.
872      * @param name The name of the declaration.
873      * @return A top-level attribute declaration or null if such declaration
874      * does not exist.
875      */

876     public XSAttributeDeclaration getAttributeDeclaration(String JavaDoc name) {
877         return getGlobalAttributeDecl(name);
878     }
879
880     /**
881      * Convenience method. Returns a top-level element declaration.
882      * @param name The name of the declaration.
883      * @return A top-level element declaration or null if such declaration
884      * does not exist.
885      */

886     public XSElementDeclaration getElementDeclaration(String JavaDoc name) {
887         return getGlobalElementDecl(name);
888     }
889
890     /**
891      * Convenience method. Returns a top-level attribute group definition.
892      * @param name The name of the definition.
893      * @return A top-level attribute group definition or null if such
894      * definition does not exist.
895      */

896     public XSAttributeGroupDefinition getAttributeGroup(String JavaDoc name) {
897         return getGlobalAttributeGroupDecl(name);
898     }
899
900     /**
901      * Convenience method. Returns a top-level model group definition.
902      *
903      * @param name The name of the definition.
904      * @return A top-level model group definition definition or null if such
905      * definition does not exist.
906      */

907     public XSModelGroupDefinition getModelGroupDefinition(String JavaDoc name) {
908         return getGlobalGroupDecl(name);
909     }
910
911     /**
912      * Convenience method. Returns a top-level notation declaration.
913      *
914      * @param name The name of the declaration.
915      * @return A top-level notation declaration or null if such declaration
916      * does not exist.
917      */

918     public XSNotationDeclaration getNotationDeclaration(String JavaDoc name) {
919         return getGlobalNotationDecl(name);
920     }
921
922
923     /**
924      * [document location]
925      * @see <a HREF="http://www.w3.org/TR/xmlschema-1/#sd-document_location">[document location]</a>
926      * @return a list of document information item
927      */

928     public StringList getDocumentLocations() {
929         return new StringListImpl(fLocations);
930     }
931     
932     /**
933      * Return an <code>XSModel</code> that represents components in this schema
934      * grammar.
935      *
936      * @return an <code>XSModel</code> representing this schema grammar
937      */

938     public XSModel toXSModel() {
939         return new XSModelImpl(new SchemaGrammar[]{this});
940     }
941
942     public XSModel toXSModel(XSGrammar[] grammars) {
943         if (grammars == null || grammars.length == 0)
944             return toXSModel();
945
946         int len = grammars.length;
947         boolean hasSelf = false;
948         for (int i = 0; i < len; i++) {
949             if (grammars[i] == this) {
950                 hasSelf = true;
951                 break;
952             }
953         }
954
955         SchemaGrammar[] gs = new SchemaGrammar[hasSelf ? len : len+1];
956         for (int i = 0; i < len; i++)
957             gs[i] = (SchemaGrammar)grammars[i];
958         if (!hasSelf)
959             gs[len] = this;
960         return new XSModelImpl(gs);
961     }
962
963     /**
964      * @see com.sun.org.apache.xerces.internal.xs.XSNamespaceItem#getAnnotations()
965      */

966     public XSObjectList getAnnotations() {
967         return new XSObjectListImpl(fAnnotations, fNumAnnotations);
968     }
969
970     public void addAnnotation(XSAnnotationImpl annotation) {
971         if(annotation == null)
972             return;
973         if(fAnnotations == null) {
974             fAnnotations = new XSAnnotationImpl[2];
975         } else if(fNumAnnotations == fAnnotations.length) {
976             XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
977             System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
978             fAnnotations = newArray;
979         }
980         fAnnotations[fNumAnnotations++] = annotation;
981     }
982
983 } // class SchemaGrammar
984
Popular Tags