1
|
package eu.dnetlib.parthenos.publisher;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
import java.util.Map.Entry;
|
6
|
import javax.xml.transform.sax.SAXSource;
|
7
|
|
8
|
import com.google.common.collect.Maps;
|
9
|
import net.sf.saxon.s9api.*;
|
10
|
import net.sf.saxon.s9api.Serializer.Property;
|
11
|
import org.springframework.stereotype.Component;
|
12
|
import org.xml.sax.InputSource;
|
13
|
|
14
|
/**
|
15
|
* Created by alessia on 10/03/17.
|
16
|
*/
|
17
|
@Component
|
18
|
public class SaxonHelper {
|
19
|
|
20
|
private Processor xmlProcessor = new Processor(false);
|
21
|
|
22
|
|
23
|
public Helper help(){
|
24
|
return new Helper();
|
25
|
}
|
26
|
|
27
|
public class Helper{
|
28
|
private Serializer serializer;
|
29
|
|
30
|
Helper() {
|
31
|
this.serializer = xmlProcessor.newSerializer();
|
32
|
serializer.setOutputProperty(Property.METHOD, "xml");
|
33
|
serializer.setOutputProperty(Property.INDENT, "yes");
|
34
|
}
|
35
|
|
36
|
public Helper setSerializerProperty(Property p, String value){
|
37
|
serializer.setOutputProperty(p, value);
|
38
|
return this;
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Evaluate the given xpath on the given XML source.
|
43
|
*
|
44
|
* @param xmlSource XML source string
|
45
|
* @param xpath the xpath to evaluate
|
46
|
* @param namespaces the map of namespaces to be declared to evaluate the xpath
|
47
|
* @return an XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
|
48
|
*/
|
49
|
public XdmItem evaluateSingle(final String xmlSource, final String xpath, final Map<String, String> namespaces) throws SaxonApiException {
|
50
|
XPathSelector xpathSelector = prepareXPathSelector(xpath, namespaces);
|
51
|
return evaluateSingle(xmlSource, xpathSelector);
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Applies the given xpath selector on the given XML source.
|
56
|
*
|
57
|
* @param xmlSource XML source string
|
58
|
* @param xpathSelector the configured xpath selector to apply
|
59
|
* @return an XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
|
60
|
*/
|
61
|
public XdmItem evaluateSingle(final String xmlSource, final XPathSelector xpathSelector) throws SaxonApiException {
|
62
|
SAXSource source = new SAXSource(new InputSource(new StringReader(xmlSource)));
|
63
|
DocumentBuilder docBuilder = xmlProcessor.newDocumentBuilder();
|
64
|
XdmNode xdmNode = docBuilder.build(source);
|
65
|
xpathSelector.setContextItem(xdmNode);
|
66
|
return xpathSelector.evaluateSingle();
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Get an XPathSelector for the given xpath and namespaces.
|
71
|
* @param xpath the xpath
|
72
|
* @param namespaces the map of namespaces to be declared to evaluate the xpath
|
73
|
* @return XPathSelector
|
74
|
* @throws SaxonApiException
|
75
|
*/
|
76
|
public XPathSelector prepareXPathSelector(final String xpath,final Map<String, String> namespaces) throws SaxonApiException {
|
77
|
XPathCompiler compiler = xmlProcessor.newXPathCompiler();
|
78
|
for (Entry<String, String> ns : namespaces.entrySet()) {
|
79
|
compiler.declareNamespace(ns.getKey(), ns.getValue());
|
80
|
}
|
81
|
XPathExecutable xpathExecutable = compiler.compile(xpath);
|
82
|
XPathSelector xpathSelector = xpathExecutable.load();
|
83
|
return xpathSelector;
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Get an XPathSelector for the given xpath and namespaces.
|
88
|
* @param xpath the xpath
|
89
|
* @param nsPrefix namespace prefix
|
90
|
* @param ns namespace URI
|
91
|
* @return XPathSelector
|
92
|
* @throws SaxonApiException
|
93
|
*/
|
94
|
public XPathSelector prepareXPathSelector(final String xpath,final String nsPrefix, final String ns) throws SaxonApiException {
|
95
|
Map<String, String> map = Maps.newHashMap();
|
96
|
map.put(nsPrefix, ns);
|
97
|
return prepareXPathSelector(xpath, map);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Evaluate the given xpath on the given XML source.
|
102
|
* @param xmlSource XML source string
|
103
|
* @param xpath the xpath to evaluate
|
104
|
* @param nsPrefix namespace prefix
|
105
|
* @param ns namespace URI
|
106
|
* @return an XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
|
107
|
* @throws SaxonApiException
|
108
|
*
|
109
|
*/
|
110
|
public XdmItem evaluateSingle(final String xmlSource, final String xpath, final String nsPrefix, final String ns) throws SaxonApiException {
|
111
|
Map<String, String> map = Maps.newHashMap();
|
112
|
map.put(nsPrefix, ns);
|
113
|
return evaluateSingle(xmlSource, xpath, map);
|
114
|
}
|
115
|
/**
|
116
|
* Evaluate the given xpath on the given XML source.
|
117
|
*
|
118
|
* @param xmlSource XML source string
|
119
|
* @param xpath the xpath to evaluate. xpath must evaluate to an xdmNode
|
120
|
* @param namespaces the map of namespaces to be declared to evaluate the xpath
|
121
|
* @return the XML serialization as string of the XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
|
122
|
* @throws ClassCastException if xpath is not evaluated to a node.
|
123
|
*/
|
124
|
public String evaluateSingleAsString(final String xmlSource, final String xpath, final Map<String, String> namespaces) throws SaxonApiException {
|
125
|
XdmItem item = evaluateSingle(xmlSource, xpath, namespaces);
|
126
|
if(item != null){
|
127
|
XdmNode node = (XdmNode) item;
|
128
|
return serializer.serializeNodeToString(node);
|
129
|
}
|
130
|
else return null;
|
131
|
}
|
132
|
|
133
|
|
134
|
/**
|
135
|
* Applies the given xpath selector on the given XML source.
|
136
|
*
|
137
|
* @param xmlSource XML source string
|
138
|
* @param xpathSelector the configured xpath selector to apply
|
139
|
* @return the XML serialization as string of the XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
|
140
|
* @throws ClassCastException if xpath is not evaluated to a node.
|
141
|
*/
|
142
|
public String evaluateSingleAsString(final String xmlSource, final XPathSelector xpathSelector) throws SaxonApiException {
|
143
|
XdmItem item = evaluateSingle(xmlSource, xpathSelector);
|
144
|
if(item != null){
|
145
|
XdmNode node = (XdmNode) item;
|
146
|
return serializer.serializeNodeToString(node);
|
147
|
}
|
148
|
else return null;
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Evaluate the given xpath on the given XML source.
|
153
|
*
|
154
|
* @param xmlSource XML source string
|
155
|
* @param xpath the xpath to evaluate. xpath must evaluate to an xdmNode
|
156
|
* @param nsPrefix namespace prefix
|
157
|
* @param ns namespace URI
|
158
|
* @return the XML serialization as string of the XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
|
159
|
* @throws ClassCastException if xpath is not evaluated to a node.
|
160
|
*/
|
161
|
public String evaluateSingleAsString(final String xmlSource, final String xpath, final String nsPrefix, final String ns) throws SaxonApiException {
|
162
|
Map<String, String> map = Maps.newHashMap();
|
163
|
map.put(nsPrefix, ns);
|
164
|
return evaluateSingleAsString(xmlSource, xpath, map);
|
165
|
}
|
166
|
|
167
|
|
168
|
}
|
169
|
|
170
|
|
171
|
|
172
|
|
173
|
|
174
|
}
|