Project

General

Profile

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
		public XdmNode parseXML(final String xmlSource) throws SaxonApiException {
42
			SAXSource source = new SAXSource(new InputSource(new StringReader(xmlSource)));
43
			DocumentBuilder docBuilder = xmlProcessor.newDocumentBuilder();
44
			return docBuilder.build(source);
45
		}
46

    
47
		/**
48
		 * Evaluate the given xpath on the given XML source.
49
		 *
50
		 * @param xmlSource  XML source string
51
		 * @param xpath      the xpath to evaluate
52
		 * @param namespaces the map of namespaces to be declared to evaluate the xpath
53
		 * @return an XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
54
		 */
55
		public XdmItem evaluateSingle(final String xmlSource, final String xpath, final Map<String, String> namespaces) throws SaxonApiException {
56
			XPathSelector xpathSelector = prepareXPathSelector(xpath, namespaces);
57
			return evaluateSingle(xmlSource, xpathSelector);
58
		}
59

    
60
		/**
61
		 * Applies the given xpath selector on the given XML source.
62
		 *
63
		 * @param xmlSource  XML source string
64
		 * @param xpathSelector    the configured xpath  selector to apply
65
		 * @return an XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
66
		 */
67
		public XdmItem evaluateSingle(final String xmlSource, final XPathSelector xpathSelector) throws SaxonApiException {
68
			XdmNode xdmNode = parseXML(xmlSource);
69
			xpathSelector.setContextItem(xdmNode);
70
			return xpathSelector.evaluateSingle();
71
		}
72

    
73
		/**
74
		 * Get an XPathSelector for the given xpath and namespaces.
75
		 * @param xpath the xpath
76
		 * @param namespaces the map of namespaces to be declared to evaluate the xpath
77
		 * @return XPathSelector
78
		 * @throws SaxonApiException
79
		 */
80
		public XPathSelector prepareXPathSelector(final String xpath,final Map<String, String> namespaces) throws SaxonApiException {
81
			XPathCompiler compiler = xmlProcessor.newXPathCompiler();
82
			for (Entry<String, String> ns : namespaces.entrySet()) {
83
				compiler.declareNamespace(ns.getKey(), ns.getValue());
84
			}
85
			XPathExecutable xpathExecutable = compiler.compile(xpath);
86
			XPathSelector xpathSelector = xpathExecutable.load();
87
			return xpathSelector;
88
		}
89

    
90
		/**
91
		 * Get an XPathSelector for the given xpath and namespaces.
92
		 * @param xpath the xpath
93
		 * @param nsPrefix namespace prefix
94
		 * @param ns namespace URI
95
		 * @return XPathSelector
96
		 * @throws SaxonApiException
97
		 */
98
		public XPathSelector prepareXPathSelector(final String xpath,final String nsPrefix, final String ns) throws SaxonApiException {
99
			Map<String, String> map = Maps.newHashMap();
100
			map.put(nsPrefix, ns);
101
			return prepareXPathSelector(xpath, map);
102
		}
103

    
104
		/**
105
		 *  Evaluate the given xpath on the given XML source.
106
		 * @param xmlSource  XML source string
107
		 * @param xpath      the xpath to evaluate
108
		 * @param nsPrefix namespace prefix
109
		 * @param ns namespace URI
110
		 * @return an XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
111
		 * @throws SaxonApiException
112
		 *
113
		 */
114
		public XdmItem evaluateSingle(final String xmlSource, final String xpath, final String nsPrefix, final String ns) throws SaxonApiException {
115
			Map<String, String> map = Maps.newHashMap();
116
			map.put(nsPrefix, ns);
117
			return evaluateSingle(xmlSource, xpath, map);
118
		}
119
		/**
120
		 * Evaluate the given xpath on the given XML source.
121
		 *
122
		 * @param xmlSource  XML source string
123
		 * @param xpath      the xpath to evaluate. xpath must evaluate to an xdmNode
124
		 * @param namespaces the map of namespaces to be declared to evaluate the xpath
125
		 * @return the XML serialization as string of the XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
126
		 * @throws ClassCastException if xpath is not evaluated to a node.
127
		 */
128
		public String evaluateSingleAsString(final String xmlSource, final String xpath, final Map<String, String> namespaces) throws SaxonApiException {
129
			XdmItem item = evaluateSingle(xmlSource, xpath, namespaces);
130
			if(item != null){
131
				XdmNode node = (XdmNode) item;
132
				return serializer.serializeNodeToString(node);
133
			}
134
			else return null;
135
		}
136

    
137

    
138
		/**
139
		 * Applies the given xpath selector on the given XML source.
140
		 *
141
		 * @param xmlSource  XML source string
142
		 * @param xpathSelector    the configured xpath  selector to apply
143
		 * @return the XML serialization as string of the XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
144
		 * @throws ClassCastException if xpath is not evaluated to a node.
145
		 */
146
		public String evaluateSingleAsString(final String xmlSource, final XPathSelector xpathSelector) throws SaxonApiException {
147
			XdmItem item = evaluateSingle(xmlSource, xpathSelector);
148
			if(item != null){
149
				XdmNode node = (XdmNode) item;
150
				return serializer.serializeNodeToString(node);
151
			}
152
			else return null;
153
		}
154

    
155
		/**
156
		 * Evaluate the given xpath on the given XML source.
157
		 *
158
		 * @param xmlSource  XML source string
159
		 * @param xpath      the xpath to evaluate. xpath must evaluate to an xdmNode
160
		 * @param nsPrefix namespace prefix
161
		 * @param ns namespace URI
162
		 * @return the XML serialization as string of the XdmItem resulting from the evaluation of the xpath. Returns null if nothing matches.
163
		 * @throws ClassCastException if xpath is not evaluated to a node.
164
		 */
165
		public String evaluateSingleAsString(final String xmlSource, final String xpath, final String nsPrefix, final String ns) throws SaxonApiException {
166
			Map<String, String> map = Maps.newHashMap();
167
			map.put(nsPrefix, ns);
168
			return evaluateSingleAsString(xmlSource, xpath, map);
169
		}
170

    
171

    
172
	}
173

    
174

    
175

    
176

    
177

    
178
}
(4-4/4)