1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 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) 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.XSConstants;
  59. import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  60. import com.sun.org.apache.xerces.internal.xs.XSParticle;
  61. import com.sun.org.apache.xerces.internal.xs.XSTerm;
  62. /**
  63. * Store schema particle declaration.
  64. *
  65. * @author Sandy Gao, IBM
  66. *
  67. * @version $Id: XSParticleDecl.java,v 1.12 2003/11/11 20:14:58 sandygao Exp $
  68. */
  69. public class XSParticleDecl implements XSParticle {
  70. // types of particles
  71. public static final short PARTICLE_EMPTY = 0;
  72. public static final short PARTICLE_ELEMENT = 1;
  73. public static final short PARTICLE_WILDCARD = 2;
  74. public static final short PARTICLE_MODELGROUP = 3;
  75. public static final short PARTICLE_ZERO_OR_MORE = 4;
  76. public static final short PARTICLE_ZERO_OR_ONE = 5;
  77. public static final short PARTICLE_ONE_OR_MORE = 6;
  78. // type of the particle
  79. public short fType = PARTICLE_EMPTY;
  80. // term of the particle
  81. // for PARTICLE_ELEMENT : the element decl
  82. // for PARTICLE_WILDCARD: the wildcard decl
  83. // for PARTICLE_MODELGROUP: the model group
  84. public XSTerm fValue = null;
  85. // minimum occurrence of this particle
  86. public int fMinOccurs = 1;
  87. // maximum occurrence of this particle
  88. public int fMaxOccurs = 1;
  89. // clone this decl
  90. public XSParticleDecl makeClone() {
  91. XSParticleDecl particle = new XSParticleDecl();
  92. particle.fType = fType;
  93. particle.fMinOccurs = fMinOccurs;
  94. particle.fMaxOccurs = fMaxOccurs;
  95. particle.fDescription = fDescription;
  96. particle.fValue = fValue;
  97. return particle;
  98. }
  99. /**
  100. * 3.9.6 Schema Component Constraint: Particle Emptiable
  101. * whether this particle is emptible
  102. */
  103. public boolean emptiable() {
  104. return minEffectiveTotalRange() == 0;
  105. }
  106. // whether this particle contains nothing
  107. public boolean isEmpty() {
  108. if (fType == PARTICLE_EMPTY)
  109. return true;
  110. if (fType == PARTICLE_ELEMENT || fType == PARTICLE_WILDCARD)
  111. return false;
  112. return ((XSModelGroupImpl)fValue).isEmpty();
  113. }
  114. /**
  115. * 3.8.6 Effective Total Range (all and sequence) and
  116. * Effective Total Range (choice)
  117. * The following methods are used to return min/max range for a particle.
  118. * They are not exactly the same as it's described in the spec, but all the
  119. * values from the spec are retrievable by these methods.
  120. */
  121. public int minEffectiveTotalRange() {
  122. if (fType == XSParticleDecl.PARTICLE_EMPTY) {
  123. return 0;
  124. }
  125. if (fType == PARTICLE_MODELGROUP) {
  126. return ((XSModelGroupImpl)fValue).minEffectiveTotalRange() * fMinOccurs;
  127. }
  128. return fMinOccurs;
  129. }
  130. public int maxEffectiveTotalRange() {
  131. if (fType == XSParticleDecl.PARTICLE_EMPTY) {
  132. return 0;
  133. }
  134. if (fType == PARTICLE_MODELGROUP) {
  135. int max = ((XSModelGroupImpl)fValue).maxEffectiveTotalRange();
  136. if (max == SchemaSymbols.OCCURRENCE_UNBOUNDED)
  137. return SchemaSymbols.OCCURRENCE_UNBOUNDED;
  138. if (max != 0 && fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)
  139. return SchemaSymbols.OCCURRENCE_UNBOUNDED;
  140. return max * fMaxOccurs;
  141. }
  142. return fMaxOccurs;
  143. }
  144. /**
  145. * get the string description of this particle
  146. */
  147. private String fDescription = null;
  148. public String toString() {
  149. if (fDescription == null) {
  150. StringBuffer buffer = new StringBuffer();
  151. appendParticle(buffer);
  152. if (!(fMinOccurs == 0 && fMaxOccurs == 0 ||
  153. fMinOccurs == 1 && fMaxOccurs == 1)) {
  154. buffer.append("{" + fMinOccurs);
  155. if (fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED)
  156. buffer.append("-UNBOUNDED");
  157. else if (fMinOccurs != fMaxOccurs)
  158. buffer.append("-" + fMaxOccurs);
  159. buffer.append("}");
  160. }
  161. fDescription = buffer.toString();
  162. }
  163. return fDescription;
  164. }
  165. /**
  166. * append the string description of this particle to the string buffer
  167. * this is for error message.
  168. */
  169. void appendParticle(StringBuffer buffer) {
  170. switch (fType) {
  171. case PARTICLE_EMPTY:
  172. buffer.append("EMPTY");
  173. break;
  174. case PARTICLE_ELEMENT:
  175. case PARTICLE_WILDCARD:
  176. buffer.append('(');
  177. buffer.append(fValue.toString());
  178. buffer.append(')');
  179. break;
  180. case PARTICLE_MODELGROUP:
  181. buffer.append(fValue.toString());
  182. break;
  183. }
  184. }
  185. public void reset(){
  186. fType = PARTICLE_EMPTY;
  187. fValue = null;
  188. fMinOccurs = 1;
  189. fMaxOccurs = 1;
  190. fDescription = null;
  191. }
  192. /**
  193. * Get the type of the object, i.e ELEMENT_DECLARATION.
  194. */
  195. public short getType() {
  196. return XSConstants.PARTICLE;
  197. }
  198. /**
  199. * The <code>name</code> of this <code>XSObject</code> depending on the
  200. * <code>XSObject</code> type.
  201. */
  202. public String getName() {
  203. return null;
  204. }
  205. /**
  206. * The namespace URI of this node, or <code>null</code> if it is
  207. * unspecified. defines how a namespace URI is attached to schema
  208. * components.
  209. */
  210. public String getNamespace() {
  211. return null;
  212. }
  213. /**
  214. * {min occurs} determines the minimum number of terms that can occur.
  215. */
  216. public int getMinOccurs() {
  217. return fMinOccurs;
  218. }
  219. /**
  220. * {max occurs} whether the maxOccurs value is unbounded.
  221. */
  222. public boolean getMaxOccursUnbounded() {
  223. return fMaxOccurs == SchemaSymbols.OCCURRENCE_UNBOUNDED;
  224. }
  225. /**
  226. * {max occurs} determines the maximum number of terms that can occur.
  227. */
  228. public int getMaxOccurs() {
  229. return fMaxOccurs;
  230. }
  231. /**
  232. * {term} One of a model group, a wildcard, or an element declaration.
  233. */
  234. public XSTerm getTerm() {
  235. return fValue;
  236. }
  237. /**
  238. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  239. */
  240. public XSNamespaceItem getNamespaceItem() {
  241. return null;
  242. }
  243. } // class XSParticle