1 28 29 package com.caucho.xpath.expr; 30 31 import com.caucho.xml.XmlUtil; 32 import com.caucho.xpath.Expr; 33 import com.caucho.xpath.ExprEnvironment; 34 import com.caucho.xpath.XPathException; 35 import com.caucho.xpath.pattern.*; 36 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 40 import java.util.ArrayList ; 41 42 public class ObjectExpr extends Expr { 43 private int _code; 44 private String _name; 45 private Expr _left; 46 private Expr _right; 47 private Expr _third; 48 49 public ObjectExpr(int code, ArrayList args) 50 { 51 _code = code; 52 53 if (args != null && args.size() > 0) 54 _left = (Expr) args.get(0); 55 if (args != null && args.size() > 1) 56 _right = (Expr) args.get(1); 57 if (args != null && args.size() > 2) 58 _third = (Expr) args.get(2); 59 60 if (_right == null || _third == null) 61 throw new NullPointerException (); 62 } 63 64 public ObjectExpr(int code, String name) 65 { 66 _code = code; 67 _name = name; 68 } 69 70 73 public boolean isNodeSet() 74 { 75 return _code == SELF; 76 } 77 78 81 public boolean isString() 82 { 83 return _code == ATTRIBUTE; 84 } 85 86 94 public boolean evalBoolean(Node node, ExprEnvironment env) 95 throws XPathException 96 { 97 switch (_code) { 98 case IF: 99 if (_left.evalBoolean(node, env)) 100 return _right.evalBoolean(node, env); 101 else 102 return _third.evalBoolean(node, env); 103 104 case ATTRIBUTE: 105 if (node instanceof Element ) 106 return ! ((Element ) node).getAttribute(_name).equals(""); 107 else 108 return false; 109 110 case SELF: 111 return true; 112 113 default: 114 return toBoolean(evalObject(node, env)); 115 } 116 } 117 118 124 public double evalNumber(Node node, ExprEnvironment env) 125 throws XPathException 126 { 127 switch (_code) { 128 case IF: 129 if (_left.evalBoolean(node, env)) 130 return _right.evalNumber(node, env); 131 else 132 return _third.evalNumber(node, env); 133 134 case ATTRIBUTE: 135 if (node instanceof Element ) 136 return toDouble(((Element ) node).getAttribute(_name)); 137 else 138 return Double.NaN; 139 140 case SELF: 141 return toDouble(XmlUtil.textValue(node)); 142 143 default: 144 return toDouble(evalObject(node, env)); 145 } 146 } 147 148 156 public String evalString(Node node, ExprEnvironment env) 157 throws XPathException 158 { 159 switch (_code) { 160 case IF: 161 if (_left.evalBoolean(node, env)) 162 return _right.evalString(node, env); 163 else 164 return _third.evalString(node, env); 165 166 case ATTRIBUTE: 167 if (node instanceof Element ) 168 return ((Element ) node).getAttribute(_name); 169 else 170 return ""; 171 172 case SELF: 173 return XmlUtil.textValue(node); 174 175 default: 176 return toString(evalObject(node, env)); 177 } 178 } 179 180 188 public Object evalObject(Node node, ExprEnvironment env) 189 throws XPathException 190 { 191 switch (_code) { 192 case IF: 193 if (_left.evalBoolean(node, env)) 194 return _right.evalObject(node, env); 195 else 196 return _third.evalObject(node, env); 197 198 case SELF: 199 return node; 200 201 case ATTRIBUTE: 202 if (node instanceof Element ) 203 return ((Element ) node).getAttributeNode(_name); 204 else 205 return null; 206 207 default: 208 return null; 209 } 210 } 211 212 218 public NodeIterator evalNodeSet(Node node, ExprEnvironment env) 219 throws XPathException 220 { 221 switch (_code) { 222 case IF: 223 if (_left.evalBoolean(node, env)) 224 return _right.evalNodeSet(node, env); 225 else 226 return _third.evalNodeSet(node, env); 227 228 case SELF: 229 return new SingleNodeIterator(env, node); 230 231 case ATTRIBUTE: 232 if (node instanceof Element ) 233 return new SingleNodeIterator(env, ((Element ) node).getAttributeNode(_name)); 234 else 235 return new SingleNodeIterator(env, null); 236 237 default: 238 return null; 239 } 240 } 241 242 245 protected AbstractPattern toNodeList() 246 { 247 switch (_code) { 248 case SELF: 249 return NodeTypePattern.create(new FromSelf(null), NodeTypePattern.ANY); 250 251 case ATTRIBUTE: 252 return new NodePattern(new FromAttributes(null), 253 _name, Node.ATTRIBUTE_NODE); 254 255 default: 256 return super.toNodeList(); 257 } 258 } 259 260 public String toString() 261 { 262 switch (_code) { 263 case IF: 264 return "if(" + _left + "," + _right + "," + _third + ")"; 265 266 case SELF: 267 return "."; 268 269 case ATTRIBUTE: 270 return "@" + _name; 271 272 default: 273 return super.toString(); 274 } 275 } 276 } 277 | Popular Tags |