1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-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.impl.dv.ValidatedInfo;
  59. import com.sun.org.apache.xerces.internal.impl.xs.identity.IdentityConstraint;
  60. import com.sun.org.apache.xerces.internal.xs.ShortList;
  61. import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
  62. import com.sun.org.apache.xerces.internal.xs.XSComplexTypeDefinition;
  63. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  64. import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
  65. import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
  66. import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  67. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  68. import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl;
  69. /**
  70. * The XML representation for an element declaration
  71. * schema component is an <element> element information item
  72. *
  73. * @author Elena Litani, IBM
  74. * @author Sandy Gao, IBM
  75. * @version $Id: XSElementDecl.java,v 1.14 2003/11/12 23:17:33 sandygao Exp $
  76. */
  77. public class XSElementDecl implements XSElementDeclaration {
  78. // scopes
  79. public final static short SCOPE_ABSENT = 0;
  80. public final static short SCOPE_GLOBAL = 1;
  81. public final static short SCOPE_LOCAL = 2;
  82. // name of the element
  83. public String fName = null;
  84. // target namespace of the element
  85. public String fTargetNamespace = null;
  86. // type of the element
  87. public XSTypeDefinition fType = null;
  88. // misc flag of the element: nillable/abstract/fixed
  89. short fMiscFlags = 0;
  90. public short fScope = XSConstants.SCOPE_ABSENT;
  91. // enclosing complex type, when the scope is local
  92. XSComplexTypeDecl fEnclosingCT = null;
  93. // block set (disallowed substitutions) of the element
  94. public short fBlock = XSConstants.DERIVATION_NONE;
  95. // final set (substitution group exclusions) of the element
  96. public short fFinal = XSConstants.DERIVATION_NONE;
  97. // optional annotation
  98. public XSAnnotationImpl fAnnotation = null;
  99. // value constraint value
  100. public ValidatedInfo fDefault = null;
  101. // the substitution group affiliation of the element
  102. public XSElementDecl fSubGroup = null;
  103. // identity constraints
  104. static final int INITIAL_SIZE = 2;
  105. int fIDCPos = 0;
  106. IdentityConstraint[] fIDConstraints = new IdentityConstraint[INITIAL_SIZE];
  107. private static final short CONSTRAINT_MASK = 3;
  108. private static final short NILLABLE = 4;
  109. private static final short ABSTRACT = 8;
  110. // methods to get/set misc flag
  111. public void setConstraintType(short constraintType) {
  112. // first clear the bits
  113. fMiscFlags ^= (fMiscFlags & CONSTRAINT_MASK);
  114. // then set the proper one
  115. fMiscFlags |= (constraintType & CONSTRAINT_MASK);
  116. }
  117. public void setIsNillable() {
  118. fMiscFlags |= NILLABLE;
  119. }
  120. public void setIsAbstract() {
  121. fMiscFlags |= ABSTRACT;
  122. }
  123. public void setIsGlobal() {
  124. fScope = SCOPE_GLOBAL;
  125. }
  126. public void setIsLocal(XSComplexTypeDecl enclosingCT) {
  127. fScope = SCOPE_LOCAL;
  128. fEnclosingCT = enclosingCT;
  129. }
  130. public void addIDConstraint(IdentityConstraint idc) {
  131. if (fIDCPos == fIDConstraints.length) {
  132. fIDConstraints = resize(fIDConstraints, fIDCPos*2);
  133. }
  134. fIDConstraints[fIDCPos++] = idc;
  135. }
  136. public IdentityConstraint[] getIDConstraints() {
  137. if (fIDCPos == 0) {
  138. return null;
  139. }
  140. if (fIDCPos < fIDConstraints.length) {
  141. fIDConstraints = resize(fIDConstraints, fIDCPos);
  142. }
  143. return fIDConstraints;
  144. }
  145. static final IdentityConstraint[] resize(IdentityConstraint[] oldArray, int newSize) {
  146. IdentityConstraint[] newArray = new IdentityConstraint[newSize];
  147. System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
  148. return newArray;
  149. }
  150. /**
  151. * get the string description of this element
  152. */
  153. private String fDescription = null;
  154. public String toString() {
  155. if (fDescription == null) {
  156. StringBuffer buffer = new StringBuffer();
  157. buffer.append("\"");
  158. if (fTargetNamespace != null)
  159. buffer.append(fTargetNamespace);
  160. buffer.append("\"");
  161. buffer.append(":");
  162. buffer.append(fName);
  163. fDescription = buffer.toString();
  164. }
  165. return fDescription;
  166. }
  167. /**
  168. * get the hash code
  169. */
  170. public int hashCode() {
  171. int code = fName.hashCode();
  172. if (fTargetNamespace != null)
  173. code = (code<<16)+fTargetNamespace.hashCode();
  174. return code;
  175. }
  176. /**
  177. * whether two decls are the same
  178. */
  179. public boolean equals(Object o) {
  180. return o == this;
  181. }
  182. /**
  183. * Reset current element declaration
  184. */
  185. public void reset(){
  186. fName = null;
  187. fTargetNamespace = null;
  188. fType = null;
  189. fMiscFlags = 0;
  190. fBlock = XSConstants.DERIVATION_NONE;
  191. fFinal = XSConstants.DERIVATION_NONE;
  192. fDefault = null;
  193. fAnnotation = null;
  194. fSubGroup = null;
  195. // reset identity constraints
  196. for (int i=0;i<fIDCPos;i++) {
  197. fIDConstraints[i] = null;
  198. }
  199. fIDCPos = 0;
  200. }
  201. /**
  202. * Get the type of the object, i.e ELEMENT_DECLARATION.
  203. */
  204. public short getType() {
  205. return XSConstants.ELEMENT_DECLARATION;
  206. }
  207. /**
  208. * The <code>name</code> of this <code>XSObject</code> depending on the
  209. * <code>XSObject</code> type.
  210. */
  211. public String getName() {
  212. return fName;
  213. }
  214. /**
  215. * The namespace URI of this node, or <code>null</code> if it is
  216. * unspecified. defines how a namespace URI is attached to schema
  217. * components.
  218. */
  219. public String getNamespace() {
  220. return fTargetNamespace;
  221. }
  222. /**
  223. * Either a simple type definition or a complex type definition.
  224. */
  225. public XSTypeDefinition getTypeDefinition() {
  226. return fType;
  227. }
  228. /**
  229. * Optional. Either global or a complex type definition (
  230. * <code>ctDefinition</code>). This property is absent in the case of
  231. * declarations within named model groups: their scope will be
  232. * determined when they are used in the construction of complex type
  233. * definitions.
  234. */
  235. public short getScope() {
  236. return fScope;
  237. }
  238. /**
  239. * Locally scoped declarations are available for use only within the
  240. * complex type definition identified by the <code>scope</code>
  241. * property.
  242. */
  243. public XSComplexTypeDefinition getEnclosingCTDefinition() {
  244. return fEnclosingCT;
  245. }
  246. /**
  247. * A value constraint: one of default, fixed.
  248. */
  249. public short getConstraintType() {
  250. return (short)(fMiscFlags & CONSTRAINT_MASK);
  251. }
  252. /**
  253. * A value constraint: The actual value (with respect to the {type
  254. * definition})
  255. */
  256. public String getConstraintValue() {
  257. // REVISIT: SCAPI: what's the proper representation
  258. return getConstraintType() == XSConstants.VC_NONE ?
  259. null :
  260. fDefault.stringValue();
  261. }
  262. /**
  263. * If {nillable} is true, then an element may also be valid if it carries
  264. * the namespace qualified attribute with [local name] nil from
  265. * namespace http://www.w3.org/2001/XMLSchema-instance and value true
  266. * (see xsi:nil (2.6.2)) even if it has no text or element content
  267. * despite a {content type} which would otherwise require content.
  268. */
  269. public boolean getNillable() {
  270. return ((fMiscFlags & NILLABLE) != 0);
  271. }
  272. /**
  273. * {identity-constraint definitions} A set of constraint definitions.
  274. */
  275. public XSNamedMap getIdentityConstraints() {
  276. return new XSNamedMapImpl(fIDConstraints, fIDCPos);
  277. }
  278. /**
  279. * {substitution group affiliation} Optional. A top-level element
  280. * definition.
  281. */
  282. public XSElementDeclaration getSubstitutionGroupAffiliation() {
  283. return fSubGroup;
  284. }
  285. /**
  286. * Convenience method. Check if <code>exclusion</code> is a substitution
  287. * group exclusion for this element declaration.
  288. * @param exclusion Extension, restriction or none. Represents final
  289. * set for the element.
  290. * @return True if <code>exclusion</code> is a part of the substitution
  291. * group exclusion subset.
  292. */
  293. public boolean isSubstitutionGroupExclusion(short exclusion) {
  294. return (fFinal & exclusion) != 0;
  295. }
  296. /**
  297. * Specifies if this declaration can be nominated as
  298. * the {substitution group affiliation} of other
  299. * element declarations having the same {type definition}
  300. * or types derived therefrom.
  301. *
  302. * @return A bit flag representing {extension, restriction} or NONE.
  303. */
  304. public short getSubstitutionGroupExclusions() {
  305. return fFinal;
  306. }
  307. /**
  308. * Convenience method. Check if <code>disallowed</code> is a disallowed
  309. * substitution for this element declaration.
  310. * @param disallowed Substitution, extension, restriction or none.
  311. * Represents a block set for the element.
  312. * @return True if <code>disallowed</code> is a part of the substitution
  313. * group exclusion subset.
  314. */
  315. public boolean isDisallowedSubstitution(short disallowed) {
  316. return (fBlock & disallowed) != 0;
  317. }
  318. /**
  319. * The supplied values for {disallowed substitutions}
  320. *
  321. * @return A bit flag representing {substitution, extension, restriction} or NONE.
  322. */
  323. public short getDisallowedSubstitutions() {
  324. return fBlock;
  325. }
  326. /**
  327. * {abstract} A boolean.
  328. */
  329. public boolean getAbstract() {
  330. return ((fMiscFlags & ABSTRACT) != 0);
  331. }
  332. /**
  333. * Optional. Annotation.
  334. */
  335. public XSAnnotation getAnnotation() {
  336. return fAnnotation;
  337. }
  338. /**
  339. * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
  340. */
  341. public XSNamespaceItem getNamespaceItem() {
  342. // REVISIT: implement
  343. return null;
  344. }
  345. public Object getActualVC() {
  346. return fDefault.actualValue;
  347. }
  348. public short getActualVCType() {
  349. return fDefault.actualValueType;
  350. }
  351. public ShortList getItemValueTypes() {
  352. return fDefault.itemValueTypes;
  353. }
  354. } // class XSElementDecl