1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2002,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) 2001, 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.xs.*;
  59. import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  60. /**
  61. * Store schema model group declaration.
  62. *
  63. * @author Sandy Gao, IBM
  64. *
  65. * @version $Id: XSModelGroupImpl.java,v 1.7 2004/04/12 20:15:35 sandygao Exp $
  66. */
  67. public class XSModelGroupImpl implements XSModelGroup {
  68. // types of model groups
  69. // REVISIT: can't use same constants as those for particles, because
  70. // there are place where the constants are used together. For example,
  71. // to check whether the content is an element or a sequence.
  72. public static final short MODELGROUP_CHOICE = 101;
  73. public static final short MODELGROUP_SEQUENCE = 102;
  74. public static final short MODELGROUP_ALL = 103;
  75. // compositor of the model group
  76. public short fCompositor;
  77. // particles
  78. public XSParticleDecl[] fParticles = null;
  79. public int fParticleCount = 0;
  80. // this particle's optional annotation
  81. public XSAnnotationImpl fAnnotation;
  82. // whether this model group contains nothing
  83. public boolean isEmpty() {
  84. for (int i = 0; i < fParticleCount; i++) {
  85. if (!fParticles[i].isEmpty())
  86. return false;
  87. }
  88. return true;
  89. }
  90. /**
  91. * 3.8.6 Effective Total Range (all and sequence) and
  92. * Effective Total Range (choice)
  93. * The following methods are used to return min/max range for a particle.
  94. * They are not exactly the same as it's described in the spec, but all the
  95. * values from the spec are retrievable by these methods.
  96. */
  97. public int minEffectiveTotalRange() {
  98. if (fCompositor == MODELGROUP_CHOICE)
  99. return minEffectiveTotalRangeChoice();
  100. else
  101. return minEffectiveTotalRangeAllSeq();
  102. }
  103. // return the sum of all min values of the particles
  104. private int minEffectiveTotalRangeAllSeq() {
  105. int total = 0;
  106. for (int i = 0; i < fParticleCount; i++)
  107. total += fParticles[i].minEffectiveTotalRange();
  108. return total;
  109. }
  110. // return the min of all min values of the particles
  111. private int minEffectiveTotalRangeChoice() {
  112. int min = 0, one;
  113. if (fParticleCount > 0)
  114. min = fParticles[0].minEffectiveTotalRange();
  115. for (int i = 1; i < fParticleCount; i++) {
  116. one = fParticles[i].minEffectiveTotalRange();
  117. if (one < min)
  118. min = one;
  119. }
  120. return min;
  121. }
  122. public int maxEffectiveTotalRange() {
  123. if (fCompositor == MODELGROUP_CHOICE)
  124. return maxEffectiveTotalRangeChoice();
  125. else
  126. return maxEffectiveTotalRangeAllSeq();
  127. }
  128. // if one of the max value of the particles is unbounded, return unbounded;
  129. // otherwise return the sum of all max values
  130. private int maxEffectiveTotalRangeAllSeq() {
  131. int total = 0, one;
  132. for (int i = 0; i < fParticleCount; i++) {
  133. one = fParticles[i].maxEffectiveTotalRange();
  134. if (one == SchemaSymbols.OCCURRENCE_UNBOUNDED)
  135. return SchemaSymbols.OCCURRENCE_UNBOUNDED;
  136. total += one;
  137. }
  138. return total;
  139. }
  140. // if one of the max value of the particles is unbounded, return unbounded;
  141. // otherwise return the max of all max values
  142. private int maxEffectiveTotalRangeChoice() {
  143. int max = 0, one;
  144. if (fParticleCount > 0) {
  145. max = fParticles[0].maxEffectiveTotalRange();
  146. if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
  147. return SchemaSymbols.OCCURRENCE_UNBOUNDED;
  148. }
  149. for (int i = 1; i < fParticleCount; i++) {
  150. one = fParticles[i].maxEffectiveTotalRange();
  151. if (one == SchemaSymbols.OCCURRENCE_UNBOUNDED)
  152. return SchemaSymbols.OCCURRENCE_UNBOUNDED;
  153. if (one > max)
  154. max = one;
  155. }
  156. return max;
  157. }
  158. /**
  159. * get the string description of this particle
  160. */
  161. private String fDescription = null;
  162. public String toString() {
  163. if (fDescription == null) {
  164. StringBuffer buffer = new StringBuffer();
  165. if (fCompositor == MODELGROUP_ALL)
  166. buffer.append("all(");
  167. else
  168. buffer.append('(');
  169. if (fParticleCount > 0)
  170. buffer.append(fParticles[0].toString());
  171. for (int i = 1; i < fParticleCount; i++) {
  172. if (fCompositor == MODELGROUP_CHOICE)
  173. buffer.append('|');
  174. else
  175. buffer.append(',');
  176. buffer.append(fParticles[i].toString());
  177. }
  178. buffer.append(')');
  179. fDescription = buffer.toString();
  180. }
  181. return fDescription;
  182. }
  183. public void reset(){
  184. fCompositor = MODELGROUP_SEQUENCE;
  185. fParticles = null;
  186. fParticleCount = 0;
  187. fDescription = null;
  188. fAnnotation = null;
  189. }
  190. /**
  191. * Get the type of the object, i.e ELEMENT_DECLARATION.
  192. */
  193. public short getType() {
  194. return XSConstants.MODEL_GROUP;
  195. }
  196. /**
  197. * The <code>name</code> of this <code>XSObject</code> depending on the
  198. * <code>XSObject</code> type.
  199. */
  200. public String getName() {
  201. return null;
  202. }
  203. /**
  204. * The namespace URI of this node, or <code>null</code> if it is
  205. * unspecified. defines how a namespace URI is attached to schema
  206. * components.
  207. */
  208. public String getNamespace() {
  209. return null;
  210. }
  211. /**
  212. * {compositor} One of all, choice or sequence. The valid constants values
  213. * are: ALL, CHOICE, SEQUENCE.
  214. */
  215. public short getCompositor() {
  216. if (fCompositor == MODELGROUP_CHOICE)
  217. return XSModelGroup.COMPOSITOR_CHOICE;
  218. else if (fCompositor == MODELGROUP_SEQUENCE)
  219. return XSModelGroup.COMPOSITOR_SEQUENCE;
  220. else
  221. return XSModelGroup.COMPOSITOR_ALL;
  222. }
  223. /**
  224. * {particles} A list of particles
  225. */
  226. public XSObjectList getParticles() {
  227. return new XSObjectListImpl(fParticles, fParticleCount);
  228. }
  229. /**
  230. * Optional. Annotation.
  231. */
  232. public XSAnnotation getAnnotation() {
  233. return fAnnotation;
  234. }
  235. /**
  236. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  237. */
  238. public XSNamespaceItem getNamespaceItem() {
  239. return null;
  240. }
  241. } // class XSModelGroupImpl