1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2002, 2003 The Apache Software Foundation.
  6. * All rights 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;
  58. import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
  59. import com.sun.org.apache.xerces.internal.xni.QName;
  60. import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
  61. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  62. import com.sun.org.apache.xerces.internal.xni.grammars.XMLSchemaDescription;
  63. /*
  64. * All information specific to XML Schema grammars.
  65. *
  66. * @author Neil Graham, IBM
  67. * @author Neeraj Bajaj, SUN Microsystems.
  68. *
  69. * @version $Id: XSDDescription.java,v 1.9 2003/03/24 21:10:58 sandygao Exp $
  70. */
  71. public class XSDDescription extends XMLResourceIdentifierImpl
  72. implements XMLSchemaDescription {
  73. // used to indicate what triggered the call
  74. /**
  75. * Indicate that this description was just initialized.
  76. */
  77. public final static short CONTEXT_INITIALIZE = -1;
  78. /**
  79. * Indicate that the current schema document is <include>d by another
  80. * schema document.
  81. */
  82. public final static short CONTEXT_INCLUDE = 0;
  83. /**
  84. * Indicate that the current schema document is <redefine>d by another
  85. * schema document.
  86. */
  87. public final static short CONTEXT_REDEFINE = 1;
  88. /**
  89. * Indicate that the current schema document is <import>ed by another
  90. * schema document.
  91. */
  92. public final static short CONTEXT_IMPORT = 2;
  93. /**
  94. * Indicate that the current schema document is being preparsed.
  95. */
  96. public final static short CONTEXT_PREPARSE = 3;
  97. /**
  98. * Indicate that the parse of the current schema document is triggered
  99. * by xsi:schemaLocation/noNamespaceSchemaLocation attribute(s) in the
  100. * instance document. This value is only used if we don't defer the loading
  101. * of schema documents.
  102. */
  103. public final static short CONTEXT_INSTANCE = 4;
  104. /**
  105. * Indicate that the parse of the current schema document is triggered by
  106. * the occurrence of an element whose namespace is the target namespace
  107. * of this schema document. This value is only used if we do defer the
  108. * loading of schema documents until a component from that namespace is
  109. * referenced from the instance.
  110. */
  111. public final static short CONTEXT_ELEMENT = 5;
  112. /**
  113. * Indicate that the parse of the current schema document is triggered by
  114. * the occurrence of an attribute whose namespace is the target namespace
  115. * of this schema document. This value is only used if we do defer the
  116. * loading of schema documents until a component from that namespace is
  117. * referenced from the instance.
  118. */
  119. public final static short CONTEXT_ATTRIBUTE = 6;
  120. /**
  121. * Indicate that the parse of the current schema document is triggered by
  122. * the occurrence of an "xsi:type" attribute, whose value (a QName) has
  123. * the target namespace of this schema document as its namespace.
  124. * This value is only used if we do defer the loading of schema documents
  125. * until a component from that namespace is referenced from the instance.
  126. */
  127. public final static short CONTEXT_XSITYPE = 7;
  128. // REVISIT: write description of these fields
  129. protected short fContextType;
  130. protected String [] fLocationHints ;
  131. protected QName fTriggeringComponent;
  132. protected QName fEnclosedElementName;
  133. protected XMLAttributes fAttributes;
  134. /**
  135. * the type of the grammar (e.g., DTD or XSD);
  136. *
  137. * @see com.sun.org.apache.xerces.internal.xni.grammars.Grammar
  138. */
  139. public String getGrammarType() {
  140. return XMLGrammarDescription.XML_SCHEMA;
  141. }
  142. /**
  143. * Get the context. The returned value is one of the pre-defined
  144. * CONTEXT_xxx constants.
  145. *
  146. * @return the value indicating the context
  147. */
  148. public short getContextType() {
  149. return fContextType ;
  150. }
  151. /**
  152. * If the context is "include" or "redefine", then return the target
  153. * namespace of the enclosing schema document; otherwise, the expected
  154. * target namespace of this document.
  155. *
  156. * @return the expected/enclosing target namespace
  157. */
  158. public String getTargetNamespace() {
  159. return fNamespace;
  160. }
  161. /**
  162. * For import and references from the instance document, it's possible to
  163. * have multiple hints for one namespace. So this method returns an array,
  164. * which contains all location hints.
  165. *
  166. * @return an array of all location hints associated to the expected
  167. * target namespace
  168. */
  169. public String[] getLocationHints() {
  170. return fLocationHints ;
  171. }
  172. /**
  173. * If a call is triggered by an element/attribute/xsi:type in the instance,
  174. * this call returns the name of such triggering component: the name of
  175. * the element/attribute, or the value of the xsi:type.
  176. *
  177. * @return the name of the triggering component
  178. */
  179. public QName getTriggeringComponent() {
  180. return fTriggeringComponent ;
  181. }
  182. /**
  183. * If a call is triggered by an attribute or xsi:type, then this mehtod
  184. * returns the enclosing element of such element.
  185. *
  186. * @return the name of the enclosing element
  187. */
  188. public QName getEnclosingElementName() {
  189. return fEnclosedElementName ;
  190. }
  191. /**
  192. * If a call is triggered by an element/attribute/xsi:type in the instance,
  193. * this call returns all attribute of such element (or enclosing element).
  194. *
  195. * @return all attributes of the tiggering/enclosing element
  196. */
  197. public XMLAttributes getAttributes() {
  198. return fAttributes;
  199. }
  200. public boolean fromInstance() {
  201. return fContextType == CONTEXT_ATTRIBUTE ||
  202. fContextType == CONTEXT_ELEMENT ||
  203. fContextType == CONTEXT_INSTANCE ||
  204. fContextType == CONTEXT_XSITYPE;
  205. }
  206. /**
  207. * Compares this grammar with the given grammar. Currently, we compare
  208. * the target namespaces.
  209. *
  210. * @param desc The description of the grammar to be compared with
  211. * @return True if they are equal, else false
  212. */
  213. public boolean equals(Object descObj) {
  214. if(!(descObj instanceof XMLSchemaDescription)) return false;
  215. XMLSchemaDescription desc = (XMLSchemaDescription)descObj;
  216. if (fNamespace != null)
  217. return fNamespace.equals(desc.getTargetNamespace());
  218. else // fNamespace == null
  219. return desc.getTargetNamespace() == null;
  220. }
  221. /**
  222. * Returns the hash code of this grammar
  223. *
  224. * @return The hash code
  225. */
  226. public int hashCode() {
  227. return (fNamespace == null) ? 0 : fNamespace.hashCode();
  228. }
  229. public void setContextType(short contextType){
  230. fContextType = contextType ;
  231. }
  232. public void setTargetNamespace(String targetNamespace){
  233. fNamespace = targetNamespace ;
  234. }
  235. public void setLocationHints(String [] locationHints){
  236. int length = locationHints.length ;
  237. fLocationHints = new String[length];
  238. System.arraycopy(locationHints, 0, fLocationHints, 0, length);
  239. //fLocationHints = locationHints ;
  240. }
  241. public void setTriggeringComponent(QName triggeringComponent){
  242. fTriggeringComponent = triggeringComponent ;
  243. }
  244. public void setEnclosingElementName(QName enclosedElementName){
  245. fEnclosedElementName = enclosedElementName ;
  246. }
  247. public void setAttributes(XMLAttributes attributes){
  248. fAttributes = attributes ;
  249. }
  250. /**
  251. * resets all the fields
  252. */
  253. public void reset(){
  254. super.clear();
  255. fContextType = CONTEXT_INITIALIZE;
  256. fLocationHints = null ;
  257. fTriggeringComponent = null ;
  258. fEnclosedElementName = null ;
  259. fAttributes = null ;
  260. }
  261. public XSDDescription makeClone() {
  262. XSDDescription desc = new XSDDescription();
  263. desc.fAttributes = this.fAttributes;
  264. desc.fBaseSystemId = this.fBaseSystemId;
  265. desc.fContextType = this.fContextType;
  266. desc.fEnclosedElementName = this.fEnclosedElementName;
  267. desc.fExpandedSystemId = this.fExpandedSystemId;
  268. desc.fLiteralSystemId = this.fLiteralSystemId;
  269. desc.fLocationHints = this.fLocationHints;
  270. desc.fPublicId = this.fPublicId;
  271. desc.fNamespace = this.fNamespace;
  272. desc.fTriggeringComponent = this.fTriggeringComponent;
  273. return desc;
  274. }
  275. } // XSDDescription