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.models;
  58. import com.sun.org.apache.xerces.internal.xni.QName;
  59. import com.sun.org.apache.xerces.internal.impl.dtd.XMLContentSpec;
  60. /**
  61. * @version $Id: MixedContentModel.java,v 1.5 2002/05/29 17:59:37 neilg Exp $
  62. *
  63. * MixedContentModel is a derivative of the abstract content model base
  64. * class that handles the special case of mixed model elements. If an element
  65. * is mixed model, it has PCDATA as its first possible content, followed
  66. * by an alternation of the possible children. The children cannot have any
  67. * numeration or order, so it must look like this:
  68. * <pre>
  69. * <!ELEMENT Foo ((#PCDATA|a|b|c|)*)>
  70. * </pre>
  71. * So, all we have to do is to keep an array of the possible children and
  72. * validate by just looking up each child being validated by looking it up
  73. * in the list.
  74. *
  75. */
  76. public class MixedContentModel
  77. implements ContentModelValidator {
  78. //
  79. // Data
  80. //
  81. /** The count of possible children that we have to deal with. */
  82. private int fCount;
  83. /** The list of possible children that we have to accept. */
  84. private QName fChildren[];
  85. /** The type of the children to support ANY. */
  86. private int fChildrenType[];
  87. /* this is the EquivClassComparator object */
  88. //private EquivClassComparator comparator = null;
  89. /**
  90. * True if mixed content model is ordered. DTD mixed content models
  91. * are <em>always</em> unordered.
  92. */
  93. private boolean fOrdered;
  94. //
  95. // Constructors
  96. //
  97. /**
  98. * Constructs a mixed content model.
  99. *
  100. * @param children The list of allowed children.
  101. * @param type The list of the types of the children.
  102. * @param offset The start offset position in the children.
  103. * @param length The child count.
  104. * @param ordered True if content must be ordered.
  105. * @param dtd True if it is for a DTDGrammar.
  106. *
  107. */
  108. public MixedContentModel(QName[] children, int[] type, int offset, int length , boolean ordered) {
  109. // Make our own copy now, which is exactly the right size
  110. fCount = length;
  111. fChildren = new QName[fCount];
  112. fChildrenType = new int[fCount];
  113. for (int i = 0; i < fCount; i++) {
  114. fChildren[i] = new QName(children[offset + i]);
  115. fChildrenType[i] = type[offset + i];
  116. }
  117. fOrdered = ordered;
  118. }
  119. //
  120. // ContentModelValidator methods
  121. //
  122. /**
  123. * Check that the specified content is valid according to this
  124. * content model. This method can also be called to do 'what if'
  125. * testing of content models just to see if they would be valid.
  126. * <p>
  127. * A value of -1 in the children array indicates a PCDATA node. All other
  128. * indexes will be positive and represent child elements. The count can be
  129. * zero, since some elements have the EMPTY content model and that must be
  130. * confirmed.
  131. *
  132. * @param children The children of this element. Each integer is an index within
  133. * the <code>StringPool</code> of the child element name. An index
  134. * of -1 is used to indicate an occurrence of non-whitespace character
  135. * data.
  136. * @param offset Offset into the array where the children starts.
  137. * @param length The number of entries in the <code>children</code> array.
  138. *
  139. * @return The value -1 if fully valid, else the 0 based index of the child
  140. * that first failed. If the value returned is equal to the number
  141. * of children, then the specified children are valid but additional
  142. * content is required to reach a valid ending state.
  143. *
  144. */
  145. public int validate(QName[] children, int offset, int length) {
  146. // must match order
  147. if (fOrdered) {
  148. int inIndex = 0;
  149. for (int outIndex = 0; outIndex < length; outIndex++) {
  150. // ignore mixed text
  151. final QName curChild = children[offset + outIndex];
  152. if (curChild.localpart == null) {
  153. continue;
  154. }
  155. // element must match
  156. int type = fChildrenType[inIndex];
  157. if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) {
  158. if (fChildren[inIndex].rawname != children[offset + outIndex].rawname) {
  159. return outIndex;
  160. }
  161. }
  162. else if (type == XMLContentSpec.CONTENTSPECNODE_ANY) {
  163. String uri = fChildren[inIndex].uri;
  164. if (uri != null && uri != children[outIndex].uri) {
  165. return outIndex;
  166. }
  167. }
  168. else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_LOCAL) {
  169. if (children[outIndex].uri != null) {
  170. return outIndex;
  171. }
  172. }
  173. else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) {
  174. if (fChildren[inIndex].uri == children[outIndex].uri) {
  175. return outIndex;
  176. }
  177. }
  178. // advance index
  179. inIndex++;
  180. }
  181. }
  182. // can appear in any order
  183. else {
  184. for (int outIndex = 0; outIndex < length; outIndex++)
  185. {
  186. // Get the current child out of the source index
  187. final QName curChild = children[offset + outIndex];
  188. // If its PCDATA, then we just accept that
  189. if (curChild.localpart == null)
  190. continue;
  191. // And try to find it in our list
  192. int inIndex = 0;
  193. for (; inIndex < fCount; inIndex++)
  194. {
  195. int type = fChildrenType[inIndex];
  196. if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) {
  197. if (curChild.rawname == fChildren[inIndex].rawname) {
  198. break;
  199. }
  200. }
  201. else if (type == XMLContentSpec.CONTENTSPECNODE_ANY) {
  202. String uri = fChildren[inIndex].uri;
  203. if (uri == null || uri == children[outIndex].uri) {
  204. break;
  205. }
  206. }
  207. else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_LOCAL) {
  208. if (children[outIndex].uri == null) {
  209. break;
  210. }
  211. }
  212. else if (type == XMLContentSpec.CONTENTSPECNODE_ANY_OTHER) {
  213. if (fChildren[inIndex].uri != children[outIndex].uri) {
  214. break;
  215. }
  216. }
  217. // REVISIT: What about checking for multiple ANY matches?
  218. // The content model ambiguity *could* be checked
  219. // by the caller before constructing the mixed
  220. // content model.
  221. }
  222. // We did not find this one, so the validation failed
  223. if (inIndex == fCount)
  224. return outIndex;
  225. }
  226. }
  227. // Everything seems to be in order, so return success
  228. return -1;
  229. } // validate
  230. } // class MixedContentModel