KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.traversers;
18
19 import java.util.Stack JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.xerces.impl.validation.ValidationState;
23 import org.apache.xerces.impl.xs.SchemaNamespaceSupport;
24 import org.apache.xerces.impl.xs.SchemaSymbols;
25 import org.apache.xerces.impl.xs.XMLSchemaException;
26 import org.apache.xerces.impl.xs.util.XInt;
27 import org.apache.xerces.util.SymbolTable;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * Objects of this class hold all information pecular to a
32  * particular XML Schema document. This is needed because
33  * namespace bindings and other settings on the <schema/> element
34  * affect the contents of that schema document alone.
35  *
36  * @xerces.internal
37  *
38  * @author Neil Graham, IBM
39  * @version $Id: XSDocumentInfo.java,v 1.21 2005/05/30 04:17:12 mrglavas Exp $
40  */

41 class XSDocumentInfo {
42
43     // Data
44
protected SchemaNamespaceSupport fNamespaceSupport;
45     protected SchemaNamespaceSupport fNamespaceSupportRoot;
46     protected Stack JavaDoc SchemaNamespaceSupportStack = new Stack JavaDoc();
47
48     // schema's attributeFormDefault
49
protected boolean fAreLocalAttributesQualified;
50
51     // elementFormDefault
52
protected boolean fAreLocalElementsQualified;
53
54     // [block | final]Default
55
protected short fBlockDefault;
56     protected short fFinalDefault;
57
58     // targetNamespace
59
String JavaDoc fTargetNamespace;
60
61     // represents whether this is a chameleon schema (i.e., whether its TNS is natural or comes from without)
62
protected boolean fIsChameleonSchema;
63
64     // the root of the schema Document tree itself
65
protected Element JavaDoc fSchemaElement;
66
67     // all namespaces that this document can refer to
68
Vector JavaDoc fImportedNS = new Vector JavaDoc();
69     
70     protected ValidationState fValidationContext = new ValidationState();
71
72     SymbolTable fSymbolTable = null;
73
74     // attribute checker to which we'll return the attributes
75
// once we've been told that we're done with them
76
protected XSAttributeChecker fAttrChecker;
77
78     // array of objects on the schema's root element. This is null
79
// once returnSchemaAttrs has been called.
80
protected Object JavaDoc [] fSchemaAttrs;
81     
82     // list of annotations contained in the schema document. This is null
83
// once removeAnnotations has been called.
84
protected XSAnnotationInfo fAnnotations = null;
85
86     // note that the caller must ensure to call returnSchemaAttrs()
87
// to avoid memory leaks!
88
XSDocumentInfo (Element JavaDoc schemaRoot, XSAttributeChecker attrChecker, SymbolTable symbolTable)
89                     throws XMLSchemaException {
90         fSchemaElement = schemaRoot;
91         fNamespaceSupport = new SchemaNamespaceSupport();
92         fNamespaceSupport.reset();
93         fIsChameleonSchema = false;
94
95         fSymbolTable = symbolTable;
96         fAttrChecker = attrChecker;
97
98         if (schemaRoot != null) {
99             Element JavaDoc root = schemaRoot;
100             fSchemaAttrs = attrChecker.checkAttributes(root, true, this);
101             // schemaAttrs == null means it's not an <xsd:schema> element
102
// throw an exception, but we don't know the document systemId,
103
// so we leave that to the caller.
104
if (fSchemaAttrs == null) {
105                 throw new XMLSchemaException(null, null);
106             }
107             fAreLocalAttributesQualified =
108                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_AFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
109             fAreLocalElementsQualified =
110                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_EFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
111             fBlockDefault =
112                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_BLOCKDEFAULT]).shortValue();
113             fFinalDefault =
114                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_FINALDEFAULT]).shortValue();
115             fTargetNamespace =
116                 (String JavaDoc)fSchemaAttrs[XSAttributeChecker.ATTIDX_TARGETNAMESPACE];
117             if (fTargetNamespace != null)
118                 fTargetNamespace = symbolTable.addSymbol(fTargetNamespace);
119
120             fNamespaceSupportRoot = new SchemaNamespaceSupport(fNamespaceSupport);
121
122             //set namespace support
123
fValidationContext.setNamespaceSupport(fNamespaceSupport);
124             fValidationContext.setSymbolTable(symbolTable);
125             // pass null as the schema document, so that the namespace
126
// context is not popped.
127

128             // don't return the attribute array yet!
129
//attrChecker.returnAttrArray(schemaAttrs, null);
130
}
131     }
132
133     // backup the current ns support, and use the one passed-in.
134
// if no ns support is passed-in, use the one for <schema> element
135
void backupNSSupport(SchemaNamespaceSupport nsSupport) {
136         SchemaNamespaceSupportStack.push(fNamespaceSupport);
137         if (nsSupport == null)
138             nsSupport = fNamespaceSupportRoot;
139         fNamespaceSupport = new SchemaNamespaceSupport(nsSupport);
140
141         fValidationContext.setNamespaceSupport(fNamespaceSupport);
142     }
143
144     void restoreNSSupport() {
145         fNamespaceSupport = (SchemaNamespaceSupport)SchemaNamespaceSupportStack.pop();
146         fValidationContext.setNamespaceSupport(fNamespaceSupport);
147     }
148
149     // some Object methods
150
public String JavaDoc toString() {
151         return fTargetNamespace == null?"no targetNamspace":"targetNamespace is " + fTargetNamespace;
152     }
153
154     public void addAllowedNS(String JavaDoc namespace) {
155         fImportedNS.addElement(namespace == null ? "" : namespace);
156     }
157     
158     public boolean isAllowedNS(String JavaDoc namespace) {
159         return fImportedNS.contains(namespace == null ? "" : namespace);
160     }
161     
162     // store whether we have reported an error about that this document
163
// can't access components from the given namespace
164
private Vector JavaDoc fReportedTNS = null;
165     // check whether we need to report an error against the given uri.
166
// if we have reported an error, then we don't need to report again;
167
// otherwise we reported the error, and remember this fact.
168
final boolean needReportTNSError(String JavaDoc uri) {
169         if (fReportedTNS == null)
170             fReportedTNS = new Vector JavaDoc();
171         else if (fReportedTNS.contains(uri))
172             return false;
173         fReportedTNS.addElement(uri);
174         return true;
175     }
176
177     // return the attributes on the schema element itself:
178
Object JavaDoc [] getSchemaAttrs () {
179         return fSchemaAttrs;
180     }
181
182     // deallocate the storage set aside for the schema element's
183
// attributes
184
void returnSchemaAttrs () {
185         fAttrChecker.returnAttrArray (fSchemaAttrs, null);
186         fSchemaAttrs = null;
187     }
188     
189     // adds an annotation to the list of annotations
190
void addAnnotation(XSAnnotationInfo info) {
191         info.next = fAnnotations;
192         fAnnotations = info;
193     }
194     
195     // returns the list of annotations conatined in the
196
// schema document or null if the document contained no annotations.
197
XSAnnotationInfo getAnnotations() {
198         return fAnnotations;
199     }
200     
201     // removes reference to annotation list
202
void removeAnnotations() {
203         fAnnotations = null;
204     }
205     
206 } // XSDocumentInfo
207
Popular Tags