From 2b125c4e039e44bc83cfd4737799f81d283b89a2 Mon Sep 17 00:00:00 2001
From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
Date: Wed, 27 Mar 2013 16:30:13 +0000
Subject: [PATCH] Switched HTML stylesheet to integrated XSLT stuff

git-svn-id: https://develop.sub.uni-goettingen.de/repos/textgrid/trunk/services/aggregator@13870 7c539038-3410-0410-b1ec-0f2a7bf1c452
---
 .../services/aggregator/html/HTML.java        | 38 +++++++++++++------
 .../WEB-INF/stylesheets}/db2xhtml.xsl         |  2 +-
 .../services/aggregator/html/HtmlIT.java      | 31 +++++++++++++++
 3 files changed, 59 insertions(+), 12 deletions(-)
 rename src/main/{resources => webapp/WEB-INF/stylesheets}/db2xhtml.xsl (99%)
 create mode 100644 src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java

diff --git a/src/main/java/info/textgrid/services/aggregator/html/HTML.java b/src/main/java/info/textgrid/services/aggregator/html/HTML.java
index 72e8364..f80df4d 100644
--- a/src/main/java/info/textgrid/services/aggregator/html/HTML.java
+++ b/src/main/java/info/textgrid/services/aggregator/html/HTML.java
@@ -16,17 +16,20 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
 import java.text.MessageFormat;
 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.Status;
 import javax.ws.rs.core.StreamingOutput;
 import javax.xml.transform.stream.StreamSource;
@@ -44,27 +47,40 @@
 @Path("/html")
 public class HTML {
 
-	private static final URL TO_HTML_XSL = HTML.class.getClassLoader()
-			.getResource("db2xhtml.xsl");
+
+	private static final String TO_HTML_XSL = "/WEB-INF/stylesheets/db2xhtml.xsl";
 
 	private ITextGridRep repository = TextGridRepProvider.getInstance();
 
 	final Logger logger = Logger
-			.getLogger("info.textgrid.services.aggregator.EPUB");
+			.getLogger("info.textgrid.services.aggregator.html.HTML");
 
 	private XsltExecutable toHtml;
 	private final Processor xsltProcessor;
 
+	@Context
+	private ServletContext servlet;
+
+	private XsltExecutable getToHtml() {
+		if (toHtml == null) {
+			try {
+				final URL stylesheet = servlet.getResource(TO_HTML_XSL);
+				final XsltCompiler xsltCompiler = xsltProcessor
+						.newXsltCompiler();
+				toHtml = xsltCompiler.compile(new StreamSource(stylesheet
+						.toString()));
+			} catch (final MalformedURLException e) {
+				throw new IllegalStateException(e);
+			} catch (final SaxonApiException e) {
+				throw new IllegalStateException(e);
+			}
+		}
+		return toHtml;
+	}
+
 	public HTML(final ITextGridRep repository) throws IOException {
 		this.repository = repository;
 		xsltProcessor = new Processor(false);
-		final XsltCompiler xsltCompiler = xsltProcessor.newXsltCompiler();
-		try {
-			toHtml = xsltCompiler.compile(new StreamSource(TO_HTML_XSL
-					.openStream()));
-		} catch (final SaxonApiException e) {
-			throw new IllegalStateException(e);
-		}
 	}
 
 	@GET
@@ -105,7 +121,7 @@ public StreamingOutput get(@PathParam("object") final URI uri,
 			tei = repository.getContent(uri, sid);
 		}
 
-		final XsltTransformer transformer = toHtml.load();
+		final XsltTransformer transformer = getToHtml().load();
 		transformer.setSource(new StreamSource(tei));
 		transformer.setParameter(new QName("graphicsURLPattern"),
 				new XdmAtomicValue(repository.getCRUDRestEndpoint()
diff --git a/src/main/resources/db2xhtml.xsl b/src/main/webapp/WEB-INF/stylesheets/db2xhtml.xsl
similarity index 99%
rename from src/main/resources/db2xhtml.xsl
rename to src/main/webapp/WEB-INF/stylesheets/db2xhtml.xsl
index 9545917..9f3149f 100644
--- a/src/main/resources/db2xhtml.xsl
+++ b/src/main/webapp/WEB-INF/stylesheets/db2xhtml.xsl
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tgs="http://www.textgrid.info/namespaces/middleware/tgsearch" exclude-result-prefixes="xs tei tgs xi" xpath-default-namespace="http://www.tei-c.org/ns/1.0" version="2.0">
 
-   <xsl:import href="/usr/share/xml/tei/stylesheet/xhtml2/tei.xsl"/>
+   <xsl:import href="../tei/stylesheet/xhtml2/tei.xsl"/>
 <!--    <xsl:import href="/db/xslt/tei/xhtml2/tei.xsl"/> -->
 
 
diff --git a/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java b/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java
new file mode 100644
index 0000000..a43c553
--- /dev/null
+++ b/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java
@@ -0,0 +1,31 @@
+package info.textgrid.services.aggregator.html;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+import com.google.common.io.ByteStreams;
+import com.google.common.io.NullOutputStream;
+
+public class HtmlIT {
+
+	@Test
+	public void testGet() throws IOException {
+		final URL url = new URL(System.getProperty("service.url")
+				+ "/html/textgrid:jfst.0");
+		final URLConnection connection = url.openConnection();
+		connection.setConnectTimeout(360000);
+		connection.connect();
+		Assert.assertEquals("text/html", connection.getContentType());
+		final InputStream content = connection.getInputStream();
+		final long realLength = ByteStreams.copy(content,
+				new NullOutputStream());
+		Assert.assertTrue("Content length > 0", realLength > 0);
+	}
+
+}
-- 
GitLab