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.models;
  58. import com.sun.org.apache.xerces.internal.xni.QName;
  59. import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl;
  60. import com.sun.org.apache.xerces.internal.impl.xs.SubstitutionGroupHandler;
  61. import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaException;
  62. import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;
  63. import java.util.Vector;
  64. /**
  65. * XSAllCM implements XSCMValidator and handles <all>
  66. *
  67. * @author Pavani Mukthipudi, Sun Microsystems Inc.
  68. * @version $Id: XSAllCM.java,v 1.8 2003/03/11 15:48:33 sandygao Exp $
  69. */
  70. public class XSAllCM implements XSCMValidator {
  71. //
  72. // Constants
  73. //
  74. // start the content model: did not see any children
  75. private static final short STATE_START = 0;
  76. private static final short STATE_VALID = 1;
  77. private static final short STATE_CHILD = 1;
  78. //
  79. // Data
  80. //
  81. private XSElementDecl fAllElements[];
  82. private boolean fIsOptionalElement[];
  83. private boolean fHasOptionalContent = false;
  84. private int fNumElements = 0;
  85. //
  86. // Constructors
  87. //
  88. public XSAllCM (boolean hasOptionalContent, int size) {
  89. fHasOptionalContent = hasOptionalContent;
  90. fAllElements = new XSElementDecl[size];
  91. fIsOptionalElement = new boolean[size];
  92. }
  93. public void addElement (XSElementDecl element, boolean isOptional) {
  94. fAllElements[fNumElements] = element;
  95. fIsOptionalElement[fNumElements] = isOptional;
  96. fNumElements++;
  97. }
  98. //
  99. // XSCMValidator methods
  100. //
  101. /**
  102. * This methods to be called on entering a first element whose type
  103. * has this content model. It will return the initial state of the
  104. * content model
  105. *
  106. * @return Start state of the content model
  107. */
  108. public int[] startContentModel() {
  109. int[] state = new int[fNumElements + 1];
  110. for (int i = 0; i <= fNumElements; i++) {
  111. state[i] = STATE_START;
  112. }
  113. return state;
  114. }
  115. // convinient method: when error occurs, to find a matching decl
  116. // from the candidate elements.
  117. Object findMatchingDecl(QName elementName, SubstitutionGroupHandler subGroupHandler) {
  118. Object matchingDecl = null;
  119. for (int i = 0; i < fNumElements; i++) {
  120. matchingDecl = subGroupHandler.getMatchingElemDecl(elementName, fAllElements[i]);
  121. if (matchingDecl != null)
  122. break;
  123. }
  124. return matchingDecl;
  125. }
  126. /**
  127. * The method corresponds to one transition in the content model.
  128. *
  129. * @param elementName
  130. * @param state Current state
  131. * @return an element decl object
  132. */
  133. public Object oneTransition (QName elementName, int[] currentState, SubstitutionGroupHandler subGroupHandler) {
  134. // error state
  135. if (currentState[0] < 0) {
  136. currentState[0] = XSCMValidator.SUBSEQUENT_ERROR;
  137. return findMatchingDecl(elementName, subGroupHandler);
  138. }
  139. // seen child
  140. currentState[0] = STATE_CHILD;
  141. Object matchingDecl = null;
  142. for (int i = 0; i < fNumElements; i++) {
  143. // we only try to look for a matching decl if we have not seen
  144. // this element yet.
  145. if (currentState[i+1] != STATE_START)
  146. continue;
  147. matchingDecl = subGroupHandler.getMatchingElemDecl(elementName, fAllElements[i]);
  148. if (matchingDecl != null) {
  149. // found the decl, mark this element as "seen".
  150. currentState[i+1] = STATE_VALID;
  151. return matchingDecl;
  152. }
  153. }
  154. // couldn't find the decl, change to error state.
  155. currentState[0] = XSCMValidator.FIRST_ERROR;
  156. return findMatchingDecl(elementName, subGroupHandler);
  157. }
  158. /**
  159. * The method indicates the end of list of children
  160. *
  161. * @param state Current state of the content model
  162. * @return true if the last state was a valid final state
  163. */
  164. public boolean endContentModel (int[] currentState) {
  165. int state = currentState[0];
  166. if (state == XSCMValidator.FIRST_ERROR || state == XSCMValidator.SUBSEQUENT_ERROR) {
  167. return false;
  168. }
  169. // If <all> has minOccurs of zero and there are
  170. // no children to validate, it is trivially valid
  171. if (fHasOptionalContent && state == STATE_START) {
  172. return true;
  173. }
  174. for (int i = 0; i < fNumElements; i++) {
  175. // if one element is required, but not present, then error
  176. if (!fIsOptionalElement[i] && currentState[i+1] == STATE_START)
  177. return false;
  178. }
  179. return true;
  180. }
  181. /**
  182. * check whether this content violates UPA constraint.
  183. *
  184. * @param errors to hold the UPA errors
  185. * @return true if this content model contains other or list wildcard
  186. */
  187. public boolean checkUniqueParticleAttribution(SubstitutionGroupHandler subGroupHandler) throws XMLSchemaException {
  188. // check whether there is conflict between any two leaves
  189. for (int i = 0; i < fNumElements; i++) {
  190. for (int j = i+1; j < fNumElements; j++) {
  191. if (XSConstraints.overlapUPA(fAllElements[i], fAllElements[j], subGroupHandler)) {
  192. // REVISIT: do we want to report all errors? or just one?
  193. throw new XMLSchemaException("cos-nonambig", new Object[]{fAllElements[i].toString(),
  194. fAllElements[j].toString()});
  195. }
  196. }
  197. }
  198. return false;
  199. }
  200. /**
  201. * Check which elements are valid to appear at this point. This method also
  202. * works if the state is in error, in which case it returns what should
  203. * have been seen.
  204. *
  205. * @param state the current state
  206. * @return a Vector whose entries are instances of
  207. * either XSWildcardDecl or XSElementDecl.
  208. */
  209. public Vector whatCanGoHere(int[] state) {
  210. Vector ret = new Vector();
  211. for (int i = 0; i < fNumElements; i++) {
  212. // we only try to look for a matching decl if we have not seen
  213. // this element yet.
  214. if (state[i+1] == STATE_START)
  215. ret.addElement(fAllElements[i]);
  216. }
  217. return ret;
  218. }
  219. } // class XSAllCM