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.XSSimpleType;
  59. import com.sun.org.apache.xerces.internal.xs.*;
  60. import com.sun.org.apache.xerces.internal.impl.xs.models.XSCMValidator;
  61. import com.sun.org.apache.xerces.internal.impl.xs.models.CMBuilder;
  62. import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  63. import org.w3c.dom.TypeInfo;
  64. ;
  65. /**
  66. * The XML representation for a complexType
  67. * schema component is a <complexType> element information item
  68. *
  69. * @author Elena Litani, IBM
  70. * @author Sandy Gao, IBM
  71. * @version $Id: XSComplexTypeDecl.java,v 1.7 2003/12/12 02:58:29 kk122374 Exp $
  72. */
  73. public class XSComplexTypeDecl implements XSComplexTypeDefinition {
  74. // name of the complexType
  75. String fName = null;
  76. // target namespace of the complexType
  77. String fTargetNamespace = null;
  78. // base type of the complexType
  79. XSTypeDefinition fBaseType = null;
  80. // derivation method of the complexType
  81. short fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
  82. // final set of the complexType
  83. short fFinal = XSConstants.DERIVATION_NONE;
  84. // block set (prohibited substitution) of the complexType
  85. short fBlock = XSConstants.DERIVATION_NONE;
  86. // flags: whether is abstract; whether contains ID type;
  87. // whether it's an anonymous tpye
  88. short fMiscFlags = 0;
  89. // the attribute group that holds the attribute uses and attribute wildcard
  90. XSAttributeGroupDecl fAttrGrp = null;
  91. // the content type of the complexType
  92. short fContentType = CONTENTTYPE_EMPTY;
  93. // if the content type is simple, then the corresponding simpleType
  94. XSSimpleType fXSSimpleType = null;
  95. // if the content type is element or mixed, the particle
  96. XSParticleDecl fParticle = null;
  97. // if there is a particle, the content model corresponding to that particle
  98. XSCMValidator fCMValidator = null;
  99. // list of annotations affiliated with this type
  100. XSObjectListImpl fAnnotations = null;
  101. public XSComplexTypeDecl() {
  102. // do-nothing constructor for now.
  103. }
  104. public void setValues(String name, String targetNamespace,
  105. XSTypeDefinition baseType, short derivedBy, short schemaFinal,
  106. short block, short contentType,
  107. boolean isAbstract, XSAttributeGroupDecl attrGrp,
  108. XSSimpleType simpleType, XSParticleDecl particle,
  109. XSObjectListImpl annotations) {
  110. fTargetNamespace = targetNamespace;
  111. fBaseType = baseType;
  112. fDerivedBy = derivedBy;
  113. fFinal = schemaFinal;
  114. fBlock = block;
  115. fContentType = contentType;
  116. if(isAbstract)
  117. fMiscFlags |= CT_IS_ABSTRACT;
  118. fAttrGrp = attrGrp;
  119. fXSSimpleType = simpleType;
  120. fParticle = particle;
  121. fAnnotations = annotations;
  122. }
  123. public void setName(String name) {
  124. fName = name;
  125. }
  126. public short getTypeCategory() {
  127. return COMPLEX_TYPE;
  128. }
  129. public String getTypeName() {
  130. return fName;
  131. }
  132. public short getFinalSet(){
  133. return fFinal;
  134. }
  135. public String getTargetNamespace(){
  136. return fTargetNamespace;
  137. }
  138. // flags for the misc flag
  139. private static final short CT_IS_ABSTRACT = 1;
  140. private static final short CT_HAS_TYPE_ID = 2;
  141. private static final short CT_IS_ANONYMOUS = 4;
  142. // methods to get/set misc flag
  143. public boolean containsTypeID () {
  144. return((fMiscFlags & CT_HAS_TYPE_ID) != 0);
  145. }
  146. public void setIsAbstractType() {
  147. fMiscFlags |= CT_IS_ABSTRACT;
  148. }
  149. public void setContainsTypeID() {
  150. fMiscFlags |= CT_HAS_TYPE_ID;
  151. }
  152. public void setIsAnonymous() {
  153. fMiscFlags |= CT_IS_ANONYMOUS;
  154. }
  155. public synchronized XSCMValidator getContentModel(CMBuilder cmBuilder) {
  156. if (fCMValidator == null)
  157. fCMValidator = cmBuilder.getContentModel(this);
  158. return fCMValidator;
  159. }
  160. // some utility methods:
  161. // return the attribute group for this complex type
  162. public XSAttributeGroupDecl getAttrGrp() {
  163. return fAttrGrp;
  164. }
  165. public String toString() {
  166. StringBuffer str = new StringBuffer();
  167. appendTypeInfo(str);
  168. return str.toString();
  169. }
  170. void appendTypeInfo(StringBuffer str) {
  171. String contentType[] = {"EMPTY", "SIMPLE", "ELEMENT", "MIXED"};
  172. String derivedBy[] = {"EMPTY", "EXTENSION", "RESTRICTION"};
  173. str.append("Complex type name='" + fTargetNamespace + "," + getTypeName() + "', ");
  174. if (fBaseType != null)
  175. str.append(" base type name='" + fBaseType.getName() + "', ");
  176. str.append(" content type='" + contentType[fContentType] + "', ");
  177. str.append(" isAbstract='" + getAbstract() + "', ");
  178. str.append(" hasTypeId='" + containsTypeID() + "', ");
  179. str.append(" final='" + fFinal + "', ");
  180. str.append(" block='" + fBlock + "', ");
  181. if (fParticle != null)
  182. str.append(" particle='" + fParticle.toString() + "', ");
  183. str.append(" derivedBy='" + derivedBy[fDerivedBy] + "'. ");
  184. }
  185. public boolean derivedFromType(XSTypeDefinition ancestor, short derivationMethod) {
  186. // ancestor is null, retur false
  187. if (ancestor == null)
  188. return false;
  189. // ancestor is anyType, return true
  190. if (ancestor == SchemaGrammar.fAnyType)
  191. return true;
  192. // recursively get base, and compare it with ancestor
  193. XSTypeDefinition type = this;
  194. while (type != ancestor && // compare with ancestor
  195. type != SchemaGrammar.fAnySimpleType && // reached anySimpleType
  196. type != SchemaGrammar.fAnyType) { // reached anyType
  197. type = type.getBaseType();
  198. }
  199. return type == ancestor;
  200. }
  201. public boolean derivedFrom(String ancestorNS, String ancestorName, short derivationMethod) {
  202. // ancestor is null, retur false
  203. if (ancestorName == null)
  204. return false;
  205. // ancestor is anyType, return true
  206. if (ancestorNS != null &&
  207. ancestorNS.equals(SchemaSymbols.URI_SCHEMAFORSCHEMA) &&
  208. ancestorName.equals(SchemaSymbols.ATTVAL_ANYTYPE)) {
  209. return true;
  210. }
  211. // recursively get base, and compare it with ancestor
  212. XSTypeDefinition type = this;
  213. while (!(ancestorName.equals(type.getName()) &&
  214. ((ancestorNS == null && type.getNamespace() == null) ||
  215. (ancestorNS != null && ancestorNS.equals(type.getNamespace())))) && // compare with ancestor
  216. type != SchemaGrammar.fAnySimpleType && // reached anySimpleType
  217. type != SchemaGrammar.fAnyType) { // reached anyType
  218. if( derivationMethod!=0 ) {
  219. if( type instanceof XSComplexTypeDefinition ) {
  220. switch( ((XSComplexTypeDefinition)type).getDerivationMethod() ) {
  221. case XSConstants.DERIVATION_EXTENSION:
  222. if( (derivationMethod&TypeInfo.DERIVATION_EXTENSION) == 0 )
  223. return false;
  224. break;
  225. case XSConstants.DERIVATION_RESTRICTION:
  226. if( (derivationMethod&TypeInfo.DERIVATION_RESTRICTION) == 0 )
  227. return false;
  228. break;
  229. }
  230. } else
  231. if( type instanceof XSSimpleTypeDefinition ) {
  232. // broken (but then the spec is broken, so what do you expect!) FIXME.
  233. switch( ((XSSimpleTypeDefinition)type).getVariety()) {
  234. case XSSimpleTypeDefinition.VARIETY_ATOMIC:
  235. if( (derivationMethod&TypeInfo.DERIVATION_RESTRICTION) == 0 )
  236. return false;
  237. break;
  238. case XSSimpleTypeDefinition.VARIETY_LIST:
  239. if( (derivationMethod&TypeInfo.DERIVATION_LIST) == 0 )
  240. return false;
  241. break;
  242. case XSSimpleTypeDefinition.VARIETY_UNION:
  243. if( (derivationMethod&TypeInfo.DERIVATION_UNION) == 0 )
  244. return false;
  245. break;
  246. }
  247. }
  248. }
  249. type = (XSTypeDefinition)type.getBaseType();
  250. }
  251. return type != SchemaGrammar.fAnySimpleType &&
  252. type != SchemaGrammar.fAnyType;
  253. }
  254. public void reset(){
  255. fName = null;
  256. fTargetNamespace = null;
  257. fBaseType = null;
  258. fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
  259. fFinal = XSConstants.DERIVATION_NONE;
  260. fBlock = XSConstants.DERIVATION_NONE;
  261. fMiscFlags = 0;
  262. // reset attribute group
  263. fAttrGrp.reset();
  264. fContentType = CONTENTTYPE_EMPTY;
  265. fXSSimpleType = null;
  266. fParticle = null;
  267. fCMValidator = null;
  268. if(fAnnotations != null) {
  269. // help out the garbage collector
  270. fAnnotations.clear();
  271. }
  272. fAnnotations = null;
  273. }
  274. /**
  275. * Get the type of the object, i.e ELEMENT_DECLARATION.
  276. */
  277. public short getType() {
  278. return XSConstants.TYPE_DEFINITION;
  279. }
  280. /**
  281. * The <code>name</code> of this <code>XSObject</code> depending on the
  282. * <code>XSObject</code> type.
  283. */
  284. public String getName() {
  285. return getAnonymous() ? null : fName;
  286. }
  287. /**
  288. * A boolean that specifies if the type definition is anonymous.
  289. * Convenience attribute. This is a field is not part of
  290. * XML Schema component model.
  291. */
  292. public boolean getAnonymous() {
  293. return((fMiscFlags & CT_IS_ANONYMOUS) != 0);
  294. }
  295. /**
  296. * The namespace URI of this node, or <code>null</code> if it is
  297. * unspecified. defines how a namespace URI is attached to schema
  298. * components.
  299. */
  300. public String getNamespace() {
  301. return fTargetNamespace;
  302. }
  303. /**
  304. * {base type definition} Either a simple type definition or a complex
  305. * type definition.
  306. */
  307. public XSTypeDefinition getBaseType() {
  308. return fBaseType;
  309. }
  310. /**
  311. * {derivation method} Either extension or restriction. The valid constant
  312. * value for this <code>XSConstants</code> EXTENTION, RESTRICTION.
  313. */
  314. public short getDerivationMethod() {
  315. return fDerivedBy;
  316. }
  317. /**
  318. * {final} For complex type definition it is a subset of {extension,
  319. * restriction}. For simple type definition it is a subset of
  320. * {extension, list, restriction, union}.
  321. * @param derivation Extension, restriction, list, union constants
  322. * (defined in <code>XSConstants</code>).
  323. * @return True if derivation is in the final set, otherwise false.
  324. */
  325. public boolean isFinal(short derivation) {
  326. return (fFinal & derivation) != 0;
  327. }
  328. /**
  329. * {final} For complex type definition it is a subset of {extension, restriction}.
  330. *
  331. * @return A bit flag that represents:
  332. * {extension, restriction) or none for complexTypes;
  333. * {extension, list, restriction, union} or none for simpleTypes;
  334. */
  335. public short getFinal() {
  336. return fFinal;
  337. }
  338. /**
  339. * {abstract} A boolean. Complex types for which {abstract} is true must
  340. * not be used as the {type definition} for the validation of element
  341. * information items.
  342. */
  343. public boolean getAbstract() {
  344. return((fMiscFlags & CT_IS_ABSTRACT) != 0);
  345. }
  346. /**
  347. * {attribute uses} A set of attribute uses.
  348. */
  349. public XSObjectList getAttributeUses() {
  350. return fAttrGrp.getAttributeUses();
  351. }
  352. /**
  353. * {attribute wildcard} Optional. A wildcard.
  354. */
  355. public XSWildcard getAttributeWildcard() {
  356. return fAttrGrp.getAttributeWildcard();
  357. }
  358. /**
  359. * {content type} One of empty, a simple type definition (see
  360. * <code>simpleType</code>, or mixed, element-only (see
  361. * <code>cmParticle</code>).
  362. */
  363. public short getContentType() {
  364. return fContentType;
  365. }
  366. /**
  367. * A simple type definition corresponding to simple content model,
  368. * otherwise <code>null</code>
  369. */
  370. public XSSimpleTypeDefinition getSimpleType() {
  371. return fXSSimpleType;
  372. }
  373. /**
  374. * A particle for mixed or element-only content model, otherwise
  375. * <code>null</code>
  376. */
  377. public XSParticle getParticle() {
  378. return fParticle;
  379. }
  380. /**
  381. * {prohibited substitutions} A subset of {extension, restriction}.
  382. * @param prohibited extention or restriction constants (defined in
  383. * <code>XSConstants</code>).
  384. * @return True if prohibited is a prohibited substitution, otherwise
  385. * false.
  386. */
  387. public boolean isProhibitedSubstitution(short prohibited) {
  388. return (fBlock & prohibited) != 0;
  389. }
  390. /**
  391. * {prohibited substitutions}
  392. *
  393. * @return A bit flag corresponding to prohibited substitutions
  394. */
  395. public short getProhibitedSubstitutions() {
  396. return fBlock;
  397. }
  398. /**
  399. * Optional. Annotation.
  400. */
  401. public XSObjectList getAnnotations() {
  402. return fAnnotations;
  403. }
  404. /* (non-Javadoc)
  405. * @see com.sun.org.apache.xerces.internal.xs.XSComplexTypeDefinition#getAttributeUse(java.lang.String, java.lang.String)
  406. */
  407. public XSAttributeUse getAttributeUse(String namespace, String name) {
  408. return fAttrGrp.getAttributeUse(namespace, name);
  409. }
  410. /**
  411. * @see com.sun.org.apache.xerces.internal.impl.xs.psvi.XSObject#getNamespaceItem()
  412. */
  413. public XSNamespaceItem getNamespaceItem() {
  414. // REVISIT: implement
  415. return null;
  416. }
  417. public String getTypeNamespace() {
  418. return getNamespace();
  419. }
  420. public boolean isDerivedFrom(String typeNamespace, String typeName, int derivationMethod) {
  421. return derivedFrom(typeNamespace,typeName,(short)derivationMethod);
  422. }
  423. } // class XSComplexTypeDecl