KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, 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.traversers;
59
60 import java.util.Stack JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 import com.sun.org.apache.xerces.internal.impl.validation.ValidationState;
64 import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
65 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
66 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaException;
67 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
68 import com.sun.org.apache.xerces.internal.util.DOMUtil;
69 import com.sun.org.apache.xerces.internal.util.SymbolTable;
70 import org.w3c.dom.Document JavaDoc;
71 import org.w3c.dom.Element JavaDoc;
72
73 /*
74  * Objects of this class hold all information pecular to a
75  * particular XML Schema document. This is needed because
76  * namespace bindings and other settings on the <schema/> element
77  * affect the contents of that schema document alone.
78  *
79  * @author Neil Graham, IBM
80  * @version $Id: XSDocumentInfo.java,v 1.17 2003/07/03 15:17:16 neilg Exp $
81  */

82 class XSDocumentInfo {
83
84     // Data
85
protected SchemaNamespaceSupport fNamespaceSupport;
86     protected SchemaNamespaceSupport fNamespaceSupportRoot;
87     protected Stack JavaDoc SchemaNamespaceSupportStack = new Stack JavaDoc();
88
89     // schema's attributeFormDefault
90
protected boolean fAreLocalAttributesQualified;
91
92     // elementFormDefault
93
protected boolean fAreLocalElementsQualified;
94
95     // [block | final]Default
96
protected short fBlockDefault;
97     protected short fFinalDefault;
98
99     // targetNamespace
100
String JavaDoc fTargetNamespace;
101
102     // represents whether this is a chameleon schema (i.e., whether its TNS is natural or comes from without)
103
protected boolean fIsChameleonSchema;
104
105     // the root of the schema Document tree itself
106
protected Document JavaDoc fSchemaDoc;
107
108     // all namespaces that this document can refer to
109
Vector JavaDoc fImportedNS = new Vector JavaDoc();
110     
111     protected ValidationState fValidationContext = new ValidationState();
112
113     SymbolTable fSymbolTable = null;
114
115     // attribute checker to which we'll return the attributes
116
// once we've been told that we're done with them
117
protected XSAttributeChecker fAttrChecker;
118
119     // array of objects on the schema's root element. This is null
120
// once returnSchemaAttrs has been called.
121
protected Object JavaDoc [] fSchemaAttrs;
122
123     // note that the caller must ensure to call returnSchemaAttrs()
124
// to avoid memory leaks!
125
XSDocumentInfo (Document JavaDoc schemaDoc, XSAttributeChecker attrChecker, SymbolTable symbolTable)
126                     throws XMLSchemaException {
127         fSchemaDoc = schemaDoc;
128         fNamespaceSupport = new SchemaNamespaceSupport();
129         fNamespaceSupport.reset();
130         fIsChameleonSchema = false;
131
132         fSymbolTable = symbolTable;
133         fAttrChecker = attrChecker;
134
135         if(schemaDoc != null) {
136             Element JavaDoc root = DOMUtil.getRoot(schemaDoc);
137             fSchemaAttrs = attrChecker.checkAttributes(root, true, this);
138             // schemaAttrs == null means it's not an <xsd:schema> element
139
// throw an exception, but we don't know the document systemId,
140
// so we leave that to the caller.
141
if (fSchemaAttrs == null) {
142                 throw new XMLSchemaException(null, null);
143             }
144             fAreLocalAttributesQualified =
145                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_AFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
146             fAreLocalElementsQualified =
147                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_EFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
148             fBlockDefault =
149                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_BLOCKDEFAULT]).shortValue();
150             fFinalDefault =
151                 ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_FINALDEFAULT]).shortValue();
152             fTargetNamespace =
153                 (String JavaDoc)fSchemaAttrs[XSAttributeChecker.ATTIDX_TARGETNAMESPACE];
154             if (fTargetNamespace != null)
155                 fTargetNamespace = symbolTable.addSymbol(fTargetNamespace);
156
157             fNamespaceSupportRoot = new SchemaNamespaceSupport(fNamespaceSupport);
158
159             //set namespace support
160
fValidationContext.setNamespaceSupport(fNamespaceSupport);
161             fValidationContext.setSymbolTable(symbolTable);
162             // pass null as the schema document, so that the namespace
163
// context is not popped.
164

165             // don't return the attribute array yet!
166
//attrChecker.returnAttrArray(schemaAttrs, null);
167
}
168     }
169
170     // backup the current ns support, and use the one passed-in.
171
// if no ns support is passed-in, use the one for <schema> element
172
void backupNSSupport(SchemaNamespaceSupport nsSupport) {
173         SchemaNamespaceSupportStack.push(fNamespaceSupport);
174         if (nsSupport == null)
175             nsSupport = fNamespaceSupportRoot;
176         fNamespaceSupport = new SchemaNamespaceSupport(nsSupport);
177
178         fValidationContext.setNamespaceSupport(fNamespaceSupport);
179     }
180
181     void restoreNSSupport() {
182         fNamespaceSupport = (SchemaNamespaceSupport)SchemaNamespaceSupportStack.pop();
183         fValidationContext.setNamespaceSupport(fNamespaceSupport);
184     }
185
186     // some Object methods
187
public String JavaDoc toString() {
188         return fTargetNamespace == null?"no targetNamspace":"targetNamespace is " + fTargetNamespace;
189     }
190
191     public void addAllowedNS(String JavaDoc namespace) {
192         fImportedNS.addElement(namespace == null ? "" : namespace);
193     }
194     
195     public boolean isAllowedNS(String JavaDoc namespace) {
196         return fImportedNS.contains(namespace == null ? "" : namespace);
197     }
198     
199     // store whether we have reported an error about that this document
200
// can't access components from the given namespace
201
private Vector JavaDoc fReportedTNS = null;
202     // check whether we need to report an error against the given uri.
203
// if we have reported an error, then we don't need to report again;
204
// otherwise we reported the error, and remember this fact.
205
final boolean needReportTNSError(String JavaDoc uri) {
206         if (fReportedTNS == null)
207             fReportedTNS = new Vector JavaDoc();
208         else if (fReportedTNS.contains(uri))
209             return false;
210         fReportedTNS.addElement(uri);
211         return true;
212     }
213
214     // return the attributes on the schema element itself:
215
Object JavaDoc [] getSchemaAttrs () {
216         return fSchemaAttrs;
217     }
218
219     // deallocate the storage set aside for the schema element's
220
// attributes
221
void returnSchemaAttrs () {
222         fAttrChecker.returnAttrArray (fSchemaAttrs, null);
223         fSchemaAttrs = null;
224     }
225     
226 } // XSDocumentInfo
227
Popular Tags