001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xs.traversers;
019:
020: import java.util.Stack;
021: import java.util.Vector;
022:
023: import org.apache.xerces.impl.validation.ValidationState;
024: import org.apache.xerces.impl.xs.SchemaNamespaceSupport;
025: import org.apache.xerces.impl.xs.SchemaSymbols;
026: import org.apache.xerces.impl.xs.XMLSchemaException;
027: import org.apache.xerces.impl.xs.util.XInt;
028: import org.apache.xerces.util.SymbolTable;
029: import org.w3c.dom.Element;
030:
031: /**
032: * Objects of this class hold all information pecular to a
033: * particular XML Schema document. This is needed because
034: * namespace bindings and other settings on the <schema/> element
035: * affect the contents of that schema document alone.
036: *
037: * @xerces.internal
038: *
039: * @author Neil Graham, IBM
040: * @version $Id: XSDocumentInfo.java 446725 2006-09-15 20:40:10Z mrglavas $
041: */
042: class XSDocumentInfo {
043:
044: // Data
045: protected SchemaNamespaceSupport fNamespaceSupport;
046: protected SchemaNamespaceSupport fNamespaceSupportRoot;
047: protected Stack SchemaNamespaceSupportStack = new Stack();
048:
049: // schema's attributeFormDefault
050: protected boolean fAreLocalAttributesQualified;
051:
052: // elementFormDefault
053: protected boolean fAreLocalElementsQualified;
054:
055: // [block | final]Default
056: protected short fBlockDefault;
057: protected short fFinalDefault;
058:
059: // targetNamespace
060: String fTargetNamespace;
061:
062: // represents whether this is a chameleon schema (i.e., whether its TNS is natural or comes from without)
063: protected boolean fIsChameleonSchema;
064:
065: // the root of the schema Document tree itself
066: protected Element fSchemaElement;
067:
068: // all namespaces that this document can refer to
069: Vector fImportedNS = new Vector();
070:
071: protected ValidationState fValidationContext = new ValidationState();
072:
073: SymbolTable fSymbolTable = null;
074:
075: // attribute checker to which we'll return the attributes
076: // once we've been told that we're done with them
077: protected XSAttributeChecker fAttrChecker;
078:
079: // array of objects on the schema's root element. This is null
080: // once returnSchemaAttrs has been called.
081: protected Object[] fSchemaAttrs;
082:
083: // list of annotations contained in the schema document. This is null
084: // once removeAnnotations has been called.
085: protected XSAnnotationInfo fAnnotations = null;
086:
087: // note that the caller must ensure to call returnSchemaAttrs()
088: // to avoid memory leaks!
089: XSDocumentInfo(Element schemaRoot, XSAttributeChecker attrChecker,
090: SymbolTable symbolTable) throws XMLSchemaException {
091:
092: fSchemaElement = schemaRoot;
093: fNamespaceSupport = new SchemaNamespaceSupport(schemaRoot,
094: symbolTable);
095: fNamespaceSupport.reset();
096: fIsChameleonSchema = false;
097:
098: fSymbolTable = symbolTable;
099: fAttrChecker = attrChecker;
100:
101: if (schemaRoot != null) {
102: Element root = schemaRoot;
103: fSchemaAttrs = attrChecker
104: .checkAttributes(root, true, this );
105: // schemaAttrs == null means it's not an <xsd:schema> element
106: // throw an exception, but we don't know the document systemId,
107: // so we leave that to the caller.
108: if (fSchemaAttrs == null) {
109: throw new XMLSchemaException(null, null);
110: }
111: fAreLocalAttributesQualified = ((XInt) fSchemaAttrs[XSAttributeChecker.ATTIDX_AFORMDEFAULT])
112: .intValue() == SchemaSymbols.FORM_QUALIFIED;
113: fAreLocalElementsQualified = ((XInt) fSchemaAttrs[XSAttributeChecker.ATTIDX_EFORMDEFAULT])
114: .intValue() == SchemaSymbols.FORM_QUALIFIED;
115: fBlockDefault = ((XInt) fSchemaAttrs[XSAttributeChecker.ATTIDX_BLOCKDEFAULT])
116: .shortValue();
117: fFinalDefault = ((XInt) fSchemaAttrs[XSAttributeChecker.ATTIDX_FINALDEFAULT])
118: .shortValue();
119: fTargetNamespace = (String) fSchemaAttrs[XSAttributeChecker.ATTIDX_TARGETNAMESPACE];
120: if (fTargetNamespace != null)
121: fTargetNamespace = symbolTable
122: .addSymbol(fTargetNamespace);
123:
124: fNamespaceSupportRoot = new SchemaNamespaceSupport(
125: fNamespaceSupport);
126:
127: //set namespace support
128: fValidationContext.setNamespaceSupport(fNamespaceSupport);
129: fValidationContext.setSymbolTable(symbolTable);
130: // pass null as the schema document, so that the namespace
131: // context is not popped.
132:
133: // don't return the attribute array yet!
134: //attrChecker.returnAttrArray(schemaAttrs, null);
135: }
136: }
137:
138: // backup the current ns support, and use the one passed-in.
139: // if no ns support is passed-in, use the one for <schema> element
140: void backupNSSupport(SchemaNamespaceSupport nsSupport) {
141: SchemaNamespaceSupportStack.push(fNamespaceSupport);
142: if (nsSupport == null)
143: nsSupport = fNamespaceSupportRoot;
144: fNamespaceSupport = new SchemaNamespaceSupport(nsSupport);
145:
146: fValidationContext.setNamespaceSupport(fNamespaceSupport);
147: }
148:
149: void restoreNSSupport() {
150: fNamespaceSupport = (SchemaNamespaceSupport) SchemaNamespaceSupportStack
151: .pop();
152: fValidationContext.setNamespaceSupport(fNamespaceSupport);
153: }
154:
155: // some Object methods
156: public String toString() {
157: return fTargetNamespace == null ? "no targetNamspace"
158: : "targetNamespace is " + fTargetNamespace;
159: }
160:
161: public void addAllowedNS(String namespace) {
162: fImportedNS.addElement(namespace == null ? "" : namespace);
163: }
164:
165: public boolean isAllowedNS(String namespace) {
166: return fImportedNS.contains(namespace == null ? "" : namespace);
167: }
168:
169: // store whether we have reported an error about that this document
170: // can't access components from the given namespace
171: private Vector fReportedTNS = null;
172:
173: // check whether we need to report an error against the given uri.
174: // if we have reported an error, then we don't need to report again;
175: // otherwise we reported the error, and remember this fact.
176: final boolean needReportTNSError(String uri) {
177: if (fReportedTNS == null)
178: fReportedTNS = new Vector();
179: else if (fReportedTNS.contains(uri))
180: return false;
181: fReportedTNS.addElement(uri);
182: return true;
183: }
184:
185: // return the attributes on the schema element itself:
186: Object[] getSchemaAttrs() {
187: return fSchemaAttrs;
188: }
189:
190: // deallocate the storage set aside for the schema element's
191: // attributes
192: void returnSchemaAttrs() {
193: fAttrChecker.returnAttrArray(fSchemaAttrs, null);
194: fSchemaAttrs = null;
195: }
196:
197: // adds an annotation to the list of annotations
198: void addAnnotation(XSAnnotationInfo info) {
199: info.next = fAnnotations;
200: fAnnotations = info;
201: }
202:
203: // returns the list of annotations conatined in the
204: // schema document or null if the document contained no annotations.
205: XSAnnotationInfo getAnnotations() {
206: return fAnnotations;
207: }
208:
209: // removes reference to annotation list
210: void removeAnnotations() {
211: fAnnotations = null;
212: }
213:
214: } // XSDocumentInfo
|