From 315a8938265927001c35f64b74ff5269d2d438a4 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 31 Aug 2010 00:15:03 +0000 Subject: Replace usage of Xpath by pure java code walking through the DOM. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@991042 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/test/java/sample/ClientTest.java | 14 ++- .../src/test/java/sample/WelloTest.java | 19 +++- .../src/test/java/sample/Xutil.java | 106 ++++++++++++++++++++- 3 files changed, 128 insertions(+), 11 deletions(-) (limited to 'sca-java-2.x/trunk/samples/implementation-extension/src') diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java index 46350e26d6..6b5c3983d1 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java @@ -20,10 +20,14 @@ package sample; import static java.lang.System.out; -import static sample.Xutil.dom; import static sample.Xutil.elem; +import static sample.Xutil.elems; +import static sample.Xutil.print; +import static sample.Xutil.select; import static sample.Xutil.text; -import static sample.Xutil.xpath; +import static sample.Xutil.xdom; +import static sample.Xutil.xfilter; +import static sample.Xutil.xreduce; import org.w3c.dom.Element; @@ -52,8 +56,10 @@ public class ClientTest { public String wello(String s) { out.println("ClientTest.wello(" + s + ")"); - final Element hreq = dom("http://sample", "hello", elem("name", text(s))); + final Element hreq = xdom("http://sample", "hello", elem("name", text(s))); + final Element hres = wello.call("hello", hreq); - return xpath("//*", hres); + + return xreduce(print, "", xfilter(select("result"), elems(hres))); } } diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java index 2f647d1a8f..9da5083dda 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java @@ -20,10 +20,15 @@ package sample; import static java.lang.System.out; -import static sample.Xutil.dom; import static sample.Xutil.elem; +import static sample.Xutil.elems; +import static sample.Xutil.print; +import static sample.Xutil.select; import static sample.Xutil.text; -import static sample.Xutil.xpath; +import static sample.Xutil.xdom; +import static sample.Xutil.xfilter; +import static sample.Xutil.xml; +import static sample.Xutil.xreduce; import org.w3c.dom.Element; @@ -42,9 +47,13 @@ public class WelloTest { WSDLReference upper; public Element call(String op, Element e) { - out.println("WelloTest." + op + "(" + Xutil.xml(e) + ")"); - final Element ureq = dom("http://sample", "upper", elem("s", text("Hello " + xpath("//name", e)))); + out.println("WelloTest." + op + "(" + xml(e) + ")"); + final String name = xreduce(print, "", xfilter(select("name"), elems(e))); + + final Element ureq = xdom("http://sample", "upper", elem("s", text("Hello " + name))); final Element ures = upper.call("upper", ureq); - return dom("http://sample", "helloResponse", elem("result", text(xpath("//*", ures)))); + + final String s = xreduce(print, "", xfilter(select("result"), elems(ures))); + return xdom("http://sample", "helloResponse", elem("result", text(s))); } } diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java index 91af7c47e9..bdad745b6f 100644 --- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java +++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java @@ -20,6 +20,9 @@ package sample; import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -36,9 +39,10 @@ import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; /** - * A little bit of magic code and utility functions to help work with DOM. + * Just for fun, a little bit of magic code and utility functions to help work with XML DOM. */ class Xutil { static class NodeBuilder { @@ -51,7 +55,7 @@ class Xutil { /** * Convert a name and a list of children to a document element. */ - static Element dom(String ns, String name, final NodeBuilder... nodes) { + static Element xdom(String ns, String name, final NodeBuilder... nodes) { return (Element)node(elem(ns, name, nodes), db.newDocument()); } @@ -139,4 +143,102 @@ class Xutil { } } + /** + * A pure Java FP-style alternative to xpath. + */ + interface Mapper { + T map(final Element e); + } + + static Mapper identity = new Mapper() { + public Element map(Element e) { + return e; + }; + }; + + interface Reducer { + T reduce(final T accum, final Element e); + } + + static Reducer print = new Reducer() { + public String reduce(String accum, Element e) { + return accum + e.getTextContent(); + } + }; + + /** + * Apply a mapper to a list of elements. + */ + static List xmap(final Mapper f, final Iterable l) { + final List v = new ArrayList(); + for(Element e: l) + v.add(f.map(e)); + return v; + } + + /** + * Apply a filter to a list of elements. + */ + static List xfilter(final Mapper f, final Iterable l) { + final List v = new ArrayList(); + for(Element e: l) + if(f.map(e)) + v.add(e); + return v; + } + + /** + * Perform a reduction over a list of elements. + */ + static T xreduce(final Reducer f, final T initial, final Iterable l) { + T accum = initial; + for(Element e: l) + accum = f.reduce(accum, e); + return accum; + } + + /** + * Return a filter that selects elements by name. + */ + static Mapper select(final String name) { + return new Mapper() { + public Boolean map(Element e) { + return name.equals(e.getLocalName()); + } + }; + } + + /** + * Return the child elements of a node. + */ + static Iterable elems(final Node parent) { + final List l = new ArrayList(); + for (Node n: children(parent)) + if (n instanceof Element) + l.add((Element)n); + return l; + } + + /** + * An iterable over the children of a node. + */ + private static Iterable children(Node parent) { + final NodeList l = parent.getChildNodes(); + final int n = l.getLength(); + return new Iterable() { + public Iterator iterator() { + return new Iterator() { + int i = 0; + public boolean hasNext() { + return i < n; + } + public Node next() { + return l.item(i++); + } + public void remove() { + } + }; + } + }; + } } -- cgit v1.2.3