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

Logging & small fixes

parent e8c6469c
No related branches found
No related tags found
No related merge requests found
...@@ -126,7 +126,7 @@ public XsltExecutable load(final URI url) throws Exception { ...@@ -126,7 +126,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.
...@@ -134,7 +134,7 @@ public XsltExecutable load(final URI url) throws Exception { ...@@ -134,7 +134,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
...@@ -150,7 +150,7 @@ protected XsltExecutable getStylesheet(final URI uri, ...@@ -150,7 +150,7 @@ protected XsltExecutable getStylesheet(final URI uri,
final Optional<String> sid, final Optional<String> sid,
final boolean forceLoad) throws SaxonApiException, IOException { final boolean forceLoad) throws SaxonApiException, IOException {
XsltExecutable executable = null; XsltExecutable executable = null;
// (1) try cached version, if it exists // (1) try cached version, if it exists
if (!forceLoad) { if (!forceLoad) {
executable = stylesheets.getIfPresent(uri); executable = stylesheets.getIfPresent(uri);
...@@ -183,7 +183,7 @@ protected XsltExecutable getStylesheet(final URI uri, ...@@ -183,7 +183,7 @@ protected XsltExecutable getStylesheet(final URI uri,
} else { } else {
logger.log(Level.INFO, "Reusing cached stylesheed {0}", uri); logger.log(Level.INFO, "Reusing cached stylesheed {0}", uri);
} }
return executable; return executable;
} }
...@@ -204,6 +204,8 @@ public StreamingOutput get( ...@@ -204,6 +204,8 @@ public StreamingOutput get(
@PathParam("object") final URI uri, @PathParam("object") final URI uri,
@Description("If given, an alternative XSLT stylesheet to use") @Description("If given, an alternative XSLT stylesheet to use")
@QueryParam("stylesheet") final URI xsluri, @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,
...@@ -215,6 +217,7 @@ public StreamingOutput get( ...@@ -215,6 +217,7 @@ public StreamingOutput get(
logger.fine("HTML called for root object: " + uri); logger.fine("HTML called for root object: " + uri);
final HTMLWriter writer = new HTMLWriter(this, uri).stylesheet(xsluri) final HTMLWriter writer = new HTMLWriter(this, uri).stylesheet(xsluri)
.embedded(pi)
.sid(sid).refresh(refreshStylesheet).css(css); .sid(sid).refresh(refreshStylesheet).css(css);
writer.loadSource(); writer.loadSource();
writer.loadStylesheet(); writer.loadStylesheet();
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
...@@ -34,13 +36,14 @@ ...@@ -34,13 +36,14 @@
import net.sf.saxon.s9api.XsltTransformer; import net.sf.saxon.s9api.XsltTransformer;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.FileBackedOutputStream; import com.google.common.io.FileBackedOutputStream;
/** /**
* The essential steps: * The essential steps:
* *
* <ol> * <ol>
* <li>Construct the transformation source. * <li>Construct the transformation source.
* <p> * <p>
...@@ -67,14 +70,16 @@ ...@@ -67,14 +70,16 @@
* </p> * </p>
* </li> * </li>
* </ol> * </ol>
* *
* @author Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de> * @author Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
* *
*/ */
public class HTMLWriter implements StreamingOutput { public class HTMLWriter implements StreamingOutput {
private final HTML service; private final HTML service;
private final static Logger logger = Logger.getLogger(HTMLWriter.class.getCanonicalName());
// Options // Options
private final URI rootURI; private final URI rootURI;
private Optional<URI> stylesheetURI = Optional.absent(); private Optional<URI> stylesheetURI = Optional.absent();
...@@ -101,9 +106,16 @@ private enum SourceType { ...@@ -101,9 +106,16 @@ private enum SourceType {
private XsltTransformer transformer; private XsltTransformer transformer;
private Stopwatch stopwatch;
private String actualStylesheet;
// Constructor and configuration // Constructor and configuration
public HTMLWriter(final HTML service, final URI rootURI) { 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.service = service;
this.rootURI = rootURI; this.rootURI = rootURI;
this.repository = service.repository; this.repository = service.repository;
...@@ -181,6 +193,8 @@ protected HTMLWriter loadSource() throws ObjectNotFoundFault, ...@@ -181,6 +193,8 @@ protected HTMLWriter loadSource() throws ObjectNotFoundFault,
this.source = new StreamSource(content.getInput(), this.source = new StreamSource(content.getInput(),
rootURI.toString()); rootURI.toString());
} }
logger.log(Level.INFO, MessageFormat.format("Fetched source for {0}, type={1}, after {2}", rootURI, sourceType, stopwatch.toString()));
return this; return this;
} }
...@@ -194,27 +208,28 @@ private void detectEmbeddedStylesheet(final InputStream input) { ...@@ -194,27 +208,28 @@ private void detectEmbeddedStylesheet(final InputStream input) {
associatedStylesheet.getSystemId())); associatedStylesheet.getSystemId()));
} catch (final TransformerConfigurationException e) { } catch (final TransformerConfigurationException e) {
// TODO Auto-generated catch block logger.log(Level.WARNING, "Failed to load stylesheet from <?xsl-stylesheet?> declaration", e);
e.printStackTrace();
} catch (final TransformerFactoryConfigurationError e) { } catch (final TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block logger.log(Level.WARNING, "Failed to load stylesheet from <?xsl-stylesheet?> declaration", e);
e.printStackTrace();
} catch (final URISyntaxException e) { } catch (final URISyntaxException e) {
// TODO Auto-generated catch block logger.log(Level.WARNING, "Failed to load stylesheet from <?xsl-stylesheet?> declaration", e);
e.printStackTrace();
} }
} }
private XsltExecutable getStylesheet() throws SaxonApiException, private XsltExecutable getStylesheet() throws SaxonApiException,
IOException { IOException {
if (stylesheetURI.isPresent()) if (stylesheetURI.isPresent()) {
actualStylesheet = stylesheetURI.get().toString() + " (explicit)";
return service.getStylesheet(stylesheetURI.get(), sid, return service.getStylesheet(stylesheetURI.get(), sid,
refreshStylesheet); refreshStylesheet);
else if (associatedStylesheet.isPresent()) } else if (associatedStylesheet.isPresent()) {
actualStylesheet = associatedStylesheet.get().toString() + " (associated)";
return service.getStylesheet(associatedStylesheet.get(), sid, return service.getStylesheet(associatedStylesheet.get(), sid,
refreshStylesheet); refreshStylesheet);
else } else {
actualStylesheet = "(internal)";
return service.getToHtml(); return service.getToHtml();
}
} }
protected HTMLWriter loadStylesheet() throws SaxonApiException, protected HTMLWriter loadStylesheet() throws SaxonApiException,
...@@ -237,7 +252,9 @@ protected HTMLWriter loadStylesheet() throws SaxonApiException, ...@@ -237,7 +252,9 @@ protected HTMLWriter loadStylesheet() throws SaxonApiException,
transformer.setParameter(new QName("cssFile"), new XdmAtomicValue( transformer.setParameter(new QName("cssFile"), new XdmAtomicValue(
css.get())); css.get()));
} }
transformer.setSource(source);
this.transformer = transformer; this.transformer = transformer;
logger.log(Level.INFO, MessageFormat.format("Prepared XSLT stylesheet {1} for {0} after {2}", rootURI, actualStylesheet, stopwatch.toString()));
return this; return this;
} }
...@@ -251,15 +268,19 @@ public void write(final OutputStream out) throws IOException, ...@@ -251,15 +268,19 @@ public void write(final OutputStream out) throws IOException,
if (transformer == null) { if (transformer == null) {
loadStylesheet(); loadStylesheet();
} }
logger.log(Level.INFO, MessageFormat.format("Ready for transformation of {0} after {1}", rootURI, stopwatch.toString()));
transformer transformer
.setDestination(service.xsltProcessor.newSerializer(out)); .setDestination(service.xsltProcessor.newSerializer(out));
transformer.transform(); transformer.transform();
} catch (final Exception e) { } 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, Throwables.propagateIfPossible(e, IOException.class,
WebApplicationException.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();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment