1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 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.XSTypeDefinition;
  60. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  61. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  62. import com.sun.org.apache.xerces.internal.xni.QName;
  63. import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
  64. /**
  65. * Schema identity constraint selector.
  66. *
  67. * @author Andy Clark, IBM
  68. * @version $Id: Selector.java,v 1.16 2003/11/15 22:03:22 neilg Exp $
  69. */
  70. public class Selector {
  71. //
  72. // Data
  73. //
  74. /** XPath. */
  75. protected Selector.XPath fXPath;
  76. /** Identity constraint. */
  77. protected IdentityConstraint fIdentityConstraint;
  78. // the Identity constraint we're the matcher for. Only
  79. // used for selectors!
  80. protected IdentityConstraint fIDConstraint;
  81. //
  82. // Constructors
  83. //
  84. /** Constructs a selector. */
  85. public Selector(Selector.XPath xpath,
  86. IdentityConstraint identityConstraint) {
  87. fXPath = xpath;
  88. fIdentityConstraint = identityConstraint;
  89. } // <init>(Selector.XPath,IdentityConstraint)
  90. //
  91. // Public methods
  92. //
  93. /** Returns the selector XPath. */
  94. public com.sun.org.apache.xerces.internal.impl.xpath.XPath getXPath() {
  95. return fXPath;
  96. } // getXPath():com.sun.org.apache.xerces.internal.v1.schema.identity.XPath
  97. /** Returns the identity constraint. */
  98. public IdentityConstraint getIDConstraint() {
  99. return fIdentityConstraint;
  100. } // getIDConstraint():IdentityConstraint
  101. // factory method
  102. /** Creates a selector matcher.
  103. * @param activator The activator for this selector's fields.
  104. * @param initialDepth The depth in the document at which this matcher began its life;
  105. * used in correctly handling recursive elements.
  106. */
  107. public XPathMatcher createMatcher(FieldActivator activator, int initialDepth) {
  108. return new Selector.Matcher(fXPath, activator, initialDepth);
  109. } // createMatcher(FieldActivator):XPathMatcher
  110. //
  111. // Object methods
  112. //
  113. /** Returns a string representation of this object. */
  114. public String toString() {
  115. return fXPath.toString();
  116. } // toString():String
  117. //
  118. // Classes
  119. //
  120. /**
  121. * Schema identity constraint selector XPath expression.
  122. *
  123. * @author Andy Clark, IBM
  124. * @version $Id: Selector.java,v 1.16 2003/11/15 22:03:22 neilg Exp $
  125. */
  126. public static class XPath
  127. extends com.sun.org.apache.xerces.internal.impl.xpath.XPath {
  128. //
  129. // Constructors
  130. //
  131. /** Constructs a selector XPath expression. */
  132. public XPath(String xpath, SymbolTable symbolTable,
  133. NamespaceContext context) throws XPathException {
  134. super(normalize(xpath), symbolTable, context);
  135. // verify that an attribute is not selected
  136. for (int i=0;i<fLocationPaths.length;i++) {
  137. com.sun.org.apache.xerces.internal.impl.xpath.XPath.Axis axis =
  138. fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis;
  139. if (axis.type == XPath.Axis.ATTRIBUTE) {
  140. throw new XPathException("c-selector-xpath");
  141. }
  142. }
  143. } // <init>(String,SymbolTable,NamespacesScope)
  144. private static String normalize(String xpath) {
  145. // NOTE: We have to prefix the selector XPath with "./" in
  146. // order to handle selectors such as "." that select
  147. // the element container because the fields could be
  148. // relative to that element. -Ac
  149. // Unless xpath starts with a descendant node -Achille Fokoue
  150. // ... or a '.' or a '/' - NG
  151. // And we also need to prefix exprs to the right of | with ./ - NG
  152. StringBuffer modifiedXPath = new StringBuffer(xpath.length()+5);
  153. int unionIndex = -1;
  154. do {
  155. if(!(xpath.trim().startsWith("/") ||xpath.trim().startsWith("."))) {
  156. modifiedXPath.append("./");
  157. }
  158. unionIndex = xpath.indexOf('|');
  159. if(unionIndex == -1) {
  160. modifiedXPath.append(xpath);
  161. break;
  162. }
  163. modifiedXPath.append(xpath.substring(0,unionIndex+1));
  164. xpath = xpath.substring(unionIndex+1, xpath.length());
  165. } while(true);
  166. return modifiedXPath.toString();
  167. }
  168. } // class Selector.XPath
  169. /**
  170. * Selector matcher.
  171. *
  172. * @author Andy Clark, IBM
  173. */
  174. public class Matcher
  175. extends XPathMatcher {
  176. //
  177. // Data
  178. //
  179. /** Field activator. */
  180. protected FieldActivator fFieldActivator;
  181. /** Initial depth in the document at which this matcher was created. */
  182. protected int fInitialDepth;
  183. /** Element depth. */
  184. protected int fElementDepth;
  185. /** Depth at match. */
  186. protected int fMatchedDepth;
  187. //
  188. // Constructors
  189. //
  190. /** Constructs a selector matcher. */
  191. public Matcher(Selector.XPath xpath, FieldActivator activator,
  192. int initialDepth) {
  193. super(xpath);
  194. fFieldActivator = activator;
  195. fInitialDepth = initialDepth;
  196. } // <init>(Selector.XPath,FieldActivator)
  197. //
  198. // XMLDocumentFragmentHandler methods
  199. //
  200. public void startDocumentFragment(){
  201. super.startDocumentFragment();
  202. fElementDepth = 0;
  203. fMatchedDepth = -1;
  204. } // startDocumentFragment()
  205. /**
  206. * The start of an element. If the document specifies the start element
  207. * by using an empty tag, then the startElement method will immediately
  208. * be followed by the endElement method, with no intervening methods.
  209. *
  210. * @param element The name of the element.
  211. * @param attributes The element attributes.
  212. * @param elementDecl: The element declaration
  213. *
  214. */
  215. public void startElement(QName element, XMLAttributes attributes) {
  216. super.startElement(element, attributes);
  217. fElementDepth++;
  218. // activate the fields, if selector is matched
  219. //int matched = isMatched();
  220. if (isMatched()) {
  221. /* (fMatchedDepth == -1 && ((matched & MATCHED) == MATCHED)) ||
  222. ((matched & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)) { */
  223. fMatchedDepth = fElementDepth;
  224. fFieldActivator.startValueScopeFor(fIdentityConstraint, fInitialDepth);
  225. int count = fIdentityConstraint.getFieldCount();
  226. for (int i = 0; i < count; i++) {
  227. Field field = fIdentityConstraint.getFieldAt(i);
  228. XPathMatcher matcher = fFieldActivator.activateField(field, fInitialDepth);
  229. matcher.startElement(element, attributes);
  230. }
  231. }
  232. } // startElement(QName,XMLAttrList,int)
  233. public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object actualValue) {
  234. super.endElement(element, type, nillable, actualValue);
  235. if (fElementDepth-- == fMatchedDepth) {
  236. fMatchedDepth = -1;
  237. fFieldActivator.endValueScopeFor(fIdentityConstraint, fInitialDepth);
  238. }
  239. }
  240. /** Returns the identity constraint. */
  241. public IdentityConstraint getIdentityConstraint() {
  242. return fIdentityConstraint;
  243. } // getIdentityConstraint():IdentityConstraint
  244. /** get the initial depth at which this selector matched. */
  245. public int getInitialDepth() {
  246. return fInitialDepth;
  247. } // getInitialDepth(): int
  248. } // class Matcher
  249. } // class Selector