1 56 57 package org.jdom.xpath; 58 59 60 import java.io.*; 61 import java.lang.reflect.*; 62 import java.util.*; 63 64 import org.jdom.*; 65 66 67 77 public abstract class XPath implements Serializable { 78 79 private static final String CVS_ID = 80 "@(#) $RCSfile: XPath.java,v $ $Revision: 1.16 $ $Date: 2004/11/03 05:17:17 $ $Name: $"; 81 82 89 private final static String XPATH_CLASS_PROPERTY = "org.jdom.xpath.class"; 90 91 94 private final static String DEFAULT_XPATH_CLASS = 95 "org.jdom.xpath.JaxenXPath"; 96 97 102 public final static String JDOM_OBJECT_MODEL_URI = 103 "http://jdom.org/jaxp/xpath/jdom"; 104 105 111 private static Constructor constructor = null; 112 113 121 public static XPath newInstance(String path) throws JDOMException { 122 try { 123 if (constructor == null) { 124 String className; 126 try { 127 className = System.getProperty(XPATH_CLASS_PROPERTY, 128 DEFAULT_XPATH_CLASS); 129 } 130 catch (SecurityException ex1) { 131 className = DEFAULT_XPATH_CLASS; 133 } 134 setXPathClass(Class.forName(className)); 135 } 136 return (XPath)constructor.newInstance(new Object [] { path }); 138 } 139 catch (JDOMException ex1) { 140 throw ex1; 141 } 142 catch (InvocationTargetException ex2) { 143 Throwable t = ex2.getTargetException(); 145 146 throw (t instanceof JDOMException)? (JDOMException)t: 147 new JDOMException(t.toString(), t); 148 } 149 catch (Exception ex3) { 150 throw new JDOMException(ex3.toString(), ex3); 152 } 153 } 154 155 167 public static void setXPathClass(Class aClass) throws JDOMException { 168 if (aClass == null) { 169 throw new IllegalArgumentException ("aClass"); 170 } 171 172 try { 173 if ((XPath.class.isAssignableFrom(aClass)) && 174 (Modifier.isAbstract(aClass.getModifiers()) == false)) { 175 constructor = aClass.getConstructor(new Class [] { String .class }); 177 } 178 else { 179 throw new JDOMException(aClass.getName() + 180 " is not a concrete JDOM XPath implementation"); 181 } 182 } 183 catch (JDOMException ex1) { 184 throw ex1; 185 } 186 catch (Exception ex2) { 187 throw new JDOMException(ex2.toString(), ex2); 189 } 190 } 191 192 208 abstract public List selectNodes(Object context) throws JDOMException; 209 210 226 abstract public Object selectSingleNode(Object context) throws JDOMException; 227 228 242 abstract public String valueOf(Object context) throws JDOMException; 243 244 262 abstract public Number numberValueOf(Object context) throws JDOMException; 263 264 276 abstract public void setVariable(String name, Object value); 277 278 288 abstract public void addNamespace(Namespace namespace); 289 290 305 public void addNamespace(String prefix, String uri) { 306 addNamespace(Namespace.getNamespace(prefix, uri)); 307 } 308 309 314 abstract public String getXPath(); 315 316 317 343 public static List selectNodes(Object context, String path) 344 throws JDOMException { 345 return newInstance(path).selectNodes(context); 346 } 347 348 374 public static Object selectSingleNode(Object context, String path) 375 throws JDOMException { 376 return newInstance(path).selectSingleNode(context); 377 } 378 379 380 384 400 protected final Object writeReplace() throws ObjectStreamException { 401 return new XPathString(this.getXPath()); 402 } 403 404 413 private final static class XPathString implements Serializable { 414 417 private String xPath = null; 418 419 425 public XPathString(String xpath) { 426 super(); 427 428 this.xPath = xpath; 429 } 430 431 441 private Object readResolve() throws ObjectStreamException { 442 try { 443 return XPath.newInstance(this.xPath); 444 } 445 catch (JDOMException ex1) { 446 throw new InvalidObjectException( 447 "Can't create XPath object for expression \"" + 448 this.xPath + "\": " + ex1.toString()); 449 } 450 } 451 } 452 } 453 454 | Popular Tags |