Mercurial > hg > mpdl-group
comparison software/mpdl-services/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/transform/XslResourceTransformer.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.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 XslResourceTransformer { | |
22 private Processor processor; | |
23 private XsltCompiler xsltCompiler; | |
24 private Serializer serializer; | |
25 private XsltTransformer xsltTransformer; | |
26 | |
27 public XslResourceTransformer(String resourceName) throws ApplicationException { | |
28 init(resourceName); | |
29 } | |
30 | |
31 private void init(String resourceName) throws ApplicationException { | |
32 try { | |
33 processor = new Processor(false); | |
34 xsltCompiler = processor.newXsltCompiler(); | |
35 URL xslUrl = XslResourceTransformer.class.getResource(resourceName); | |
36 StreamSource xslStreamSource = new StreamSource(xslUrl.openStream()); | |
37 XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource); | |
38 xsltTransformer = 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 xmlFileName) throws ApplicationException { | |
47 String result = null; | |
48 try { | |
49 StreamSource xmlDoc = new StreamSource(xmlFileName); | |
50 serializer = new Serializer(); | |
51 serializer.setOutputWriter(new StringWriter()); | |
52 xsltTransformer.setSource(xmlDoc); // needs some time for bigger documents | |
53 xsltTransformer.setDestination(serializer); | |
54 xsltTransformer.transform(); // needs some time for bigger documents | |
55 result = serializer.getOutputDestination().toString(); | |
56 } catch (Exception e) { | |
57 throw new ApplicationException(e); | |
58 } | |
59 return result; | |
60 } | |
61 | |
62 public String transformStr(String xmlStr) throws ApplicationException { | |
63 String retStr = null; | |
64 try { | |
65 StringReader inputStrReader = new StringReader(xmlStr); | |
66 StreamSource xmlDoc = new StreamSource(inputStrReader); | |
67 serializer = new Serializer(); | |
68 serializer.setOutputWriter(new StringWriter()); | |
69 xsltTransformer.setSource(xmlDoc); // needs some time for bigger documents | |
70 xsltTransformer.setDestination(serializer); | |
71 xsltTransformer.transform(); // needs some time for bigger documents | |
72 retStr = serializer.getOutputDestination().toString(); | |
73 } catch (Exception e) { | |
74 throw new ApplicationException(e); | |
75 } | |
76 return retStr; | |
77 } | |
78 | |
79 public void setParameter(QName name, XdmValue value) throws ApplicationException { | |
80 try { | |
81 xsltTransformer.setParameter(name, value); | |
82 } catch (Exception e) { | |
83 throw new ApplicationException(e); | |
84 } | |
85 } | |
86 | |
87 public void setOutputProperty(Serializer.Property property, String value) throws ApplicationException { | |
88 try { | |
89 serializer.setOutputProperty(property, value); | |
90 } catch (Exception e) { | |
91 throw new ApplicationException(e); | |
92 } | |
93 } | |
94 } |