1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: UnionPattern.java,v 1.13 2004/02/17 04:35:37 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.patterns;
  20. import com.sun.org.apache.xpath.internal.Expression;
  21. import com.sun.org.apache.xpath.internal.ExpressionOwner;
  22. import com.sun.org.apache.xpath.internal.XPathContext;
  23. import com.sun.org.apache.xpath.internal.XPathVisitor;
  24. import com.sun.org.apache.xpath.internal.objects.XObject;
  25. /**
  26. * This class represents a union pattern, which can have multiple individual
  27. * StepPattern patterns.
  28. * @xsl.usage advanced
  29. */
  30. public class UnionPattern extends Expression
  31. {
  32. /** Array of the contained step patterns to be tested.
  33. * @serial */
  34. private StepPattern[] m_patterns;
  35. /**
  36. * No arguments to process, so this does nothing.
  37. */
  38. public void fixupVariables(java.util.Vector vars, int globalsSize)
  39. {
  40. for (int i = 0; i < m_patterns.length; i++)
  41. {
  42. m_patterns[i].fixupVariables(vars, globalsSize);
  43. }
  44. }
  45. /**
  46. * Tell if this expression or it's subexpressions can traverse outside
  47. * the current subtree.
  48. *
  49. * @return true if traversal outside the context node's subtree can occur.
  50. */
  51. public boolean canTraverseOutsideSubtree()
  52. {
  53. if(null != m_patterns)
  54. {
  55. int n = m_patterns.length;
  56. for (int i = 0; i < n; i++)
  57. {
  58. if(m_patterns[i].canTraverseOutsideSubtree())
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. /**
  65. * Set the contained step patterns to be tested.
  66. *
  67. *
  68. * @param patterns the contained step patterns to be tested.
  69. */
  70. public void setPatterns(StepPattern[] patterns)
  71. {
  72. m_patterns = patterns;
  73. if(null != patterns)
  74. {
  75. for(int i = 0; i < patterns.length; i++)
  76. {
  77. patterns[i].exprSetParent(this);
  78. }
  79. }
  80. }
  81. /**
  82. * Get the contained step patterns to be tested.
  83. *
  84. *
  85. * @return an array of the contained step patterns to be tested.
  86. */
  87. public StepPattern[] getPatterns()
  88. {
  89. return m_patterns;
  90. }
  91. /**
  92. * Test a node to see if it matches any of the patterns in the union.
  93. *
  94. * @param xctxt XPath runtime context.
  95. *
  96. * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
  97. * {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
  98. * {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
  99. * {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
  100. * {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
  101. *
  102. * @throws javax.xml.transform.TransformerException
  103. */
  104. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  105. {
  106. XObject bestScore = null;
  107. int n = m_patterns.length;
  108. for (int i = 0; i < n; i++)
  109. {
  110. XObject score = m_patterns[i].execute(xctxt);
  111. if (score != NodeTest.SCORE_NONE)
  112. {
  113. if (null == bestScore)
  114. bestScore = score;
  115. else if (score.num() > bestScore.num())
  116. bestScore = score;
  117. }
  118. }
  119. if (null == bestScore)
  120. {
  121. bestScore = NodeTest.SCORE_NONE;
  122. }
  123. return bestScore;
  124. }
  125. class UnionPathPartOwner implements ExpressionOwner
  126. {
  127. int m_index;
  128. UnionPathPartOwner(int index)
  129. {
  130. m_index = index;
  131. }
  132. /**
  133. * @see ExpressionOwner#getExpression()
  134. */
  135. public Expression getExpression()
  136. {
  137. return m_patterns[m_index];
  138. }
  139. /**
  140. * @see ExpressionOwner#setExpression(Expression)
  141. */
  142. public void setExpression(Expression exp)
  143. {
  144. exp.exprSetParent(UnionPattern.this);
  145. m_patterns[m_index] = (StepPattern)exp;
  146. }
  147. }
  148. /**
  149. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  150. */
  151. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  152. {
  153. visitor.visitUnionPattern(owner, this);
  154. if(null != m_patterns)
  155. {
  156. int n = m_patterns.length;
  157. for(int i = 0; i < n; i++)
  158. {
  159. m_patterns[i].callVisitors(new UnionPathPartOwner(i), visitor);
  160. }
  161. }
  162. }
  163. /**
  164. * @see Expression#deepEquals(Expression)
  165. */
  166. public boolean deepEquals(Expression expr)
  167. {
  168. if(!isSameClass(expr))
  169. return false;
  170. UnionPattern up = (UnionPattern)expr;
  171. if(null != m_patterns)
  172. {
  173. int n = m_patterns.length;
  174. if((null == up.m_patterns) || (up.m_patterns.length != n))
  175. return false;
  176. for(int i = 0; i < n; i++)
  177. {
  178. if(!m_patterns[i].deepEquals(up.m_patterns[i]))
  179. return false;
  180. }
  181. }
  182. else if(up.m_patterns != null)
  183. return false;
  184. return true;
  185. }
  186. }