1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 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 "Xalan" 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, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.patterns;
  58. import java.util.Vector;
  59. import javax.xml.transform.TransformerException;
  60. import org.apache.xpath.Expression;
  61. import org.apache.xpath.ExpressionOwner;
  62. import org.apache.xpath.XPathContext;
  63. import org.apache.xpath.XPathVisitor;
  64. import org.apache.xpath.objects.XObject;
  65. /**
  66. * <meta name="usage" content="advanced"/>
  67. * This class represents a union pattern, which can have multiple individual
  68. * StepPattern patterns.
  69. */
  70. public class UnionPattern extends Expression
  71. {
  72. /** Array of the contained step patterns to be tested.
  73. * @serial */
  74. private StepPattern[] m_patterns;
  75. /**
  76. * No arguments to process, so this does nothing.
  77. */
  78. public void fixupVariables(java.util.Vector vars, int globalsSize)
  79. {
  80. for (int i = 0; i < m_patterns.length; i++)
  81. {
  82. m_patterns[i].fixupVariables(vars, globalsSize);
  83. }
  84. }
  85. /**
  86. * Tell if this expression or it's subexpressions can traverse outside
  87. * the current subtree.
  88. *
  89. * @return true if traversal outside the context node's subtree can occur.
  90. */
  91. public boolean canTraverseOutsideSubtree()
  92. {
  93. if(null != m_patterns)
  94. {
  95. int n = m_patterns.length;
  96. for (int i = 0; i < n; i++)
  97. {
  98. if(m_patterns[i].canTraverseOutsideSubtree())
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * Set the contained step patterns to be tested.
  106. *
  107. *
  108. * @param patterns the contained step patterns to be tested.
  109. */
  110. public void setPatterns(StepPattern[] patterns)
  111. {
  112. m_patterns = patterns;
  113. if(null != patterns)
  114. {
  115. for(int i = 0; i < patterns.length; i++)
  116. {
  117. patterns[i].exprSetParent(this);
  118. }
  119. }
  120. }
  121. /**
  122. * Get the contained step patterns to be tested.
  123. *
  124. *
  125. * @return an array of the contained step patterns to be tested.
  126. */
  127. public StepPattern[] getPatterns()
  128. {
  129. return m_patterns;
  130. }
  131. /**
  132. * Test a node to see if it matches any of the patterns in the union.
  133. *
  134. * @param xctxt XPath runtime context.
  135. *
  136. * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
  137. * {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
  138. * {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
  139. * {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
  140. * {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
  141. *
  142. * @throws javax.xml.transform.TransformerException
  143. */
  144. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  145. {
  146. XObject bestScore = null;
  147. int n = m_patterns.length;
  148. for (int i = 0; i < n; i++)
  149. {
  150. XObject score = m_patterns[i].execute(xctxt);
  151. if (score != NodeTest.SCORE_NONE)
  152. {
  153. if (null == bestScore)
  154. bestScore = score;
  155. else if (score.num() > bestScore.num())
  156. bestScore = score;
  157. }
  158. }
  159. if (null == bestScore)
  160. {
  161. bestScore = NodeTest.SCORE_NONE;
  162. }
  163. return bestScore;
  164. }
  165. class UnionPathPartOwner implements ExpressionOwner
  166. {
  167. int m_index;
  168. UnionPathPartOwner(int index)
  169. {
  170. m_index = index;
  171. }
  172. /**
  173. * @see ExpressionOwner#getExpression()
  174. */
  175. public Expression getExpression()
  176. {
  177. return m_patterns[m_index];
  178. }
  179. /**
  180. * @see ExpressionOwner#setExpression(Expression)
  181. */
  182. public void setExpression(Expression exp)
  183. {
  184. exp.exprSetParent(UnionPattern.this);
  185. m_patterns[m_index] = (StepPattern)exp;
  186. }
  187. }
  188. /**
  189. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  190. */
  191. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  192. {
  193. visitor.visitUnionPattern(owner, this);
  194. if(null != m_patterns)
  195. {
  196. int n = m_patterns.length;
  197. for(int i = 0; i < n; i++)
  198. {
  199. m_patterns[i].callVisitors(new UnionPathPartOwner(i), visitor);
  200. }
  201. }
  202. }
  203. /**
  204. * @see Expression#deepEquals(Expression)
  205. */
  206. public boolean deepEquals(Expression expr)
  207. {
  208. if(!isSameClass(expr))
  209. return false;
  210. UnionPattern up = (UnionPattern)expr;
  211. if(null != m_patterns)
  212. {
  213. int n = m_patterns.length;
  214. if((null == up.m_patterns) || (up.m_patterns.length != n))
  215. return false;
  216. for(int i = 0; i < n; i++)
  217. {
  218. if(!m_patterns[i].deepEquals(up.m_patterns[i]))
  219. return false;
  220. }
  221. }
  222. else if(up.m_patterns != null)
  223. return false;
  224. return true;
  225. }
  226. }