1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2004 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.traversers;
  58. import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
  59. import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
  60. import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
  61. import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
  62. import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
  63. import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl;
  64. import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;
  65. import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl;
  66. import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;
  67. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  68. import com.sun.org.apache.xerces.internal.xs.XSObject;
  69. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  70. import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
  71. import com.sun.org.apache.xerces.internal.util.DOMUtil;
  72. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  73. import com.sun.org.apache.xerces.internal.xni.QName;
  74. import org.w3c.dom.Element;
  75. import org.w3c.dom.Attr;
  76. /**
  77. * The element declaration schema component traverser.
  78. * <element
  79. * abstract = boolean : false
  80. * block = (#all | List of (extension | restriction | substitution))
  81. * default = string
  82. * final = (#all | List of (extension | restriction))
  83. * fixed = string
  84. * form = (qualified | unqualified)
  85. * id = ID
  86. * maxOccurs = (nonNegativeInteger | unbounded) : 1
  87. * minOccurs = nonNegativeInteger : 1
  88. * name = NCName
  89. * nillable = boolean : false
  90. * ref = QName
  91. * substitutionGroup = QName
  92. * type = QName
  93. * {any attributes with non-schema namespace . . .}>
  94. * Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  95. * </element>
  96. *
  97. * @author Sandy Gao, IBM
  98. *
  99. * @version $Id: XSDElementTraverser.java,v 1.29 2004/02/03 17:27:45 sandygao Exp $
  100. */
  101. class XSDElementTraverser extends XSDAbstractTraverser {
  102. protected final XSElementDecl fTempElementDecl = new XSElementDecl();
  103. // this controls what happens when a local element is encountered.
  104. // We may not encounter all local elements when first parsing.
  105. boolean fDeferTraversingLocalElements;
  106. XSDElementTraverser (XSDHandler handler,
  107. XSAttributeChecker gAttrCheck) {
  108. super(handler, gAttrCheck);
  109. }
  110. /**
  111. * Traverse a locally declared element (or an element reference).
  112. *
  113. * To handle the recursive cases efficiently, we delay the traversal
  114. * and return an empty particle node. We'll fill in this particle node
  115. * later after we've done with all the global declarations.
  116. * This method causes a number of data structures in the schema handler to be filled in.
  117. *
  118. * @param elmDecl
  119. * @param schemaDoc
  120. * @param grammar
  121. * @return the particle
  122. */
  123. XSParticleDecl traverseLocal(Element elmDecl,
  124. XSDocumentInfo schemaDoc,
  125. SchemaGrammar grammar,
  126. int allContextFlags,
  127. XSObject parent) {
  128. XSParticleDecl particle = null;
  129. if (fSchemaHandler.fDeclPool !=null) {
  130. particle = fSchemaHandler.fDeclPool.getParticleDecl();
  131. } else {
  132. particle = new XSParticleDecl();
  133. }
  134. if(fDeferTraversingLocalElements) {
  135. // The only thing we care about now is whether this element has
  136. // minOccurs=0. This affects (if the element appears in a complex
  137. // type) whether a type has emptiable content.
  138. particle.fType = XSParticleDecl.PARTICLE_ELEMENT;
  139. Attr attr = elmDecl.getAttributeNode(SchemaSymbols.ATT_MINOCCURS);
  140. if (attr != null) {
  141. String min = attr.getValue();
  142. try {
  143. int m = Integer.parseInt(min.trim());
  144. if (m >= 0)
  145. particle.fMinOccurs = m;
  146. }
  147. catch (NumberFormatException ex) {
  148. }
  149. }
  150. fSchemaHandler.fillInLocalElemInfo(elmDecl, schemaDoc, allContextFlags, parent, particle);
  151. } else {
  152. traverseLocal(particle, elmDecl, schemaDoc, grammar, allContextFlags, parent);
  153. // If it's an empty particle, return null.
  154. if (particle.fType == XSParticleDecl.PARTICLE_EMPTY)
  155. particle = null;
  156. }
  157. return particle;
  158. }
  159. /**
  160. * Traverse a locally declared element (or an element reference).
  161. *
  162. * This is the real traversal method. It's called after we've done with
  163. * all the global declarations.
  164. *
  165. * @param index
  166. */
  167. protected void traverseLocal(XSParticleDecl particle,
  168. Element elmDecl,
  169. XSDocumentInfo schemaDoc,
  170. SchemaGrammar grammar,
  171. int allContextFlags,
  172. XSObject parent) {
  173. // General Attribute Checking
  174. Object[] attrValues = fAttrChecker.checkAttributes(elmDecl, false, schemaDoc);
  175. QName refAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
  176. XInt minAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS];
  177. XInt maxAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS];
  178. XSElementDecl element = null;
  179. if (elmDecl.getAttributeNode(SchemaSymbols.ATT_REF) != null) {
  180. if (refAtt != null) {
  181. element = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, refAtt, elmDecl);
  182. Element child = DOMUtil.getFirstChildElement(elmDecl);
  183. if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
  184. // REVISIT: put this somewhere
  185. traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  186. child = DOMUtil.getNextSiblingElement(child);
  187. }
  188. // Element Declaration Representation OK
  189. // 2 If the item's parent is not <schema>, then all of the following must be true:
  190. // 2.1 One of ref or name must be present, but not both.
  191. // 2.2 If ref is present, then all of <complexType>, <simpleType>, <key>, <keyref>, <unique>, nillable, default, fixed, form, block and type must be absent, i.e. only minOccurs, maxOccurs, id are allowed in addition to ref, along with <annotation>.
  192. if (child != null) {
  193. reportSchemaError("src-element.2.2", new Object[]{refAtt.rawname, DOMUtil.getLocalName(child)}, child);
  194. }
  195. } else {
  196. element = null;
  197. }
  198. } else {
  199. element = traverseNamedElement(elmDecl, attrValues, schemaDoc, grammar, false, parent);
  200. }
  201. particle.fMinOccurs = minAtt.intValue();
  202. particle.fMaxOccurs = maxAtt.intValue();
  203. if (element != null) {
  204. particle.fType = XSParticleDecl.PARTICLE_ELEMENT;
  205. particle.fValue = element;
  206. }
  207. else {
  208. particle.fType = XSParticleDecl.PARTICLE_EMPTY;
  209. }
  210. Long defaultVals = (Long)attrValues[XSAttributeChecker.ATTIDX_FROMDEFAULT];
  211. checkOccurrences(particle, SchemaSymbols.ELT_ELEMENT,
  212. (Element)elmDecl.getParentNode(), allContextFlags,
  213. defaultVals.longValue());
  214. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  215. }
  216. /**
  217. * Traverse a globally declared element.
  218. *
  219. * @param elmDecl
  220. * @param schemaDoc
  221. * @param grammar
  222. * @return the element declaration
  223. */
  224. XSElementDecl traverseGlobal(Element elmDecl,
  225. XSDocumentInfo schemaDoc,
  226. SchemaGrammar grammar) {
  227. // General Attribute Checking
  228. Object[] attrValues = fAttrChecker.checkAttributes(elmDecl, true, schemaDoc);
  229. XSElementDecl element = traverseNamedElement(elmDecl, attrValues, schemaDoc, grammar, true, null);
  230. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  231. return element;
  232. }
  233. /**
  234. * Traverse a globally declared element.
  235. *
  236. * @param elmDecl
  237. * @param attrValues
  238. * @param schemaDoc
  239. * @param grammar
  240. * @param isGlobal
  241. * @return the element declaration
  242. */
  243. XSElementDecl traverseNamedElement(Element elmDecl,
  244. Object[] attrValues,
  245. XSDocumentInfo schemaDoc,
  246. SchemaGrammar grammar,
  247. boolean isGlobal,
  248. XSObject parent) {
  249. Boolean abstractAtt = (Boolean) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT];
  250. XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK];
  251. String defaultAtt = (String) attrValues[XSAttributeChecker.ATTIDX_DEFAULT];
  252. XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL];
  253. String fixedAtt = (String) attrValues[XSAttributeChecker.ATTIDX_FIXED];
  254. XInt formAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FORM];
  255. String nameAtt = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
  256. Boolean nillableAtt = (Boolean) attrValues[XSAttributeChecker.ATTIDX_NILLABLE];
  257. QName subGroupAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_SUBSGROUP];
  258. QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE];
  259. // Step 1: get declaration information
  260. XSElementDecl element = null;
  261. if (fSchemaHandler.fDeclPool !=null) {
  262. element = fSchemaHandler.fDeclPool.getElementDecl();
  263. } else {
  264. element = new XSElementDecl();
  265. }
  266. // get 'name'
  267. if (nameAtt != null)
  268. element.fName = fSymbolTable.addSymbol(nameAtt);
  269. // get 'target namespace'
  270. if (isGlobal) {
  271. element.fTargetNamespace = schemaDoc.fTargetNamespace;
  272. element.setIsGlobal();
  273. }
  274. else {
  275. if (parent instanceof XSComplexTypeDecl)
  276. element.setIsLocal((XSComplexTypeDecl)parent);
  277. if (formAtt != null) {
  278. if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED)
  279. element.fTargetNamespace = schemaDoc.fTargetNamespace;
  280. else
  281. element.fTargetNamespace = null;
  282. } else if (schemaDoc.fAreLocalElementsQualified) {
  283. element.fTargetNamespace = schemaDoc.fTargetNamespace;
  284. } else {
  285. element.fTargetNamespace = null;
  286. }
  287. }
  288. // get 'block', 'final', 'nillable', 'abstract'
  289. element.fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue();
  290. element.fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue();
  291. // discard valid Block/Final 'Default' values that are invalid for Block/Final
  292. element.fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION | XSConstants.DERIVATION_SUBSTITUTION);
  293. element.fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
  294. if (nillableAtt.booleanValue())
  295. element.setIsNillable();
  296. if (abstractAtt != null && abstractAtt.booleanValue())
  297. element.setIsAbstract();
  298. // get 'value constraint'
  299. if (fixedAtt != null) {
  300. element.fDefault = new ValidatedInfo();
  301. element.fDefault.normalizedValue = fixedAtt;
  302. element.setConstraintType(XSConstants.VC_FIXED);
  303. } else if (defaultAtt != null) {
  304. element.fDefault = new ValidatedInfo();
  305. element.fDefault.normalizedValue = defaultAtt;
  306. element.setConstraintType(XSConstants.VC_DEFAULT);
  307. } else {
  308. element.setConstraintType(XSConstants.VC_NONE);
  309. }
  310. // get 'substitutionGroup affiliation'
  311. if (subGroupAtt != null) {
  312. element.fSubGroup = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, subGroupAtt, elmDecl);
  313. }
  314. // get 'annotation'
  315. Element child = DOMUtil.getFirstChildElement(elmDecl);
  316. XSAnnotationImpl annotation = null;
  317. if(child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
  318. annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  319. child = DOMUtil.getNextSiblingElement(child);
  320. }
  321. element.fAnnotation = annotation;
  322. // get 'type definition'
  323. XSTypeDefinition elementType = null;
  324. boolean haveAnonType = false;
  325. // Handle Anonymous type if there is one
  326. if (child != null) {
  327. String childName = DOMUtil.getLocalName(child);
  328. if (childName.equals(SchemaSymbols.ELT_COMPLEXTYPE)) {
  329. elementType = fSchemaHandler.fComplexTypeTraverser.traverseLocal(child, schemaDoc, grammar);
  330. haveAnonType = true;
  331. child = DOMUtil.getNextSiblingElement(child);
  332. }
  333. else if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) {
  334. elementType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar);
  335. haveAnonType = true;
  336. child = DOMUtil.getNextSiblingElement(child);
  337. }
  338. }
  339. // Handler type attribute
  340. if (elementType == null && typeAtt != null) {
  341. elementType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, elmDecl);
  342. }
  343. // Get it from the substitutionGroup declaration
  344. if (elementType == null && element.fSubGroup != null) {
  345. elementType = element.fSubGroup.fType;
  346. }
  347. if (elementType == null) {
  348. elementType = SchemaGrammar.fAnyType;
  349. }
  350. element.fType = elementType;
  351. // get 'identity constraint'
  352. // see if there's something here; it had better be key, keyref or unique.
  353. if (child != null) {
  354. String childName = DOMUtil.getLocalName(child);
  355. while (child != null &&
  356. (childName.equals(SchemaSymbols.ELT_KEY) ||
  357. childName.equals(SchemaSymbols.ELT_KEYREF) ||
  358. childName.equals(SchemaSymbols.ELT_UNIQUE))) {
  359. if (childName.equals(SchemaSymbols.ELT_KEY) ||
  360. childName.equals(SchemaSymbols.ELT_UNIQUE)) {
  361. // need to set <key>/<unique> to hidden before traversing it,
  362. // because it has global scope
  363. DOMUtil.setHidden(child);
  364. fSchemaHandler.fUniqueOrKeyTraverser.traverse(child, element, schemaDoc, grammar);
  365. if(DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME).length() != 0 ) {
  366. fSchemaHandler.checkForDuplicateNames(
  367. (schemaDoc.fTargetNamespace == null) ? ","+DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME)
  368. : schemaDoc.fTargetNamespace+","+ DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME),
  369. fSchemaHandler.getIDRegistry(),
  370. child, schemaDoc);
  371. }
  372. } else if (childName.equals(SchemaSymbols.ELT_KEYREF)) {
  373. fSchemaHandler.storeKeyRef(child, schemaDoc, element);
  374. }
  375. child = DOMUtil.getNextSiblingElement(child);
  376. if (child != null) {
  377. childName = DOMUtil.getLocalName(child);
  378. }
  379. }
  380. }
  381. // Step 2: register the element decl to the grammar
  382. if (isGlobal && nameAtt != null)
  383. grammar.addGlobalElementDecl(element);
  384. // Step 3: check against schema for schemas
  385. // required attributes
  386. if (nameAtt == null) {
  387. if (isGlobal)
  388. reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_ELEMENT, SchemaSymbols.ATT_NAME}, elmDecl);
  389. else
  390. reportSchemaError("src-element.2.1", null, elmDecl);
  391. nameAtt = NO_NAME;
  392. }
  393. // element
  394. if (child != null) {
  395. reportSchemaError("s4s-elt-must-match.1", new Object[]{nameAtt, "(annotation?, (simpleType | complexType)?, (unique | key | keyref)*))", DOMUtil.getLocalName(child)}, child);
  396. }
  397. // Step 4: check 3.3.3 constraints
  398. // src-element
  399. // 1 default and fixed must not both be present.
  400. if (defaultAtt != null && fixedAtt != null) {
  401. reportSchemaError("src-element.1", new Object[]{nameAtt}, elmDecl);
  402. }
  403. // 2 If the item's parent is not <schema>, then all of the following must be true:
  404. // 2.1 One of ref or name must be present, but not both.
  405. // This is checked in XSAttributeChecker
  406. // 2.2 If ref is present, then all of <complexType>, <simpleType>, <key>, <keyref>, <unique>, nillable, default, fixed, form, block and type must be absent, i.e. only minOccurs, maxOccurs, id are allowed in addition to ref, along with <annotation>.
  407. // Attributes are checked in XSAttributeChecker, elements are checked in "traverse" method
  408. // 3 type and either <simpleType> or <complexType> are mutually exclusive.
  409. if (haveAnonType && (typeAtt != null)) {
  410. reportSchemaError("src-element.3", new Object[]{nameAtt}, elmDecl);
  411. }
  412. // Step 5: check 3.3.6 constraints
  413. // check for NOTATION type
  414. checkNotationType(nameAtt, elementType, elmDecl);
  415. // e-props-correct
  416. // 2 If there is a {value constraint}, the canonical lexical representation of its value must be valid with respect to the {type definition} as defined in Element Default Valid (Immediate) (3.3.6).
  417. if (element.fDefault != null) {
  418. fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
  419. if (XSConstraints.ElementDefaultValidImmediate(element.fType, element.fDefault.normalizedValue, fValidationState, element.fDefault) == null) {
  420. reportSchemaError ("e-props-correct.2", new Object[]{nameAtt, element.fDefault.normalizedValue}, elmDecl);
  421. element.setConstraintType(XSConstants.VC_NONE);
  422. }
  423. }
  424. // 4 If there is an {substitution group affiliation}, the {type definition} of the element declaration must be validly derived from the {type definition} of the {substitution group affiliation}, given the value of the {substitution group exclusions} of the {substitution group affiliation}, as defined in Type Derivation OK (Complex) (3.4.6) (if the {type definition} is complex) or as defined in Type Derivation OK (Simple) (3.14.6) (if the {type definition} is simple).
  425. if (element.fSubGroup != null) {
  426. if (!XSConstraints.checkTypeDerivationOk(element.fType, element.fSubGroup.fType, element.fSubGroup.fFinal)) {
  427. reportSchemaError ("e-props-correct.4", new Object[]{nameAtt, subGroupAtt.prefix+":"+subGroupAtt.localpart}, elmDecl);
  428. }
  429. }
  430. // 5 If the {type definition} or {type definition}'s {content type} is or is derived from ID then there must not be a {value constraint}.
  431. if (element.fDefault != null) {
  432. if ((elementType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE &&
  433. ((XSSimpleType)elementType).isIDType()) ||
  434. (elementType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE &&
  435. ((XSComplexTypeDecl)elementType).containsTypeID())) {
  436. reportSchemaError ("e-props-correct.5", new Object[]{element.fName}, elmDecl);
  437. }
  438. }
  439. // Element without a name. Return null.
  440. if (element.fName == null)
  441. return null;
  442. return element;
  443. }
  444. void reset(SymbolTable symbolTable) {
  445. super.reset(symbolTable);
  446. fDeferTraversingLocalElements = true;
  447. } // reset()
  448. }