Skip to content
Snippets Groups Projects
Commit 2b125c4e authored by Thorsten Vitt's avatar Thorsten Vitt
Browse files

Switched HTML stylesheet to integrated XSLT stuff

parent a0ecba23
No related branches found
No related tags found
No related merge requests found
...@@ -16,17 +16,20 @@ ...@@ -16,17 +16,20 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput; import javax.ws.rs.core.StreamingOutput;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
...@@ -44,27 +47,40 @@ ...@@ -44,27 +47,40 @@
@Path("/html") @Path("/html")
public class 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(); private ITextGridRep repository = TextGridRepProvider.getInstance();
final Logger logger = Logger final Logger logger = Logger
.getLogger("info.textgrid.services.aggregator.EPUB"); .getLogger("info.textgrid.services.aggregator.html.HTML");
private XsltExecutable toHtml; private XsltExecutable toHtml;
private final Processor xsltProcessor; 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 { public HTML(final ITextGridRep repository) throws IOException {
this.repository = repository; this.repository = repository;
xsltProcessor = new Processor(false); 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 @GET
...@@ -105,7 +121,7 @@ public StreamingOutput get(@PathParam("object") final URI uri, ...@@ -105,7 +121,7 @@ public StreamingOutput get(@PathParam("object") final URI uri,
tei = repository.getContent(uri, sid); tei = repository.getContent(uri, sid);
} }
final XsltTransformer transformer = toHtml.load(); final XsltTransformer transformer = getToHtml().load();
transformer.setSource(new StreamSource(tei)); transformer.setSource(new StreamSource(tei));
transformer.setParameter(new QName("graphicsURLPattern"), transformer.setParameter(new QName("graphicsURLPattern"),
new XdmAtomicValue(repository.getCRUDRestEndpoint() new XdmAtomicValue(repository.getCRUDRestEndpoint()
......
<?xml version="1.0" encoding="UTF-8"?> <?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: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"/> --> <!-- <xsl:import href="/db/xslt/tei/xhtml2/tei.xsl"/> -->
......
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment