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.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.XSAttributeGroupDecl;
  62. import com.sun.org.apache.xerces.internal.util.DOMUtil;
  63. import com.sun.org.apache.xerces.internal.util.XMLSymbols;
  64. import com.sun.org.apache.xerces.internal.xni.QName;
  65. import org.w3c.dom.Element;
  66. /**
  67. * The attribute group definition schema component traverser.
  68. *
  69. * <attributeGroup
  70. * id = ID
  71. * name = NCName
  72. * ref = QName
  73. * {any attributes with non-schema namespace . . .}>
  74. * Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  75. * </attributeGroup>
  76. *
  77. * @author Rahul Srivastava, Sun Microsystems Inc.
  78. * @author Sandy Gao, IBM
  79. *
  80. * @version $Id: XSDAttributeGroupTraverser.java,v 1.11 2003/06/23 16:35:22 neilg Exp $
  81. */
  82. class XSDAttributeGroupTraverser extends XSDAbstractTraverser {
  83. XSDAttributeGroupTraverser (XSDHandler handler,
  84. XSAttributeChecker gAttrCheck) {
  85. super(handler, gAttrCheck);
  86. }
  87. XSAttributeGroupDecl traverseLocal(Element elmNode,
  88. XSDocumentInfo schemaDoc,
  89. SchemaGrammar grammar) {
  90. // General Attribute Checking for elmNode declared locally
  91. Object[] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc);
  92. // get attribute
  93. QName refAttr = (QName) attrValues[XSAttributeChecker.ATTIDX_REF];
  94. XSAttributeGroupDecl attrGrp = null;
  95. // ref should be here.
  96. if (refAttr == null) {
  97. reportSchemaError("s4s-att-must-appear", new Object[]{"attributeGroup (local)", "ref"}, elmNode);
  98. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  99. return null;
  100. }
  101. // get global decl
  102. attrGrp = (XSAttributeGroupDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ATTRIBUTEGROUP_TYPE, refAttr, elmNode);
  103. // no children are allowed here except annotation, which is optional.
  104. Element child = DOMUtil.getFirstChildElement(elmNode);
  105. if (child != null) {
  106. String childName = DOMUtil.getLocalName(child);
  107. if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
  108. traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  109. child = DOMUtil.getNextSiblingElement(child);
  110. }
  111. if (child != null) {
  112. Object[] args = new Object [] {refAttr.rawname, "(annotation?)", DOMUtil.getLocalName(child)};
  113. reportSchemaError("s4s-elt-must-match.1", args, child);
  114. }
  115. } // if
  116. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  117. return attrGrp;
  118. } // traverseLocal
  119. XSAttributeGroupDecl traverseGlobal(Element elmNode,
  120. XSDocumentInfo schemaDoc,
  121. SchemaGrammar grammar) {
  122. XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
  123. // General Attribute Checking for elmNode declared globally
  124. Object[] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc);
  125. String nameAttr = (String) attrValues[XSAttributeChecker.ATTIDX_NAME];
  126. // global declaration must have a name
  127. if (nameAttr == null) {
  128. reportSchemaError("s4s-att-must-appear", new Object[]{"attributeGroup (global)", "name"}, elmNode);
  129. nameAttr = "no name";
  130. }
  131. attrGrp.fName = nameAttr;
  132. attrGrp.fTargetNamespace = schemaDoc.fTargetNamespace;
  133. // check the content
  134. Element child = DOMUtil.getFirstChildElement(elmNode);
  135. XSAnnotationImpl annotation = null;
  136. if (child!=null) {
  137. String childName = DOMUtil.getLocalName(child);
  138. if (childName.equals(SchemaSymbols.ELT_ANNOTATION)) {
  139. annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
  140. child = DOMUtil.getNextSiblingElement(child);
  141. }
  142. }
  143. // Traverse the attribute and attribute group elements and fill in the
  144. // attributeGroup structure
  145. Element nextNode = traverseAttrsAndAttrGrps(child, attrGrp, schemaDoc, grammar, null);
  146. if (nextNode!=null) {
  147. // An invalid element was found...
  148. Object[] args = new Object [] {nameAttr, "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))", DOMUtil.getLocalName(nextNode)};
  149. reportSchemaError("s4s-elt-must-match.1", args, nextNode);
  150. }
  151. // Remove prohibited attributes from the set
  152. attrGrp.removeProhibitedAttrs();
  153. // check for restricted redefine:
  154. XSAttributeGroupDecl redefinedAttrGrp = (XSAttributeGroupDecl)fSchemaHandler.getGrpOrAttrGrpRedefinedByRestriction(
  155. XSDHandler.ATTRIBUTEGROUP_TYPE,
  156. new QName(XMLSymbols.EMPTY_STRING, nameAttr, nameAttr, schemaDoc.fTargetNamespace),
  157. schemaDoc, elmNode);
  158. if(redefinedAttrGrp != null) {
  159. Object[] errArgs = attrGrp.validRestrictionOf(nameAttr, redefinedAttrGrp);
  160. if (errArgs != null) {
  161. reportSchemaError((String)errArgs[errArgs.length-1], errArgs, child);
  162. reportSchemaError("src-redefine.7.2.2", new Object [] {nameAttr, errArgs[errArgs.length-1]}, child);
  163. }
  164. }
  165. attrGrp.fAnnotation = annotation;
  166. // make an entry in global declarations.
  167. grammar.addGlobalAttributeGroupDecl(attrGrp);
  168. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  169. return attrGrp;
  170. } // traverseGlobal
  171. } // XSDAttributeGroupTraverser