diff --git a/src/main/java/info/textgrid/services/aggregator/pdf/PDF.java b/src/main/java/info/textgrid/services/aggregator/pdf/PDF.java index 267301d9e6a741b920757fcd8ad53081b2c2b36a..c2164a79c4c3fd1041e056b62c5f4983e55613ba 100644 --- a/src/main/java/info/textgrid/services/aggregator/pdf/PDF.java +++ b/src/main/java/info/textgrid/services/aggregator/pdf/PDF.java @@ -14,17 +14,21 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URI; +import java.net.URL; import java.nio.charset.Charset; import java.util.Set; import java.util.logging.Logger; +import javax.servlet.ServletContext; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.xml.stream.XMLStreamException; @@ -49,21 +53,20 @@ "this service is deployed.") public class PDF { - final Logger logger = Logger.getLogger("info.textgrid.services.aggregator"); + private static final String TO_PDF_XSL = "/WEB-INF/tei/stylesheet/latex2/tei.xsl"; + + final Logger logger = Logger + .getLogger("info.textgrid.services.aggregator.pdf"); private XsltExecutable teiToLatex; private final Processor xsltProcessor; private final ITextGridRep repository; + @Context + private ServletContext servlet; + public PDF(final ITextGridRep repository) { this.repository = repository; xsltProcessor = new Processor(false); - final XsltCompiler xsltCompiler = xsltProcessor.newXsltCompiler(); - try { - teiToLatex = xsltCompiler.compile(new StreamSource( - "/usr/share/xml/tei/stylesheet/latex2/tei.xsl")); - } catch (final SaxonApiException e) { - throw new IllegalStateException(e); - } } @GET @@ -108,18 +111,19 @@ public Response get( final String format = supplier.getMetadata().getGeneric() .getProvided().getFormat(); String fileName = null; - if (format.equals("image/jpeg")) + if (format.equals("image/jpeg")) { fileName = missingURI.getSchemeSpecificPart() .replace('.', '-').concat(".jpg"); - else if (format.equals("image/png")) + } else if (format.equals("image/png")) { fileName = missingURI.getSchemeSpecificPart() .replace('.', '-').concat(".png"); - else if (format.equals("image/tiff")) + } else if (format.equals("image/tiff")) { fileName = missingURI.getSchemeSpecificPart() .replace('.', '-').concat(".tif"); - else if (format.equals("application/pdf")) + } else if (format.equals("application/pdf")) { fileName = missingURI.getSchemeSpecificPart() .replace('.', '-').concat(".pdf"); + } if (fileName != null) { mapping.add(missingReference, fileName, null, RewriteMethod.NONE); @@ -136,7 +140,7 @@ else if (format.equals("application/pdf")) buffer.close(); // now generate the TeX - final XsltTransformer transformer = teiToLatex.load(); + final XsltTransformer transformer = getTeiToLatex().load(); final File tex = new File(workingDir, "data.tex"); transformer.setDestination(xsltProcessor.newSerializer(tex)); transformer.setSource(new StreamSource(teiFile)); @@ -178,4 +182,24 @@ else if (format.equals("application/pdf")) } } + public XsltExecutable getTeiToLatex() { + if (teiToLatex == null) { + try { + final URL stylesheet = servlet.getResource(TO_PDF_XSL); + final XsltCompiler xsltCompiler = xsltProcessor + .newXsltCompiler(); + teiToLatex = xsltCompiler.compile(new StreamSource(stylesheet + .toString())); + } catch (final MalformedURLException e) { + throw new IllegalStateException( + "Failed to initialize TEI to LaTeX stylesheet", e); + } catch (final SaxonApiException e) { + throw new IllegalStateException( + "Failed to initialize TEI to LaTeX stylesheet", e); + } + } + return teiToLatex; + } + + }