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

[HTML] embedded=true only returns the HTML body's contents.

Fails if the stylesheet doesn't output XHTML. The children
of the <body> element are wrapped in a <div class="body">,
no XML declaration is included.
parent 2db8dd02
No related branches found
No related tags found
No related merge requests found
...@@ -50,8 +50,8 @@ ...@@ -50,8 +50,8 @@
@Description("Creates an HTML representation of the given TEI document, or aggregation of TEI documents. This is currently extremely experimental and probably broken.") @Description("Creates an HTML representation of the given TEI document, or aggregation of TEI documents. This is currently extremely experimental and probably broken.")
public class HTML { public class HTML {
private static final String TO_HTML_XSL = "/WEB-INF/stylesheets/db2xhtml.xsl"; private static final String TO_HTML_XSL = "/WEB-INF/stylesheets/db2xhtml.xsl";
private static final String EXTRACT_BODY_XSL = "/WEB-INF/stylesheets/extractbody.xsl";
ITextGridRep repository = TextGridRepProvider.getInstance(); ITextGridRep repository = TextGridRepProvider.getInstance();
...@@ -59,6 +59,8 @@ public class HTML { ...@@ -59,6 +59,8 @@ public class HTML {
.getLogger("info.textgrid.services.aggregator.html.HTML"); .getLogger("info.textgrid.services.aggregator.html.HTML");
private XsltExecutable toHtml; private XsltExecutable toHtml;
private XsltExecutable extractBody;
final Processor xsltProcessor; final Processor xsltProcessor;
@Context @Context
...@@ -83,6 +85,22 @@ XsltExecutable getToHtml() { ...@@ -83,6 +85,22 @@ XsltExecutable getToHtml() {
return toHtml; return toHtml;
} }
XsltExecutable getExtractBody() {
if (extractBody == null) {
try {
URL stylesheet;
stylesheet = servlet.getResource(EXTRACT_BODY_XSL);
extractBody = xsltProcessor.newXsltCompiler().compile(
new StreamSource(stylesheet.toString()));
} catch (final MalformedURLException e) {
throw new IllegalStateException(e);
} catch (final SaxonApiException e) {
throw new IllegalStateException(e);
}
}
return extractBody;
}
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);
...@@ -102,8 +120,7 @@ public void onRemoval( ...@@ -102,8 +120,7 @@ public void onRemoval(
notification.getKey(), notification.getKey(),
notification.getCause())); notification.getCause()));
} }
}) }).build(new CacheLoader<URI, XsltExecutable>() {
.build(new CacheLoader<URI, XsltExecutable>() {
@Override @Override
public XsltExecutable load(final URI url) throws Exception { public XsltExecutable load(final URI url) throws Exception {
...@@ -127,7 +144,7 @@ public XsltExecutable load(final URI url) throws Exception { ...@@ -127,7 +144,7 @@ public XsltExecutable load(final URI url) throws Exception {
/** /**
* Returns an appropriate stylesheet for the given URI. * Returns an appropriate stylesheet for the given URI.
* *
* Basically, we try the following options in order: * Basically, we try the following options in order:
* <ol> * <ol>
* <li>The stylesheet is cached -> return the cached version. * <li>The stylesheet is cached -> return the cached version.
...@@ -135,7 +152,7 @@ public XsltExecutable load(final URI url) throws Exception { ...@@ -135,7 +152,7 @@ public XsltExecutable load(final URI url) throws Exception {
* <li>The stylesheet is non-public TextGrid internal -> load & do not cache * <li>The stylesheet is non-public TextGrid internal -> load & do not cache
* it. * it.
* </ol> * </ol>
* *
* @param uri * @param uri
* the URI of the stylesheet to load * the URI of the stylesheet to load
* @param sid * @param sid
...@@ -148,8 +165,8 @@ public XsltExecutable load(final URI url) throws Exception { ...@@ -148,8 +165,8 @@ public XsltExecutable load(final URI url) throws Exception {
* if saxon fails to compile the stylesheet. * if saxon fails to compile the stylesheet.
*/ */
protected XsltExecutable getStylesheet(final URI uri, protected XsltExecutable getStylesheet(final URI uri,
final Optional<String> sid, final Optional<String> sid, final boolean forceLoad)
final boolean forceLoad) throws SaxonApiException, IOException { throws SaxonApiException, IOException {
XsltExecutable executable = null; XsltExecutable executable = null;
// (1) try cached version, if it exists // (1) try cached version, if it exists
...@@ -201,18 +218,13 @@ private static boolean isPublic(final ObjectType metadata) { ...@@ -201,18 +218,13 @@ private static boolean isPublic(final ObjectType metadata) {
@Path(value = "/{object}") @Path(value = "/{object}")
@Produces(value = "text/xml") @Produces(value = "text/xml")
public Response get( public Response get(
@Description("The TextGrid URI of the TEI document or aggregation to transform") @Description("The TextGrid URI of the TEI document or aggregation to transform") @PathParam("object") final URI uri,
@PathParam("object") final URI uri, @Description("If given, an alternative XSLT stylesheet to use") @QueryParam("stylesheet") final URI xsluri,
@Description("If given, an alternative XSLT stylesheet to use") @Description("If true, check for an <?xsl-stylesheet?> processing instruction in the document to render") @QueryParam("pi") final boolean pi,
@QueryParam("stylesheet") final URI xsluri,
@Description("If true, check for an <?xsl-stylesheet?> processing instruction in the document to render")
@QueryParam("pi") final boolean pi,
@Description("If true and a stylesheet has been given, force reloading the stylesheet, do not cache") @QueryParam("refreshStylesheet") final boolean refreshStylesheet, @Description("If true and a stylesheet has been given, force reloading the stylesheet, do not cache") @QueryParam("refreshStylesheet") final boolean refreshStylesheet,
@Description("Session ID to access protected resources") @Description("Session ID to access protected resources") @QueryParam("sid") final String sid,
@QueryParam("sid") final String sid,
@Description("If true, pass the information the stylesheet that its result will be embedded into some website") @QueryParam("embedded") final boolean embedded, @Description("If true, pass the information the stylesheet that its result will be embedded into some website") @QueryParam("embedded") final boolean embedded,
@Description("URL of the CSS that should be referenced in the HTML that is created") @Description("URL of the CSS that should be referenced in the HTML that is created") @QueryParam("css") final URI css,
@QueryParam("css") final URI css,
@Description("The requested content type. E.g., text/html or text/xml") @QueryParam("mediatype") final String mediaType, @Description("The requested content type. E.g., text/html or text/xml") @QueryParam("mediatype") final String mediaType,
@Context final Request request) throws ObjectNotFoundFault, @Context final Request request) throws ObjectNotFoundFault,
MetadataParseFault, IoFault, AuthFault, MetadataParseFault, IoFault, AuthFault,
......
...@@ -319,8 +319,14 @@ public void write(final OutputStream out) throws IOException, ...@@ -319,8 +319,14 @@ public void write(final OutputStream out) throws IOException,
logger.log(Level.INFO, MessageFormat.format("Ready for transformation of {0} after {1}", rootURI, stopwatch.toString())); logger.log(Level.INFO, MessageFormat.format("Ready for transformation of {0} after {1}", rootURI, stopwatch.toString()));
final Serializer serializer = service.xsltProcessor final Serializer serializer = service.xsltProcessor
.newSerializer(out); .newSerializer(out);
transformer if (embedded) {
.setDestination(serializer); final XsltTransformer extractBody = service.getExtractBody()
.load();
extractBody.setDestination(serializer);
transformer.setDestination(extractBody);
} else {
transformer.setDestination(serializer);
}
transformer.transform(); transformer.transform();
} catch (final Exception e) { } catch (final Exception e) {
......
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/1999/xhtml"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xs"
version="2.0">
<!--
Extracts the contents of the body of an XHTML document and wraps it in a <div class="body">.
Used by the html export when embedded=true is specified.
-->
<xsl:output method="xhtml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="//body">
<div class="body">
<xsl:copy-of select="//body/*"/>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="/*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment