xslt errors
This commit is contained in:
parent
ab234a5d32
commit
98d8499eec
|
@ -22,16 +22,12 @@ public class XsltTransformFactory {
|
|||
}
|
||||
|
||||
public RecordTransformer<String, String> getTransformer(final String ruleId, final Map<String, Object> initialParams) throws TransformationException {
|
||||
try {
|
||||
final String xsltText =
|
||||
this.clientFactory.getClient(SimpleResourceClient.class)
|
||||
.findResourceContent(SimpleResourceClient.ResourceType.transformation_rule_xslt, ruleId, String.class);
|
||||
final String xsltText =
|
||||
this.clientFactory.getClient(SimpleResourceClient.class)
|
||||
.findResourceContent(SimpleResourceClient.ResourceType.transformation_rule_xslt, ruleId, String.class);
|
||||
|
||||
return XsltUtils.newTransformer(xsltText, initialParams, this.xsltFunctions);
|
||||
return XsltUtils.newTransformer(xsltText, initialParams, this.xsltFunctions);
|
||||
|
||||
} catch (final Throwable e) {
|
||||
throw new TransformationException("error preparing xslt transfomer", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import eu.dnetlib.common.mapping.RecordTransformer;
|
||||
import eu.dnetlib.errors.TransformationException;
|
||||
import net.sf.saxon.s9api.ExtensionFunction;
|
||||
import net.sf.saxon.s9api.Processor;
|
||||
import net.sf.saxon.s9api.QName;
|
||||
|
@ -21,6 +22,7 @@ import net.sf.saxon.s9api.SaxonApiException;
|
|||
import net.sf.saxon.s9api.Serializer;
|
||||
import net.sf.saxon.s9api.XdmAtomicValue;
|
||||
import net.sf.saxon.s9api.XdmNode;
|
||||
import net.sf.saxon.s9api.XmlProcessingError;
|
||||
import net.sf.saxon.s9api.XsltCompiler;
|
||||
import net.sf.saxon.s9api.XsltExecutable;
|
||||
import net.sf.saxon.s9api.XsltTransformer;
|
||||
|
@ -41,43 +43,54 @@ public class XsltUtils {
|
|||
|
||||
public static RecordTransformer<String, String> newTransformer(final String xsltText,
|
||||
final Map<String, Object> params,
|
||||
final List<DnetXsltFunction> xsltFunctions) throws Exception {
|
||||
final List<DnetXsltFunction> xsltFunctions) throws TransformationException {
|
||||
|
||||
final Processor processor = new Processor(false);
|
||||
|
||||
for (final DnetXsltFunction f : xsltFunctions) {
|
||||
final ExtensionFunction extFunction = f.asExtensionFunction();
|
||||
processor.registerExtensionFunction(extFunction);
|
||||
log.info("New XSLT function registered: " + extFunction.getName());
|
||||
}
|
||||
|
||||
final List<XmlProcessingError> errorList = new ArrayList<>();
|
||||
|
||||
final XsltCompiler comp = processor.newXsltCompiler();
|
||||
comp.setErrorList(errorList);
|
||||
params.forEach((k, v) -> comp.setParameter(new QName(k), XdmAtomicValue.makeAtomicValue(v)));
|
||||
|
||||
final XsltExecutable xslt = comp
|
||||
.compile(new StreamSource(IOUtils.toInputStream(xsltText, StandardCharsets.UTF_8)));
|
||||
try {
|
||||
final XsltExecutable xslt = comp
|
||||
.compile(new StreamSource(IOUtils.toInputStream(xsltText, StandardCharsets.UTF_8)));
|
||||
|
||||
return xml -> {
|
||||
try {
|
||||
final XdmNode source = processor
|
||||
.newDocumentBuilder()
|
||||
.build(new StreamSource(IOUtils.toInputStream(xml, StandardCharsets.UTF_8)));
|
||||
return xml -> {
|
||||
try {
|
||||
final XdmNode source = processor
|
||||
.newDocumentBuilder()
|
||||
.build(new StreamSource(IOUtils.toInputStream(xml, StandardCharsets.UTF_8)));
|
||||
|
||||
final XsltTransformer trans = xslt.load();
|
||||
trans.setInitialContextNode(source);
|
||||
final XsltTransformer trans = xslt.load();
|
||||
trans.setInitialContextNode(source);
|
||||
|
||||
final StringWriter output = new StringWriter();
|
||||
final Serializer out = processor.newSerializer(output);
|
||||
out.setOutputProperty(Serializer.Property.METHOD, "xml");
|
||||
out.setOutputProperty(Serializer.Property.INDENT, "yes");
|
||||
final StringWriter output = new StringWriter();
|
||||
final Serializer out = processor.newSerializer(output);
|
||||
out.setOutputProperty(Serializer.Property.METHOD, "xml");
|
||||
out.setOutputProperty(Serializer.Property.INDENT, "yes");
|
||||
|
||||
trans.setDestination(out);
|
||||
trans.transform();
|
||||
trans.setDestination(out);
|
||||
trans.transform();
|
||||
|
||||
return output.toString();
|
||||
} catch (final SaxonApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
return output.toString();
|
||||
} catch (final SaxonApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
} catch (final Throwable e) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
sw.append("XSLT failure");
|
||||
errorList.forEach(err -> sw.append("\n\t[XSLT ERR] " + err.getMessage()));
|
||||
throw new TransformationException(sw.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue