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 ad7f358820f133b10af10f2f348a4fbf47c08b45..7c7f3cd6701babf735df8f55740d890f99d3a6d6 100644 --- a/src/main/java/info/textgrid/services/aggregator/html/HTML.java +++ b/src/main/java/info/textgrid/services/aggregator/html/HTML.java @@ -126,7 +126,7 @@ public XsltExecutable load(final URI url) throws Exception { /** * Returns an appropriate stylesheet for the given URI. - * + * * Basically, we try the following options in order: * <ol> * <li>The stylesheet is cached -> return the cached version. @@ -134,7 +134,7 @@ public XsltExecutable load(final URI url) throws Exception { * <li>The stylesheet is non-public TextGrid internal -> load & do not cache * it. * </ol> - * + * * @param uri * the URI of the stylesheet to load * @param sid @@ -150,7 +150,7 @@ protected XsltExecutable getStylesheet(final URI uri, final Optional<String> sid, final boolean forceLoad) throws SaxonApiException, IOException { XsltExecutable executable = null; - + // (1) try cached version, if it exists if (!forceLoad) { executable = stylesheets.getIfPresent(uri); @@ -183,7 +183,7 @@ protected XsltExecutable getStylesheet(final URI uri, } else { logger.log(Level.INFO, "Reusing cached stylesheed {0}", uri); } - + return executable; } @@ -204,6 +204,8 @@ public StreamingOutput get( @PathParam("object") final URI uri, @Description("If given, an alternative XSLT stylesheet to use") @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("Session ID to access protected resources") @QueryParam("sid") final String sid, @@ -215,6 +217,7 @@ public StreamingOutput get( logger.fine("HTML called for root object: " + uri); final HTMLWriter writer = new HTMLWriter(this, uri).stylesheet(xsluri) + .embedded(pi) .sid(sid).refresh(refreshStylesheet).css(css); writer.loadSource(); writer.loadStylesheet(); diff --git a/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java b/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java index b52d46a46992cc95dbd0db62a7be166f07cf669f..5500393e0fbfcd9a787e56d72be4985fe3d75cd9 100644 --- a/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java +++ b/src/main/java/info/textgrid/services/aggregator/html/HTMLWriter.java @@ -17,6 +17,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.text.MessageFormat; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response.Status; @@ -34,13 +36,14 @@ import net.sf.saxon.s9api.XsltTransformer; import com.google.common.base.Optional; +import com.google.common.base.Stopwatch; import com.google.common.base.Throwables; import com.google.common.io.ByteStreams; import com.google.common.io.FileBackedOutputStream; /** * The essential steps: - * + * * <ol> * <li>Construct the transformation source. * <p> @@ -67,14 +70,16 @@ * </p> * </li> * </ol> - * + * * @author Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de> - * + * */ public class HTMLWriter implements StreamingOutput { private final HTML service; + private final static Logger logger = Logger.getLogger(HTMLWriter.class.getCanonicalName()); + // Options private final URI rootURI; private Optional<URI> stylesheetURI = Optional.absent(); @@ -101,9 +106,16 @@ private enum SourceType { private XsltTransformer transformer; + private Stopwatch stopwatch; + + private String actualStylesheet; + // Constructor and configuration public HTMLWriter(final HTML service, final URI rootURI) { + stopwatch = new Stopwatch(); + stopwatch.start(); + logger.log(Level.INFO, "Starting HTML export for {0}", rootURI); this.service = service; this.rootURI = rootURI; this.repository = service.repository; @@ -181,6 +193,8 @@ protected HTMLWriter loadSource() throws ObjectNotFoundFault, this.source = new StreamSource(content.getInput(), rootURI.toString()); } + logger.log(Level.INFO, MessageFormat.format("Fetched source for {0}, type={1}, after {2}", rootURI, sourceType, stopwatch.toString())); + return this; } @@ -194,27 +208,28 @@ private void detectEmbeddedStylesheet(final InputStream input) { associatedStylesheet.getSystemId())); } catch (final TransformerConfigurationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.log(Level.WARNING, "Failed to load stylesheet from <?xsl-stylesheet?> declaration", e); } catch (final TransformerFactoryConfigurationError e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.log(Level.WARNING, "Failed to load stylesheet from <?xsl-stylesheet?> declaration", e); } catch (final URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.log(Level.WARNING, "Failed to load stylesheet from <?xsl-stylesheet?> declaration", e); } } private XsltExecutable getStylesheet() throws SaxonApiException, IOException { - if (stylesheetURI.isPresent()) + if (stylesheetURI.isPresent()) { + actualStylesheet = stylesheetURI.get().toString() + " (explicit)"; return service.getStylesheet(stylesheetURI.get(), sid, refreshStylesheet); - else if (associatedStylesheet.isPresent()) + } else if (associatedStylesheet.isPresent()) { + actualStylesheet = associatedStylesheet.get().toString() + " (associated)"; return service.getStylesheet(associatedStylesheet.get(), sid, refreshStylesheet); - else + } else { + actualStylesheet = "(internal)"; return service.getToHtml(); + } } protected HTMLWriter loadStylesheet() throws SaxonApiException, @@ -237,7 +252,9 @@ protected HTMLWriter loadStylesheet() throws SaxonApiException, transformer.setParameter(new QName("cssFile"), new XdmAtomicValue( css.get())); } + transformer.setSource(source); this.transformer = transformer; + logger.log(Level.INFO, MessageFormat.format("Prepared XSLT stylesheet {1} for {0} after {2}", rootURI, actualStylesheet, stopwatch.toString())); return this; } @@ -251,15 +268,19 @@ public void write(final OutputStream out) throws IOException, if (transformer == null) { loadStylesheet(); } + logger.log(Level.INFO, MessageFormat.format("Ready for transformation of {0} after {1}", rootURI, stopwatch.toString())); transformer .setDestination(service.xsltProcessor.newSerializer(out)); transformer.transform(); } catch (final Exception e) { + logger.log(Level.SEVERE, MessageFormat.format("Transformation of {0} failed ({2}) after {1}", rootURI, stopwatch.toString(), e.getMessage())); Throwables.propagateIfPossible(e, IOException.class, WebApplicationException.class); + Throwables.propagate(e); } - + logger.log(Level.INFO, MessageFormat.format("Finished and delivered transformation of {0} after {1}", rootURI, stopwatch.toString())); + stopwatch.stop(); } }