1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 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.dtd;
  58. /**
  59. * ContentSpec really exists to aid the parser classes in implementing
  60. * access to the grammar.
  61. * <p>
  62. * This class is used by the DTD scanner and the validator classes,
  63. * allowing them to be used separately or together. This "struct"
  64. * class is used to build content models for validation, where it
  65. * is more efficient to fetch all of the information for each of
  66. * these content model "fragments" than to fetch each field one at
  67. * a time. Since configurations are allowed to have validators
  68. * without a DTD scanner (i.e. a schema validator) and a DTD scanner
  69. * without a validator (non-validating processor), this class can be
  70. * used by each without requiring the presence of the other.
  71. * <p>
  72. * When processing element declarations, the DTD scanner will build
  73. * up a representation of the content model using the node types that
  74. * are defined here. Since a non-validating processor only needs to
  75. * remember the type of content model declared (i.e. ANY, EMPTY, MIXED,
  76. * or CHILDREN), it is free to discard the specific details of the
  77. * MIXED and CHILDREN content models described using this class.
  78. * <p>
  79. * In the typical case of a validating processor reading the grammar
  80. * of the document from a DTD, the information about the content model
  81. * declared will be preserved and later "compiled" into an efficient
  82. * form for use during element validation. Each content spec node
  83. * that is saved is assigned a unique index that is used as a handle
  84. * for the "value" or "otherValue" fields of other content spec nodes.
  85. * A leaf node has a "value" that is either an index in the string
  86. * pool of the element type of that leaf, or a value of -1 to indicate
  87. * the special "#PCDATA" leaf type used in a mixed content model.
  88. * <p>
  89. * For a mixed content model, the content spec will be made up of
  90. * leaf and choice content spec nodes, with an optional "zero or more"
  91. * node. For example, the mixed content declaration "(#PCDATA)" would
  92. * contain a single leaf node with a node value of -1. A mixed content
  93. * declaration of "(#PCDATA|foo)*" would have a content spec consisting
  94. * of two leaf nodes, for the "#PCDATA" and "foo" choices, a choice node
  95. * with the "value" set to the index of the "#PCDATA" leaf node and the
  96. * "otherValue" set to the index of the "foo" leaf node, and a "zero or
  97. * more" node with the "value" set to the index of the choice node. If
  98. * the content model has more choices, for example "(#PCDATA|a|b)*", then
  99. * there will be more corresponding choice and leaf nodes, the choice
  100. * nodes will be chained together through the "value" field with each
  101. * leaf node referenced by the "otherValue" field.
  102. * <p>
  103. * For element content models, there are sequence nodes and also "zero or
  104. * one" and "one or more" nodes. The leaf nodes would always have a valid
  105. * string pool index, as the "#PCDATA" leaf is not used in the declarations
  106. * for element content models.
  107. *
  108. * @version $Id: XMLContentSpec.java,v 1.3 2002/01/29 01:15:10 lehors Exp $
  109. */
  110. public class XMLContentSpec {
  111. //
  112. // Constants
  113. //
  114. /**
  115. * Name or #PCDATA. Leaf nodes that represent parsed character
  116. * data (#PCDATA) have values of -1.
  117. */
  118. public static final short CONTENTSPECNODE_LEAF = 0;
  119. /** Represents a zero or one occurence count, '?'. */
  120. public static final short CONTENTSPECNODE_ZERO_OR_ONE = 1;
  121. /** Represents a zero or more occurence count, '*'. */
  122. public static final short CONTENTSPECNODE_ZERO_OR_MORE = 2;
  123. /** Represents a one or more occurence count, '+'. */
  124. public static final short CONTENTSPECNODE_ONE_OR_MORE = 3;
  125. /** Represents choice, '|'. */
  126. public static final short CONTENTSPECNODE_CHOICE = 4;
  127. /** Represents sequence, ','. */
  128. public static final short CONTENTSPECNODE_SEQ = 5;
  129. /**
  130. * Represents any namespace specified namespace. When the element
  131. * found in the document must belong to a specific namespace,
  132. * <code>otherValue</code> will contain the name of the namespace.
  133. * If <code>otherValue</code> is <code>-1</code> then the element
  134. * can be from any namespace.
  135. * <p>
  136. * Lists of valid namespaces are created from choice content spec
  137. * nodes that have any content spec nodes as children.
  138. */
  139. public static final short CONTENTSPECNODE_ANY = 6;
  140. /**
  141. * Represents any other namespace (XML Schema: ##other).
  142. * <p>
  143. * When the content spec node type is set to CONTENTSPECNODE_ANY_OTHER,
  144. * <code>value</code> will contain the namespace that <em>cannot</em>
  145. * occur.
  146. */
  147. public static final short CONTENTSPECNODE_ANY_OTHER = 7;
  148. /** Represents any local element (XML Schema: ##local). */
  149. public static final short CONTENTSPECNODE_ANY_LOCAL = 8;
  150. /** prcessContent is 'lax' **/
  151. public static final short CONTENTSPECNODE_ANY_LAX = 22;
  152. public static final short CONTENTSPECNODE_ANY_OTHER_LAX = 23;
  153. public static final short CONTENTSPECNODE_ANY_LOCAL_LAX = 24;
  154. /** processContent is 'skip' **/
  155. public static final short CONTENTSPECNODE_ANY_SKIP = 38;
  156. public static final short CONTENTSPECNODE_ANY_OTHER_SKIP = 39;
  157. public static final short CONTENTSPECNODE_ANY_LOCAL_SKIP = 40;
  158. //
  159. // Data
  160. //
  161. /**
  162. * The content spec node type.
  163. *
  164. * @see #CONTENTSPECNODE_LEAF
  165. * @see #CONTENTSPECNODE_ZERO_OR_ONE
  166. * @see #CONTENTSPECNODE_ZERO_OR_MORE
  167. * @see #CONTENTSPECNODE_ONE_OR_MORE
  168. * @see #CONTENTSPECNODE_CHOICE
  169. * @see #CONTENTSPECNODE_SEQ
  170. */
  171. public short type;
  172. /**
  173. * The "left hand" value object of the content spec node.
  174. * leaf name.localpart, single child for unary ops, left child for binary ops.
  175. */
  176. public Object value;
  177. /**
  178. * The "right hand" value of the content spec node.
  179. * leaf name.uri, right child for binary ops
  180. */
  181. public Object otherValue;
  182. //
  183. // Constructors
  184. //
  185. /** Default constructor. */
  186. public XMLContentSpec() {
  187. clear();
  188. }
  189. /** Constructs a content spec with the specified values. */
  190. public XMLContentSpec(short type, Object value, Object otherValue) {
  191. setValues(type, value, otherValue);
  192. }
  193. /**
  194. * Constructs a content spec from the values in the specified content spec.
  195. */
  196. public XMLContentSpec(XMLContentSpec contentSpec) {
  197. setValues(contentSpec);
  198. }
  199. /**
  200. * Constructs a content spec from the values specified by the given
  201. * content spec provider and identifier.
  202. */
  203. public XMLContentSpec(XMLContentSpec.Provider provider,
  204. int contentSpecIndex) {
  205. setValues(provider, contentSpecIndex);
  206. }
  207. //
  208. // Public methods
  209. //
  210. /** Clears the values. */
  211. public void clear() {
  212. type = -1;
  213. value = null;
  214. otherValue = null;
  215. }
  216. /** Sets the values. */
  217. public void setValues(short type, Object value, Object otherValue) {
  218. this.type = type;
  219. this.value = value;
  220. this.otherValue = otherValue;
  221. }
  222. /** Sets the values of the specified content spec. */
  223. public void setValues(XMLContentSpec contentSpec) {
  224. type = contentSpec.type;
  225. value = contentSpec.value;
  226. otherValue = contentSpec.otherValue;
  227. }
  228. /**
  229. * Sets the values from the values specified by the given content spec
  230. * provider and identifier. If the specified content spec cannot be
  231. * provided, the values of this content spec are cleared.
  232. */
  233. public void setValues(XMLContentSpec.Provider provider,
  234. int contentSpecIndex) {
  235. if (!provider.getContentSpec(contentSpecIndex, this)) {
  236. clear();
  237. }
  238. }
  239. //
  240. // Object methods
  241. //
  242. /** Returns a hash code for this node. */
  243. public int hashCode() {
  244. return type << 16 |
  245. value.hashCode() << 8 |
  246. otherValue.hashCode();
  247. }
  248. /** Returns true if the two objects are equal. */
  249. public boolean equals(Object object) {
  250. if (object != null && object instanceof XMLContentSpec) {
  251. XMLContentSpec contentSpec = (XMLContentSpec)object;
  252. return type == contentSpec.type &&
  253. value == contentSpec.value &&
  254. otherValue == contentSpec.otherValue;
  255. }
  256. return false;
  257. }
  258. //
  259. // Interfaces
  260. //
  261. /**
  262. * Provides a means for walking the structure built out of
  263. * content spec "nodes". The user of this provider interface is
  264. * responsible for knowing what the content spec node values
  265. * "mean". If those values refer to content spec identifiers,
  266. * then the user can call back into the provider to get the
  267. * next content spec node in the structure.
  268. */
  269. public interface Provider {
  270. //
  271. // XMLContentSpec.Provider methods
  272. //
  273. /**
  274. * Fills in the provided content spec structure with content spec
  275. * information for a unique identifier.
  276. *
  277. * @param contentSpecIndex The content spec identifier. All content
  278. * spec "nodes" have a unique identifier.
  279. * @param contentSpec The content spec struct to fill in with
  280. * the information.
  281. *
  282. * @return Returns true if the contentSpecIndex was found.
  283. */
  284. public boolean getContentSpec(int contentSpecIndex, XMLContentSpec contentSpec);
  285. } // interface Provider
  286. } // class XMLContentSpec