1 16 19 package org.apache.xpath.patterns; 20 21 import org.apache.xpath.Expression; 22 import org.apache.xpath.ExpressionOwner; 23 import org.apache.xpath.XPathContext; 24 import org.apache.xpath.XPathVisitor; 25 import org.apache.xpath.objects.XObject; 26 27 32 public class UnionPattern extends Expression 33 { 34 35 37 private StepPattern[] m_patterns; 38 39 42 public void fixupVariables(java.util.Vector vars, int globalsSize) 43 { 44 for (int i = 0; i < m_patterns.length; i++) 45 { 46 m_patterns[i].fixupVariables(vars, globalsSize); 47 } 48 } 49 50 51 57 public boolean canTraverseOutsideSubtree() 58 { 59 if(null != m_patterns) 60 { 61 int n = m_patterns.length; 62 for (int i = 0; i < n; i++) 63 { 64 if(m_patterns[i].canTraverseOutsideSubtree()) 65 return true; 66 } 67 } 68 return false; 69 } 70 71 77 public void setPatterns(StepPattern[] patterns) 78 { 79 m_patterns = patterns; 80 if(null != patterns) 81 { 82 for(int i = 0; i < patterns.length; i++) 83 { 84 patterns[i].exprSetParent(this); 85 } 86 } 87 88 } 89 90 96 public StepPattern[] getPatterns() 97 { 98 return m_patterns; 99 } 100 101 114 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 115 { 116 117 XObject bestScore = null; 118 int n = m_patterns.length; 119 120 for (int i = 0; i < n; i++) 121 { 122 XObject score = m_patterns[i].execute(xctxt); 123 124 if (score != NodeTest.SCORE_NONE) 125 { 126 if (null == bestScore) 127 bestScore = score; 128 else if (score.num() > bestScore.num()) 129 bestScore = score; 130 } 131 } 132 133 if (null == bestScore) 134 { 135 bestScore = NodeTest.SCORE_NONE; 136 } 137 138 return bestScore; 139 } 140 141 class UnionPathPartOwner implements ExpressionOwner 142 { 143 int m_index; 144 145 UnionPathPartOwner(int index) 146 { 147 m_index = index; 148 } 149 150 153 public Expression getExpression() 154 { 155 return m_patterns[m_index]; 156 } 157 158 159 162 public void setExpression(Expression exp) 163 { 164 exp.exprSetParent(UnionPattern.this); 165 m_patterns[m_index] = (StepPattern)exp; 166 } 167 } 168 169 172 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 173 { 174 visitor.visitUnionPattern(owner, this); 175 if(null != m_patterns) 176 { 177 int n = m_patterns.length; 178 for(int i = 0; i < n; i++) 179 { 180 m_patterns[i].callVisitors(new UnionPathPartOwner(i), visitor); 181 } 182 } 183 } 184 185 188 public boolean deepEquals(Expression expr) 189 { 190 if(!isSameClass(expr)) 191 return false; 192 193 UnionPattern up = (UnionPattern)expr; 194 195 if(null != m_patterns) 196 { 197 int n = m_patterns.length; 198 if((null == up.m_patterns) || (up.m_patterns.length != n)) 199 return false; 200 201 for(int i = 0; i < n; i++) 202 { 203 if(!m_patterns[i].deepEquals(up.m_patterns[i])) 204 return false; 205 } 206 } 207 else if(up.m_patterns != null) 208 return false; 209 210 return true; 211 212 } 213 214 215 } 216 | Popular Tags |