View Javadoc

1   package org.tinyjee.maven.dim.utils;
2   
3   import org.w3c.dom.Document;
4   import org.w3c.dom.Node;
5   import org.w3c.dom.NodeList;
6   
7   import javax.xml.namespace.NamespaceContext;
8   import javax.xml.transform.OutputKeys;
9   import javax.xml.transform.Transformer;
10  import javax.xml.transform.TransformerException;
11  import javax.xml.transform.TransformerFactory;
12  import javax.xml.transform.dom.DOMSource;
13  import javax.xml.transform.stream.StreamResult;
14  import javax.xml.xpath.XPath;
15  import javax.xml.xpath.XPathConstants;
16  import javax.xml.xpath.XPathExpressionException;
17  import javax.xml.xpath.XPathFactory;
18  import java.io.StringWriter;
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  /**
24   * Implements {@link XPathEvaluator}.
25   *
26   * @author Juergen_Kellerer, 2011-10-12
27   */
28  public class XPathEvaluatorImplementation implements XPathEvaluator {
29  
30  	public static final String DEFAULT_PREFIX = "default";
31  
32  	public static String serializeNode(Node node, boolean omitDeclaration, boolean standalone) {
33  		try {
34  			StringWriter buffer = new StringWriter(4096);
35  			Transformer transformer = TransformerFactory.newInstance().newTransformer();
36  			if (omitDeclaration) transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
37  			if (standalone) transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
38  			transformer.transform(new DOMSource(node), new StreamResult(buffer));
39  			return buffer.toString();
40  		} catch (TransformerException e) {
41  			throw new RuntimeException(e);
42  		}
43  	}
44  
45  	private final Node context;
46  	private final XPath xPath;
47  
48  	/**
49  	 * Creates a new evaluator around the given document.
50  	 *
51  	 * @param document the document to evaluate on.
52  	 */
53  	public XPathEvaluatorImplementation(final Document document) {
54  		context = document;
55  		xPath = XPathFactory.newInstance().newXPath();
56  		xPath.setNamespaceContext(new NamespaceContext() {
57  			public String getNamespaceURI(String prefix) {
58  				return DEFAULT_PREFIX.equals(prefix) ? document.lookupNamespaceURI(null) : document.lookupNamespaceURI(prefix);
59  			}
60  
61  			public String getPrefix(String namespaceURI) {
62  				return namespaceURI.equals(document.lookupNamespaceURI(null)) ? DEFAULT_PREFIX : document.lookupPrefix(namespaceURI);
63  			}
64  
65  			public Iterator getPrefixes(String namespaceURI) {
66  				return Collections.singleton(getPrefix(namespaceURI)).iterator();
67  			}
68  		});
69  	}
70  
71  	private XPathEvaluatorImplementation(XPath xPath, Node context) {
72  		this.xPath = xPath;
73  		this.context = context;
74  	}
75  
76  	public String findText(String xpathExpression) {
77  		try {
78  			return (String) xPath.evaluate(xpathExpression, context, XPathConstants.STRING);
79  		} catch (XPathExpressionException e) {
80  			throw new RuntimeException(e);
81  		}
82  	}
83  
84  	public List<Node> findNodes(String xpathExpression) {
85  		try {
86  			final NodeList nodeList = (NodeList) xPath.evaluate(xpathExpression, context, XPathConstants.NODESET);
87  			return asList(nodeList);
88  		} catch (XPathExpressionException e) {
89  			throw new RuntimeException(e);
90  		}
91  	}
92  
93  	public String serialize(Node node) {
94  		return serializeNode(node, true, true);
95  	}
96  
97  	public List<Node> asList(final NodeList nodeList) {
98  		return new NodeListAdapter(nodeList);
99  	}
100 
101 	public XPathEvaluator newEvaluator(Node node) {
102 		return new XPathEvaluatorImplementation(xPath, node);
103 	}
104 }