[Practicalxml-commits] SF.net SVN: practicalxml:[70] trunk/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-30 14:08:12
|
Revision: 70 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=70&view=rev Author: kdgregory Date: 2008-12-30 14:08:10 +0000 (Tue, 30 Dec 2008) Log Message: ----------- XPathWrapper: all forms of evaluate() now take a Node, for consistency with the rest of the world - refactor evaluation Modified Paths: -------------- trunk/src/main/java/net/sf/practicalxml/xpath/XPathWrapper.java trunk/src/test/java/net/sf/practicalxml/xpath/TestXPathWrapper.java Modified: trunk/src/main/java/net/sf/practicalxml/xpath/XPathWrapper.java =================================================================== --- trunk/src/main/java/net/sf/practicalxml/xpath/XPathWrapper.java 2008-12-30 14:01:57 UTC (rev 69) +++ trunk/src/main/java/net/sf/practicalxml/xpath/XPathWrapper.java 2008-12-30 14:08:10 UTC (rev 70) @@ -1,6 +1,5 @@ package net.sf.practicalxml.xpath; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -13,11 +12,10 @@ import javax.xml.xpath.XPathFunction; import javax.xml.xpath.XPathVariableResolver; -import org.w3c.dom.Document; -import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import net.sf.practicalxml.DomUtil; import net.sf.practicalxml.XmlException; @@ -142,7 +140,7 @@ * * @return The wrapper, so that calls may be chained. */ - public XPathWrapper bindFunction(AbstractFunction func) + public XPathWrapper bindFunction(AbstractFunction<?> func) { _functions.addFunction(func); return this; @@ -164,7 +162,7 @@ * * @return The wrapper, so that calls may be chained. */ - public XPathWrapper bindFunction(AbstractFunction func, String prefix) + public XPathWrapper bindFunction(AbstractFunction<?> func, String prefix) { _functions.addFunction(func); return bindNamespace(prefix, func.getNamespaceUri()); @@ -254,121 +252,44 @@ /** - * Applies this expression to the root of the specified document, - * converting the resulting NodeList into a java.util.List for ease - * in iteration. + * Applies this expression to the the specified node, converting the + * resulting <code>NodeList</code> into a <code>java.util.List</code>. */ - public List<Node> evaluate(Document context) + public List<Node> evaluate(Node context) { - return evaluate(context.getDocumentElement()); + return DomUtil.asList( + evaluate(context, XPathConstants.NODESET, NodeList.class), + Node.class); } /** - * Applies this expression to the specified element, converting the - * resulting NodeList into a java.util.List for ease in iteration. - */ - public List<Node> evaluate(Element context) - { - compileIfNeeded(); - try - { - NodeList result = (NodeList)_compiled.evaluate(context, XPathConstants.NODESET); - List<Node> ret = new ArrayList<Node>(result.getLength()); - for (int ii = 0 ; ii < result.getLength() ; ii++) - { - ret.add(result.item(ii)); - } - return ret; - } - catch (Exception ee) - { - throw new XmlException("unable to evaluate: " + _expr, ee); - } - } - - - /** - * Applies this expression to the root of the specified document, - * requesting the <code>STRING</code> return type. - */ - public String evaluateAsString(Document context) - { - return evaluateAsString(context.getDocumentElement()); - } - - - /** - * Applies this expression to the specified element, requesting the + * Applies this expression to the specified node, requesting the * <code>STRING</code> return type. */ - public String evaluateAsString(Element context) + public String evaluateAsString(Node context) { - compileIfNeeded(); - try - { - return _compiled.evaluate(context); - } - catch (Exception ee) - { - throw new XmlException("unable to evaluate: " + _expr, ee); - } + return evaluate(context, XPathConstants.STRING, String.class); } /** - * Applies this expression to the root of the specified document, - * requesting the <code>NUMBER</code> return type. - */ - public Double evaluateAsNumber(Document context) - { - return evaluateAsNumber(context.getDocumentElement()); - } - - - /** - * Applies this expression to the specified element, requesting the + * Applies this expression to the specified node, requesting the * <code>NUMBER</code> return type. */ - public Double evaluateAsNumber(Element context) + public Number evaluateAsNumber(Node context) { - compileIfNeeded(); - try - { - return (Double)_compiled.evaluate(context, XPathConstants.NUMBER); - } - catch (Exception ee) - { - throw new XmlException("unable to evaluate: " + _expr, ee); - } + return evaluate(context, XPathConstants.NUMBER, Number.class); } /** - * Applies this expression to the root of the specified document, - * requesting the <code>BOOLEAN</code> return type. - */ - public Boolean evaluateAsBoolean(Document context) - { - return evaluateAsBoolean(context.getDocumentElement()); - } - - - /** - * Applies this expression to the specified element, requesting the + * Applies this expression to the the specified node, requesting the * <code>BOOLEAN</code> return type. */ - public Boolean evaluateAsBoolean(Element context) + public Boolean evaluateAsBoolean(Node context) { - compileIfNeeded(); - try - { - return (Boolean)_compiled.evaluate(context, XPathConstants.BOOLEAN); - } - catch (Exception ee) - { - throw new XmlException("unable to evaluate: " + _expr, ee); - } + return evaluate(context, XPathConstants.BOOLEAN, Boolean.class); } @@ -376,7 +297,6 @@ // Overrides of Object //---------------------------------------------------------------------------- - /** * Two instances are considered equal if they have the same expression, * namespace mappings, variable mappings, and function mappings. Note @@ -450,6 +370,24 @@ /** + * Compiles and executes the expression in the context of the specified + * node, returning the specified result type. + */ + private <T> T evaluate(Node context, QName returnType, Class<T> castTo) + { + compileIfNeeded(); + try + { + return castTo.cast(_compiled.evaluate(context, returnType)); + } + catch (Exception ee) + { + throw new XmlException("unable to evaluate: " + _expr, ee); + } + } + + + /** * Resolver for variable references. */ private class MyVariableResolver Modified: trunk/src/test/java/net/sf/practicalxml/xpath/TestXPathWrapper.java =================================================================== --- trunk/src/test/java/net/sf/practicalxml/xpath/TestXPathWrapper.java 2008-12-30 14:01:57 UTC (rev 69) +++ trunk/src/test/java/net/sf/practicalxml/xpath/TestXPathWrapper.java 2008-12-30 14:08:10 UTC (rev 70) @@ -104,7 +104,7 @@ List<Node> result1 = xpath.evaluate(_dom); assertEquals(1, result1.size()); - assertSame(_root, result1.get(0)); + assertSame(_dom, result1.get(0)); List<Node> result2 = xpath.evaluate(_root); assertEquals(1, result2.size()); @@ -123,9 +123,7 @@ _root.setAttribute("argle", "bargle"); XPathWrapper xpath = new XPathWrapper("@foo"); - assertEquals("bar", xpath.evaluateAsString(_root)); - assertEquals("bar", xpath.evaluateAsString(_dom)); } @@ -135,9 +133,7 @@ _root.setAttribute("foo", "10"); XPathWrapper xpath = new XPathWrapper("@foo"); - - assertEquals(Double.valueOf(10.0), xpath.evaluateAsNumber(_root)); - assertEquals(Double.valueOf(10.0), xpath.evaluateAsNumber(_dom)); + assertEquals(10, xpath.evaluateAsNumber(_root).intValue()); } @@ -147,14 +143,13 @@ _root.setAttribute("foo", "10"); XPathWrapper xpath1 = new XPathWrapper("@foo=10"); - assertTrue(xpath1.evaluateAsBoolean(_root).booleanValue()); - assertTrue(xpath1.evaluateAsBoolean(_dom).booleanValue()); _root.setAttribute("foo", "20"); - assertFalse(xpath1.evaluateAsBoolean(_root).booleanValue()); - assertFalse(xpath1.evaluateAsBoolean(_dom).booleanValue()); + + XPathWrapper xpath2 = new XPathWrapper("."); + assertTrue(xpath2.evaluateAsBoolean(_root).booleanValue()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |