Mercurial > hg > mpdl-group
view 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 |
line wrap: on
line source
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.XdmAtomicValue; 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 TocTransformer { private Processor processor; private XsltCompiler xsltCompiler; private XsltTransformer tocTransformer; public TocTransformer() throws ApplicationException { init(); } private void init() throws ApplicationException { try { processor = new Processor(false); xsltCompiler = processor.newXsltCompiler(); URL tocXslUrl = TocTransformer.class.getResource("tocOut.xsl"); StreamSource xslStreamSource = new StreamSource(tocXslUrl.openStream()); XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource); tocTransformer = xsltExecutable.load(); } catch (SaxonApiException e) { throw new ApplicationException(e); } catch (IOException e) { throw new ApplicationException(e); } } public String transform(String inputStr, String type, String outputFormat) throws ApplicationException { String result = null; try { StringReader inputStrReader = new StringReader(inputStr); StreamSource xmlDoc = new StreamSource(inputStrReader); Serializer serializer = new Serializer(); serializer.setOutputWriter(new StringWriter()); serializer.setOutputProperty(Serializer.Property.SAXON_STYLESHEET_VERSION, "2.0"); serializer.setOutputProperty(Serializer.Property.METHOD, "xhtml"); serializer.setOutputProperty(Serializer.Property.INDENT, "no"); serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(Serializer.Property.ENCODING, "utf-8"); tocTransformer.setSource(xmlDoc); tocTransformer.setDestination(serializer); QName typeQName = new QName("type"); XdmValue typeXdmValue = new XdmAtomicValue(type); QName outputFormatQName = new QName("outputFormat"); XdmValue outputFormatXdmValue = new XdmAtomicValue(outputFormat); tocTransformer.setParameter(typeQName, typeXdmValue); tocTransformer.setParameter(outputFormatQName, outputFormatXdmValue); tocTransformer.transform(); result = serializer.getOutputDestination().toString(); } catch (Exception e) { throw new ApplicationException(e); } return result; } }