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.xs.SchemaGrammar;
  59. import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
  60. import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl;
  61. import com.sun.org.apache.xerces.internal.impl.xs.XSGroupDecl;
  62. import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl;
  63. import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;
  64. import com.sun.org.apache.xerces.internal.impl.xs.util.XInt;
  65. import com.sun.org.apache.xerces.internal.util.DOMUtil;
  66. import com.sun.org.apache.xerces.internal.util.XMLSymbols;
  67. import com.sun.org.apache.xerces.internal.xni.QName;
  68. import org.w3c.dom.Element;
  69. /**
  70. * The model group schema component traverser.
  71. *
  72. * <group
  73. * name = NCName>
  74. * Content: (annotation?, (all | choice | sequence))
  75. * </group>
  76. *
  77. * @author Rahul Srivastava, Sun Microsystems Inc.
  78. * @author Elena Litani, IBM
  79. * @author Lisa Martin, IBM
  80. * @version $Id: XSDGroupTraverser.java,v 1.19 2004/02/03 17:27:45 sandygao Exp $
  81. */
  82. class XSDGroupTraverser extends XSDAbstractParticleTraverser {
  83. XSDGroupTraverser (XSDHandler handler,
  84. XSAttributeChecker gAttrCheck) {
  85. super(handler, gAttrCheck);
  86. }
  87. XSParticleDecl traverseLocal(Element elmNode,
  88. XSDocumentInfo schemaDoc,
  89. SchemaGrammar grammar) {
  90. // General Attribute Checking for elmNode declared locally
  91. Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false,
  92. schemaDoc);
  93. QName refAttr = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
  94. XInt minAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS];
  95. XInt maxAttr = (XInt) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS];
  96. XSGroupDecl group = null;
  97. // ref should be here.
  98. if (refAttr == null) {
  99. reportSchemaError("s4s-att-must-appear", new Object[]{"group (local)", "ref"}, elmNode);
  100. } else {
  101. // get global decl
  102. // index is a particle index.
  103. group = (XSGroupDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.GROUP_TYPE, refAttr, elmNode);
  104. }
  105. // no children other than "annotation?" are allowed
  106. Element child = DOMUtil.getFirstChildElement(elmNode);
  107. if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
  108. // REVISIT: put this somewhere
  109. traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  110. child = DOMUtil.getNextSiblingElement(child);
  111. }
  112. if (child != null) {
  113. reportSchemaError("s4s-elt-must-match.1", new Object[]{"group (local)", "(annotation?)", DOMUtil.getLocalName(elmNode)}, elmNode);
  114. }
  115. int minOccurs = minAttr.intValue();
  116. int maxOccurs = maxAttr.intValue();
  117. XSParticleDecl particle = null;
  118. // not empty group, not empty particle
  119. if (group != null && group.fModelGroup != null &&
  120. !(minOccurs == 0 && maxOccurs == 0)) {
  121. // create a particle to contain this model group
  122. if (fSchemaHandler.fDeclPool != null) {
  123. particle = fSchemaHandler.fDeclPool.getParticleDecl();
  124. } else {
  125. particle = new XSParticleDecl();
  126. }
  127. particle.fType = XSParticleDecl.PARTICLE_MODELGROUP;
  128. particle.fValue = group.fModelGroup;
  129. particle.fMinOccurs = minOccurs;
  130. particle.fMaxOccurs = maxOccurs;
  131. }
  132. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  133. return particle;
  134. } // traverseLocal
  135. XSGroupDecl traverseGlobal(Element elmNode,
  136. XSDocumentInfo schemaDoc,
  137. SchemaGrammar grammar) {
  138. // General Attribute Checking for elmNode declared globally
  139. Object[] attrValues = fAttrChecker.checkAttributes(elmNode, true,
  140. schemaDoc);
  141. String strNameAttr = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
  142. // must have a name
  143. if (strNameAttr == null) {
  144. reportSchemaError("s4s-att-must-appear", new Object[]{"group (global)", "name"}, elmNode);
  145. }
  146. XSGroupDecl group = null;
  147. XSParticleDecl particle = null;
  148. // must have at least one child
  149. Element l_elmChild = DOMUtil.getFirstChildElement(elmNode);
  150. XSAnnotationImpl annotation = null;
  151. if (l_elmChild == null) {
  152. reportSchemaError("s4s-elt-must-match.2",
  153. new Object[]{"group (global)", "(annotation?, (all | choice | sequence))"},
  154. elmNode);
  155. } else {
  156. // Create the group defi up-front, so it can be passed
  157. // to the traversal methods
  158. group = new XSGroupDecl();
  159. String childName = l_elmChild.getLocalName();
  160. if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
  161. annotation = traverseAnnotationDecl(l_elmChild, attrValues, true, schemaDoc);
  162. l_elmChild = DOMUtil.getNextSiblingElement(l_elmChild);
  163. if (l_elmChild != null)
  164. childName = l_elmChild.getLocalName();
  165. }
  166. if (l_elmChild == null) {
  167. reportSchemaError("s4s-elt-must-match.2",
  168. new Object[]{"group (global)", "(annotation?, (all | choice | sequence))"},
  169. elmNode);
  170. } else if (childName.equals(SchemaSymbols.ELT_ALL)) {
  171. particle = traverseAll(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
  172. } else if (childName.equals(SchemaSymbols.ELT_CHOICE)) {
  173. particle = traverseChoice(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
  174. } else if (childName.equals(SchemaSymbols.ELT_SEQUENCE)) {
  175. particle = traverseSequence(l_elmChild, schemaDoc, grammar, CHILD_OF_GROUP, group);
  176. } else {
  177. reportSchemaError("s4s-elt-must-match.1",
  178. new Object[]{"group (global)", "(annotation?, (all | choice | sequence))", DOMUtil.getLocalName(l_elmChild)},
  179. l_elmChild);
  180. }
  181. if (l_elmChild != null &&
  182. DOMUtil.getNextSiblingElement(l_elmChild) != null) {
  183. reportSchemaError("s4s-elt-must-match.1",
  184. new Object[]{"group (global)", "(annotation?, (all | choice | sequence))",
  185. DOMUtil.getLocalName(DOMUtil.getNextSiblingElement(l_elmChild))},
  186. DOMUtil.getNextSiblingElement(l_elmChild));
  187. }
  188. // add global group declaration to the grammar
  189. if (strNameAttr != null) {
  190. group.fName = strNameAttr;
  191. group.fTargetNamespace = schemaDoc.fTargetNamespace;
  192. if (particle != null)
  193. group.fModelGroup = (XSModelGroupImpl)particle.fValue;
  194. group.fAnnotation = annotation;
  195. grammar.addGlobalGroupDecl(group);
  196. }
  197. else {
  198. // name attribute is not there, don't return this group.
  199. group = null;
  200. }
  201. }
  202. if(group != null) {
  203. // store groups redefined by restriction in the grammar so
  204. // that we can get at them at full-schema-checking time.
  205. Object redefinedGrp = fSchemaHandler.getGrpOrAttrGrpRedefinedByRestriction(XSDHandler.GROUP_TYPE,
  206. new QName(XMLSymbols.EMPTY_STRING, strNameAttr, strNameAttr, schemaDoc.fTargetNamespace),
  207. schemaDoc, elmNode);
  208. if(redefinedGrp != null) {
  209. // store in grammar
  210. grammar.addRedefinedGroupDecl(group, (XSGroupDecl)redefinedGrp,
  211. fSchemaHandler.element2Locator(elmNode));
  212. }
  213. }
  214. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  215. return group;
  216. } // traverseGlobal
  217. }