1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.NodeEnumeration; 4 import com.icl.saxon.functions.*; 5 import com.icl.saxon.output.Outputter; 6 import javax.xml.transform.TransformerException ; 7 8 9 13 14 public abstract class Expression { 15 16 protected StaticContext staticContext; 17 18 24 25 public static Expression make(String expression, StaticContext env) throws XPathException { 26 try { 27 Expression exp = (new ExpressionParser()).parse(expression, env).simplify(); 28 exp.staticContext = env; 29 return exp; 30 } catch (XPathException err) { 31 if (env.forwardsCompatibleModeIsEnabled()) { 32 return new ErrorExpression(err); 33 } else { 34 throw err; 35 } 36 } 37 } 38 39 43 44 public Expression simplify() throws XPathException { 45 return this; 46 }; 47 48 51 52 public final void setStaticContext(StaticContext sc) { 53 staticContext = sc; 54 } 55 56 59 60 public final StaticContext getStaticContext() { 61 return staticContext; 62 } 63 64 68 69 public boolean containsReferences() throws XPathException { 70 return (getDependencies() & Context.VARIABLES) != 0; 71 } 72 73 78 79 public abstract Value evaluate(Context context) throws XPathException; 80 81 89 90 public boolean evaluateAsBoolean(Context context) throws XPathException { 91 return evaluate(context).asBoolean(); 92 } 93 94 102 103 public double evaluateAsNumber(Context context) throws XPathException { 104 return evaluate(context).asNumber(); 105 } 106 107 115 116 public String evaluateAsString(Context context) throws XPathException { 117 return evaluate(context).asString(); 118 } 119 120 126 127 public void outputStringValue(Outputter out, Context context) throws TransformerException { 128 out.writeContent(evaluateAsString(context)); 129 } 130 131 139 140 public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException { 141 Value val = evaluate(context); 143 if (val instanceof NodeSetValue) 144 return ((NodeSetValue)val); 145 throw new XPathException("The value is not a node-set"); 146 } 147 148 155 156 public NodeEnumeration enumerate(Context context, boolean sorted) throws XPathException { 157 Value val = evaluate(context); 159 if (val instanceof NodeSetValue) { 160 if (sorted) { 161 ((NodeSetValue)val).sort(); 162 } 163 NodeEnumeration z = ((NodeSetValue)val).enumerate(); 164 return z; 165 } 166 throw new XPathException("The value is not a node-set"); 167 } 168 169 174 175 public abstract int getDataType(); 176 177 182 183 public boolean isContextDocumentNodeSet() { 184 return false; 185 } 186 187 191 192 public boolean usesCurrent() { 193 return (getDependencies() & Context.CURRENT_NODE) != 0; 194 } 195 196 201 202 public abstract int getDependencies(); 203 204 211 212 public abstract Expression reduce(int dependencies, Context context) throws XPathException; 213 214 217 218 public abstract void display(int level); 219 220 223 224 protected static String indent(int level) { 225 String s = ""; 226 for (int i=0; i<level; i++) { 227 s += " "; 228 } 229 return s; 230 } 231 } 232 233 | Popular Tags |