Skip to content

Commit

Permalink
Merge pull request #86 from ChrisBethgster/fix/3_x_use_templates_inst…
Browse files Browse the repository at this point in the history
…ead_of_transformer

Fix/3 x use templates instead of transformer
  • Loading branch information
tdonohue authored Oct 27, 2023
2 parents 3747282 + 2c79b4b commit 5679c2e
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
import com.lyncode.xoai.dataprovider.data.ItemIdentifier;
import com.lyncode.xoai.dataprovider.filter.conditions.Condition;

import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;

/**
* @version 3.1.0
*/
public class MetadataFormat {
private String prefix;
private Transformer xsltTransformer;
private Templates xsltTemplates;
private String namespace;
private String schemaLocation;
private Condition filter;

public MetadataFormat(String prefix, Transformer transformer, String namespace, String schemaLocation) {
public MetadataFormat(String prefix, Templates xsltTemplates, String namespace, String schemaLocation) {
this.prefix = prefix;
this.xsltTransformer = transformer;
this.xsltTemplates = xsltTemplates;
this.namespace = namespace;
this.schemaLocation = schemaLocation;
}
Expand All @@ -43,8 +43,8 @@ public String getPrefix() {
return prefix;
}

public Transformer getTransformer() {
return xsltTransformer;
public Templates getXsltTemplates() {
return xsltTemplates;
}

public String getNamespace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@

package com.lyncode.xoai.dataprovider.data.internal;

import com.lyncode.xoai.dataprovider.transform.XSLTransformer;
import com.lyncode.xoai.dataprovider.transform.XSLTemplates;

/**
* @version 3.1.0
*/
public class MetadataTransformer {
private XSLTransformer xslTransformer;
private XSLTemplates xslTemplates;

public MetadataTransformer() {
xslTransformer = null;
xslTemplates = null;
}

public MetadataTransformer(XSLTransformer xsltTransformer) {
this.xslTransformer = xsltTransformer;
public MetadataTransformer(XSLTemplates xslTemplates) {
this.xslTemplates = xslTemplates;
}

public boolean hasTransformer() {
return (this.xslTransformer != null);
public boolean hasXslTemplates() {
return (this.xslTemplates != null);
}

public XSLTransformer getXslTransformer() {
return this.xslTransformer;
public XSLTemplates getXslTemplates() {
return this.xslTemplates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -48,17 +48,17 @@ public MetadataFormatManager(ResourceResolver resolver, List<FormatConfiguration
FilterManager filterManager) throws ConfigurationException {
contexts = new HashMap<String, MetadataFormat>();
for (FormatConfiguration format : config) {
Transformer transformer = null;
Templates templates = null;

try {
transformer = resolver.getTransformer(format.getXslt());
templates = resolver.getTemplates(format.getXslt());
} catch (TransformerConfigurationException e) {
throw new ConfigurationException(e.getMessage(), e);
} catch (IOException e) {
throw new ConfigurationException(e.getMessage(), e);
}

MetadataFormat metadataFormat = new MetadataFormat(format.getPrefix(), transformer, format.getNamespace(), format.getSchemaLocation());
MetadataFormat metadataFormat = new MetadataFormat(format.getPrefix(), templates, format.getNamespace(), format.getSchemaLocation());
if (format.hasFilter())
metadataFormat.setFilter(filterManager.getFilter(format.getFilter().getReference()));
contexts.put(format.getId(), metadataFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public GetRecordType handle(OAIParameters parameters) throws OAIException, Handl
if (!itemHelper.getItem().isDeleted()) {
MetadataType metadata = null;
try {
if (context.getTransformer().hasTransformer()) {
if (context.getTransformer().hasXslTemplates()) {
metadata = new MetadataType(itemHelper.toPipeline(true)
.apply(context.getTransformer().getXslTransformer().getValue())
.apply(format.getTransformer())
.apply(context.getTransformer().getXslTemplates().getValue())
.apply(format.getXsltTemplates())
.getTransformed());
} else {
metadata = new MetadataType(itemHelper.toPipeline(true)
.apply(format.getTransformer())
.apply(format.getXsltTemplates())
.getTransformed());
}
} catch (WritingXmlException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ private RecordType createRecord(OAIParameters parameters, Item item)
if (!item.isDeleted()) {
MetadataType metadata = null;
try {
if (context.getTransformer().hasTransformer()) {
if (context.getTransformer().hasXslTemplates()) {
metadata = new MetadataType(itemHelperWrap.toPipeline(true)
.apply(context.getTransformer().getXslTransformer().getValue())
.apply(format.getTransformer())
.apply(context.getTransformer().getXslTemplates().getValue())
.apply(format.getXsltTemplates())
.getTransformed());
} else {
metadata = new MetadataType(itemHelperWrap.toPipeline(true)
.apply(format.getTransformer())
.apply(format.getXsltTemplates())
.getTransformed());
}
} catch (WritingXmlException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.lyncode.xoai.dataprovider.services.api;

import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import java.io.IOException;
import java.io.InputStream;

public interface ResourceResolver {
InputStream getResource(String path) throws IOException;
Transformer getTransformer (String path) throws IOException, TransformerConfigurationException;

Templates getTemplates(String path) throws IOException, TransformerConfigurationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.lyncode.xoai.dataprovider.services.api.ResourceResolver;

import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
Expand All @@ -24,8 +24,9 @@ public InputStream getResource(String path) throws IOException {
return new FileInputStream(new File(basePath, path));
}


@Override
public Transformer getTransformer(String path) throws IOException, TransformerConfigurationException {
return tFactory.newTransformer(new StreamSource(getResource(path)));
public Templates getTemplates(String path) throws IOException, TransformerConfigurationException {
return tFactory.newTemplates(new StreamSource(getResource(path)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public TransformManager(ResourceResolver resolver, List<TransformerConfiguration
for (TransformerConfiguration t : transformerConfigurations) {
try {
_contexts.put(t.getId(),
new MetadataTransformer(new XSLTransformer(
resolver.getTransformer(t.getXslt())
new MetadataTransformer(new XSLTemplates(
resolver.getTemplates(t.getXslt())
))
);
} catch (TransformerConfigurationException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.lyncode.xoai.dataprovider.transform;

import com.lyncode.xoai.util.TinyType;

import javax.xml.transform.Templates;

public class XSLTemplates extends TinyType<Templates> {
public XSLTemplates(Templates templates) {
super(templates);
}
}

This file was deleted.

4 changes: 3 additions & 1 deletion src/main/java/com/lyncode/xoai/util/XSLPipeline.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lyncode.xoai.util;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
Expand All @@ -19,8 +20,9 @@ public XSLPipeline(InputStream inputStream, boolean omitXMLDeclaration) {
this.omitXMLDeclaration = omitXMLDeclaration;
}

public XSLPipeline apply(Transformer xslTransformer) throws TransformerException {
public XSLPipeline apply(Templates xslTemplates) throws TransformerException {
outputStream = new ByteArrayOutputStream();
Transformer xslTransformer = xslTemplates.newTransformer();
xslTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, (omitXMLDeclaration) ? "yes" : "no");
xslTransformer.transform(new StreamSource(inputStream), new StreamResult(outputStream));
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
Expand Down Expand Up @@ -70,7 +71,9 @@ public abstract class AbstractDataProviderTest {

@Before
public void setUp() throws IOException, TransformerConfigurationException, ParseException {
when(resourceResolver.getTransformer(XOAI_XSLT_LOCATION)).thenReturn(tFactory.newTransformer());
when(resourceResolver.getTemplates(XOAI_XSLT_LOCATION)).
thenReturn(tFactory.newTemplates(new StreamSource(
this.getClass().getClassLoader().getResourceAsStream("identity_transform.xsl"))));

configuration = new Configuration().withIndented(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;

public abstract class XmlTest {
Expand All @@ -27,9 +28,10 @@ protected String theOutput() {
return output.toString();
}

protected Transformer identityTransformer() {
protected Templates identityTemplate() {
try {
return tFactory.newTransformer();
return tFactory.newTemplates(new StreamSource(
this.getClass().getClassLoader().getResourceAsStream("identity_transform.xsl")));
} catch (TransformerConfigurationException e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public void shouldGiveTheSameIfNoTransformationIsApplied() throws IOException {
@Test
public void shouldTransformWithXmlDeclarationOnTop() throws TransformerException, IOException {
XSLPipeline underTest = new XSLPipeline(input, false);
underTest.apply(identityTransformer());
underTest.apply(identityTemplate());
assertThat(IOUtils.toString(underTest.getTransformed()), containsString("<?xml"));
}

@Test
public void shouldTransformWithoutXmlDeclarationOnTop() throws TransformerException, IOException {
XSLPipeline underTest = new XSLPipeline(input, true);
underTest.apply(identityTransformer());
underTest.apply(identityTemplate());
assertThat(IOUtils.toString(underTest.getTransformed()), not(containsString("<?xml")));
}
}
7 changes: 7 additions & 0 deletions src/test/resources/identity_transform.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

0 comments on commit 5679c2e

Please sign in to comment.