comparison software/mpdl-services/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/transform/TocTransformer.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.StringReader;
5 import java.io.StringWriter;
6 import java.net.URL;
7
8 import javax.xml.transform.stream.StreamSource;
9
10 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
11
12 import net.sf.saxon.s9api.Processor;
13 import net.sf.saxon.s9api.QName;
14 import net.sf.saxon.s9api.SaxonApiException;
15 import net.sf.saxon.s9api.Serializer;
16 import net.sf.saxon.s9api.XdmAtomicValue;
17 import net.sf.saxon.s9api.XdmValue;
18 import net.sf.saxon.s9api.XsltCompiler;
19 import net.sf.saxon.s9api.XsltExecutable;
20 import net.sf.saxon.s9api.XsltTransformer;
21
22 public class TocTransformer {
23 private Processor processor;
24 private XsltCompiler xsltCompiler;
25 private XsltTransformer tocTransformer;
26
27 public TocTransformer() throws ApplicationException {
28 init();
29 }
30
31 private void init() throws ApplicationException {
32 try {
33 processor = new Processor(false);
34 xsltCompiler = processor.newXsltCompiler();
35 URL tocXslUrl = TocTransformer.class.getResource("tocOut.xsl");
36 StreamSource xslStreamSource = new StreamSource(tocXslUrl.openStream());
37 XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource);
38 tocTransformer = xsltExecutable.load();
39 } catch (SaxonApiException e) {
40 throw new ApplicationException(e);
41 } catch (IOException e) {
42 throw new ApplicationException(e);
43 }
44 }
45
46 public String transform(String inputStr, String type, String outputFormat) throws ApplicationException {
47 String result = null;
48 try {
49 StringReader inputStrReader = new StringReader(inputStr);
50 StreamSource xmlDoc = new StreamSource(inputStrReader);
51 Serializer serializer = new Serializer();
52 serializer.setOutputWriter(new StringWriter());
53 serializer.setOutputProperty(Serializer.Property.SAXON_STYLESHEET_VERSION, "2.0");
54 serializer.setOutputProperty(Serializer.Property.METHOD, "xhtml");
55 serializer.setOutputProperty(Serializer.Property.INDENT, "no");
56 serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
57 serializer.setOutputProperty(Serializer.Property.ENCODING, "utf-8");
58 tocTransformer.setSource(xmlDoc);
59 tocTransformer.setDestination(serializer);
60 QName typeQName = new QName("type");
61 XdmValue typeXdmValue = new XdmAtomicValue(type);
62 QName outputFormatQName = new QName("outputFormat");
63 XdmValue outputFormatXdmValue = new XdmAtomicValue(outputFormat);
64 tocTransformer.setParameter(typeQName, typeXdmValue);
65 tocTransformer.setParameter(outputFormatQName, outputFormatXdmValue);
66 tocTransformer.transform();
67 result = serializer.getOutputDestination().toString();
68 } catch (Exception e) {
69 throw new ApplicationException(e);
70 }
71 return result;
72 }
73
74 }