|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Abstraction for holding and evaluating XPaths into XML documents.
XPaths can be evaluated multiple times, but implementations are free to synchronized access so that only one thread at a time may be evaluating the path.
Instances should be created by an XPathFactory (i.e. abstract factory
pattern).
Supported API: true
Extendable: false
XPathFactory
Method Summary | |
Object |
evaluate(Node node_to_operate_on)
Evaluates the xpath relative to the specified node and returns the result as the most appropriate object. |
void |
evaluate(Node node_to_operate_on,
XPathCallback callback)
Evaluates the xpath relative to the specified node and invokes the callback method most appropriate for the located object. |
Method Detail |
public void evaluate(Node node_to_operate_on, XPathCallback callback) throws XPathException
You must supply an XPathCallback object to use this method to evaluate an XPath against a document. The XPath implementation is responsble for switching to the appropriate callback handler based on the XML nodes referenced by the XPath. This necessitates a style of programming that uses inner or anonymous class to handle the results of the XPath.
The motivation to implement XPath support this was largely to discourage
styles of programming that use switches or cascading if-else-ifs where
the type of the result is checked via the instanceof
operator.
To see how to effectively use this means of evaluation, look at the
following example code.
public class SomeUtilityClass { private XPathFactory factory_ = ...; public static boolean doesXPathPointToSomething( Document doc_to_search, String x_path_expression) throws XPathException { // this inner class is the callback for the evaluated xpath class WasExpressionFoundCallback extends AbstractXPathCallback { private boolean wasFound = true; public void processNotFound() { wasFound = false; } }; XPath x_path = factory_.newXPath(x_path_expression); WasExpressionFoundCallback callback = new WasExpressionFoundCallback(); x_path.evaluate(doc_to_search, callback); return callback.wasFound; } }
node_to_operate_on
- node to search relative tocallback
- interface bearing methods for each type of located object
XPathException
- implementations should rethrow all failures
as XPathExceptionsXPathCallback
public Object evaluate(Node node_to_operate_on) throws XPathException
One should not use this method as an excuse to implement poor
code consisting of switches or cascading if-else-ifs where the type
of the result is checked via the instanceof
operator.
This method should only be used when a single result type is demanded.
Supported API: true
node_to_operate_on
-
XPathException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |