comparison software/mpdl-services/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/transform/QueryTransformer.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents
children
comparison
equal deleted inserted replaced
22:6a45a982c333 23:e845310098ba
1 package de.mpg.mpiwg.berlin.mpdl.cms.transform;
2
3 import java.io.IOException;
4 import java.io.StringWriter;
5 import java.net.URL;
6
7 import javax.xml.transform.stream.StreamSource;
8
9 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
10
11 import net.sf.saxon.s9api.Processor;
12 import net.sf.saxon.s9api.QName;
13 import net.sf.saxon.s9api.SaxonApiException;
14 import net.sf.saxon.s9api.Serializer;
15 import net.sf.saxon.s9api.XdmAtomicValue;
16 import net.sf.saxon.s9api.XdmValue;
17 import net.sf.saxon.s9api.XsltCompiler;
18 import net.sf.saxon.s9api.XsltExecutable;
19 import net.sf.saxon.s9api.XsltTransformer;
20
21 public class QueryTransformer {
22 private Processor processor;
23 private XsltCompiler xsltCompiler;
24 private XsltTransformer xsltQueryDocument;
25
26 public QueryTransformer() throws ApplicationException {
27 init();
28 }
29
30 private void init() throws ApplicationException {
31 try {
32 processor = new Processor(false);
33 xsltCompiler = processor.newXsltCompiler();
34 URL queryDocumentXslUrl = QueryTransformer.class.getResource("queryDocument.xsl");
35 StreamSource xslStreamSource = new StreamSource(queryDocumentXslUrl.openStream());
36 XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource);
37 xsltQueryDocument = xsltExecutable.load();
38 } catch (SaxonApiException e) {
39 throw new ApplicationException(e);
40 } catch (IOException e) {
41 throw new ApplicationException(e);
42 }
43 }
44
45 public String queryDocument(String xmlFileName, String query, String flags, String outputFormat) throws ApplicationException {
46 String pageFragment = null;
47 try {
48 StreamSource xmlDoc = new StreamSource(xmlFileName);
49 Serializer serializer = new Serializer();
50 serializer.setOutputWriter(new StringWriter());
51 serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
52 xsltQueryDocument.setSource(xmlDoc); // needs some time for bigger documents
53 xsltQueryDocument.setDestination(serializer);
54 QName queryQName = new QName("query");
55 XdmValue queryXdmValue = new XdmAtomicValue(query);
56 QName flagsQName = new QName("flags");
57 XdmValue flagsXdmValue = new XdmAtomicValue(flags);
58 QName outputFormatQName = new QName("outputFormat");
59 XdmValue outputFormatXdmValue = new XdmAtomicValue(outputFormat);
60 xsltQueryDocument.setParameter(queryQName, queryXdmValue);
61 xsltQueryDocument.setParameter(flagsQName, flagsXdmValue);
62 xsltQueryDocument.setParameter(outputFormatQName, outputFormatXdmValue);
63 xsltQueryDocument.transform(); // needs some time for bigger documents
64 pageFragment = serializer.getOutputDestination().toString();
65 } catch (Exception e) {
66 throw new ApplicationException(e);
67 }
68 return pageFragment;
69 }
70 }