1 56 57 package org.jdom.xpath; 58 59 60 import java.util.*; 61 62 import org.jaxen.*; 63 import org.jaxen.jdom.*; 64 import org.jdom.*; 65 66 67 73 class JaxenXPath extends XPath { 75 private static final String CVS_ID = 76 "@(#) $RCSfile: JaxenXPath.java,v $ $Revision: 1.19 $ $Date: 2004/09/03 07:27:39 $ $Name: $"; 77 78 83 private transient JDOMXPath xPath; 84 85 88 private Object currentContext; 89 90 98 public JaxenXPath(String expr) throws JDOMException { 99 setXPath(expr); 100 } 101 102 118 public List selectNodes(Object context) throws JDOMException { 119 try { 120 currentContext = context; 121 122 return xPath.selectNodes(context); 123 } 124 catch (JaxenException ex1) { 125 throw new JDOMException("XPath error while evaluating \"" + 126 xPath.toString() + "\": " + ex1.getMessage(), ex1); 127 } 128 finally { 129 currentContext = null; 130 } 131 } 132 133 149 public Object selectSingleNode(Object context) throws JDOMException { 150 try { 151 currentContext = context; 152 153 return xPath.selectSingleNode(context); 154 } 155 catch (JaxenException ex1) { 156 throw new JDOMException("XPath error while evaluating \"" + 157 xPath.toString() + "\": " + ex1.getMessage(), ex1); 158 } 159 finally { 160 currentContext = null; 161 } 162 } 163 164 178 public String valueOf(Object context) throws JDOMException { 179 try { 180 currentContext = context; 181 182 return xPath.stringValueOf(context); 183 } 184 catch (JaxenException ex1) { 185 throw new JDOMException("XPath error while evaluating \"" + 186 xPath.toString() + "\": " + ex1.getMessage(), ex1); 187 } 188 finally { 189 currentContext = null; 190 } 191 } 192 193 211 public Number numberValueOf(Object context) throws JDOMException { 212 try { 213 currentContext = context; 214 215 return xPath.numberValueOf(context); 216 } 217 catch (JaxenException ex1) { 218 throw new JDOMException("XPath error while evaluating \"" + 219 xPath.toString() + "\": " + ex1.getMessage(), ex1); 220 } 221 finally { 222 currentContext = null; 223 } 224 } 225 226 238 public void setVariable(String name, Object value) 239 throws IllegalArgumentException { 240 Object o = xPath.getVariableContext(); 241 if (o instanceof SimpleVariableContext) { 242 ((SimpleVariableContext)o).setVariableValue(null, name, value); 243 } 244 } 245 246 256 public void addNamespace(Namespace namespace) { 257 try { 258 xPath.addNamespace(namespace.getPrefix(), namespace.getURI()); 259 } 260 catch (JaxenException ex1) { } 261 } 262 263 268 public String getXPath() { 269 return (xPath.toString()); 270 } 271 272 279 private void setXPath(String expr) throws JDOMException { 280 try { 281 xPath = new JDOMXPath(expr); 282 xPath.setNamespaceContext(new NSContext()); 283 } 284 catch (Exception ex1) { 285 throw new JDOMException( 286 "Invalid XPath expression: \"" + expr + "\"", ex1); 287 } 288 } 289 290 public String toString() { 291 return (xPath.toString()); 292 } 293 294 public boolean equals(Object o) { 295 if (o instanceof JaxenXPath) { 296 JaxenXPath x = (JaxenXPath)o; 297 298 return (super.equals(o) && 299 xPath.toString().equals(x.xPath.toString())); 300 } 301 return false; 302 } 303 304 public int hashCode() { 305 return xPath.hashCode(); 306 } 307 308 private class NSContext extends SimpleNamespaceContext { 309 public NSContext() { 310 super(); 311 } 312 313 322 public String translateNamespacePrefixToUri(String prefix) { 323 if ((prefix == null) || (prefix.length() == 0)) { 324 return null; 325 } 326 327 String uri = super.translateNamespacePrefixToUri(prefix); 328 if (uri == null) { 329 Object ctx = currentContext; 330 if (ctx != null) { 331 Element elt = null; 332 333 if (ctx instanceof Element) { 335 elt = (Element)ctx; 336 } else if (ctx instanceof Attribute) { 337 elt = ((Attribute)ctx).getParent(); 338 } else if (ctx instanceof Content) { 339 elt = ((Content) ctx).getParentElement(); 340 } else if (ctx instanceof Document) { 341 elt = ((Document)ctx).getRootElement(); 342 } 343 344 if (elt != null) { 345 Namespace ns = elt.getNamespace(prefix); 346 if (ns != null) { 347 uri = ns.getURI(); 348 } 349 } 350 } 351 } 352 return uri; 353 } 354 } 355 } 356 357 | Popular Tags |