summaryrefslogtreecommitdiffstats
path: root/sandbox/samples/temp/implementation-extension/src/test/java/sample/Xutil.java
blob: ce89f9dd8ca8637ef4703a08a3bdf56fc9cac865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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.
 */
class Xutil {
    interface NodeBuilder {
        Node build(Document doc);
    }

    /**
     * Convert a name and a list of children to a document element.
     */
    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.
     */
    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;
            }
        };
    }

    static NodeBuilder elem(final String n, final NodeBuilder... nodes) {
        return elem(null, n, nodes);
    }

    /**
     * Convert a string to a text element.
     */
    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();

    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();

    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> {
        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() {
                    }
                };
            }  
        };
    }
}