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. package com.sun.org.apache.xerces.internal.impl.xs.traversers;
  58. import java.util.Stack;
  59. import java.util.Vector;
  60. import com.sun.org.apache.xerces.internal.impl.validation.ValidationState;
  61. import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport;
  62. import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
  63. import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaException;
  64. import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
  65. import com.sun.org.apache.xerces.internal.util.DOMUtil;
  66. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  67. import org.w3c.dom.Document;
  68. import org.w3c.dom.Element;
  69. /*
  70. * Objects of this class hold all information pecular to a
  71. * particular XML Schema document. This is needed because
  72. * namespace bindings and other settings on the <schema/> element
  73. * affect the contents of that schema document alone.
  74. *
  75. * @author Neil Graham, IBM
  76. * @version $Id: XSDocumentInfo.java,v 1.17 2003/07/03 15:17:16 neilg Exp $
  77. */
  78. class XSDocumentInfo {
  79. // Data
  80. protected SchemaNamespaceSupport fNamespaceSupport;
  81. protected SchemaNamespaceSupport fNamespaceSupportRoot;
  82. protected Stack SchemaNamespaceSupportStack = new Stack();
  83. // schema's attributeFormDefault
  84. protected boolean fAreLocalAttributesQualified;
  85. // elementFormDefault
  86. protected boolean fAreLocalElementsQualified;
  87. // [block | final]Default
  88. protected short fBlockDefault;
  89. protected short fFinalDefault;
  90. // targetNamespace
  91. String fTargetNamespace;
  92. // represents whether this is a chameleon schema (i.e., whether its TNS is natural or comes from without)
  93. protected boolean fIsChameleonSchema;
  94. // the root of the schema Document tree itself
  95. protected Document fSchemaDoc;
  96. // all namespaces that this document can refer to
  97. Vector fImportedNS = new Vector();
  98. protected ValidationState fValidationContext = new ValidationState();
  99. SymbolTable fSymbolTable = null;
  100. // attribute checker to which we'll return the attributes
  101. // once we've been told that we're done with them
  102. protected XSAttributeChecker fAttrChecker;
  103. // array of objects on the schema's root element. This is null
  104. // once returnSchemaAttrs has been called.
  105. protected Object [] fSchemaAttrs;
  106. // note that the caller must ensure to call returnSchemaAttrs()
  107. // to avoid memory leaks!
  108. XSDocumentInfo (Document schemaDoc, XSAttributeChecker attrChecker, SymbolTable symbolTable)
  109. throws XMLSchemaException {
  110. fSchemaDoc = schemaDoc;
  111. fNamespaceSupport = new SchemaNamespaceSupport();
  112. fNamespaceSupport.reset();
  113. fIsChameleonSchema = false;
  114. fSymbolTable = symbolTable;
  115. fAttrChecker = attrChecker;
  116. if(schemaDoc != null) {
  117. Element root = DOMUtil.getRoot(schemaDoc);
  118. fSchemaAttrs = attrChecker.checkAttributes(root, true, this);
  119. // schemaAttrs == null means it's not an <xsd:schema> element
  120. // throw an exception, but we don't know the document systemId,
  121. // so we leave that to the caller.
  122. if (fSchemaAttrs == null) {
  123. throw new XMLSchemaException(null, null);
  124. }
  125. fAreLocalAttributesQualified =
  126. ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_AFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
  127. fAreLocalElementsQualified =
  128. ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_EFORMDEFAULT]).intValue() == SchemaSymbols.FORM_QUALIFIED;
  129. fBlockDefault =
  130. ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_BLOCKDEFAULT]).shortValue();
  131. fFinalDefault =
  132. ((XInt)fSchemaAttrs[XSAttributeChecker.ATTIDX_FINALDEFAULT]).shortValue();
  133. fTargetNamespace =
  134. (String)fSchemaAttrs[XSAttributeChecker.ATTIDX_TARGETNAMESPACE];
  135. if (fTargetNamespace != null)
  136. fTargetNamespace = symbolTable.addSymbol(fTargetNamespace);
  137. fNamespaceSupportRoot = new SchemaNamespaceSupport(fNamespaceSupport);
  138. //set namespace support
  139. fValidationContext.setNamespaceSupport(fNamespaceSupport);
  140. fValidationContext.setSymbolTable(symbolTable);
  141. // pass null as the schema document, so that the namespace
  142. // context is not popped.
  143. // don't return the attribute array yet!
  144. //attrChecker.returnAttrArray(schemaAttrs, null);
  145. }
  146. }
  147. // backup the current ns support, and use the one passed-in.
  148. // if no ns support is passed-in, use the one for <schema> element
  149. void backupNSSupport(SchemaNamespaceSupport nsSupport) {
  150. SchemaNamespaceSupportStack.push(fNamespaceSupport);
  151. if (nsSupport == null)
  152. nsSupport = fNamespaceSupportRoot;
  153. fNamespaceSupport = new SchemaNamespaceSupport(nsSupport);
  154. fValidationContext.setNamespaceSupport(fNamespaceSupport);
  155. }
  156. void restoreNSSupport() {
  157. fNamespaceSupport = (SchemaNamespaceSupport)SchemaNamespaceSupportStack.pop();
  158. fValidationContext.setNamespaceSupport(fNamespaceSupport);
  159. }
  160. // some Object methods
  161. public String toString() {
  162. return fTargetNamespace == null?"no targetNamspace":"targetNamespace is " + fTargetNamespace;
  163. }
  164. public void addAllowedNS(String namespace) {
  165. fImportedNS.addElement(namespace == null ? "" : namespace);
  166. }
  167. public boolean isAllowedNS(String namespace) {
  168. return fImportedNS.contains(namespace == null ? "" : namespace);
  169. }
  170. // store whether we have reported an error about that this document
  171. // can't access components from the given namespace
  172. private Vector fReportedTNS = null;
  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. // return the attributes on the schema element itself:
  185. Object [] getSchemaAttrs () {
  186. return fSchemaAttrs;
  187. }
  188. // deallocate the storage set aside for the schema element's
  189. // attributes
  190. void returnSchemaAttrs () {
  191. fAttrChecker.returnAttrArray (fSchemaAttrs, null);
  192. fSchemaAttrs = null;
  193. }
  194. } // XSDocumentInfo