1 28 29 package com.caucho.xpath.expr; 30 31 import com.caucho.xml.NodeListImpl; 32 import com.caucho.xml.XmlUtil; 33 import com.caucho.xpath.Expr; 34 import com.caucho.xpath.ExprEnvironment; 35 import com.caucho.xpath.XPathException; 36 import com.caucho.xpath.pattern.*; 37 38 import org.w3c.dom.Node ; 39 40 public class NodeSetExpr extends Expr { 41 private AbstractPattern _pattern; 42 43 NodeSetExpr(AbstractPattern pattern) 44 { 45 _pattern = pattern; 46 } 47 48 51 public static Expr create(AbstractPattern pattern) 52 { 53 if (pattern instanceof NodeTypePattern && 54 pattern.getParent() instanceof FromSelf && 55 pattern.toString().equals(".")) 56 return new ObjectExpr(SELF, "."); 57 else if (pattern instanceof FromContext && 58 ((FromContext) pattern).getCount() == 0 && 59 pattern.getParent() == null) 60 return new ObjectExpr(SELF, "."); 61 else if (pattern instanceof NodePattern && 62 pattern.getParent() instanceof FromAttributes && 63 pattern.getParent().getParent() instanceof FromContext && 64 ((FromContext) pattern.getParent().getParent()).getCount() == 0) 65 return new ObjectExpr(ATTRIBUTE, ((NodePattern) pattern).getNodeName()); 66 else 67 return new NodeSetExpr(pattern); 68 } 69 70 73 public AbstractPattern getPattern() 74 { 75 return _pattern; 76 } 77 78 81 public boolean isNodeSet() 82 { 83 return true; 84 } 85 86 92 public double evalNumber(Node node, ExprEnvironment env) 93 throws XPathException 94 { 95 Node value = _pattern.findAny(node, env); 96 97 if (value == null) 98 return Double.NaN; 99 100 String string = XmlUtil.textValue(value); 101 102 return stringToNumber(string); 103 } 104 105 111 public boolean evalBoolean(Node node, ExprEnvironment env) 112 throws XPathException 113 { 114 return _pattern.findAny(node, env) != null; 115 } 116 117 126 public String evalString(Node node, ExprEnvironment env) 127 throws XPathException 128 { 129 Node value = _pattern.findAny(node, env); 130 131 if (value == null) 132 return ""; 133 else 134 return XmlUtil.textValue(value); 135 } 136 137 145 public Object evalObject(Node node, ExprEnvironment env) 146 throws XPathException 147 { 148 NodeListImpl list = new NodeListImpl(); 149 150 NodeIterator iter = _pattern.select(node, env); 151 Node value = null; 152 while ((value = iter.nextNode()) != null) 153 list.add(value); 154 155 return list; 156 } 157 158 166 public NodeIterator evalNodeSet(Node node, ExprEnvironment env) 167 throws XPathException 168 { 169 return _pattern.select(node, env); 170 } 171 172 175 protected AbstractPattern toNodeList() 176 { 177 return _pattern; 178 } 179 180 public boolean equals(Object b) 181 { 182 if (! (b instanceof NodeSetExpr)) 183 return false; 184 185 return _pattern.equals(((NodeSetExpr) b)._pattern); 186 } 187 188 public String toString() 189 { 190 return _pattern.toString(); 191 } 192 } 193 | Popular Tags |