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 java.util.Vector;
  59. import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeFacetException;
  60. import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory;
  61. import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
  62. import com.sun.org.apache.xerces.internal.impl.dv.xs.SchemaDVFactoryImpl;
  63. import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
  64. import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
  65. import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
  66. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  67. import com.sun.org.apache.xerces.internal.xs.XSObjectList;
  68. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  69. import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  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.xni.QName;
  73. import org.w3c.dom.Element;
  74. /**
  75. * The simple type definition schema component traverser.
  76. *
  77. * <simpleType
  78. * final = (#all | (list | union | restriction))
  79. * id = ID
  80. * name = NCName
  81. * {any attributes with non-schema namespace . . .}>
  82. * Content: (annotation?, (restriction | list | union))
  83. * </simpleType>
  84. *
  85. * <restriction
  86. * base = QName
  87. * id = ID
  88. * {any attributes with non-schema namespace . . .}>
  89. * Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*))
  90. * </restriction>
  91. *
  92. * <list
  93. * id = ID
  94. * itemType = QName
  95. * {any attributes with non-schema namespace . . .}>
  96. * Content: (annotation?, (simpleType?))
  97. * </list>
  98. *
  99. * <union
  100. * id = ID
  101. * memberTypes = List of QName
  102. * {any attributes with non-schema namespace . . .}>
  103. * Content: (annotation?, (simpleType*))
  104. * </union>
  105. *
  106. * @author Elena Litani, IBM
  107. * @author Neeraj Bajaj, Sun Microsystems, Inc.
  108. * @author Sandy Gao, IBM
  109. *
  110. * @version $Id: XSDSimpleTypeTraverser.java,v 1.26 2003/11/11 20:15:00 sandygao Exp $
  111. */
  112. class XSDSimpleTypeTraverser extends XSDAbstractTraverser {
  113. // the factory used to query/create simple types
  114. private final SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
  115. // whether the type being parsed is a S4S built-in type.
  116. private boolean fIsBuiltIn = false;
  117. XSDSimpleTypeTraverser (XSDHandler handler,
  118. XSAttributeChecker gAttrCheck) {
  119. super(handler, gAttrCheck);
  120. if (schemaFactory instanceof SchemaDVFactoryImpl) {
  121. ((SchemaDVFactoryImpl)schemaFactory).setDeclPool(handler.fDeclPool);
  122. }
  123. }
  124. //return qualified name of simpleType or empty string if error occured
  125. XSSimpleType traverseGlobal(Element elmNode,
  126. XSDocumentInfo schemaDoc,
  127. SchemaGrammar grammar) {
  128. // General Attribute Checking
  129. Object[] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc);
  130. String nameAtt = (String)attrValues[XSAttributeChecker.ATTIDX_NAME];
  131. XSSimpleType type = traverseSimpleTypeDecl(elmNode, attrValues, schemaDoc, grammar);
  132. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  133. // if it's a global type without a name, return null
  134. if (nameAtt == null) {
  135. reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, SchemaSymbols.ATT_NAME}, elmNode);
  136. type = null;
  137. }
  138. // don't add global components without name to the grammar
  139. if (type != null) {
  140. grammar.addGlobalTypeDecl(type);
  141. }
  142. return type;
  143. }
  144. XSSimpleType traverseLocal(Element elmNode,
  145. XSDocumentInfo schemaDoc,
  146. SchemaGrammar grammar) {
  147. // General Attribute Checking
  148. Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
  149. XSSimpleType type = traverseSimpleTypeDecl (elmNode, attrValues, schemaDoc, grammar);
  150. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  151. return type;
  152. }
  153. private XSSimpleType traverseSimpleTypeDecl(Element simpleTypeDecl,
  154. Object[] attrValues,
  155. XSDocumentInfo schemaDoc,
  156. SchemaGrammar grammar) {
  157. // get name and final values
  158. String name = (String)attrValues[XSAttributeChecker.ATTIDX_NAME];
  159. XInt finalAttr = (XInt)attrValues[XSAttributeChecker.ATTIDX_FINAL];
  160. int finalProperty = finalAttr == null ? schemaDoc.fFinalDefault : finalAttr.intValue();
  161. // annotation?,(list|restriction|union)
  162. Element child = DOMUtil.getFirstChildElement(simpleTypeDecl);
  163. XSAnnotationImpl [] annotations = null;
  164. if (child != null) {
  165. // traverse annotation if any
  166. if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
  167. XSAnnotationImpl annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  168. if (annotation != null)
  169. annotations = new XSAnnotationImpl [] {annotation};
  170. child = DOMUtil.getNextSiblingElement(child);
  171. }
  172. }
  173. // (list|restriction|union)
  174. if (child == null) {
  175. reportSchemaError("s4s-elt-must-match.2", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))"}, simpleTypeDecl);
  176. return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION);
  177. }
  178. // derivation type: restriction/list/union
  179. String varietyProperty = DOMUtil.getLocalName(child);
  180. short refType = XSConstants.DERIVATION_RESTRICTION;
  181. boolean restriction = false, list = false, union = false;
  182. if (varietyProperty.equals(SchemaSymbols.ELT_RESTRICTION)) {
  183. refType = XSConstants.DERIVATION_RESTRICTION;
  184. restriction = true;
  185. }
  186. else if (varietyProperty.equals(SchemaSymbols.ELT_LIST)) {
  187. refType = XSConstants.DERIVATION_LIST;
  188. list = true;
  189. }
  190. else if (varietyProperty.equals(SchemaSymbols.ELT_UNION)) {
  191. refType = XSConstants.DERIVATION_UNION;
  192. union = true;
  193. }
  194. else {
  195. reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", varietyProperty}, simpleTypeDecl);
  196. return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION);
  197. }
  198. // nothing should follow this element
  199. Element nextChild = DOMUtil.getNextSiblingElement(child);
  200. if (nextChild != null) {
  201. reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", DOMUtil.getLocalName(nextChild)}, nextChild);
  202. }
  203. // General Attribute Checking: get base/item/member types
  204. Object[] contentAttrs = fAttrChecker.checkAttributes(child, false, schemaDoc);
  205. QName baseTypeName = (QName)contentAttrs[restriction ?
  206. XSAttributeChecker.ATTIDX_BASE :
  207. XSAttributeChecker.ATTIDX_ITEMTYPE];
  208. Vector memberTypes = (Vector)contentAttrs[XSAttributeChecker.ATTIDX_MEMBERTYPES];
  209. //content = {annotation?,simpleType?...}
  210. Element content = DOMUtil.getFirstChildElement(child);
  211. //check content (annotation?, ...)
  212. if (content != null) {
  213. // traverse annotation if any
  214. if (DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_ANNOTATION)) {
  215. XSAnnotationImpl annotation = traverseAnnotationDecl(content, contentAttrs, false, schemaDoc);
  216. if(annotation != null ) {
  217. if(annotations == null)
  218. annotations = new XSAnnotationImpl [] {annotation};
  219. else {
  220. XSAnnotationImpl [] tempArray = new XSAnnotationImpl[2];
  221. tempArray[0] = annotations[0];
  222. annotations = tempArray;
  223. annotations[1] = annotation;
  224. }
  225. }
  226. content = DOMUtil.getNextSiblingElement(content);
  227. }
  228. }
  229. // get base type from "base" attribute
  230. XSSimpleType baseValidator = null;
  231. if ((restriction || list) && baseTypeName != null) {
  232. baseValidator = findDTValidator(child, name, baseTypeName, refType, schemaDoc);
  233. // if its the built-in type, return null from here
  234. if (baseValidator == null && fIsBuiltIn) {
  235. fIsBuiltIn = false;
  236. return null;
  237. }
  238. }
  239. // get types from "memberTypes" attribute
  240. Vector dTValidators = null;
  241. XSSimpleType dv = null;
  242. XSObjectList dvs;
  243. if (union && memberTypes != null && memberTypes.size() > 0) {
  244. int size = memberTypes.size();
  245. dTValidators = new Vector(size, 2);
  246. // for each qname in the list
  247. for (int i = 0; i < size; i++) {
  248. // get the type decl
  249. dv = findDTValidator(child, name, (QName)memberTypes.elementAt(i),
  250. XSConstants.DERIVATION_UNION, schemaDoc);
  251. if (dv != null) {
  252. // if it's a union, expand it
  253. if (dv.getVariety() == XSSimpleType.VARIETY_UNION) {
  254. dvs = dv.getMemberTypes();
  255. for (int j = 0; j < dvs.getLength(); j++)
  256. dTValidators.addElement(dvs.item(j));
  257. } else {
  258. dTValidators.addElement(dv);
  259. }
  260. }
  261. }
  262. }
  263. // when there is an error finding the base type of a restriction
  264. // we use anySimpleType as the base, then we should skip the facets,
  265. // because anySimpleType doesn't recognize any facet.
  266. boolean skipFacets = false;
  267. // check if there is a child "simpleType"
  268. if (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_SIMPLETYPE)) {
  269. if (restriction || list) {
  270. // it's an error for both "base" and "simpleType" to appear
  271. if (baseTypeName != null) {
  272. reportSchemaError(list ? "src-simple-type.3.a" : "src-simple-type.2.a", null, content);
  273. }
  274. else {
  275. // traver this child to get the base type
  276. baseValidator = traverseLocal(content, schemaDoc, grammar);
  277. }
  278. // get the next element
  279. content = DOMUtil.getNextSiblingElement(content);
  280. }
  281. else if (union) {
  282. if (dTValidators == null) {
  283. dTValidators = new Vector(2, 2);
  284. }
  285. do {
  286. // traver this child to get the member type
  287. dv = traverseLocal(content, schemaDoc, grammar);
  288. if (dv != null) {
  289. // if it's a union, expand it
  290. if (dv.getVariety() == XSSimpleType.VARIETY_UNION) {
  291. dvs = dv.getMemberTypes();
  292. for (int j = 0; j < dvs.getLength(); j++)
  293. dTValidators.addElement(dvs.item(j));
  294. } else {
  295. dTValidators.addElement(dv);
  296. }
  297. }
  298. // get the next element
  299. content = DOMUtil.getNextSiblingElement(content);
  300. } while (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_SIMPLETYPE));
  301. }
  302. }
  303. else if ((restriction || list) && baseTypeName == null) {
  304. // it's an error if neither "base" nor "simpleType" appears
  305. reportSchemaError(list ? "src-simple-type.3.b" : "src-simple-type.2.b", null, child);
  306. // base can't be found, skip the facets.
  307. skipFacets = true;
  308. baseValidator = SchemaGrammar.fAnySimpleType;
  309. }
  310. else if (union && (memberTypes == null || memberTypes.size() == 0)) {
  311. // it's an error if "memberTypes" is empty and no "simpleType" appears
  312. reportSchemaError("src-union-memberTypes-or-simpleTypes", null, child);
  313. dTValidators = new Vector(1);
  314. dTValidators.addElement(SchemaGrammar.fAnySimpleType);
  315. }
  316. // error finding "base" or error traversing "simpleType".
  317. // don't need to report an error, since some error has been reported.
  318. if ((restriction || list) && baseValidator == null) {
  319. baseValidator = SchemaGrammar.fAnySimpleType;
  320. }
  321. // error finding "memberTypes" or error traversing "simpleType".
  322. // don't need to report an error, since some error has been reported.
  323. if (union && (dTValidators == null || dTValidators.size() == 0)) {
  324. dTValidators = new Vector(1);
  325. dTValidators.addElement(SchemaGrammar.fAnySimpleType);
  326. }
  327. // item type of list types can't have list content
  328. if (list && isListDatatype(baseValidator)) {
  329. reportSchemaError("cos-st-restricts.2.1", new Object[]{name, baseValidator.getName()}, child);
  330. }
  331. // create the simple type based on the "base" type
  332. XSSimpleType newDecl = null;
  333. if (restriction) {
  334. newDecl = schemaFactory.createTypeRestriction(name, schemaDoc.fTargetNamespace, (short)finalProperty, baseValidator,
  335. annotations == null? null : new XSObjectListImpl(annotations, annotations.length));
  336. }
  337. else if (list) {
  338. newDecl = schemaFactory.createTypeList(name, schemaDoc.fTargetNamespace, (short)finalProperty, baseValidator,
  339. annotations == null? null : new XSObjectListImpl(annotations, annotations.length));
  340. }
  341. else if (union) {
  342. XSSimpleType[] memberDecls = new XSSimpleType[dTValidators.size()];
  343. for (int i = 0; i < dTValidators.size(); i++)
  344. memberDecls[i] = (XSSimpleType)dTValidators.elementAt(i);
  345. newDecl = schemaFactory.createTypeUnion(name, schemaDoc.fTargetNamespace, (short)finalProperty, memberDecls,
  346. annotations == null? null : new XSObjectListImpl(annotations, annotations.length));
  347. }
  348. // now traverse facets, if it's derived by restriction
  349. if (restriction && content != null) {
  350. FacetInfo fi = traverseFacets(content, baseValidator, schemaDoc);
  351. content = fi.nodeAfterFacets;
  352. if (!skipFacets) {
  353. try {
  354. fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
  355. newDecl.applyFacets(fi.facetdata, fi.fPresentFacets, fi.fFixedFacets, fValidationState);
  356. } catch (InvalidDatatypeFacetException ex) {
  357. reportSchemaError(ex.getKey(), ex.getArgs(), child);
  358. }
  359. }
  360. }
  361. // now element should appear after this point
  362. if (content != null) {
  363. if (restriction) {
  364. reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_RESTRICTION, "(annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*))", DOMUtil.getLocalName(content)}, content);
  365. }
  366. else if (list) {
  367. reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_LIST, "(annotation?, (simpleType?))", DOMUtil.getLocalName(content)}, content);
  368. }
  369. else if (union) {
  370. reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_UNION, "(annotation?, (simpleType*))", DOMUtil.getLocalName(content)}, content);
  371. }
  372. }
  373. fAttrChecker.returnAttrArray(contentAttrs, schemaDoc);
  374. // return the new type
  375. return newDecl;
  376. }
  377. //@param: elm - top element
  378. //@param: baseTypeStr - type (base/itemType/memberTypes)
  379. //@param: baseRefContext: whether the caller is using this type as a base for restriction, union or list
  380. //return XSSimpleType available for the baseTypeStr, null if not found or disallowed.
  381. // also throws an error if the base type won't allow itself to be used in this context.
  382. // REVISIT: can this code be re-used?
  383. private XSSimpleType findDTValidator(Element elm, String refName,
  384. QName baseTypeStr, short baseRefContext,
  385. XSDocumentInfo schemaDoc) {
  386. if (baseTypeStr == null)
  387. return null;
  388. XSTypeDefinition baseType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, baseTypeStr, elm);
  389. if (baseType != null) {
  390. // if it's a complex type, or if its restriction of anySimpleType
  391. if (baseType.getTypeCategory() != XSTypeDefinition.SIMPLE_TYPE ||
  392. (baseType == SchemaGrammar.fAnySimpleType &&
  393. baseRefContext == XSConstants.DERIVATION_RESTRICTION)) {
  394. // if the base type is anySimpleType and the current type is
  395. // a S4S built-in type, return null. (not an error).
  396. if (baseType == SchemaGrammar.fAnySimpleType &&
  397. checkBuiltIn(refName, schemaDoc.fTargetNamespace)) {
  398. return null;
  399. }
  400. reportSchemaError("cos-st-restricts.1.1", new Object[]{baseTypeStr.rawname, refName}, elm);
  401. return SchemaGrammar.fAnySimpleType;
  402. }
  403. if ((baseType.getFinal() & baseRefContext) != 0) {
  404. if (baseRefContext == XSConstants.DERIVATION_RESTRICTION) {
  405. reportSchemaError("st-props-correct.3", new Object[]{refName, baseTypeStr.rawname}, elm);
  406. }
  407. else if (baseRefContext == XSConstants.DERIVATION_LIST) {
  408. reportSchemaError("cos-st-restricts.2.3.1.1", new Object[]{baseTypeStr.rawname, refName}, elm);
  409. }
  410. else if (baseRefContext == XSConstants.DERIVATION_UNION) {
  411. reportSchemaError("cos-st-restricts.3.3.1.1", new Object[]{baseTypeStr.rawname, refName}, elm);
  412. }
  413. }
  414. }
  415. return (XSSimpleType)baseType;
  416. }
  417. // check whethe the type denoted by the name and namespace is a S4S
  418. // built-in type. update fIsBuiltIn at the same time.
  419. private final boolean checkBuiltIn(String name, String namespace) {
  420. if (namespace != SchemaSymbols.URI_SCHEMAFORSCHEMA)
  421. return false;
  422. if (SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(name) != null)
  423. fIsBuiltIn = true;
  424. return fIsBuiltIn;
  425. }
  426. // find if a datatype validator is a list or has list datatype member.
  427. private boolean isListDatatype(XSSimpleType validator) {
  428. if (validator.getVariety() == XSSimpleType.VARIETY_LIST)
  429. return true;
  430. if (validator.getVariety() == XSSimpleType.VARIETY_UNION) {
  431. XSObjectList temp = validator.getMemberTypes();
  432. for (int i = 0; i < temp.getLength(); i++) {
  433. if (((XSSimpleType)temp.item(i)).getVariety() == XSSimpleType.VARIETY_LIST) {
  434. return true;
  435. }
  436. }
  437. }
  438. return false;
  439. }//isListDatatype(XSSimpleTypeDecl):boolean
  440. private XSSimpleType errorType(String name, String namespace, short refType) {
  441. switch (refType) {
  442. case XSConstants.DERIVATION_RESTRICTION:
  443. return schemaFactory.createTypeRestriction(name, namespace, (short)0,
  444. SchemaGrammar.fAnySimpleType, null);
  445. case XSConstants.DERIVATION_LIST:
  446. return schemaFactory.createTypeList(name, namespace, (short)0,
  447. SchemaGrammar.fAnySimpleType, null);
  448. case XSConstants.DERIVATION_UNION:
  449. return schemaFactory.createTypeUnion(name, namespace, (short)0,
  450. new XSSimpleType[]{SchemaGrammar.fAnySimpleType}, null);
  451. }
  452. return null;
  453. }
  454. }//class XSDSimpleTypeTraverser