summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/samples/implementation-extension/src
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-08-31 00:15:03 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-08-31 00:15:03 +0000
commit315a8938265927001c35f64b74ff5269d2d438a4 (patch)
tree71d0bee1dd396ff9677e156e2c7d45fe22f723b9 /sca-java-2.x/trunk/samples/implementation-extension/src
parentaa35e08825bbea8e664cceddc7844cba5cc4ef30 (diff)
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
Diffstat (limited to 'sca-java-2.x/trunk/samples/implementation-extension/src')
-rw-r--r--sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/ClientTest.java14
-rw-r--r--sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/WelloTest.java19
-rw-r--r--sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java106
3 files changed, 128 insertions, 11 deletions
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> {
+ T map(final Element e);
+ }
+
+ static Mapper<Element> identity = new Mapper<Element>() {
+ public Element map(Element e) {
+ return e;
+ };
+ };
+
+ interface Reducer<T> {
+ T reduce(final T accum, final Element e);
+ }
+
+ static Reducer<String> print = new Reducer<String>() {
+ public String reduce(String accum, Element e) {
+ return accum + e.getTextContent();
+ }
+ };
+
+ /**
+ * Apply a mapper to a list of elements.
+ */
+ static <T> List<T> xmap(final Mapper<T> f, final Iterable<Element> l) {
+ final List<T> v = new ArrayList<T>();
+ for(Element e: l)
+ v.add(f.map(e));
+ return v;
+ }
+
+ /**
+ * Apply a filter to a list of elements.
+ */
+ static List<Element> xfilter(final Mapper<Boolean> f, final Iterable<Element> l) {
+ final List<Element> v = new ArrayList<Element>();
+ for(Element e: l)
+ if(f.map(e))
+ v.add(e);
+ return v;
+ }
+
+ /**
+ * Perform a reduction over a list of elements.
+ */
+ static <T> T xreduce(final Reducer<T> f, final T initial, final Iterable<Element> 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<Boolean> select(final String name) {
+ return new Mapper<Boolean>() {
+ public Boolean map(Element e) {
+ return name.equals(e.getLocalName());
+ }
+ };
+ }
+
+ /**
+ * Return the child elements of a node.
+ */
+ static Iterable<Element> elems(final Node parent) {
+ final List<Element> l = new ArrayList<Element>();
+ 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<Node> children(Node parent) {
+ final NodeList l = parent.getChildNodes();
+ final int n = l.getLength();
+ return new Iterable<Node>() {
+ public Iterator<Node> iterator() {
+ return new Iterator<Node>() {
+ int i = 0;
+ public boolean hasNext() {
+ return i < n;
+ }
+ public Node next() {
+ return l.item(i++);
+ }
+ public void remove() {
+ }
+ };
+ }
+ };
+ }
}