comparison software/mpdl-services/mpiwg-mpdl-xml/src/de/mpg/mpiwg/berlin/mpdl/xml/xquery/XQueryEvaluator.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents dc5e9fcb3fdc
children
comparison
equal deleted inserted replaced
22:6a45a982c333 23:e845310098ba
1 package de.mpg.mpiwg.berlin.mpdl.xml.xquery; 1 package de.mpg.mpiwg.berlin.mpdl.xml.xquery;
2 2
3 import java.io.StringReader; 3 import java.io.StringReader;
4 import java.io.StringWriter; 4 import java.io.StringWriter;
5 import java.net.URL; 5 import java.net.URL;
6 import java.util.ArrayList;
6 7
7 import javax.xml.transform.stream.StreamSource; 8 import javax.xml.transform.stream.StreamSource;
8 9
10 import org.xml.sax.SAXParseException;
11
9 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; 12 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
10 13
14 import net.sf.saxon.s9api.Axis;
15 import net.sf.saxon.s9api.DocumentBuilder;
11 import net.sf.saxon.s9api.Processor; 16 import net.sf.saxon.s9api.Processor;
17 import net.sf.saxon.s9api.QName;
12 import net.sf.saxon.s9api.SaxonApiException; 18 import net.sf.saxon.s9api.SaxonApiException;
13 import net.sf.saxon.s9api.Serializer; 19 import net.sf.saxon.s9api.Serializer;
14 import net.sf.saxon.s9api.XQueryCompiler; 20 import net.sf.saxon.s9api.XQueryCompiler;
15 import net.sf.saxon.s9api.XQueryExecutable; 21 import net.sf.saxon.s9api.XQueryExecutable;
22 import net.sf.saxon.s9api.XdmAtomicValue;
23 import net.sf.saxon.s9api.XdmEmptySequence;
16 import net.sf.saxon.s9api.XdmItem; 24 import net.sf.saxon.s9api.XdmItem;
25 import net.sf.saxon.s9api.XdmNode;
26 import net.sf.saxon.s9api.XdmNodeKind;
17 import net.sf.saxon.s9api.XdmSequenceIterator; 27 import net.sf.saxon.s9api.XdmSequenceIterator;
18 import net.sf.saxon.s9api.XdmValue; 28 import net.sf.saxon.s9api.XdmValue;
29 import net.sf.saxon.trans.XPathException;
19 30
20 public class XQueryEvaluator { 31 public class XQueryEvaluator {
21 private Processor processor; 32 private Processor processor;
22 private XQueryCompiler xQueryCompiler; 33 private XQueryCompiler xQueryCompiler;
23 34
24 public XQueryEvaluator() { 35 public XQueryEvaluator() {
25 processor = new Processor(false); 36 processor = new Processor(false);
26 xQueryCompiler = processor.newXQueryCompiler(); 37 xQueryCompiler = processor.newXQueryCompiler();
27 } 38 }
28 39
40 public XdmNode parse(URL srcUrl) throws ApplicationException {
41 try {
42 DocumentBuilder docBuilder = processor.newDocumentBuilder();
43 StreamSource xmlDoc = new StreamSource(srcUrl.toString());
44 XdmNode docNode = docBuilder.build(xmlDoc);
45 return docNode;
46 } catch (SaxonApiException e) {
47 String message = e.getMessage();
48 Throwable t = e.getCause();
49 if (t instanceof XPathException) {
50 XPathException xpathException = (XPathException) t;
51 Throwable t2 = xpathException.getException();
52 if (t2 instanceof SAXParseException) {
53 SAXParseException saxParseException = (SAXParseException) t2;
54 int lineNumber = saxParseException.getLineNumber();
55 int columnNumber = saxParseException.getColumnNumber();
56 message = "Line: " + lineNumber + ", Column: " + columnNumber + ": " + e.getMessage();
57 }
58 }
59 throw new ApplicationException("Your source file (" + srcUrl.toString() + ") is not valid: " + message);
60 }
61 }
62
63 public Hits evaluate(URL srcUrl, String xqueryStr, int from, int to) throws ApplicationException {
64 Hits result = (Hits) evaluate(srcUrl, xqueryStr, from, to, "hits");
65 return result;
66 }
67
29 public XdmValue evaluate(String srcXmlStr, String xqueryStr) throws ApplicationException { 68 public XdmValue evaluate(String srcXmlStr, String xqueryStr) throws ApplicationException {
30 XdmValue result = (XdmValue) evaluate(srcXmlStr, xqueryStr, null); 69 XdmValue result = (XdmValue) evaluate(srcXmlStr, xqueryStr, 0, 9, "XdmValue");
31 return result; 70 return result;
32 } 71 }
33 72
34 public XdmValue evaluate(URL srcUrl, String xqueryStr) throws ApplicationException { 73 public XdmValue evaluate(URL srcUrl, String xqueryStr) throws ApplicationException {
35 XdmValue result = (XdmValue) evaluate(srcUrl, xqueryStr, null); 74 XdmValue result = (XdmValue) evaluate(srcUrl, xqueryStr, 0, 9, "XdmValue");
36 return result; 75 return result;
37 } 76 }
38 77
39 public String evaluateAsString(String srcXmlStr, String xqueryStr) throws ApplicationException { 78 public String evaluateAsString(String srcXmlStr, String xqueryStr) throws ApplicationException {
40 Object result = evaluate(srcXmlStr, xqueryStr, "asString"); 79 Object result = evaluate(srcXmlStr, xqueryStr, 0, 9, "string");
41 return (String) result; 80 if (result == null)
81 return null;
82 else
83 return (String) result;
42 } 84 }
43 85
44 public String evaluateAsString(URL srcUrl, String xqueryStr) throws ApplicationException { 86 public String evaluateAsString(URL srcUrl, String xqueryStr) throws ApplicationException {
45 Object result = evaluate(srcUrl, xqueryStr, "asString"); 87 Object result = evaluate(srcUrl, xqueryStr, 0, 9, "string");
46 return (String) result; 88 if (result == null)
47 } 89 return null;
48 90 else
49 public String evaluateAsStringValue(String srcXmlStr, String xqueryStr) throws ApplicationException { 91 return (String) result;
50 XdmValue val = (XdmValue) evaluate(srcXmlStr, xqueryStr, null); 92 }
51 return toStringValue(val); 93
52 } 94 public String evaluateAsStringValueJoined(String srcXmlStr, String xqueryStr) throws ApplicationException {
53 95 return evaluateAsStringValueJoined(srcXmlStr, xqueryStr, " ");
54 public String evaluateAsStringValue(URL srcUrl, String xqueryStr) throws ApplicationException { 96 }
55 XdmValue val = (XdmValue) evaluate(srcUrl, xqueryStr, null); 97
56 return toStringValue(val); 98 public String evaluateAsStringValueJoined(URL srcUrl, String xqueryStr) throws ApplicationException {
57 } 99 return evaluateAsStringValueJoined(srcUrl, xqueryStr, " ");
58 100 }
59 public Object evaluate(String srcXmlStr, String xqueryStr, String optionsStr) throws ApplicationException { 101
102 public String evaluateAsStringValueJoined(String srcXmlStr, String xqueryStr, String separator) throws ApplicationException {
103 XdmValue val = (XdmValue) evaluate(srcXmlStr, xqueryStr, 0, 9, "XdmValue");
104 return stringJoin(val, separator);
105 }
106
107 public String evaluateAsStringValueJoined(URL srcUrl, String xqueryStr, String separator) throws ApplicationException {
108 XdmValue val = (XdmValue) evaluate(srcUrl, xqueryStr, 0, 9, "XdmValue");
109 return stringJoin(val, separator);
110 }
111
112 public Object evaluate(String srcXmlStr, String xqueryStr, int from, int to, String resultType) throws ApplicationException {
60 try { 113 try {
61 StringReader srcXmlStrReader = new StringReader(srcXmlStr); 114 StringReader srcXmlStrReader = new StringReader(srcXmlStr);
62 StreamSource xmlDoc = new StreamSource(srcXmlStrReader); 115 StreamSource xmlDoc = new StreamSource(srcXmlStrReader);
63 XQueryExecutable xQueryExecutable = xQueryCompiler.compile(xqueryStr); 116 XQueryExecutable xQueryExecutable = xQueryCompiler.compile(xqueryStr);
64 Serializer serializer = new Serializer(); 117 Serializer serializer = new Serializer();
65 serializer.setOutputWriter(new StringWriter()); 118 serializer.setOutputWriter(new StringWriter());
66 net.sf.saxon.s9api.XQueryEvaluator xQueryEvaluator = xQueryExecutable.load(); 119 net.sf.saxon.s9api.XQueryEvaluator xQueryEvaluator = xQueryExecutable.load();
67 xQueryEvaluator.setSource(xmlDoc); 120 xQueryEvaluator.setSource(xmlDoc);
68 XdmValue val = xQueryEvaluator.evaluate(); 121 XdmValue val = xQueryEvaluator.evaluate();
69 Object result = val; 122 Object result = val;
70 if (optionsStr != null && optionsStr.contains("asString")) { 123 if (resultType != null && resultType.equals("string")) {
71 int size = val.size(); 124 result = toString(val);
72 if (size <= 0) 125 } else if (resultType != null && resultType.equals("XdmValue")) {
73 result = ""; 126 result = val;
74 else 127 } else if (resultType != null && resultType.equals("hits")) {
75 result = toString(val); 128 result = toHits(val, from, to);
76 } 129 }
77 return result; 130 return result;
78 } catch (SaxonApiException e) { 131 } catch (SaxonApiException e) {
79 throw new ApplicationException(e); 132 throw new ApplicationException(e);
80 } 133 }
81 } 134 }
82 135
83 public Object evaluate(URL srcUrl, String xqueryStr, String optionsStr) throws ApplicationException { 136 public Object evaluate(URL srcUrl, String xqueryStr, int from, int to, String resultType) throws ApplicationException {
84 try { 137 try {
85 StreamSource xmlDoc = new StreamSource(srcUrl.toString()); 138 StreamSource xmlDoc = new StreamSource(srcUrl.toString());
86 XQueryExecutable xQueryExecutable = xQueryCompiler.compile(xqueryStr); 139 XQueryExecutable xQueryExecutable = xQueryCompiler.compile(xqueryStr);
87 Serializer serializer = new Serializer(); 140 Serializer serializer = new Serializer();
88 serializer.setOutputWriter(new StringWriter()); 141 serializer.setOutputWriter(new StringWriter());
89 net.sf.saxon.s9api.XQueryEvaluator xQueryEvaluator = xQueryExecutable.load(); 142 net.sf.saxon.s9api.XQueryEvaluator xQueryEvaluator = xQueryExecutable.load();
90 xQueryEvaluator.setSource(xmlDoc); 143 xQueryEvaluator.setSource(xmlDoc);
91 XdmValue val = xQueryEvaluator.evaluate(); 144 XdmValue val = xQueryEvaluator.evaluate();
92 Object result = val; 145 Object result = null;
93 if (optionsStr != null && optionsStr.contains("asString")) { 146 if (resultType != null && resultType.equals("string")) {
94 int size = val.size(); 147 result = toString(val);
95 if (size <= 0) 148 } else if (resultType != null && resultType.equals("XdmValue")) {
96 result = ""; 149 result = val;
97 else 150 } else if (resultType != null && resultType.equals("hits")) {
98 result = toString(val); 151 result = toHits(val, from, to);
99 } 152 }
100 return result; 153 return result;
101 } catch (SaxonApiException e) { 154 } catch (SaxonApiException e) {
102 throw new ApplicationException(e); 155 throw new ApplicationException(e);
103 } 156 }
104 } 157 }
105 158
159 private Hits toHits(XdmValue xdmValue, int from, int to) throws ApplicationException {
160 Hits result = null;
161 if (xdmValue instanceof XdmAtomicValue) {
162 XdmAtomicValue av = (XdmAtomicValue) xdmValue;
163 String avStr = av.getStringValue();
164 ArrayList<Hit> hits = new ArrayList<Hit>();
165 Hit hit = new Hit(avStr);
166 hit.setType(Hit.TYPE_ATOMIC_VALUE);
167 hits.add(hit);
168 result = new Hits(hits, from, to);
169 result.setSize(1);
170 return result;
171 }
172 XdmSequenceIterator iter = xdmValue.iterator();
173 int size = xdmValue.size();
174 if (size > 0) {
175 int counter = 0;
176 ArrayList<Hit> hits = new ArrayList<Hit>();
177 while (iter.hasNext() && counter <= to) {
178 XdmItem item = iter.next();
179 if (counter >= from) {
180 XdmNode n = (XdmNode) item;
181 ArrayList<XdmNode> precNodes = getPreceding(n, "pb");
182 int page = precNodes.size();
183 String nodeName = n.getNodeName().getLocalName();
184 String nodeKindName = n.getNodeKind().name();
185 ArrayList<XdmNode> precNodesUntilPB = getPrecedingUntil(n, nodeName, "pb");
186 int hitPagePosition = precNodesUntilPB.size() + 1;
187 String itemStr = item.toString();
188 // itemStr = itemStr.replaceAll("[ \n\t]+xmlns.*?\".*?\"", ""); // remove the namespace
189 itemStr = itemStr.replaceAll("[ \n\t]+xmlns", " xmlns"); // remove the blanks before the namespace
190 Hit hit = new Hit(itemStr);
191 hit.setPage(page);
192 hit.setName(nodeName);
193 if (nodeKindName != null && nodeKindName.equals("ELEMENT"))
194 hit.setType(Hit.TYPE_ELEMENT);
195 else if (nodeKindName != null && nodeKindName.equals("ATTRIBUTE"))
196 hit.setType(Hit.TYPE_ATTRIBUTE);
197 if (hit.getType() == Hit.TYPE_ELEMENT)
198 hit.setHitPagePosition(hitPagePosition);
199 hits.add(hit);
200 }
201 counter++;
202 }
203 result = new Hits(hits, from, to);
204 result.setSize(size);
205 }
206 return result;
207 }
208
106 private String toString(XdmValue xdmValue) { 209 private String toString(XdmValue xdmValue) {
210 if (xdmValue instanceof XdmAtomicValue) {
211 XdmAtomicValue av = (XdmAtomicValue) xdmValue;
212 String avStr = av.getStringValue();
213 return avStr;
214 }
215 String result = null;
216 XdmSequenceIterator iter = xdmValue.iterator();
217 int size = xdmValue.size();
218 if (size > 0) {
219 result = "";
220 while (iter.hasNext()) {
221 XdmItem item = iter.next();
222 String itemStr = item.toString();
223 result += itemStr;
224 }
225 }
226 return result;
227 }
228
229 private String stringJoin(XdmValue xdmValue, String separator) {
230 if (xdmValue == null || xdmValue instanceof XdmEmptySequence)
231 return null;
232 if (xdmValue instanceof XdmAtomicValue) {
233 XdmAtomicValue av = (XdmAtomicValue) xdmValue;
234 String avStr = av.getStringValue();
235 return avStr;
236 }
107 String result = ""; 237 String result = "";
108 XdmSequenceIterator iter = xdmValue.iterator(); 238 XdmSequenceIterator iter = null;
239 if (xdmValue instanceof XdmNode) {
240 XdmNode xdmNode = (XdmNode) xdmValue;
241 iter = xdmNode.axisIterator(Axis.CHILD);
242 } else if (xdmValue instanceof XdmValue) {
243 iter = xdmValue.iterator();
244 }
109 while (iter.hasNext()) { 245 while (iter.hasNext()) {
110 XdmItem item = iter.next(); 246 XdmNode node = (XdmNode) iter.next();
111 String itemStr = item.toString(); 247 XdmNodeKind nodeKind = node.getNodeKind();
112 result += itemStr; 248 if (nodeKind == XdmNodeKind.TEXT) {
113 } 249 String nodeStr = node.getStringValue();
114 return result; 250 String trimmedNodeStr = nodeStr.trim();
115 } 251 result = result + trimmedNodeStr;
116 252 } else if (nodeKind == XdmNodeKind.ELEMENT) {
117 private String toStringValue(XdmValue xdmValue) { 253 result = result + stringJoin(node, separator) + separator; // put a separator between child element nodes
118 return xdmValue.itemAt(0).getStringValue(); 254 }
255 }
256 return result;
257 }
258
259 private ArrayList<XdmNode> getPreceding(XdmNode startNode, String ofNodeName) {
260 ArrayList<XdmNode> retNodes = null;
261 XdmSequenceIterator iter = startNode.axisIterator(Axis.PRECEDING);
262 if (iter != null) {
263 retNodes = new ArrayList<XdmNode>();
264 while (iter.hasNext()) {
265 XdmNode n = (XdmNode) iter.next();
266 QName qName = n.getNodeName();
267 if (qName != null) {
268 String name = qName.getLocalName();
269 if (name != null && name.equals(ofNodeName))
270 retNodes.add(n);
271 }
272 }
273 }
274 return retNodes;
275 }
276
277 private ArrayList<XdmNode> getPrecedingUntil(XdmNode startNode, String ofNodeName, String untilNodeName) {
278 ArrayList<XdmNode> retNodes = null;
279 XdmSequenceIterator iter = startNode.axisIterator(Axis.PRECEDING);
280 if (iter != null) {
281 retNodes = new ArrayList<XdmNode>();
282 while (iter.hasNext()) {
283 XdmNode n = (XdmNode) iter.next();
284 QName qName = n.getNodeName();
285 if (qName != null) {
286 String name = qName.getLocalName();
287 if (name != null && name.equals(ofNodeName))
288 retNodes.add(n);
289 if (name != null && name.equals(untilNodeName))
290 return retNodes;
291 }
292 }
293 }
294 return retNodes;
119 } 295 }
120 } 296 }