1 package com.icl.saxon.expr; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.Controller; 4 import com.icl.saxon.om.NodeInfo; 5 import com.icl.saxon.om.NodeEnumeration; 6 import com.icl.saxon.output.Outputter; 7 8 import javax.xml.transform.TransformerException; 9 10 14 15 16 public abstract class NodeSetExpression extends Expression { 17 18 26 27 public abstract NodeEnumeration enumerate(Context context, boolean sorted) throws XPathException; 28 29 33 34 public Value evaluate(Context context) throws XPathException { 35 36 39 Expression exp = reduce(Context.ALL_DEPENDENCIES, context); 40 if (exp instanceof NodeSetValue) { 41 return (Value)exp; 42 } else if (exp instanceof NodeSetExpression) { 43 return new NodeSetIntent((NodeSetExpression)exp, context.getController()); 44 } else { 45 Value value = exp.evaluate(context); 46 if (value instanceof NodeSetValue) { 47 return value; 48 } else { 49 throw new XPathException("Value must be a node-set: it is a " + exp.getClass()); 50 } 51 } 52 } 53 54 61 62 public NodeInfo selectFirst(Context context) throws XPathException { 63 NodeEnumeration enum = enumerate(context, false); 64 if (enum.isSorted()) { 65 if (enum.hasMoreElements()) { 66 return enum.nextElement(); 67 } else { 68 return null; 69 } 70 } else { 71 Controller controller = context.getController(); 74 NodeInfo first = null; 75 while (enum.hasMoreElements()) { 76 NodeInfo next = enum.nextElement(); 77 if (first==null || controller.compare(next, first)<0) { 78 first = next; 79 } 80 } 81 return first; 82 } 83 } 84 85 91 92 public String evaluateAsString(Context context) throws XPathException { 93 NodeInfo e = selectFirst(context); 94 if (e==null) return ""; 95 return e.getStringValue(); 96 } 97 98 104 105 public void outputStringValue(Outputter out, Context context) throws TransformerException { 106 NodeInfo first = selectFirst(context); 107 if (first!=null) { 108 first.copyStringValue(out); 109 } 110 } 111 112 118 119 public boolean evaluateAsBoolean(Context context) throws XPathException { 120 return enumerate(context, false).hasMoreElements(); 121 } 122 123 128 129 public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException { 130 return (NodeSetValue)this.evaluate(context); 131 } 132 133 137 138 public int getDataType() { 139 return Value.NODESET; 140 } 141 142 } 143 144 | Popular Tags |