1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.*; 4 import org.w3c.dom.Node ; 5 import org.w3c.dom.NodeList ; 6 7 10 11 public class SingletonNodeSet extends NodeSetValue implements NodeList { 12 13 protected NodeInfo node = null; 14 protected boolean generalUseAllowed = true; 15 16 20 21 public void allowGeneralUse() { 22 generalUseAllowed = true; 23 } 24 25 28 29 public boolean isGeneralUseAllowed() { 30 return generalUseAllowed; 31 } 32 33 36 37 public SingletonNodeSet() { 38 node = null; 39 } 40 41 44 45 public SingletonNodeSet(NodeInfo node) { 46 this.node = node; 47 } 48 49 52 53 public Expression simplify() { 54 if (node==null) { 55 return new EmptyNodeSet(); 56 } else { 57 return this; 58 } 59 } 60 61 65 66 public Value evaluate(Context context) { 67 return this; 68 } 69 70 75 76 public NodeSetValue evaluateAsNodeSet(Context context) { 77 return this; 78 } 79 80 86 87 public void setSorted(boolean isSorted) {} 88 89 94 95 public boolean isSorted() { 96 return true; 97 } 98 99 104 105 public String asString() { 106 if (node==null) { 107 return ""; 108 } else { 109 return node.getStringValue(); 110 } 111 } 112 113 117 118 public boolean asBoolean() { 119 return node!=null; 120 } 121 122 126 127 public int getCount() { 128 return (node==null ? 0 : 1); 129 } 130 131 137 138 public NodeSetValue sort() { 139 return this; 140 } 141 142 146 147 public NodeInfo getFirst() { 148 return node; 149 } 150 151 152 155 156 public boolean equals(Value other) throws XPathException { 157 158 if (node==null) { 159 if (other instanceof BooleanValue) { 160 return !other.asBoolean(); 161 } else { 162 return false; 163 } 164 } 165 166 if (other instanceof StringValue || 167 other instanceof FragmentValue || 168 other instanceof TextFragmentValue || 169 other instanceof ObjectValue) { 170 return node.getStringValue().equals(other.asString()); 171 172 } else if (other instanceof NodeSetValue) { 173 174 176 try { 177 String value = node.getStringValue(); 178 NodeEnumeration e2 = ((NodeSetValue)other).enumerate(); 179 while (e2.hasMoreElements()) { 180 if (e2.nextElement().getStringValue().equals(value)) return true; 181 } 182 return false; 183 } catch (XPathException err) { 184 throw new InternalSaxonError(err.getMessage()); 185 } 186 187 } else if (other instanceof NumericValue) { 188 return Value.stringToNumber(node.getStringValue())==other.asNumber(); 189 190 } else if (other instanceof BooleanValue) { 191 return other.asBoolean(); 192 193 } else { 194 throw new InternalSaxonError("Unknown data type in a relational expression"); 195 } 196 } 197 198 201 202 public boolean notEquals(Value other) throws XPathException { 203 204 if (node==null) { 205 if (other instanceof BooleanValue) { 206 return other.asBoolean(); 207 } else { 208 return false; 209 } 210 } 211 212 if (other instanceof StringValue || 213 other instanceof FragmentValue || 214 other instanceof TextFragmentValue || 215 other instanceof ObjectValue) { 216 return !node.getStringValue().equals(other.asString()); 217 218 } else if (other instanceof NodeSetValue) { 219 220 try { 221 String value = node.getStringValue(); 222 223 NodeEnumeration e2 = ((NodeSetValue)other).enumerate(); 224 while (e2.hasMoreElements()) { 225 if (!e2.nextElement().getStringValue().equals(value)) return true; 226 } 227 return false; 228 } catch (XPathException err) { 229 throw new InternalSaxonError(err.getMessage()); 230 } 231 232 } else if (other instanceof NumericValue) { 233 return Value.stringToNumber(node.getStringValue())!=other.asNumber(); 234 235 } else if (other instanceof BooleanValue) { 236 return !other.asBoolean(); 237 238 } else { 239 throw new InternalSaxonError("Unknown data type in a relational expression"); 240 241 } 242 } 243 244 247 248 public NodeEnumeration enumerate() throws XPathException { 249 return new SingletonEnumeration(node); 250 } 251 252 254 257 258 public int getLength() { 259 return getCount(); 260 } 261 262 265 266 public Node item(int index) { 267 if (index==0 && (node instanceof Node )) { 268 return (Node )node; 269 } else { 270 return null; 271 } 272 } 273 274 } 275 276 295 | Popular Tags |