1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2003 The Apache Software Foundation.
  6. * All rights 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) 1999, 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.identity;
  58. import com.sun.org.apache.xerces.internal.impl.xpath.XPathException;
  59. import com.sun.org.apache.xerces.internal.xs.XSComplexTypeDefinition;
  60. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  61. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  62. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  63. /**
  64. * Schema identity constraint field.
  65. *
  66. * @author Andy Clark, IBM
  67. * @version $Id: Field.java,v 1.17 2004/03/11 16:10:46 mrglavas Exp $
  68. */
  69. public class Field {
  70. //
  71. // Data
  72. //
  73. /** Field XPath. */
  74. protected Field.XPath fXPath;
  75. /** Identity constraint. */
  76. protected IdentityConstraint fIdentityConstraint;
  77. //
  78. // Constructors
  79. //
  80. /** Constructs a field. */
  81. public Field(Field.XPath xpath,
  82. IdentityConstraint identityConstraint) {
  83. fXPath = xpath;
  84. fIdentityConstraint = identityConstraint;
  85. } // <init>(Field.XPath,IdentityConstraint)
  86. //
  87. // Public methods
  88. //
  89. /** Returns the field XPath. */
  90. public com.sun.org.apache.xerces.internal.impl.xpath.XPath getXPath() {
  91. return fXPath;
  92. } // getXPath():com.sun.org.apache.xerces.internal.impl.v1.schema.identity.XPath
  93. /** Returns the identity constraint. */
  94. public IdentityConstraint getIdentityConstraint() {
  95. return fIdentityConstraint;
  96. } // getIdentityConstraint():IdentityConstraint
  97. // factory method
  98. /** Creates a field matcher. */
  99. public XPathMatcher createMatcher(FieldActivator activator, ValueStore store) {
  100. return new Field.Matcher(fXPath, activator, store);
  101. } // createMatcher(ValueStore):XPathMatcher
  102. //
  103. // Object methods
  104. //
  105. /** Returns a string representation of this object. */
  106. public String toString() {
  107. return fXPath.toString();
  108. } // toString():String
  109. //
  110. // Classes
  111. //
  112. /**
  113. * Field XPath.
  114. *
  115. * @author Andy Clark, IBM
  116. */
  117. public static class XPath
  118. extends com.sun.org.apache.xerces.internal.impl.xpath.XPath {
  119. //
  120. // Constructors
  121. //
  122. /** Constructs a field XPath expression. */
  123. public XPath(String xpath,
  124. SymbolTable symbolTable,
  125. NamespaceContext context) throws XPathException {
  126. // NOTE: We have to prefix the field XPath with "./" in
  127. // order to handle selectors such as "@attr" that
  128. // select the attribute because the fields could be
  129. // relative to the selector element. -Ac
  130. // Unless xpath starts with a descendant node -Achille Fokoue
  131. // ... or a / or a . - NG
  132. super(((xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))?
  133. xpath:"./"+xpath),
  134. symbolTable, context);
  135. // verify that only one attribute is selected per branch
  136. for (int i=0;i<fLocationPaths.length;i++) {
  137. for(int j=0; j<fLocationPaths[i].steps.length; j++) {
  138. com.sun.org.apache.xerces.internal.impl.xpath.XPath.Axis axis =
  139. fLocationPaths[i].steps[j].axis;
  140. if (axis.type == XPath.Axis.ATTRIBUTE &&
  141. (j < fLocationPaths[i].steps.length-1)) {
  142. throw new XPathException("c-fields-xpaths");
  143. }
  144. }
  145. }
  146. } // <init>(String,SymbolTable,NamespacesContext)
  147. } // class XPath
  148. /**
  149. * Field matcher.
  150. *
  151. * @author Andy Clark, IBM
  152. */
  153. protected class Matcher
  154. extends XPathMatcher {
  155. //
  156. // Data
  157. //
  158. /** Field activator. */
  159. protected FieldActivator fFieldActivator;
  160. /** Value store for data values. */
  161. protected ValueStore fStore;
  162. //
  163. // Constructors
  164. //
  165. /** Constructs a field matcher. */
  166. public Matcher(Field.XPath xpath, FieldActivator activator, ValueStore store) {
  167. super(xpath);
  168. fFieldActivator = activator;
  169. fStore = store;
  170. } // <init>(Field.XPath,ValueStore)
  171. //
  172. // XPathHandler methods
  173. //
  174. /**
  175. * This method is called when the XPath handler matches the
  176. * XPath expression.
  177. */
  178. protected void matched(Object actualValue, boolean isNil) {
  179. super.matched(actualValue, isNil);
  180. if(isNil && (fIdentityConstraint.getCategory() == IdentityConstraint.IC_KEY)) {
  181. String code = "KeyMatchesNillable";
  182. fStore.reportError(code, new Object[]{fIdentityConstraint.getElementName()});
  183. }
  184. fStore.addValue(Field.this, actualValue,fCurrMatchedType);
  185. // once we've stored the value for this field, we set the mayMatch
  186. // member to false so that, in the same scope, we don't match any more
  187. // values (and throw an error instead).
  188. fFieldActivator.setMayMatch(Field.this, Boolean.FALSE);
  189. } // matched(String)
  190. protected void handleContent(XSTypeDefinition type, boolean nillable, Object actualValue) {
  191. if (type == null ||
  192. type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE &&
  193. ((XSComplexTypeDefinition) type).getContentType()
  194. != XSComplexTypeDefinition.CONTENTTYPE_SIMPLE) {
  195. // the content must be simpleType content
  196. fStore.reportError( "cvc-id.3", new Object[] {
  197. fIdentityConstraint.getName(),
  198. fIdentityConstraint.getElementName()});
  199. }
  200. fCurrMatchedType = type;
  201. fMatchedString = actualValue;
  202. matched(fMatchedString, nillable);
  203. } // handleContent(XSElementDecl, String)
  204. } // class Matcher
  205. } // class Field