Skip to content

Commit

Permalink
Upgraded Saxon dependency from Saxon 9.9.1-7 -> 10.2, including the n…
Browse files Browse the repository at this point in the history
…ecessary code changes. Upgraded Saxon-JS integration to version 2.
  • Loading branch information
Maarten Kroon committed Sep 10, 2020
1 parent 6bf8d24 commit d794bef
Show file tree
Hide file tree
Showing 71 changed files with 5,903 additions and 536 deletions.
2 changes: 1 addition & 1 deletion home/webapps/ROOT/xsl/itworks.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</head>
<body>
<h1>It works!</h1>
<p>Go to the <a href="{/*/req:context-path}/documentation/XSLWeb-3_0-Quick-Start-Guide.pdf">documentation</a> (in PDF)
<p>Go to the <a href="https://armatiek.github.io/xslweb/XSLWeb%20Developer%20Manual.html" target="_blank">documentation</a>
or to the <a href="{/*/req:context-path}/examples">examples</a></p>
<br/>
<p><i>XSLWeb version <xsl:value-of select="$config:version"/></i></p>
Expand Down
8 changes: 0 additions & 8 deletions home/webapps/examples/static/scripts/saxon-js/SaxonJS.min.js

This file was deleted.

4,568 changes: 4,568 additions & 0 deletions home/webapps/examples/static/scripts/saxon-js/SaxonJS2.js

Large diffs are not rendered by default.

958 changes: 958 additions & 0 deletions home/webapps/examples/static/scripts/saxon-js/SaxonJS2.rt.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion home/webapps/examples/xsl/request-dispatcher.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
</pipeline:pipeline>
</xsl:template>

<xsl:template name="books-sef" match="/req:request[req:path = '/books.sef.xml']">
<xsl:template name="books-sef" match="/req:request[req:path = '/books.sef.json']">
<pipeline:pipeline>
<pipeline:stylesheet-export-file name="books-sef" xsl-path="saxon-js/books.xsl" log="true"/>
</pipeline:pipeline>
Expand Down
10 changes: 6 additions & 4 deletions home/webapps/examples/xsl/saxon-js/saxon-js.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
<head>
<title>Book Catalogue</title>
<link href="styles/saxon-js/books.css" rel="stylesheet" type="text/css"/>
<script src="scripts/saxon-js/SaxonJS.min.js" type="text/javascript"></script>
<script src="scripts/saxon-js/SaxonJS2.rt.js" type="text/javascript"></script>
<script>
window.onload = function() {
SaxonJS.transform({
stylesheetLocation: "books.sef.xml",
sourceLocation: "books.xml"
});
stylesheetLocation: "books.sef.json",
sourceLocation: "books.xml",
logLevel: 2
},
"async");
}
</script>
</head>
Expand Down
57 changes: 25 additions & 32 deletions src/main/java/nl/armatiek/xslweb/configuration/WebApp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package nl.armatiek.xslweb.configuration;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand All @@ -16,19 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.armatiek.xslweb.configuration;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -114,6 +111,7 @@
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.UnprefixedElementMatchingPolicy;
import net.sf.saxon.s9api.XQueryCompiler;
import net.sf.saxon.s9api.XQueryExecutable;
import net.sf.saxon.s9api.XdmDestination;
Expand Down Expand Up @@ -144,7 +142,7 @@ public class WebApp implements ErrorHandler {
private Map<String, XQueryExecutable> xqueryExecutableCache = new ConcurrentHashMap<String, XQueryExecutable>();
private Map<String, Templates> templatesCache = new ConcurrentHashMap<String, Templates>();
private Map<String, Schema> schemaCache = new ConcurrentHashMap<String, Schema>();
private Map<String, XdmNode> stylesheetExportFileCache = new ConcurrentHashMap<String, XdmNode>();
private Map<String, byte[]> stylesheetExportFileCache = new ConcurrentHashMap<String, byte[]>();
private Map<String, Collection<Attribute>> attributes = new ConcurrentHashMap<String, Collection<Attribute>>();
private Map<String, ComboPooledDataSource> dataSourceCache = new ConcurrentHashMap<String, ComboPooledDataSource>();
private Map<String, FopFactory> fopFactoryCache = new ConcurrentHashMap<String, FopFactory>();
Expand Down Expand Up @@ -422,7 +420,7 @@ public void executeEvent(final QName templateName) {
return;
}
TransformationErrorListener errorListener = new TransformationErrorListener(null, developmentMode);
XsltExecutable templates = getXsltExecutable("events.xsl", errorListener, false);
XsltExecutable templates = getXsltExecutable("events.xsl", errorListener);
Xslt30Transformer transformer = templates.load30();
SaxonUtils.setMessageEmitter(transformer.getUnderlyingController(), getConfiguration(), errorListener);
transformer.setErrorListener(errorListener);
Expand Down Expand Up @@ -584,16 +582,16 @@ public Processor getProcessor() {
return processor;
}

public XsltExecutable getRequestDispatcherTemplates(ErrorListener errorListener, boolean tracing) throws Exception {
public XsltExecutable getRequestDispatcherTemplates(ErrorListener errorListener) throws Exception {
return tryXsltExecutableCache(new File(getHomeDir(),
Definitions.PATHNAME_REQUESTDISPATCHER_XSL).getAbsolutePath(), errorListener, tracing);
Definitions.PATHNAME_REQUESTDISPATCHER_XSL).getAbsolutePath(), errorListener);
}

public XsltExecutable getXsltExecutable(String path, ErrorListener errorListener, boolean tracing) throws Exception {
public XsltExecutable getXsltExecutable(String path, ErrorListener errorListener) throws Exception {
if (new File(path).isAbsolute()) {
return tryXsltExecutableCache(path, errorListener, tracing);
return tryXsltExecutableCache(path, errorListener);
}
return tryXsltExecutableCache(new File(getHomeDir(), "xsl" + "/" + path).getAbsolutePath(), errorListener, tracing);
return tryXsltExecutableCache(new File(getHomeDir(), "xsl" + "/" + path).getAbsolutePath(), errorListener);
}

public XsltExecutable getIdentityXsltExecutable() {
Expand All @@ -607,11 +605,11 @@ public Templates getTemplates(String path, ErrorListener errorListener) throws E
return tryTemplatesCache(new File(getHomeDir(), "stx" + "/" + path).getAbsolutePath(), errorListener);
}

public XQueryExecutable getQuery(String path, ErrorListener errorListener, boolean tracing) throws Exception {
public XQueryExecutable getQuery(String path, ErrorListener errorListener) throws Exception {
if (new File(path).isAbsolute()) {
return tryQueryCache(path, errorListener, tracing);
return tryQueryCache(path, errorListener);
}
return tryQueryCache(new File(getHomeDir(), "xquery" + "/" + path).getAbsolutePath(), errorListener, tracing);
return tryQueryCache(new File(getHomeDir(), "xquery" + "/" + path).getAbsolutePath(), errorListener);
}

public Schema getSchema(Collection<String> schemaPaths, ErrorListener errorListener) throws Exception {
Expand All @@ -633,11 +631,11 @@ public XsltExecutable getSchematron(String path, String phase, ErrorListener err
return trySchematronCache(new File(getHomeDir(), "sch" + "/" + path).getAbsolutePath(), phase, errorListener);
}

public XdmNode getStylesheetExportFile(String xslPath, ErrorListener errorListener, boolean tracing) throws Exception {
public byte[] getStylesheetExportFile(String xslPath, ErrorListener errorListener) throws Exception {
if (new File(xslPath).isAbsolute()) {
return tryStylesheetExportFile(xslPath, tracing, errorListener);
return tryStylesheetExportFile(xslPath, errorListener);
}
return tryStylesheetExportFile(new File(getHomeDir(), "xsl" + "/" + xslPath).getAbsolutePath(), tracing, errorListener);
return tryStylesheetExportFile(new File(getHomeDir(), "xsl" + "/" + xslPath).getAbsolutePath(), errorListener);
}

public File getStaticFile(String path) {
Expand All @@ -658,8 +656,7 @@ public Resource matchesResource(String path) {
return null;
}

public XsltExecutable tryXsltExecutableCache(String transformationPath,
ErrorListener errorListener, boolean tracing) throws Exception {
public XsltExecutable tryXsltExecutableCache(String transformationPath, ErrorListener errorListener) throws Exception {
String key = FilenameUtils.normalize(transformationPath);
XsltExecutable xsltExecutable = xsltExecutableCache.get(key);
if (xsltExecutable == null) {
Expand All @@ -673,7 +670,6 @@ public XsltExecutable tryXsltExecutableCache(String transformationPath,
XMLReader reader = parser.getXMLReader();
Source source = new SAXSource(reader, new InputSource(transformationPath));
XsltCompiler comp = processor.newXsltCompiler();
comp.setCompileWithTracing(tracing);
comp.setErrorListener(errorListener);
xsltExecutable = comp.compile(source);
} catch (Exception e) {
Expand Down Expand Up @@ -716,16 +712,14 @@ public Templates tryTemplatesCache(String transformationPath,
return templates;
}

public XQueryExecutable tryQueryCache(String xqueryPath,
ErrorListener errorListener, boolean tracing) throws Exception {
public XQueryExecutable tryQueryCache(String xqueryPath, ErrorListener errorListener) throws Exception {
String key = FilenameUtils.normalize(xqueryPath);
XQueryExecutable xquery = xqueryExecutableCache.get(key);
if (xquery == null) {
logger.info("Compiling and caching xquery \"" + xqueryPath + "\" ...");
try {
XQueryCompiler comp = processor.newXQueryCompiler();
comp.setErrorListener(errorListener);
comp.setCompileWithTracing(tracing);
xquery = comp.compile(new File(xqueryPath));
} catch (Exception e) {
logger.error("Could not compile XQuery \"" + xqueryPath + "\"", e);
Expand Down Expand Up @@ -780,13 +774,13 @@ public XsltExecutable trySchematronCache(String schematronPath, String phase,

ErrorListener listener = new TransformationErrorListener(null, developmentMode);

Xslt30Transformer stage1 = tryXsltExecutableCache(new File(schematronDir, "iso_dsdl_include.xsl").getAbsolutePath(), errorListener, false).load30();
Xslt30Transformer stage1 = tryXsltExecutableCache(new File(schematronDir, "iso_dsdl_include.xsl").getAbsolutePath(), errorListener).load30();
stage1.setErrorListener(listener);
SaxonUtils.setMessageEmitter(stage1.getUnderlyingController(), getConfiguration(), errorListener);
Xslt30Transformer stage2 = tryXsltExecutableCache(new File(schematronDir, "iso_abstract_expand.xsl").getAbsolutePath(), errorListener, false).load30();
Xslt30Transformer stage2 = tryXsltExecutableCache(new File(schematronDir, "iso_abstract_expand.xsl").getAbsolutePath(), errorListener).load30();
stage2.setErrorListener(listener);
SaxonUtils.setMessageEmitter(stage2.getUnderlyingController(), getConfiguration(), errorListener);
Xslt30Transformer stage3 = tryXsltExecutableCache(new File(schematronDir, "iso_svrl_for_xslt2.xsl").getAbsolutePath(), errorListener, false).load30();
Xslt30Transformer stage3 = tryXsltExecutableCache(new File(schematronDir, "iso_svrl_for_xslt2.xsl").getAbsolutePath(), errorListener).load30();
stage3.setErrorListener(listener);
SaxonUtils.setMessageEmitter(stage3.getUnderlyingController(), getConfiguration(), errorListener);

Expand Down Expand Up @@ -832,23 +826,22 @@ public XsltExecutable trySchematronCache(String schematronPath, String phase,
return templates;
}

public XdmNode tryStylesheetExportFile(String xslPath, boolean tracing, ErrorListener errorListener) throws Exception {
public byte[] tryStylesheetExportFile(String xslPath, ErrorListener errorListener) throws Exception {
String key = FilenameUtils.normalize(xslPath);
XdmNode sef = stylesheetExportFileCache.get(key);
byte[] sef = stylesheetExportFileCache.get(key);
if (sef == null) {
logger.info("Generating and caching stylesheet export file for \"" + xslPath + "\" ...");
try {
XsltCompiler comp = processor.newXsltCompiler();
comp.setTargetEdition("JS");
comp.setCompileWithTracing(tracing);
comp.setErrorListener(errorListener);
comp.setJustInTimeCompilation(false);
comp.setDefaultElementNamespace(Definitions.NAMESPACEURI_XHTML);
comp.setUnprefixedElementMatchingPolicy(UnprefixedElementMatchingPolicy.DEFAULT_NAMESPACE_OR_NONE);
XsltExecutable exec = comp.compile(new StreamSource(xslPath));
String encoding = exec.getUnderlyingCompiledStylesheet().getOutputProperties().getProperty(OutputKeys.ENCODING);
encoding = (encoding == null) ? StandardCharsets.UTF_8.name() : encoding;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exec.export(baos, "JS");
sef = processor.newDocumentBuilder().build(new StreamSource(new ByteArrayInputStream(baos.toByteArray())));
sef = baos.toByteArray();
} catch (Exception e) {
logger.error("Could not generate stylesheet export file for \"" + xslPath + "\"", e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.ErrorListener;

import org.xml.sax.Attributes;

Expand All @@ -42,8 +43,8 @@ public FopSerializerStep(Attributes atts) {
}

@Override
public Destination getDestination(WebApp webApp, HttpServletRequest req,
HttpServletResponse resp, OutputStream os, Properties outputProperties) throws Exception {
public Destination getDestination(WebApp webApp, HttpServletRequest req, HttpServletResponse resp,
OutputStream os, Properties outputProperties, ErrorListener errorListener) throws Exception {
return new SAXDestination(new FopSerializer(webApp, req, resp, os));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package nl.armatiek.xslweb.pipeline;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand All @@ -16,11 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.armatiek.xslweb.pipeline;

import java.io.File;

import nl.armatiek.xslweb.configuration.Context;
import nl.armatiek.xslweb.configuration.WebApp;

/**
* Pipeline step that performs an identity transformation
Expand All @@ -30,8 +28,8 @@
*/
public class IdentityTransformerStep extends TransformerStep {

public IdentityTransformerStep(WebApp webApp) {
super(webApp, new File(Context.getInstance().getHomeDir(), "common/xsl/system/identity/identity.xsl").getAbsolutePath(), "identity", false);
public IdentityTransformerStep() {
super(new File(Context.getInstance().getHomeDir(), "common/xsl/system/identity/identity.xsl").getAbsolutePath(), "identity", false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.ErrorListener;

import org.xml.sax.Attributes;

Expand Down Expand Up @@ -78,8 +79,8 @@ public void addNamespaceDeclaration(String namespace, String name) {
}

@Override
public Destination getDestination(WebApp webApp, HttpServletRequest req,
HttpServletResponse resp, OutputStream os, Properties outputProperties) throws XMLStreamException {
public Destination getDestination(WebApp webApp, HttpServletRequest req, HttpServletResponse resp,
OutputStream os, Properties outputProperties, ErrorListener errorListener) throws XMLStreamException {
JsonXMLConfig config = new JsonXMLConfigBuilder()
.autoArray(autoArray)
.autoPrimitive(autoPrimitive)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package nl.armatiek.xslweb.pipeline;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand All @@ -16,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.armatiek.xslweb.pipeline;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -53,10 +52,8 @@ public class PipelineHandler implements ContentHandler {
private StringBuilder chars = new StringBuilder();
private Processor processor;
private Configuration conf;
private WebApp webApp;

public PipelineHandler(WebApp webApp) {
this.webApp = webApp;
this.processor = webApp.getProcessor();
this.conf = webApp.getConfiguration();
}
Expand Down Expand Up @@ -159,15 +156,15 @@ public void startElement(String uri, String localName, String qName, Attributes
}
String name = getAttribute(atts, "name", "transformer-" + Integer.toString(pipelineSteps.size()+1));
boolean log = getAttribute(atts, "log", "false").equals("true");
pipelineSteps.add(new TransformerStep(webApp, xslPath, name, log));
pipelineSteps.add(new TransformerStep(xslPath, name, log));
} else if (localName.equals("query")) {
String xqueryPath = getAttribute(atts, "xquery-path", null);
if (StringUtils.isBlank(xqueryPath)) {
throw new SAXException("Query step must have an attribute \"xquery-path\"");
}
String name = getAttribute(atts, "name", "query-" + Integer.toString(pipelineSteps.size()+1));
boolean log = getAttribute(atts, "log", "false").equals("true");
pipelineSteps.add(new QueryStep(webApp, xqueryPath, name, log));
pipelineSteps.add(new QueryStep(xqueryPath, name, log));
} else if (localName.equals("transformer-stx")) {
String stxPath = getAttribute(atts, "stx-path", null);
if (StringUtils.isBlank(stxPath)) {
Expand Down
23 changes: 4 additions & 19 deletions src/main/java/nl/armatiek/xslweb/pipeline/QueryStep.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package nl.armatiek.xslweb.pipeline;

import net.sf.saxon.lib.TraceListener;
import net.sf.saxon.trace.XQueryTraceListener;
import nl.armatiek.xslweb.configuration.WebApp;
import nl.armatiek.xslweb.saxon.trace.XSLWebTimingTraceListener;

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand All @@ -21,32 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.armatiek.xslweb.pipeline;

/**
* Pipeline step that executes an XQuery
*
* @author Maarten Kroon
*/
public class QueryStep extends TraceablePipelineStep {
public class QueryStep extends ParameterizablePipelineStep {

private String xqueryPath;

public QueryStep(WebApp webApp, String xqueryPath, String name, boolean log) {
super(webApp, name, log);
public QueryStep(String xqueryPath, String name, boolean log) {
super(name, log);
this.xqueryPath = xqueryPath;
}

public String getXQueryPath() {
return this.xqueryPath;
}

@Override
protected TraceListener getTraceListenerInternal(TraceType traceType) {
if (traceType.equals(TraceType.BASIC))
return new XQueryTraceListener();
else if (traceType.equals(TraceType.TIMING))
return new XSLWebTimingTraceListener(webApp);
return null;
}

}
Loading

0 comments on commit d794bef

Please sign in to comment.