286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xs.traversers;
286N/A
286N/Aimport java.util.Stack;
286N/Aimport java.util.Vector;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.validation.ValidationState;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaException;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
286N/Aimport com.sun.org.apache.xerces.internal.util.SymbolTable;
286N/Aimport org.w3c.dom.Element;
286N/Aimport org.w3c.dom.Node;
286N/Aimport org.w3c.dom.Attr;
286N/Aimport org.w3c.dom.NamedNodeMap;
286N/A
286N/A/**
286N/A * Objects of this class hold all information pecular to a
286N/A * particular XML Schema document. This is needed because
286N/A * namespace bindings and other settings on the <schema/> element
286N/A * affect the contents of that schema document alone.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Neil Graham, IBM
286N/A * @version $Id: XSDocumentInfo.java,v 1.5 2007/10/15 22:27:48 spericas Exp $
286N/A */
286N/Aclass XSDocumentInfo {
286N/A
286N/A // Data
286N/A protected SchemaNamespaceSupport fNamespaceSupport;
286N/A protected SchemaNamespaceSupport fNamespaceSupportRoot;
286N/A protected Stack SchemaNamespaceSupportStack = new Stack();
286N/A
286N/A // schema's attributeFormDefault
286N/A protected boolean fAreLocalAttributesQualified;
286N/A
286N/A // elementFormDefault
286N/A protected boolean fAreLocalElementsQualified;
286N/A
286N/A // [block | final]Default
286N/A protected short fBlockDefault;
286N/A protected short fFinalDefault;
286N/A
286N/A // targetNamespace
286N/A String fTargetNamespace;
286N/A
286N/A // represents whether this is a chameleon schema (i.e., whether its TNS is natural or comes from without)
286N/A protected boolean fIsChameleonSchema;
286N/A
286N/A // the root of the schema Document tree itself
286N/A protected Element fSchemaElement;
286N/A
286N/A // all namespaces that this document can refer to
286N/A Vector fImportedNS = new Vector();
286N/A
286N/A protected ValidationState fValidationContext = new ValidationState();
286N/A
286N/A SymbolTable fSymbolTable = null;
286N/A
286N/A // attribute checker to which we'll return the attributes
286N/A // once we've been told that we're done with them
286N/A protected XSAttributeChecker fAttrChecker;
286N/A
286N/A // array of objects on the schema's root element. This is null
286N/A // once returnSchemaAttrs has been called.
286N/A protected Object [] fSchemaAttrs;
286N/A
286N/A // list of annotations contained in the schema document. This is null
286N/A // once removeAnnotations has been called.
286N/A protected XSAnnotationInfo fAnnotations = null;
286N/A
286N/A // note that the caller must ensure to call returnSchemaAttrs()
286N/A // to avoid memory leaks!
286N/A XSDocumentInfo (Element schemaRoot, XSAttributeChecker attrChecker, SymbolTable symbolTable)
286N/A throws XMLSchemaException {
286N/A fSchemaElement = schemaRoot;
286N/A initNamespaceSupport(schemaRoot);
286N/A fIsChameleonSchema = false;
286N/A
286N/A fSymbolTable = symbolTable;
286N/A fAttrChecker = attrChecker;
286N/A
286N/A if (schemaRoot != null) {
286N/A Element root = schemaRoot;
286N/A fSchemaAttrs = attrChecker.checkAttributes(root, true, this);
286N/A // schemaAttrs == null means it's not an <xsd:schema> element
286N/A // throw an exception, but we don't know the document systemId,
286N/A // so we leave that to the caller.
286N/A if (fSchemaAttrs == null) {
286N/A throw new XMLSchemaException(null, null);
286N/A }
286N/A fAreLocalAttributesQualified =
286N/A ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_AFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
286N/A fAreLocalElementsQualified =
286N/A ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_EFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
286N/A fBlockDefault =
286N/A ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_BLOCKDEFAULT]).shortValue();
286N/A fFinalDefault =
286N/A ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_FINALDEFAULT]).shortValue();
286N/A fTargetNamespace =
286N/A (String)fSchemaAttrs[XSAttributeChecker.ATTIDX_TARGETNAMESPACE];
286N/A if (fTargetNamespace != null)
286N/A fTargetNamespace = symbolTable.addSymbol(fTargetNamespace);
286N/A
286N/A fNamespaceSupportRoot = new SchemaNamespaceSupport(fNamespaceSupport);
286N/A
286N/A //set namespace support
286N/A fValidationContext.setNamespaceSupport(fNamespaceSupport);
286N/A fValidationContext.setSymbolTable(symbolTable);
286N/A // pass null as the schema document, so that the namespace
286N/A // context is not popped.
286N/A
286N/A // don't return the attribute array yet!
286N/A //attrChecker.returnAttrArray(schemaAttrs, null);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Initialize namespace support by collecting all of the namespace
286N/A * declarations in the root's ancestors. This is necessary to
286N/A * support schemas fragments, i.e. schemas embedded in other
286N/A * documents. See,
286N/A *
286N/A * https://jaxp.dev.java.net/issues/show_bug.cgi?id=43
286N/A *
286N/A * Requires the DOM to be created with namespace support enabled.
286N/A */
286N/A private void initNamespaceSupport(Element schemaRoot) {
286N/A fNamespaceSupport = new SchemaNamespaceSupport();
286N/A fNamespaceSupport.reset();
286N/A
286N/A Node parent = schemaRoot.getParentNode();
286N/A while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE
286N/A && !parent.getNodeName().equals("DOCUMENT_NODE"))
286N/A {
286N/A Element eparent = (Element) parent;
286N/A NamedNodeMap map = eparent.getAttributes();
286N/A int length = (map != null) ? map.getLength() : 0;
286N/A for (int i = 0; i < length; i++) {
286N/A Attr attr = (Attr) map.item(i);
286N/A String uri = attr.getNamespaceURI();
286N/A
286N/A // Check if attribute is an ns decl -- requires ns support
286N/A if (uri != null && uri.equals("http://www.w3.org/2000/xmlns/")) {
286N/A String prefix = attr.getLocalName().intern();
286N/A if (prefix == "xmlns") prefix = "";
286N/A // Declare prefix if not set -- moving upwards
286N/A if (fNamespaceSupport.getURI(prefix) == null) {
286N/A fNamespaceSupport.declarePrefix(prefix,
286N/A attr.getValue().intern());
286N/A }
286N/A }
286N/A }
286N/A parent = parent.getParentNode();
286N/A }
286N/A }
286N/A
286N/A // backup the current ns support, and use the one passed-in.
286N/A // if no ns support is passed-in, use the one for <schema> element
286N/A void backupNSSupport(SchemaNamespaceSupport nsSupport) {
286N/A SchemaNamespaceSupportStack.push(fNamespaceSupport);
286N/A if (nsSupport == null)
286N/A nsSupport = fNamespaceSupportRoot;
286N/A fNamespaceSupport = new SchemaNamespaceSupport(nsSupport);
286N/A
286N/A fValidationContext.setNamespaceSupport(fNamespaceSupport);
286N/A }
286N/A
286N/A void restoreNSSupport() {
286N/A fNamespaceSupport = (SchemaNamespaceSupport)SchemaNamespaceSupportStack.pop();
286N/A fValidationContext.setNamespaceSupport(fNamespaceSupport);
286N/A }
286N/A
286N/A // some Object methods
286N/A public String toString() {
286N/A return fTargetNamespace == null?"no targetNamspace":"targetNamespace is " + fTargetNamespace;
286N/A }
286N/A
286N/A public void addAllowedNS(String namespace) {
286N/A fImportedNS.addElement(namespace == null ? "" : namespace);
286N/A }
286N/A
286N/A public boolean isAllowedNS(String namespace) {
286N/A return fImportedNS.contains(namespace == null ? "" : namespace);
286N/A }
286N/A
286N/A // store whether we have reported an error about that this document
286N/A // can't access components from the given namespace
286N/A private Vector fReportedTNS = null;
286N/A // check whether we need to report an error against the given uri.
286N/A // if we have reported an error, then we don't need to report again;
286N/A // otherwise we reported the error, and remember this fact.
286N/A final boolean needReportTNSError(String uri) {
286N/A if (fReportedTNS == null)
286N/A fReportedTNS = new Vector();
286N/A else if (fReportedTNS.contains(uri))
286N/A return false;
286N/A fReportedTNS.addElement(uri);
286N/A return true;
286N/A }
286N/A
286N/A // return the attributes on the schema element itself:
286N/A Object [] getSchemaAttrs () {
286N/A return fSchemaAttrs;
286N/A }
286N/A
286N/A // deallocate the storage set aside for the schema element's
286N/A // attributes
286N/A void returnSchemaAttrs () {
286N/A fAttrChecker.returnAttrArray (fSchemaAttrs, null);
286N/A fSchemaAttrs = null;
286N/A }
286N/A
286N/A // adds an annotation to the list of annotations
286N/A void addAnnotation(XSAnnotationInfo info) {
286N/A info.next = fAnnotations;
286N/A fAnnotations = info;
286N/A }
286N/A
286N/A // returns the list of annotations conatined in the
286N/A // schema document or null if the document contained no annotations.
286N/A XSAnnotationInfo getAnnotations() {
286N/A return fAnnotations;
286N/A }
286N/A
286N/A // removes reference to annotation list
286N/A void removeAnnotations() {
286N/A fAnnotations = null;
286N/A }
286N/A
286N/A} // XSDocumentInfo