1 16 19 package com.sun.org.apache.xpath.internal.axes; 20 21 import com.sun.org.apache.xml.internal.dtm.Axis; 22 import com.sun.org.apache.xml.internal.dtm.DTM; 23 import com.sun.org.apache.xml.internal.dtm.DTMAxisTraverser; 24 import com.sun.org.apache.xml.internal.dtm.DTMIterator; 25 import com.sun.org.apache.xpath.internal.XPathContext; 26 import com.sun.org.apache.xpath.internal.compiler.Compiler; 27 import com.sun.org.apache.xpath.internal.objects.XObject; 28 import com.sun.org.apache.xpath.internal.patterns.NodeTest; 29 import com.sun.org.apache.xpath.internal.patterns.StepPattern; 30 31 39 public class MatchPatternIterator extends LocPathIterator 40 { 41 42 43 protected StepPattern m_pattern; 44 45 46 protected int m_superAxis = -1; 47 48 49 protected DTMAxisTraverser m_traverser; 50 51 52 private static final boolean DEBUG = false; 53 54 56 70 MatchPatternIterator(Compiler compiler, int opPos, int analysis) 71 throws javax.xml.transform.TransformerException 72 { 73 74 super(compiler, opPos, analysis, false); 75 76 int firstStepPos = compiler.getFirstChildPos(opPos); 77 78 m_pattern = WalkerFactory.loadSteps(this, compiler, firstStepPos, 0); 79 80 boolean fromRoot = false; 81 boolean walkBack = false; 82 boolean walkDescendants = false; 83 boolean walkAttributes = false; 84 85 if (0 != (analysis & (WalkerFactory.BIT_ROOT | 86 WalkerFactory.BIT_ANY_DESCENDANT_FROM_ROOT))) 87 fromRoot = true; 88 89 if (0 != (analysis 90 & (WalkerFactory.BIT_ANCESTOR 91 | WalkerFactory.BIT_ANCESTOR_OR_SELF 92 | WalkerFactory.BIT_PRECEDING 93 | WalkerFactory.BIT_PRECEDING_SIBLING 94 | WalkerFactory.BIT_FOLLOWING 95 | WalkerFactory.BIT_FOLLOWING_SIBLING 96 | WalkerFactory.BIT_PARENT | WalkerFactory.BIT_FILTER))) 97 walkBack = true; 98 99 if (0 != (analysis 100 & (WalkerFactory.BIT_DESCENDANT_OR_SELF 101 | WalkerFactory.BIT_DESCENDANT 102 | WalkerFactory.BIT_CHILD))) 103 walkDescendants = true; 104 105 if (0 != (analysis 106 & (WalkerFactory.BIT_ATTRIBUTE | WalkerFactory.BIT_NAMESPACE))) 107 walkAttributes = true; 108 109 if(false || DEBUG) 110 { 111 System.out.print("analysis: "+Integer.toBinaryString(analysis)); 112 System.out.println(", "+WalkerFactory.getAnalysisString(analysis)); 113 } 114 115 if(fromRoot || walkBack) 116 { 117 if(walkAttributes) 118 { 119 m_superAxis = Axis.ALL; 120 } 121 else 122 { 123 m_superAxis = Axis.DESCENDANTSFROMROOT; 124 } 125 } 126 else if(walkDescendants) 127 { 128 if(walkAttributes) 129 { 130 m_superAxis = Axis.ALLFROMNODE; 131 } 132 else 133 { 134 m_superAxis = Axis.DESCENDANTORSELF; 135 } 136 } 137 else 138 { 139 m_superAxis = Axis.ALL; 140 } 141 if(false || DEBUG) 142 { 143 System.out.println("axis: "+Axis.names[m_superAxis]); 144 } 145 146 } 147 148 149 156 public void setRoot(int context, Object environment) 157 { 158 super.setRoot(context, environment); 159 m_traverser = m_cdtm.getAxisTraverser(m_superAxis); 160 } 161 162 169 public void detach() 170 { 171 if(m_allowDetach) 172 { 173 m_traverser = null; 174 175 super.detach(); 177 } 178 } 179 180 184 protected int getNextNode() 185 { 186 m_lastFetched = (DTM.NULL == m_lastFetched) 187 ? m_traverser.first(m_context) 188 : m_traverser.next(m_context, m_lastFetched); 189 return m_lastFetched; 190 } 191 192 199 public int nextNode() 200 { 201 if(m_foundLast) 202 return DTM.NULL; 203 204 int next; 205 206 com.sun.org.apache.xpath.internal.VariableStack vars; 207 int savedStart; 208 if (-1 != m_stackFrame) 209 { 210 vars = m_execContext.getVarStack(); 211 212 savedStart = vars.getStackFrame(); 214 215 vars.setStackFrame(m_stackFrame); 216 } 217 else 218 { 219 vars = null; 221 savedStart = 0; 222 } 223 224 try 225 { 226 if(DEBUG) 227 System.out.println("m_pattern"+m_pattern.toString()); 228 229 do 230 { 231 next = getNextNode(); 232 233 if (DTM.NULL != next) 234 { 235 if(DTMIterator.FILTER_ACCEPT == acceptNode(next, m_execContext)) 236 break; 237 else 238 continue; 239 } 240 else 241 break; 242 } 243 while (next != DTM.NULL); 244 245 if (DTM.NULL != next) 246 { 247 if(DEBUG) 248 { 249 System.out.println("next: "+next); 250 System.out.println("name: "+m_cdtm.getNodeName(next)); 251 } 252 incrementCurrentPos(); 253 254 return next; 255 } 256 else 257 { 258 m_foundLast = true; 259 260 return DTM.NULL; 261 } 262 } 263 finally 264 { 265 if (-1 != m_stackFrame) 266 { 267 vars.setStackFrame(savedStart); 269 } 270 } 271 272 } 273 274 283 public short acceptNode(int n, XPathContext xctxt) 284 { 285 286 try 287 { 288 xctxt.pushCurrentNode(n); 289 xctxt.pushIteratorRoot(m_context); 290 if(DEBUG) 291 { 292 System.out.println("traverser: "+m_traverser); 293 System.out.print("node: "+n); 294 System.out.println(", "+m_cdtm.getNodeName(n)); 295 System.out.println("pattern: "+m_pattern.toString()); 297 m_pattern.debugWhatToShow(m_pattern.getWhatToShow()); 298 } 299 300 XObject score = m_pattern.execute(xctxt); 301 302 if(DEBUG) 303 { 304 System.out.println("score: "+score); 306 System.out.println("skip: "+(score == NodeTest.SCORE_NONE)); 307 } 308 309 return (score == NodeTest.SCORE_NONE) ? DTMIterator.FILTER_SKIP 311 : DTMIterator.FILTER_ACCEPT; 312 } 313 catch (javax.xml.transform.TransformerException se) 314 { 315 316 throw new RuntimeException (se.getMessage()); 318 } 319 finally 320 { 321 xctxt.popCurrentNode(); 322 xctxt.popIteratorRoot(); 323 } 324 325 } 326 327 } 328 | Popular Tags |