Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XSLT / Add centralized Saxon configuration #88

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Serializer.Property;
import org.fao.geonet.common.search.domain.UserInfo;
import org.fao.geonet.common.xml.XsltTransformerFactory;
import org.fao.geonet.common.xml.XsltUtil;
import org.fao.geonet.domain.Metadata;
import org.fao.geonet.index.model.gn.IndexRecordFieldNames;
Expand All @@ -34,7 +35,7 @@ public void processResponse(HttpSession httpSession,
InputStream streamFromServer, OutputStream streamToClient,
UserInfo userInfo, String bucket, Boolean addPermissions) throws Exception {

Processor p = new Processor(false);
Processor p = XsltTransformerFactory.getProcessor();
Serializer s = p.newSerializer();
s.setOutputProperty(Property.INDENT, "no");
s.setOutputStream(streamToClient);
Expand Down Expand Up @@ -65,7 +66,9 @@ public void processResponse(HttpSession httpSession,
records.forEach(r -> {
String xsltFileName = String.format(
"xslt/ogcapir/formats/%s/%s-%s.xsl",
transformation, transformation, r.getDataInfo().getSchemaId(), transformation);
transformation,
transformation,
r.getDataInfo().getSchemaId());
try (InputStream xsltFile =
new ClassPathResource(xsltFileName).getInputStream()) {
// if (!xsltFile.exists()) {
Expand All @@ -77,7 +80,8 @@ public void processResponse(HttpSession httpSession,
XsltUtil.transformAndStreamInDocument(
r.getData(),
xsltFile,
generator
generator,
null
);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.sf.saxon.s9api.Serializer.Property;
import org.fao.geonet.common.search.GnMediaType;
import org.fao.geonet.common.search.domain.UserInfo;
import org.fao.geonet.common.xml.XsltTransformerFactory;
import org.fao.geonet.common.xml.XsltUtil;
import org.fao.geonet.domain.Metadata;
import org.fao.geonet.index.model.gn.IndexRecordFieldNames;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void processResponse(HttpSession httpSession,
InputStream streamFromServer, OutputStream streamToClient,
UserInfo userInfo, String bucket, Boolean addPermissions) throws Exception {

Processor p = new Processor(false);
Processor p = XsltTransformerFactory.getProcessor();
Serializer s = p.newSerializer();
s.setOutputProperty(Property.INDENT, "no");
s.setOutputStream(streamToClient);
Expand Down Expand Up @@ -91,7 +92,8 @@ public void processResponse(HttpSession httpSession,
XsltUtil.transformAndStreamInDocument(
r.getData(),
xsltFile,
generator
generator,
null
);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
Expand Down
4 changes: 4 additions & 0 deletions modules/library/common-utility/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.fao.geonet.common.xml;

import javax.xml.transform.stream.StreamSource;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.Configuration;
import net.sf.saxon.TransformerFactoryImpl;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.trans.XPathException;

/**
* Setup Saxon transformer with configuration which register Java function extensions.
*/
@Slf4j(topic = "org.fao.geonet.common")
public class XsltTransformerFactory {

public static final String SAXON_CONFIGURATION_PATH = "/saxon-configuration.xml";
public static final String CHECK_SAXON_CONFIGURATION = "Check saxon configuration. Error is: {}";

private static TransformerFactoryImpl factory;
private static Processor processor;

/**
* Get configured Saxon transformer factory.
*/
public static TransformerFactoryImpl get() {
if (factory != null) {
return factory;
}
try {
factory = new net.sf.saxon.TransformerFactoryImpl(getConfiguration());
} catch (XPathException e) {
log.error(CHECK_SAXON_CONFIGURATION, e.getMessage());
factory = new TransformerFactoryImpl();
}
return factory;

}

/**
* Get configured Saxon processor.
*/
public static Processor getProcessor() {
if (processor != null) {
return processor;
}
try {
processor = new Processor(getConfiguration());
} catch (XPathException e) {
log.error(CHECK_SAXON_CONFIGURATION, e.getMessage());
processor = new Processor(false);
}
return processor;
}

private static Configuration getConfiguration() throws XPathException {
return Configuration
.readConfiguration(new StreamSource(
XsltTransformerFactory.class.getResourceAsStream(SAXON_CONFIGURATION_PATH)));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/**
* (c) 2020 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license,
* available at the root application directory.
* (c) 2020 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/

package org.fao.geonet.common.xml;


import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.util.JAXBResult;
Expand All @@ -20,6 +22,7 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Xslt30Transformer;
import net.sf.saxon.s9api.XsltCompiler;
Expand All @@ -37,7 +40,7 @@ public static <T> T transformXmlToObject(
File xsltFile,
Class<T> objectClass
) {
TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
TransformerFactory factory = XsltTransformerFactory.get();
StreamSource xslt = new StreamSource(xsltFile);
StreamSource text = new StreamSource(new StringReader(inputXmlString));
try {
Expand Down Expand Up @@ -69,18 +72,74 @@ public static <T> T transformXmlToObject(
public static void transformAndStreamInDocument(
String inputXmlString,
InputStream xsltFile,
XMLStreamWriter streamWriter) {
XMLStreamWriter streamWriter,
Map<QName, net.sf.saxon.s9api.XdmValue> xslParameters) {
try {
Processor proc = new Processor(false);
Processor proc = XsltTransformerFactory.getProcessor();
XsltCompiler compiler = proc.newXsltCompiler();

XsltExecutable xsl = compiler.compile(new StreamSource(xsltFile));
Xslt30Transformer transformer = xsl.load30();
if (xslParameters != null) {
transformer.setStylesheetParameters(xslParameters);
}
transformer.transform(
new StreamSource(new StringReader(inputXmlString)),
new XmlStreamWriterDestinationInDocument(streamWriter));
} catch (SaxonApiException e) {
e.printStackTrace();
}
}

/**
* Transform XML string in OutputStream.
*/
public static void transformXmlAsOutputStream(
String inputXmlString,
InputStream xsltFile,
Map<QName, net.sf.saxon.s9api.XdmValue> xslParameters,
OutputStream outputStream) {
try {
Processor proc = XsltTransformerFactory.getProcessor();
XsltCompiler compiler = proc.newXsltCompiler();

XsltExecutable xsl = compiler.compile(new StreamSource(xsltFile));
Xslt30Transformer transformer = xsl.load30();
if (xslParameters != null) {
transformer.setStylesheetParameters(xslParameters);
}
transformer.transform(
new StreamSource(new StringReader(inputXmlString)),
proc.newSerializer(outputStream));
} catch (SaxonApiException e) {
e.printStackTrace();
}
}

/**
* Transform XML string as String.
*/
public static String transformXmlAsString(
String inputXmlString,
InputStream xsltFile,
Map<QName, net.sf.saxon.s9api.XdmValue> xslParameters) {
try {
Processor proc = XsltTransformerFactory.getProcessor();
XsltCompiler compiler = proc.newXsltCompiler();

XsltExecutable xsl = compiler.compile(new StreamSource(xsltFile));
Xslt30Transformer transformer = xsl.load30();
if (xslParameters != null) {
transformer.setStylesheetParameters(xslParameters);
}
StringWriter stringWriter = new StringWriter();
transformer.transform(
new StreamSource(new StringReader(inputXmlString)),
proc.newSerializer(stringWriter));
return stringWriter.toString();
} catch (SaxonApiException e) {
e.printStackTrace();
}
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.fao.geonet.common.xml.xslt.functions;

import java.util.UUID;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.StringValue;

public class UuidFromStringFn extends ExtensionFunctionDefinition {

@Override
public StructuredQName getFunctionQName() {
return new StructuredQName(XslFn.PREFIX, XslFn.URI, "uuidFromString");
}

@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_STRING};
}

@Override
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_STRING;
}

@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
@Override
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
String stringToConvert = arguments[0].head().getStringValue();
return StringValue.makeStringValue(
UUID.nameUUIDFromBytes(stringToConvert.getBytes()).toString()
);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.fao.geonet.common.xml.xslt.functions;

public class XslFn {
public static final String PREFIX = "gn-util";
public static final String URI = "https://geonetwork-opensource.org/xsl-extension";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<configuration xmlns="http://saxon.sf.net/ns/configuration"
edition="HE">
<global lineNumbering="true"
preEvaluateDoc="false"/>
<resources>
<extensionFunction>org.fao.geonet.common.xml.xslt.functions.UuidFromStringFn</extensionFunction>
</resources>
</configuration>