[Practicalxml-commits] SF.net SVN: practicalxml:[66] trunk/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-29 02:52:08
|
Revision: 66 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=66&view=rev Author: kdgregory Date: 2008-12-29 02:52:04 +0000 (Mon, 29 Dec 2008) Log Message: ----------- XsiBoolean Added Paths: ----------- trunk/src/main/java/net/sf/practicalxml/xpath/function/XsiBoolean.java trunk/src/test/java/net/sf/practicalxml/xpath/function/TestXsiBoolean.java Added: trunk/src/main/java/net/sf/practicalxml/xpath/function/XsiBoolean.java =================================================================== --- trunk/src/main/java/net/sf/practicalxml/xpath/function/XsiBoolean.java (rev 0) +++ trunk/src/main/java/net/sf/practicalxml/xpath/function/XsiBoolean.java 2008-12-29 02:52:04 UTC (rev 66) @@ -0,0 +1,69 @@ +package net.sf.practicalxml.xpath.function; + +import org.w3c.dom.Node; + +import net.sf.practicalxml.xpath.AbstractFunction; + + +/** + * Converts its argument to a boolean value, using a modification of the rules + * for Schema instances: true is represented by the literal values "true" or + * 1, ignoring case, while false is everything else. This is very different + * from the XPath function <code>boolean()</code>, in which any non-zero value + * or non-empty string/nodeset is true. + * <p> + * Note: the name of this class is <code>XsiBoolean</code>, but it's name in + * an XPath expression is "<code>boolean</code>". This is to prevent name + * collision with <code>java.lang.Boolean</code>. + */ +public class XsiBoolean +extends AbstractFunction<Boolean> +{ + public XsiBoolean() + { + super(Constants.COMMON_NS_URI, "boolean", 1); + } + + + @Override + protected Boolean processArg(int index, Node value, Boolean helper) + throws Exception + { + return (value != null) + ? processArg(index, value.getTextContent(), helper) + : Boolean.FALSE; + } + + + @Override + protected Boolean processArg(int index, String value, Boolean helper) + throws Exception + { + return "true".equals(value.toLowerCase()) + || "1".equals(value); + } + + + @Override + protected Boolean processArg(int index, Boolean value, Boolean helper) + throws Exception + { + return value; + } + + + @Override + protected Boolean processArg(int index, Number value, Boolean helper) + throws Exception + { + return Boolean.valueOf(value.intValue() == 1); + } + + + @Override + protected Boolean processNullArg(int index, Boolean helper) + throws Exception + { + return Boolean.FALSE; + } +} \ No newline at end of file Added: trunk/src/test/java/net/sf/practicalxml/xpath/function/TestXsiBoolean.java =================================================================== --- trunk/src/test/java/net/sf/practicalxml/xpath/function/TestXsiBoolean.java (rev 0) +++ trunk/src/test/java/net/sf/practicalxml/xpath/function/TestXsiBoolean.java 2008-12-29 02:52:04 UTC (rev 66) @@ -0,0 +1,183 @@ +package net.sf.practicalxml.xpath.function; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.xml.xpath.XPathFunctionException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import net.sf.practicalxml.AbstractTestCase; +import net.sf.practicalxml.DomUtil; +import net.sf.practicalxml.xpath.XPathWrapper; + + +public class TestXsiBoolean +extends AbstractTestCase +{ + public TestXsiBoolean(String name) + { + super(name); + } + + +//---------------------------------------------------------------------------- +// Setup +//---------------------------------------------------------------------------- + + private Element _root; + private Element _child1; + private Element _child2; + private Element _child3; + private Element _child4; + private Element _child5; + + @Override + protected void setUp() throws Exception + { + _root = DomUtil.newDocument("foo"); + _child1 = DomUtil.appendChild(_root, "bar"); + _child2 = DomUtil.appendChild(_root, "baz"); + _child3 = DomUtil.appendChild(_root, "false"); + _child4 = DomUtil.appendChild(_root, "true"); + _child5 = DomUtil.appendChild(_root, "x"); + + DomUtil.setText(_child1, "true"); + DomUtil.setText(_child2, "false"); + DomUtil.setText(_child3, "1"); + DomUtil.setText(_child4, "0"); + + _child1.setAttribute("attr", "False"); + _child2.setAttribute("attr", "True"); + _child3.setAttribute("attr", "0"); + _child4.setAttribute("attr", "1"); + } + + +//---------------------------------------------------------------------------- +// Test Cases +//---------------------------------------------------------------------------- + + public void testConstruction() throws Exception + { + XsiBoolean fn = new XsiBoolean(); + assertEquals(Constants.COMMON_NS_URI, fn.getNamespaceUri()); + assertEquals("boolean", fn.getName()); + assertEquals(1, fn.getMinArgCount()); + assertEquals(1, fn.getMaxArgCount()); + } + + + public void testLiteralString() throws Exception + { + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList("true"))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList("false"))); + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList("TrUe"))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList("FaLsE"))); + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList("1"))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList("0"))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList("zippy"))); + } + + + public void testLiteralNumber() throws Exception + { + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList(Double.valueOf(1)))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList(Double.valueOf(0)))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList(Double.valueOf(10)))); + } + + + public void testNodeList() throws Exception + { + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList(_root.getChildNodes()))); + } + + + public void testNode() throws Exception + { + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList(_child1))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList(_child2))); + assertEquals( + Boolean.TRUE, + new XsiBoolean().evaluate(Arrays.asList(_child3))); + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList(_child4))); + } + + + public void testEmptyNodeList() throws Exception + { + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList(_child5.getChildNodes()))); + } + + + public void testNull() throws Exception + { + assertEquals( + Boolean.FALSE, + new XsiBoolean().evaluate(Arrays.asList((String)null))); + } + + + public void testEmptyArglist() throws Exception + { + try + { + new XsiBoolean().evaluate(Collections.<String>emptyList()); + fail("didn't throw on empty list"); + } + catch (XPathFunctionException e) + { + // success + } + } + + + public void testInSitu() throws Exception + { + XPathWrapper xpath1 = new XPathWrapper("//*[ns:boolean(text())]") + .bindFunction(new XsiBoolean(), "ns"); + List<Node> result1 = xpath1.evaluate(_root); + assertEquals(2, result1.size()); + assertSame(_child1, result1.get(0)); + assertSame(_child3, result1.get(1)); + + XPathWrapper xpath2 = new XPathWrapper("//*[ns:boolean(@attr)]") + .bindFunction(new XsiBoolean(), "ns"); + List<Node> result2 = xpath2.evaluate(_root); + assertEquals(2, result2.size()); + assertSame(_child2, result2.get(0)); + assertSame(_child4, result2.get(1)); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |