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.SchemaSymbols;
  59. import com.sun.org.apache.xerces.internal.util.DOMUtil;
  60. import org.w3c.dom.Element;
  61. import com.sun.org.apache.xerces.internal.impl.xs.identity.*;
  62. import com.sun.org.apache.xerces.internal.impl.xpath.*;
  63. /**
  64. * This class contains code that all three IdentityConstraint
  65. * traversers (the XSDUniqueTraverser, XSDKeyTraverser and
  66. * XSDKeyrefTraverser) rely upon.
  67. *
  68. * @version $Id: XSDAbstractIDConstraintTraverser.java,v 1.8 2003/06/23 16:35:22 neilg Exp $
  69. */
  70. class XSDAbstractIDConstraintTraverser extends XSDAbstractTraverser {
  71. public XSDAbstractIDConstraintTraverser (XSDHandler handler,
  72. XSAttributeChecker gAttrCheck) {
  73. super(handler, gAttrCheck);
  74. }
  75. void traverseIdentityConstraint(IdentityConstraint ic,
  76. Element icElem, XSDocumentInfo schemaDoc, Object [] icElemAttrs) {
  77. // General Attribute Checking will have been done on icElem by caller
  78. // check for <annotation> and get selector
  79. Element sElem = DOMUtil.getFirstChildElement(icElem);
  80. if(sElem == null) {
  81. reportSchemaError("s4s-elt-must-match.2",
  82. new Object[]{"identity constraint", "(annotation?, selector, field+)"},
  83. icElem);
  84. return;
  85. }
  86. // General Attribute Checking on sElem
  87. // first child could be an annotation
  88. if (DOMUtil.getLocalName(sElem).equals(SchemaSymbols.ELT_ANNOTATION)) {
  89. ic.addAnnotation(traverseAnnotationDecl(sElem, icElemAttrs, false, schemaDoc));
  90. sElem = DOMUtil.getNextSiblingElement(sElem);
  91. }
  92. // if no more children report an error
  93. if(sElem == null) {
  94. reportSchemaError("s4s-elt-must-match.2", new Object[]{"identity constraint", "(annotation?, selector, field+)"}, icElem);
  95. return;
  96. }
  97. Object [] attrValues = fAttrChecker.checkAttributes(sElem, false, schemaDoc);
  98. // if more than one annotation report an error
  99. if(!DOMUtil.getLocalName(sElem).equals(SchemaSymbols.ELT_SELECTOR)) {
  100. reportSchemaError("s4s-elt-must-match.1", new Object[]{"identity constraint", "(annotation?, selector, field+)", SchemaSymbols.ELT_SELECTOR}, sElem);
  101. }
  102. // and make sure <selector>'s content is fine:
  103. Element selChild = DOMUtil.getFirstChildElement(sElem);
  104. if (selChild !=null) {
  105. // traverse annotation if any
  106. if (DOMUtil.getLocalName(selChild).equals(SchemaSymbols.ELT_ANNOTATION)) {
  107. ic.addAnnotation(traverseAnnotationDecl(selChild, attrValues, false, schemaDoc));
  108. selChild = DOMUtil.getNextSiblingElement(selChild);
  109. }
  110. else {
  111. reportSchemaError("s4s-elt-must-match.1", new Object[]{SchemaSymbols.ELT_SELECTOR, "(annotation?)", DOMUtil.getLocalName(selChild)}, selChild);
  112. }
  113. if (selChild != null) {
  114. reportSchemaError("s4s-elt-must-match.1", new Object [] {SchemaSymbols.ELT_SELECTOR, "(annotation?)", DOMUtil.getLocalName(selChild)}, selChild);
  115. }
  116. }
  117. String sText = ((String)attrValues[XSAttributeChecker.ATTIDX_XPATH]);
  118. if(sText == null) {
  119. reportSchemaError("s4s-att-must-appear", new Object [] {SchemaSymbols.ELT_SELECTOR, SchemaSymbols.ATT_XPATH}, sElem);
  120. return;
  121. }
  122. sText = sText.trim();
  123. Selector.XPath sXpath = null;
  124. try {
  125. sXpath = new Selector.XPath(sText, fSymbolTable,
  126. schemaDoc.fNamespaceSupport);
  127. Selector selector = new Selector(sXpath, ic);
  128. ic.setSelector(selector);
  129. }
  130. catch (XPathException e) {
  131. reportSchemaError(e.getKey(), new Object[]{sText}, sElem);
  132. // put back attr values...
  133. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  134. return;
  135. }
  136. // put back attr values...
  137. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  138. // get fields
  139. Element fElem = DOMUtil.getNextSiblingElement(sElem);
  140. if(fElem == null) {
  141. reportSchemaError("s4s-elt-must-match.2", new Object[]{"identity constraint", "(annotation?, selector, field+)"}, sElem);
  142. }
  143. while (fElem != null) {
  144. // General Attribute Checking
  145. attrValues = fAttrChecker.checkAttributes(fElem, false, schemaDoc);
  146. if(!DOMUtil.getLocalName(fElem).equals(SchemaSymbols.ELT_FIELD))
  147. reportSchemaError("s4s-elt-must-match.1", new Object[]{"identity constraint", "(annotation?, selector, field+)", SchemaSymbols.ELT_FIELD}, fElem);
  148. // and make sure <field>'s content is fine:
  149. Element fieldChild = DOMUtil.getFirstChildElement(fElem);
  150. if (fieldChild != null) {
  151. // traverse annotation
  152. if (DOMUtil.getLocalName(fieldChild).equals(SchemaSymbols.ELT_ANNOTATION)) {
  153. ic.addAnnotation(traverseAnnotationDecl(fieldChild, attrValues, false, schemaDoc));
  154. fieldChild = DOMUtil.getNextSiblingElement(fieldChild);
  155. }
  156. }
  157. if (fieldChild != null) {
  158. reportSchemaError("s4s-elt-must-match.1", new Object [] {SchemaSymbols.ELT_FIELD, "(annotation?)", DOMUtil.getLocalName(fieldChild)}, fieldChild);
  159. }
  160. String fText = ((String)attrValues[XSAttributeChecker.ATTIDX_XPATH]);
  161. if(fText == null) {
  162. reportSchemaError("s4s-att-must-appear", new Object [] {SchemaSymbols.ELT_FIELD, SchemaSymbols.ATT_XPATH}, fElem);
  163. return;
  164. }
  165. fText = fText.trim();
  166. try {
  167. Field.XPath fXpath = new Field.XPath(fText, fSymbolTable,
  168. schemaDoc.fNamespaceSupport);
  169. Field field = new Field(fXpath, ic);
  170. ic.addField(field);
  171. }
  172. catch (XPathException e) {
  173. reportSchemaError(e.getKey(), new Object[]{fText}, fElem);
  174. // put back attr values...
  175. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  176. return;
  177. }
  178. fElem = DOMUtil.getNextSiblingElement(fElem);
  179. // put back attr values...
  180. fAttrChecker.returnAttrArray(attrValues, schemaDoc);
  181. }
  182. } // traverseIdentityConstraint(IdentityConstraint,Element, XSDocumentInfo)
  183. } // XSDAbstractIDConstraintTraverser