9043 use Templates for compiled XSLT instead of Transformer - use Templates are thread-safe and NOT Transformer

This commit is contained in:
Christian Bethge
2023-08-29 15:28:21 +02:00
parent c7c30227ec
commit 1160341cb2
3 changed files with 5 additions and 6 deletions

View File

@@ -15,7 +15,7 @@
<properties> <properties>
<!-- This is the path to the root [dspace-src] directory. --> <!-- This is the path to the root [dspace-src] directory. -->
<root.basedir>${basedir}/..</root.basedir> <root.basedir>${basedir}/..</root.basedir>
<xoai.version>3.3.0</xoai.version> <xoai.version>3.3.1-SNAPSHOT</xoai.version>
<jtwig.version>5.87.0.RELEASE</jtwig.version> <jtwig.version>5.87.0.RELEASE</jtwig.version>
</properties> </properties>

View File

@@ -12,7 +12,7 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer; import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
@@ -40,8 +40,7 @@ public class DSpaceResourceResolver implements ResourceResolver {
} }
@Override @Override
public Transformer getTransformer(String path) throws IOException, public Templates getTemplates(String path) throws IOException, TransformerConfigurationException {
TransformerConfigurationException {
// construct a Source that reads from an InputStream // construct a Source that reads from an InputStream
Source mySrc = new StreamSource(getResource(path)); Source mySrc = new StreamSource(getResource(path));
// specify a system ID (the path to the XSLT-file on the filesystem) // specify a system ID (the path to the XSLT-file on the filesystem)
@@ -49,6 +48,6 @@ public class DSpaceResourceResolver implements ResourceResolver {
// XSLT-files (like <xsl:import href="utils.xsl"/>) // XSLT-files (like <xsl:import href="utils.xsl"/>)
String systemId = basePath + "/" + path; String systemId = basePath + "/" + path;
mySrc.setSystemId(systemId); mySrc.setSystemId(systemId);
return transformerFactory.newTransformer(mySrc); return transformerFactory.newTemplates(mySrc);
} }
} }

View File

@@ -29,7 +29,7 @@ public class PipelineTest {
InputStream input = PipelineTest.class.getClassLoader().getResourceAsStream("item.xml"); InputStream input = PipelineTest.class.getClassLoader().getResourceAsStream("item.xml");
InputStream xslt = PipelineTest.class.getClassLoader().getResourceAsStream("oai_dc.xsl"); InputStream xslt = PipelineTest.class.getClassLoader().getResourceAsStream("oai_dc.xsl");
String output = FileUtils.readAllText(new XSLPipeline(input, true) String output = FileUtils.readAllText(new XSLPipeline(input, true)
.apply(factory.newTransformer(new StreamSource(xslt))) .apply(factory.newTemplates(new StreamSource(xslt)))
.getTransformed()); .getTransformed());
assertThat(output, oai_dc().withXPath("/oai_dc:dc/dc:title", equalTo("Teste"))); assertThat(output, oai_dc().withXPath("/oai_dc:dc/dc:title", equalTo("Teste")));