summaryrefslogtreecommitdiffstats
path: root/sandbox/sebastien/java/extend/samples/implementation-extension/src/test/java/sample/Xutil.java
blob: 91af7c47e9ef3a5b69b5b6186ba6d700f16ab7a5 (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
/*
 * 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 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;

/**
 * A little bit of magic code and utility functions to help work with DOM.
 */
class Xutil {
    static class NodeBuilder {
        String ns;
        String name;
        NodeBuilder[] children;
        String text;
    }

    /**
     * Convert a name and a list of children to a document element.
     */
    static Element dom(String ns, String name, final NodeBuilder... nodes) {
        return (Element)node(elem(ns, name, nodes), 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() {
            {
                this.ns = uri;
                this.name = n;
                this.children = nodes;
            }
        };
    }

    static NodeBuilder elem(final String n, final NodeBuilder... nodes) {
        return new NodeBuilder() {
            {
                this.name = n;
                this.children = nodes;
            }
        };
    }

    /**
     * Convert a string to a text element.
     */
    static NodeBuilder text(final String t) {
        return new NodeBuilder() {
            {
                this.text = t;
            }
        };
    }

    private final static DocumentBuilder db = db();

    private static DocumentBuilder db() {
        try {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch(ParserConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    private static Element link(final Element e, final Document doc, final NodeBuilder... nodes) {
        for(final NodeBuilder c: nodes)
            e.appendChild(node(c, doc));
        return e;
    }

    private static Node node(NodeBuilder node, Document doc) {
        if(node.text != null)
            return doc.createTextNode(node.text);
        return link(doc.createElementNS(node.ns, node.name), doc, node.children);
    }

    /**
     * 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);
        }
    }

}