1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.*; 4 import com.icl.saxon.functions.*; 5 import com.icl.saxon.pattern.NodeTest; 6 7 import java.util.*; 8 9 12 13 public final class Step { 14 15 private byte axis; 16 private NodeTest test; 17 18 private Expression[] filters = new Expression[3]; private int numberOfFilters = 0; 20 21 public Step(byte axis, NodeTest nodeTest) { 22 this.axis = axis; 23 this.test = nodeTest; 24 } 25 26 public Step addFilter(Expression exp) { 27 if (numberOfFilters == filters.length) { 28 Expression[] f2 = new Expression[numberOfFilters * 2]; 29 System.arraycopy(filters, 0, f2, 0, numberOfFilters); 30 filters = f2; 31 } 32 filters[numberOfFilters++] = exp; 33 return this; 34 } 35 36 public void setFilters(Expression[] filters, int count) { 37 this.filters = filters; 38 this.numberOfFilters = count; 39 } 40 41 public byte getAxis() { 42 return axis; 43 } 44 45 public NodeTest getNodeTest() { 46 return test; 47 } 48 49 public Expression[] getFilters() { 50 return filters; 51 } 52 53 public int getNumberOfFilters() { 54 return numberOfFilters; 55 } 56 57 61 62 public Step simplify() throws XPathException { 63 64 for (int i=numberOfFilters-1; i>=0; i--) { 65 Expression exp = filters[i].simplify(); 66 filters[i] = exp; 67 68 71 if (exp instanceof Value && !(exp instanceof NumericValue)) { 72 if (((Value)exp).asBoolean()) { if (i==numberOfFilters-1) { 75 numberOfFilters--; 76 } 77 } else { return null; 80 } 81 } 82 83 85 if (exp instanceof Last) { 86 filters[i] = new IsLastExpression(true); 87 } 88 89 } 90 return this; 91 } 92 93 100 101 public NodeEnumeration enumerate(NodeInfo node, Context context) 102 throws XPathException { 103 104 NodeEnumeration enum = node.getEnumeration(axis, test); 105 if (enum.hasMoreElements()) { for (int i=0; i<numberOfFilters; i++) { 107 enum = new FilterEnumerator(enum, filters[i], 108 context, false); 109 } 110 } 111 return enum; 112 113 } 114 115 118 119 public void display(int level) { 120 System.err.println(Expression.indent(level) + "Step " + Axis.axisName[axis] + "::" + test.toString() + 121 (numberOfFilters > 0 ? " [" : "")); 122 for (int f=0; f<numberOfFilters; f++) { 123 filters[f].display(level+1); 124 } 125 } 126 127 128 } 129 130 131 132 | Popular Tags |