Mercurial > hg > mpdl-group
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/software/mpdl-services/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/transform/XslResourceTransformer.java Tue Nov 27 12:35:19 2012 +0100 @@ -0,0 +1,94 @@ +package de.mpg.mpiwg.berlin.mpdl.cms.transform; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.URL; + +import javax.xml.transform.stream.StreamSource; + +import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; + +import net.sf.saxon.s9api.Processor; +import net.sf.saxon.s9api.QName; +import net.sf.saxon.s9api.SaxonApiException; +import net.sf.saxon.s9api.Serializer; +import net.sf.saxon.s9api.XdmValue; +import net.sf.saxon.s9api.XsltCompiler; +import net.sf.saxon.s9api.XsltExecutable; +import net.sf.saxon.s9api.XsltTransformer; + +public class XslResourceTransformer { + private Processor processor; + private XsltCompiler xsltCompiler; + private Serializer serializer; + private XsltTransformer xsltTransformer; + + public XslResourceTransformer(String resourceName) throws ApplicationException { + init(resourceName); + } + + private void init(String resourceName) throws ApplicationException { + try { + processor = new Processor(false); + xsltCompiler = processor.newXsltCompiler(); + URL xslUrl = XslResourceTransformer.class.getResource(resourceName); + StreamSource xslStreamSource = new StreamSource(xslUrl.openStream()); + XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource); + xsltTransformer = xsltExecutable.load(); + } catch (SaxonApiException e) { + throw new ApplicationException(e); + } catch (IOException e) { + throw new ApplicationException(e); + } + } + + public String transform(String xmlFileName) throws ApplicationException { + String result = null; + try { + StreamSource xmlDoc = new StreamSource(xmlFileName); + serializer = new Serializer(); + serializer.setOutputWriter(new StringWriter()); + xsltTransformer.setSource(xmlDoc); // needs some time for bigger documents + xsltTransformer.setDestination(serializer); + xsltTransformer.transform(); // needs some time for bigger documents + result = serializer.getOutputDestination().toString(); + } catch (Exception e) { + throw new ApplicationException(e); + } + return result; + } + + public String transformStr(String xmlStr) throws ApplicationException { + String retStr = null; + try { + StringReader inputStrReader = new StringReader(xmlStr); + StreamSource xmlDoc = new StreamSource(inputStrReader); + serializer = new Serializer(); + serializer.setOutputWriter(new StringWriter()); + xsltTransformer.setSource(xmlDoc); // needs some time for bigger documents + xsltTransformer.setDestination(serializer); + xsltTransformer.transform(); // needs some time for bigger documents + retStr = serializer.getOutputDestination().toString(); + } catch (Exception e) { + throw new ApplicationException(e); + } + return retStr; + } + + public void setParameter(QName name, XdmValue value) throws ApplicationException { + try { + xsltTransformer.setParameter(name, value); + } catch (Exception e) { + throw new ApplicationException(e); + } + } + + public void setOutputProperty(Serializer.Property property, String value) throws ApplicationException { + try { + serializer.setOutputProperty(property, value); + } catch (Exception e) { + throw new ApplicationException(e); + } + } +}