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.traversers;
  58. import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  59. import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
  60. import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
  61. import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
  62. import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
  63. import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeDecl;
  64. import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
  65. import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeUseImpl;
  66. import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl;
  67. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  68. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  69. import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
  70. import com.sun.org.apache.xerces.internal.util.DOMUtil;
  71. import com.sun.org.apache.xerces.internal.util.XMLSymbols;
  72. import com.sun.org.apache.xerces.internal.xni.QName;
  73. import org.w3c.dom.Element;
  74. /**
  75. * The attribute declaration schema component traverser.
  76. *
  77. * <attribute
  78. * default = string
  79. * fixed = string
  80. * form = (qualified | unqualified)
  81. * id = ID
  82. * name = NCName
  83. * ref = QName
  84. * type = QName
  85. * use = (optional | prohibited | required) : optional
  86. * {any attributes with non-schema namespace . . .}>
  87. * Content: (annotation?, (simpleType?))
  88. * </attribute>
  89. *
  90. * @author Sandy Gao, IBM
  91. * @author Neeraj Bajaj, Sun Microsystems, inc.
  92. * @version $Id: XSDAttributeTraverser.java,v 1.27 2003/11/11 20:15:00 sandygao Exp $
  93. */
  94. class XSDAttributeTraverser extends XSDAbstractTraverser {
  95. public XSDAttributeTraverser (XSDHandler handler,
  96. XSAttributeChecker gAttrCheck) {
  97. super(handler, gAttrCheck);
  98. }
  99. protected XSAttributeUseImpl traverseLocal(Element attrDecl,
  100. XSDocumentInfo schemaDoc,
  101. SchemaGrammar grammar,
  102. XSComplexTypeDecl enclosingCT) {
  103. // General Attribute Checking
  104. Object[] attrValues = fAttrChecker.checkAttributes(attrDecl, false, schemaDoc);
  105. String defaultAtt = (String) attrValues[XSAttributeChecker.ATTIDX_DEFAULT];
  106. String fixedAtt = (String) attrValues[XSAttributeChecker.ATTIDX_FIXED];
  107. String nameAtt = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
  108. QName refAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
  109. XInt useAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_USE];
  110. // get 'attribute declaration'
  111. XSAttributeDecl attribute = null;
  112. if (attrDecl.getAttributeNode(SchemaSymbols.ATT_REF) != null) {
  113. if (refAtt != null) {
  114. attribute = (XSAttributeDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ATTRIBUTE_TYPE, refAtt, attrDecl);
  115. Element child = DOMUtil.getFirstChildElement(attrDecl);
  116. if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
  117. // REVISIT: put this somewhere
  118. traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  119. child = DOMUtil.getNextSiblingElement(child);
  120. }
  121. if (child != null) {
  122. reportSchemaError("src-attribute.3.2", new Object[]{refAtt.rawname}, child);
  123. }
  124. // for error reporting
  125. nameAtt = refAtt.localpart;
  126. } else {
  127. attribute = null;
  128. }
  129. } else {
  130. attribute = traverseNamedAttr(attrDecl, attrValues, schemaDoc, grammar, false, enclosingCT);
  131. }
  132. // get 'value constraint'
  133. short consType = XSConstants.VC_NONE;
  134. if (defaultAtt != null) {
  135. consType = XSConstants.VC_DEFAULT;
  136. } else if (fixedAtt != null) {
  137. consType = XSConstants.VC_FIXED;
  138. defaultAtt = fixedAtt;
  139. fixedAtt = null;
  140. }
  141. XSAttributeUseImpl attrUse = null;
  142. if (attribute != null) {
  143. if (fSchemaHandler.fDeclPool !=null) {
  144. attrUse = fSchemaHandler.fDeclPool.getAttributeUse();
  145. } else {
  146. attrUse = new XSAttributeUseImpl();
  147. }
  148. attrUse.fAttrDecl = attribute;
  149. attrUse.fUse = useAtt.shortValue();
  150. attrUse.fConstraintType = consType;
  151. if (defaultAtt != null) {
  152. attrUse.fDefault = new ValidatedInfo();
  153. attrUse.fDefault.normalizedValue = defaultAtt;
  154. }
  155. }
  156. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  157. //src-attribute
  158. // 1 default and fixed must not both be present.
  159. if (defaultAtt != null && fixedAtt != null) {
  160. reportSchemaError("src-attribute.1", new Object[]{nameAtt}, attrDecl);
  161. }
  162. // 2 If default and use are both present, use must have the actual value optional.
  163. if (consType == XSConstants.VC_DEFAULT &&
  164. useAtt != null && useAtt.intValue() != SchemaSymbols.USE_OPTIONAL) {
  165. reportSchemaError("src-attribute.2", new Object[]{nameAtt}, attrDecl);
  166. }
  167. // a-props-correct
  168. if (defaultAtt != null && attrUse != null) {
  169. // 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 String Valid (3.14.4).
  170. fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
  171. try {
  172. checkDefaultValid(attrUse);
  173. }
  174. catch (InvalidDatatypeValueException ide) {
  175. reportSchemaError (ide.getKey(), ide.getArgs(), attrDecl);
  176. reportSchemaError ("a-props-correct.2", new Object[]{nameAtt, defaultAtt}, attrDecl);
  177. }
  178. // 3 If the {type definition} is or is derived from ID then there must not be a {value constraint}.
  179. if (((XSSimpleType)attribute.getTypeDefinition()).isIDType() ) {
  180. reportSchemaError ("a-props-correct.3", new Object[]{nameAtt}, attrDecl);
  181. }
  182. // check 3.5.6 constraint
  183. // Attribute Use Correct
  184. // 2 If the {attribute declaration} has a fixed {value constraint}, then if the attribute use itself has a {value constraint}, it must also be fixed and its value must match that of the {attribute declaration}'s {value constraint}.
  185. if (attrUse.fAttrDecl.getConstraintType() == XSConstants.VC_FIXED &&
  186. attrUse.fConstraintType != XSConstants.VC_NONE) {
  187. if (attrUse.fConstraintType != XSConstants.VC_FIXED ||
  188. !attrUse.fAttrDecl.getValInfo().actualValue.equals(attrUse.fDefault.actualValue)) {
  189. reportSchemaError ("au-props-correct.2", new Object[]{nameAtt, attrUse.fAttrDecl.getValInfo().stringValue()}, attrDecl);
  190. }
  191. }
  192. }
  193. return attrUse;
  194. }
  195. protected XSAttributeDecl traverseGlobal(Element attrDecl,
  196. XSDocumentInfo schemaDoc,
  197. SchemaGrammar grammar) {
  198. // General Attribute Checking
  199. Object[] attrValues = fAttrChecker.checkAttributes(attrDecl, true, schemaDoc);
  200. XSAttributeDecl attribute = traverseNamedAttr(attrDecl, attrValues, schemaDoc, grammar, true, null);
  201. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  202. return attribute;
  203. }
  204. /**
  205. * Traverse a globally declared attribute.
  206. *
  207. * @param attrDecl
  208. * @param attrValues
  209. * @param schemaDoc
  210. * @param grammar
  211. * @param isGlobal
  212. * @return the attribute declaration index
  213. */
  214. XSAttributeDecl traverseNamedAttr(Element attrDecl,
  215. Object[] attrValues,
  216. XSDocumentInfo schemaDoc,
  217. SchemaGrammar grammar,
  218. boolean isGlobal,
  219. XSComplexTypeDecl enclosingCT) {
  220. String defaultAtt = (String) attrValues[XSAttributeChecker.ATTIDX_DEFAULT];
  221. String fixedAtt = (String) attrValues[XSAttributeChecker.ATTIDX_FIXED];
  222. XInt formAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FORM];
  223. String nameAtt = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
  224. QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE];
  225. // Step 1: get declaration information
  226. XSAttributeDecl attribute = null;
  227. if (fSchemaHandler.fDeclPool !=null) {
  228. attribute = fSchemaHandler.fDeclPool.getAttributeDecl();
  229. } else {
  230. attribute = new XSAttributeDecl();
  231. }
  232. // get 'name'
  233. if (nameAtt != null)
  234. nameAtt = fSymbolTable.addSymbol(nameAtt);
  235. // get 'target namespace'
  236. String tnsAtt = null;
  237. XSComplexTypeDecl enclCT = null;
  238. short scope = XSAttributeDecl.SCOPE_ABSENT;
  239. if (isGlobal) {
  240. tnsAtt = schemaDoc.fTargetNamespace;
  241. scope = XSAttributeDecl.SCOPE_GLOBAL;
  242. }
  243. else {
  244. if (enclosingCT != null) {
  245. enclCT = enclosingCT;
  246. scope = XSAttributeDecl.SCOPE_LOCAL;
  247. }
  248. if (formAtt != null) {
  249. if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED)
  250. tnsAtt = schemaDoc.fTargetNamespace;
  251. } else if (schemaDoc.fAreLocalAttributesQualified) {
  252. tnsAtt = schemaDoc.fTargetNamespace;
  253. }
  254. }
  255. // get 'value constraint'
  256. // for local named attribute, value constraint is absent
  257. ValidatedInfo attDefault = null;
  258. short constraintType = XSConstants.VC_NONE;
  259. if (isGlobal) {
  260. if (fixedAtt != null) {
  261. attDefault = new ValidatedInfo();
  262. attDefault.normalizedValue = fixedAtt;
  263. constraintType = XSConstants.VC_FIXED;
  264. } else if (defaultAtt != null) {
  265. attDefault = new ValidatedInfo();
  266. attDefault.normalizedValue = defaultAtt;
  267. constraintType = XSConstants.VC_DEFAULT;
  268. }
  269. }
  270. // get 'annotation'
  271. Element child = DOMUtil.getFirstChildElement(attrDecl);
  272. XSAnnotationImpl annotation = null;
  273. if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
  274. annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  275. child = DOMUtil.getNextSiblingElement(child);
  276. }
  277. // get 'type definition'
  278. XSSimpleType attrType = null;
  279. boolean haveAnonType = false;
  280. // Handle Anonymous type if there is one
  281. if (child != null) {
  282. String childName = DOMUtil.getLocalName(child);
  283. if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) {
  284. attrType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar);
  285. haveAnonType = true;
  286. child = DOMUtil.getNextSiblingElement(child);
  287. }
  288. }
  289. // Handler type attribute
  290. if (attrType == null && typeAtt != null) {
  291. XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, attrDecl);
  292. if (type != null && type.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE)
  293. attrType = (XSSimpleType)type;
  294. else
  295. reportSchemaError("src-resolve", new Object[]{typeAtt.rawname, "simpleType definition"}, attrDecl);
  296. }
  297. if (attrType == null) {
  298. attrType = SchemaGrammar.fAnySimpleType;
  299. }
  300. attribute.setValues(nameAtt, tnsAtt, attrType,
  301. constraintType, scope, attDefault, enclCT, annotation);
  302. // Step 2: register attribute decl to the grammar
  303. if (isGlobal && nameAtt != null)
  304. grammar.addGlobalAttributeDecl(attribute);
  305. // Step 3: check against schema for schemas
  306. // required attributes
  307. if (nameAtt == null) {
  308. if (isGlobal)
  309. reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_ATTRIBUTE, SchemaSymbols.ATT_NAME}, attrDecl);
  310. else
  311. reportSchemaError("src-attribute.3.1", null, attrDecl);
  312. nameAtt = NO_NAME;
  313. }
  314. // element
  315. if (child != null) {
  316. reportSchemaError("s4s-elt-must-match.1", new Object[]{nameAtt, "(annotation?, (simpleType?))", DOMUtil.getLocalName(child)}, child);
  317. }
  318. // Step 4: check 3.2.3 constraints
  319. // src-attribute
  320. // 1 default and fixed must not both be present.
  321. if (defaultAtt != null && fixedAtt != null) {
  322. reportSchemaError("src-attribute.1", new Object[]{nameAtt}, attrDecl);
  323. }
  324. // 2 If default and use are both present, use must have the actual value optional.
  325. // This is checked in "traverse" method
  326. // 3 If the item's parent is not <schema>, then all of the following must be true:
  327. // 3.1 One of ref or name must be present, but not both.
  328. // This is checked in XSAttributeChecker
  329. // 3.2 If ref is present, then all of <simpleType>, form and type must be absent.
  330. // Attributes are checked in XSAttributeChecker, elements are checked in "traverse" method
  331. // 4 type and <simpleType> must not both be present.
  332. if (haveAnonType && (typeAtt != null)) {
  333. reportSchemaError( "src-attribute.4", new Object[]{nameAtt}, attrDecl);
  334. }
  335. // Step 5: check 3.2.6 constraints
  336. // check for NOTATION type
  337. checkNotationType(nameAtt, attrType, attrDecl);
  338. // a-props-correct
  339. // 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 String Valid (3.14.4).
  340. if (attDefault != null) {
  341. fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
  342. try {
  343. checkDefaultValid(attribute);
  344. }
  345. catch (InvalidDatatypeValueException ide) {
  346. reportSchemaError (ide.getKey(), ide.getArgs(), attrDecl);
  347. reportSchemaError ("a-props-correct.2", new Object[]{nameAtt, attDefault.normalizedValue}, attrDecl);
  348. }
  349. }
  350. // 3 If the {type definition} is or is derived from ID then there must not be a {value constraint}.
  351. if (attDefault != null) {
  352. if (attrType.isIDType() ) {
  353. reportSchemaError ("a-props-correct.3", new Object[]{nameAtt}, attrDecl);
  354. }
  355. }
  356. // no-xmlns
  357. // The {name} of an attribute declaration must not match xmlns.
  358. if (nameAtt != null && nameAtt.equals(XMLSymbols.PREFIX_XMLNS)) {
  359. reportSchemaError("no-xmlns", null, attrDecl);
  360. }
  361. // no-xsi
  362. // The {target namespace} of an attribute declaration, whether local or top-level, must not match http://www.w3.org/2001/XMLSchema-instance (unless it is one of the four built-in declarations given in the next section).
  363. if (tnsAtt != null && tnsAtt.equals(SchemaSymbols.URI_XSI)) {
  364. reportSchemaError("no-xsi", new Object[]{SchemaSymbols.URI_XSI}, attrDecl);
  365. }
  366. // Attribute without a name. Return null.
  367. if (attribute.getName() == null)
  368. return null;
  369. return attribute;
  370. }
  371. // throws an error if the constraint value is invalid for the given type
  372. void checkDefaultValid(XSAttributeDecl attribute) throws InvalidDatatypeValueException {
  373. // validate the original lexical rep, and set the actual value
  374. ((XSSimpleType)attribute.getTypeDefinition()).validate(attribute.getValInfo().normalizedValue, fValidationState, attribute.getValInfo());
  375. // validate the canonical lexical rep
  376. ((XSSimpleType)attribute.getTypeDefinition()).validate(attribute.getValInfo().stringValue(), fValidationState, attribute.getValInfo());
  377. }
  378. // throws an error if the constraint value is invalid for the given type
  379. void checkDefaultValid(XSAttributeUseImpl attrUse) throws InvalidDatatypeValueException {
  380. // validate the original lexical rep, and set the actual value
  381. ((XSSimpleType)attrUse.fAttrDecl.getTypeDefinition()).validate(attrUse.fDefault.normalizedValue, fValidationState, attrUse.fDefault);
  382. // validate the canonical lexical rep
  383. ((XSSimpleType)attrUse.fAttrDecl.getTypeDefinition()).validate(attrUse.fDefault.stringValue(), fValidationState, attrUse.fDefault);
  384. }
  385. }