1 package net.sf.saxon.expr; 2 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.value.Value; 5 import net.sf.saxon.event.SequenceOutputter; 6 import net.sf.saxon.om.*; 7 import net.sf.saxon.trans.XPathException; 8 import net.sf.saxon.type.ItemType; 9 import net.sf.saxon.type.Type; 10 11 import java.io.PrintStream ; 12 import java.util.Arrays ; 13 import java.util.Iterator ; 14 15 16 20 21 public abstract class SimpleExpression extends ComputedExpression { 22 23 public static final Expression[] NO_ARGUMENTS = new Expression[0]; 24 25 protected Expression[] arguments = NO_ARGUMENTS; 26 27 30 31 public SimpleExpression() { 32 } 33 34 35 36 40 41 public void setArguments(Expression[] sub) { 42 arguments = sub; 43 for (int i=0; i<sub.length; i++) { 44 adoptChildExpression(sub[i]); 45 } 46 } 47 48 52 53 public Iterator iterateSubExpressions() { 54 return Arrays.asList(arguments).iterator(); 55 } 56 57 61 62 public Expression simplify(StaticContext env) throws XPathException { 63 for (int i = 0; i < arguments.length; i++) { 64 if (arguments[i] != null) { 65 arguments[i] = arguments[i].simplify(env); 66 } 67 } 68 return this; 69 } 70 71 72 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 73 for (int i = 0; i < arguments.length; i++) { 74 if (arguments[i] != null) { 75 arguments[i] = arguments[i].typeCheck(env, contextItemType); 76 } 77 } 78 return this; 79 } 80 81 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 82 for (int i = 0; i < arguments.length; i++) { 83 if (arguments[i] != null) { 84 arguments[i] = arguments[i].optimize(opt, env, contextItemType); 85 } 86 } 87 return this; 88 } 89 90 91 106 107 public Expression promote(PromotionOffer offer) throws XPathException { 108 for (int i = 0; i < arguments.length; i++) { 109 if (arguments[i] != null) { 110 arguments[i] = doPromotion(arguments[i], offer); 111 } 112 } 113 return this; 114 } 115 116 117 122 123 public ItemType getItemType() { 124 return Type.ITEM_TYPE; 125 } 126 127 131 132 public int computeCardinality() { 133 if ((getImplementationMethod() & Expression.EVALUATE_METHOD) == 0) { 134 return StaticProperty.ALLOWS_ONE_OR_MORE; 135 } else { 136 return StaticProperty.ALLOWS_ZERO_OR_ONE; 137 } 138 } 139 140 148 149 public int computeDependencies() { 150 return super.computeDependencies(); 151 } 152 153 167 168 public Item evaluateItem(XPathContext context) throws XPathException { 169 int m = getImplementationMethod(); 170 if ((m & Expression.EVALUATE_METHOD) != 0) { 171 dynamicError("evaluateItem() is not implemented in the subclass " + this.getClass(), context); 172 } else if ((m & Expression.ITERATE_METHOD) != 0) { 173 return iterate(context).next(); 174 } else { 175 Controller controller = context.getController(); 176 XPathContext c2 = context.newMinorContext(); 177 c2.setOrigin(this); 178 SequenceOutputter seq = new SequenceOutputter(1); 179 seq.setPipelineConfiguration(controller.makePipelineConfiguration()); 180 c2.setTemporaryReceiver(seq); 181 process(c2); 182 return seq.getFirstItem(); 183 } 184 return null; 185 } 186 187 200 201 public SequenceIterator iterate(XPathContext context) throws XPathException { 202 int m = getImplementationMethod(); 203 if ((m & Expression.EVALUATE_METHOD) != 0) { 204 Item item = evaluateItem(context); 205 if (item==null) { 206 return EmptyIterator.getInstance(); 207 } else { 208 return SingletonIterator.makeIterator(item); 209 } 210 } else if ((m & Expression.ITERATE_METHOD) != 0) { 211 dynamicError("iterate() is not implemented in the subclass " + this.getClass(), context); 212 213 } else { 214 Controller controller = context.getController(); 215 XPathContext c2 = context.newMinorContext(); 216 c2.setOrigin(this); 217 SequenceOutputter seq = new SequenceOutputter(); 218 seq.setPipelineConfiguration(controller.makePipelineConfiguration()); 219 c2.setTemporaryReceiver(seq); 220 221 process(c2); 222 223 return Value.getIterator(seq.getSequence()); 224 } 225 return null; 226 } 227 228 233 234 public void process(XPathContext context) throws XPathException { 235 int m = getImplementationMethod(); 236 if ((m & Expression.EVALUATE_METHOD) == 0) { 237 SequenceIterator iter = iterate(context); 238 while (true) { 239 Item it = iter.next(); 240 if (it==null) break; 241 context.getReceiver().append(it, locationId, NodeInfo.ALL_NAMESPACES); 242 } 243 } else { 244 Item item = evaluateItem(context); 245 context.getReceiver().append(item, locationId, NodeInfo.ALL_NAMESPACES); 246 } 247 } 248 249 256 257 public void display(int level, NamePool pool, PrintStream out) { 258 out.println(ExpressionTool.indent(level) + getExpressionType()); 259 for (int i = 0; i < arguments.length; i++) { 260 arguments[i].display(level+1, pool, out); 261 } 262 } 263 264 268 269 public String getExpressionType() { 270 return getClass().getName(); 271 } 272 273 } 274 275 | Popular Tags |