From 1c0d8a7ab823150bdbece285e71cd6f414621cfc Mon Sep 17 00:00:00 2001 From: antelder Date: Mon, 27 Dec 2010 10:50:45 +0000 Subject: Start branch for beta2 release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1053042 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/test/java/sample/Xutil.java | 225 +++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 sca-java-2.x/branches/2.0-Beta2/unreleased/samples/implementation-sample-async/src/test/java/sample/Xutil.java (limited to 'sca-java-2.x/branches/2.0-Beta2/unreleased/samples/implementation-sample-async/src/test/java/sample/Xutil.java') diff --git a/sca-java-2.x/branches/2.0-Beta2/unreleased/samples/implementation-sample-async/src/test/java/sample/Xutil.java b/sca-java-2.x/branches/2.0-Beta2/unreleased/samples/implementation-sample-async/src/test/java/sample/Xutil.java new file mode 100644 index 0000000000..264ce7fc85 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/unreleased/samples/implementation-sample-async/src/test/java/sample/Xutil.java @@ -0,0 +1,225 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +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; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +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; + +/** + * Just for fun, a little bit of magic code and utility functions to help work with XML DOM. + */ +public class Xutil { + public interface NodeBuilder { + Node build(Document doc); + } + + /** + * Convert a name and a list of children to a document element. + */ + public static Element xdom(String ns, String name, final NodeBuilder... nodes) { + return (Element)elem(ns, name, nodes).build(db.newDocument()); + } + + /** + * Convert a name and children to an element. + */ + public static NodeBuilder elem(final String uri, final String n, final NodeBuilder... nodes) { + return new NodeBuilder() { + public Node build(Document doc) { + final Element e = doc.createElementNS(uri, n); + for(final NodeBuilder n: nodes) + e.appendChild(n.build(doc)); + return e; + } + }; + } + + public static NodeBuilder elem(final String n, final NodeBuilder... nodes) { + return elem(null, n, nodes); + } + + /** + * Convert a string to a text element. + */ + public static NodeBuilder text(final String t) { + return new NodeBuilder() { + public Node build(final Document doc) { + return doc.createTextNode(t); + } + }; + } + + private final static DocumentBuilder db = db(); + + private static DocumentBuilder db() { + try { + return DocumentBuilderFactory.newInstance().newDocumentBuilder(); + } catch(ParserConfigurationException e) { + throw new RuntimeException(e); + } + } + + /** + * Convert an element to XML. + */ + static TransformerFactory trf = TransformerFactory.newInstance(); + + public static String xml(final Node node) { + try { + final StreamResult r = new StreamResult(new StringWriter()); + trf.newTransformer().transform(new DOMSource(node), r); + return r.getWriter().toString(); + } catch(TransformerException e) { + throw new RuntimeException(e); + } + } + + /** + * Evaluate an xpath expression. + */ + private static XPathFactory xpf = XPathFactory.newInstance(); + + public static String xpath(final String expr, final Node node) { + final XPath xp = xpf.newXPath(); + try { + return (String)xp.evaluate(expr, node, XPathConstants.STRING); + } catch(XPathExpressionException e) { + throw new RuntimeException(e); + } + } + + /** + * A pure Java FP-style alternative to xpath for DOM. + */ + 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); + } + + public static Reducer print = new Reducer() { + public String reduce(String accum, Element e) { + return accum + e.getTextContent(); + } + }; + + /** + * Apply a mapper to a list of elements. + */ + public 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. + */ + public 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. + */ + public 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. + */ + public 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. + */ + public 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