1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.coeus.s2sgen.impl.util;
20
21 import org.apache.xpath.XPathAPI;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Node;
24
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.transform.TransformerException;
27 import java.io.ByteArrayInputStream;
28
29
30
31
32
33 public class XPathExecutor {
34
35 private Document doc;
36
37 public XPathExecutor( String xml ) throws Exception{
38
39 init( xml );
40 }
41
42
43
44
45
46
47
48
49 public String execute( String xPath ) throws Exception {
50
51 if ( xPath == null ){
52 return null;
53 }
54
55
56 return XPathAPI.eval( getDoc(), xPath ).toString();
57
58
59 }
60
61
62
63
64
65
66
67 public Node getNode(String xpath)
68 throws TransformerException
69 {
70 return XPathAPI.selectSingleNode(getDoc(), xpath);
71 }
72
73 private void init( String xml ) throws Exception {
74
75 if ( xml == null ) {
76 return;
77 }
78
79
80 try(ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes())) {
81 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
82 dfactory.setNamespaceAware(true);
83 setDoc( dfactory.newDocumentBuilder().parse(stream) );
84 }
85 }
86
87
88
89
90
91 public Document getDoc() {
92 return doc;
93 }
94
95
96
97 public void setDoc(Document doc) {
98 this.doc = doc;
99 }
100
101 }
102