This is an automated email from the ASF dual-hosted git repository.
hansva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new 7d821ddbfc Fix#9172 XSLT action: persist settings, unique output
name, and missing-XSL log (#9212)
7d821ddbfc is described below
commit 7d821ddbfc099c9f4f44423d6ab2a4796af35e8b
Author: Lance <[email protected]>
AuthorDate: Tue Nov 3 17:44:03 2026 +0800
Fix#9172 XSLT action: persist settings, unique output name, and
missing-XSL log (#9212)
* Fix XSLT action: persist settings, unique output name, and missing-XSL log
Signed-off-by: leehaut <[email protected]>
* Fix keep shared zip/jar VFS open, legacy iffileexists
Signed-off-by: leehaut <[email protected]>
---------
Signed-off-by: leehaut <[email protected]>
---
.../org/apache/hop/core/xml/HopVfsUriResolver.java | 44 ++-
.../apache/hop/workflow/actions/xml/xslt/Xslt.java | 356 ++++++++++-----------
.../hop/workflow/actions/xml/xslt/XsltDialog.java | 59 ++--
.../workflow/actions/xml/xslt/XsltCheckTest.java | 6 +-
.../xml/xslt/XsltRelativeReferencesTest.java | 38 ++-
.../hop/workflow/actions/xml/xslt/XsltTest.java | 189 +++++++++++
.../actions/xml/src/test/resources/xslt-action.xml | 39 +++
7 files changed, 490 insertions(+), 241 deletions(-)
diff --git a/core/src/main/java/org/apache/hop/core/xml/HopVfsUriResolver.java
b/core/src/main/java/org/apache/hop/core/xml/HopVfsUriResolver.java
index 704273f976..229edd1d42 100644
--- a/core/src/main/java/org/apache/hop/core/xml/HopVfsUriResolver.java
+++ b/core/src/main/java/org/apache/hop/core/xml/HopVfsUriResolver.java
@@ -65,10 +65,21 @@ public class HopVfsUriResolver implements URIResolver {
return isUrl(href) ? null : source(HopVfs.getFileObject(href,
variables));
}
if (!Utils.isEmpty(base)) {
- FileObject parent = HopVfs.getFileObject(base, variables).getParent();
- FileObject file = parent == null ? null : parent.resolveFile(href);
- if (file != null && file.exists()) {
- return source(file);
+ FileObject baseFile = null;
+ FileObject file = null;
+ try {
+ baseFile = HopVfs.getFileObject(base, variables);
+ FileObject parent = baseFile.getParent();
+ file = parent == null ? null : parent.resolveFile(href);
+ if (file != null && file.exists()) {
+ // source() closes file; skip the finally close for it
+ Source resolved = source(file);
+ file = null;
+ return resolved;
+ }
+ } finally {
+ closeQuietly(file);
+ closeQuietly(baseFile);
}
}
} catch (Exception e) {
@@ -91,12 +102,27 @@ public class HopVfsUriResolver implements URIResolver {
* other file system is read through Hop VFS.
*/
private static Source source(FileObject file) throws Exception {
- String uri = file.getName().getURI();
- if ("file".equalsIgnoreCase(file.getName().getScheme())) {
- return new StreamSource(uri);
+ try {
+ String uri = file.getName().getURI();
+ if ("file".equalsIgnoreCase(file.getName().getScheme())) {
+ return new StreamSource(uri);
+ }
+ try (InputStream inputStream = HopVfs.getInputStream(file)) {
+ return new StreamSource(new
ByteArrayInputStream(inputStream.readAllBytes()), uri);
+ }
+ } finally {
+ closeQuietly(file);
}
- try (InputStream inputStream = HopVfs.getInputStream(file)) {
- return new StreamSource(new
ByteArrayInputStream(inputStream.readAllBytes()), uri);
+ }
+
+ private static void closeQuietly(FileObject file) {
+ if (file == null) {
+ return;
+ }
+ try {
+ file.close();
+ } catch (Exception ignored) {
+ // ignore close failures
}
}
diff --git
a/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/Xslt.java
b/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/Xslt.java
index 8989df8a4b..07a890913e 100644
---
a/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/Xslt.java
+++
b/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/Xslt.java
@@ -26,6 +26,7 @@ import static
org.apache.hop.workflow.action.validator.AndValidator.putValidator
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.xml.transform.Source;
@@ -34,9 +35,11 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
+import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.vfs2.FileObject;
+import org.apache.hop.core.Const;
import org.apache.hop.core.ICheckResult;
import org.apache.hop.core.Result;
import org.apache.hop.core.ResultFile;
@@ -49,9 +52,11 @@ import org.apache.hop.core.util.Utils;
import org.apache.hop.core.variables.IVariables;
import org.apache.hop.core.vfs.HopVfs;
import org.apache.hop.core.xml.HopVfsUriResolver;
+import org.apache.hop.core.xml.XmlHandler;
import org.apache.hop.i18n.BaseMessages;
import org.apache.hop.metadata.api.HopMetadataProperty;
import org.apache.hop.metadata.api.IHopMetadataProvider;
+import org.apache.hop.metadata.serializer.xml.ILegacyXml;
import org.apache.hop.resource.ResourceEntry;
import org.apache.hop.resource.ResourceReference;
import org.apache.hop.workflow.WorkflowMeta;
@@ -59,10 +64,11 @@ import org.apache.hop.workflow.action.ActionBase;
import org.apache.hop.workflow.action.IAction;
import org.apache.hop.workflow.action.validator.ValidatorContext;
import org.apache.hop.workflow.engine.IWorkflowEngine;
+import org.w3c.dom.Node;
+/** This defines a 'xslt' job entry. */
@Getter
@Setter
-/** This defines a 'xslt' job entry. */
@Action(
id = "XSLT",
name = "i18n::XSLT.Name",
@@ -71,7 +77,7 @@ import org.apache.hop.workflow.engine.IWorkflowEngine;
categoryDescription = "i18n::XSLT.Category",
keywords = "i18n::Xslt.keyword",
documentationUrl = "/workflow/actions/xslt.html")
-public class Xslt extends ActionBase implements Cloneable, IAction {
+public class Xslt extends ActionBase implements Cloneable, IAction, ILegacyXml
{
private static final Class<?> PKG = Xslt.class;
public static final String FACTORY_JAXP = "JAXP";
@@ -81,123 +87,113 @@ public class Xslt extends ActionBase implements
Cloneable, IAction {
public static final String CONST_ACTION_XSLT_OUPUT_FILE_EXISTS_2_LABEL =
"ActionXSLT.OuputFileExists2.Label";
- @HopMetadataProperty private String xmlfilename;
-
- @HopMetadataProperty private String xslfilename;
+ @HopMetadataProperty(key = "xmlfilename")
+ private String xmlFilename;
- @HopMetadataProperty private String outputfilename;
+ @HopMetadataProperty(key = "xslfilename")
+ private String xslFilename;
- @HopMetadataProperty public int ifFileExists;
+ @HopMetadataProperty(key = "outputfilename")
+ private String outputFilename;
- private boolean addfiletoresult;
+ @HopMetadataProperty private int ifFileExists;
- private String xsltfactory;
- private boolean filenamesfromprevious;
+ @HopMetadataProperty(key = "addfiletoresult")
+ private boolean addFileToResult;
- /** output property name */
- private String[] outputPropertyName;
+ /** Stored under the legacy XML tag {@code xsltfactory}. */
+ @HopMetadataProperty(key = "xsltfactory")
+ private String transformerFactory;
- /** output property value */
- private String[] outputPropertyValue;
+ @HopMetadataProperty(key = "filenamesfromprevious")
+ private boolean filenamesFromPrevious;
- /** parameter name */
- private String[] parameterName;
+ @HopMetadataProperty(key = "parameter", groupKey = "parameters")
+ private List<Parameter> parameters;
- /** parameter field */
- private String[] parameterField;
+ @HopMetadataProperty(key = "outputproperty", groupKey = "outputproperties")
+ private List<OutputProperty> outputPropertyList;
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
private int nrParams;
+
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
private String[] nameOfParams;
+
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
private String[] valueOfParams;
+
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
private boolean useParameters;
- private Properties outputProperties;
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
+ private Properties transformerOutputProperties;
+
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
private boolean setOutputProperties;
public Xslt(String n) {
super(n, "");
- xmlfilename = null;
- xslfilename = null;
- outputfilename = null;
+ xmlFilename = null;
+ xslFilename = null;
+ outputFilename = null;
ifFileExists = 1;
- addfiletoresult = false;
- filenamesfromprevious = false;
- xsltfactory = FACTORY_JAXP;
- int nrparams = 0;
- int nroutputproperties = 0;
- allocate(nrparams, nroutputproperties);
-
- for (int i = 0; i < nrparams; i++) {
- parameterField[i] = "param" + i;
- parameterName[i] = "param";
- }
- for (int i = 0; i < nroutputproperties; i++) {
- outputPropertyName[i] = "outputprop" + i;
- outputPropertyValue[i] = "outputprop";
- }
- }
-
- public void allocate(int nrParameters, int outputProps) {
- parameterName = new String[nrParameters];
- parameterField = new String[nrParameters];
-
- outputPropertyName = new String[outputProps];
- outputPropertyValue = new String[outputProps];
+ addFileToResult = false;
+ filenamesFromPrevious = false;
+ transformerFactory = FACTORY_JAXP;
+ parameters = new ArrayList<>();
+ outputPropertyList = new ArrayList<>();
}
public Xslt() {
this("");
}
+ /**
+ * Before the XML cleanup (57bd0dcdab), {@code getXml()} / {@code loadXml()}
used the tag {@code
+ * iffileexists}. Annotation serialization writes {@code ifFileExists};
workflows saved before the
+ * cleanup still carry the old tag and would otherwise fall back to the
default (1, do nothing).
+ */
+ @Override
+ public void convertLegacyXml(Node node) {
+ if (XmlHandler.getTagValue(node, "ifFileExists") == null) {
+ String legacy = XmlHandler.getTagValue(node, "iffileexists");
+ if (legacy != null) {
+ ifFileExists = Const.toInt(legacy, 1);
+ }
+ }
+ }
+
@Override
public Object clone() {
Xslt je = (Xslt) super.clone();
- int nrparams = parameterName.length;
- int nroutputprops = outputPropertyName.length;
- je.allocate(nrparams, nroutputprops);
-
- for (int i = 0; i < nrparams; i++) {
- je.parameterName[i] = parameterName[i];
- je.parameterField[i] = parameterField[i];
+ je.parameters = new ArrayList<>();
+ for (Parameter parameter : parameters) {
+ je.parameters.add(new Parameter(parameter));
}
- for (int i = 0; i < nroutputprops; i++) {
- je.outputPropertyName[i] = outputPropertyName[i];
- je.outputPropertyValue[i] = outputPropertyValue[i];
+ je.outputPropertyList = new ArrayList<>();
+ for (OutputProperty outputProperty : outputPropertyList) {
+ je.outputPropertyList.add(new OutputProperty(outputProperty));
}
-
return je;
}
- public String getXSLTFactory() {
- return xsltfactory;
- }
-
- public void setXSLTFactory(String xsltfactoryin) {
- xsltfactory = xsltfactoryin;
- }
-
- public void setFilenamesFromPrevious(boolean filenamesfromprevious) {
- this.filenamesfromprevious = filenamesfromprevious;
+ public String getRealXmlFilename() {
+ return resolve(xmlFilename);
}
- public String getRealxmlfilename() {
- return resolve(getXmlfilename());
+ public String getRealXslFilename() {
+ return resolve(xslFilename);
}
- public String getoutputfilename() {
- return resolve(getOutputfilename());
- }
-
- public String getOutputfilename() {
- return outputfilename;
- }
-
- public String getRealxslfilename() {
- return resolve(getXslfilename());
- }
-
- public boolean isFilenamesFromPrevious() {
- return filenamesfromprevious;
+ public String getRealOutputFilename() {
+ return resolve(outputFilename);
}
@Override
@@ -207,23 +203,26 @@ public class Xslt extends ActionBase implements
Cloneable, IAction {
int nrSuccess = 0;
// Check output parameters
- int nrOutputProps = getOutputPropertyName() == null ? 0 :
getOutputPropertyName().length;
+ int nrOutputProps = outputPropertyList == null ? 0 :
outputPropertyList.size();
if (nrOutputProps > 0) {
- outputProperties = new Properties();
+ transformerOutputProperties = new Properties();
for (int i = 0; i < nrOutputProps; i++) {
- outputProperties.put(getOutputPropertyName()[i],
resolve(getOutputPropertyValue()[i]));
+ OutputProperty outputProperty = outputPropertyList.get(i);
+ transformerOutputProperties.put(
+ outputProperty.getName(), resolve(outputProperty.getValue()));
}
setOutputProperties = true;
}
// Check parameters
- nrParams = getParameterField() == null ? 0 : getParameterField().length;
+ nrParams = parameters == null ? 0 : parameters.size();
if (nrParams > 0) {
nameOfParams = new String[nrParams];
valueOfParams = new String[nrParams];
for (int i = 0; i < nrParams; i++) {
- String name = resolve(getParameterName()[i]);
- String value = resolve(getParameterField()[i]);
+ Parameter parameter = parameters.get(i);
+ String name = resolve(parameter.getName());
+ String value = resolve(parameter.getField());
if (Utils.isEmpty(value)) {
throw new HopTransformException(
BaseMessages.getString(PKG,
"Xslt.Exception.ParameterFieldMissing", name, i));
@@ -272,14 +271,14 @@ public class Xslt extends ActionBase implements
Cloneable, IAction {
}
}
} else {
- String realxmlfilename = getRealxmlfilename();
- String realxslfilename = getRealxslfilename();
- String realoutputfilename = getoutputfilename();
- if (!Utils.isEmpty(realxmlfilename)
- && !Utils.isEmpty(realxslfilename)
- && !Utils.isEmpty(realoutputfilename)) {
+ String realXmlFilename = getRealXmlFilename();
+ String realXslFilename = getRealXslFilename();
+ String realOutputFilename = getRealOutputFilename();
+ if (!Utils.isEmpty(realXmlFilename)
+ && !Utils.isEmpty(realXslFilename)
+ && !Utils.isEmpty(realOutputFilename)) {
if (processOneXMLFile(
- realxmlfilename, realxslfilename, realoutputfilename, result,
parentWorkflow)) {
+ realXmlFilename, realXslFilename, realOutputFilename, result,
parentWorkflow)) {
nrSuccess++;
} else {
nrErrors++;
@@ -353,6 +352,14 @@ public class Xslt extends ActionBase implements Cloneable,
IAction {
// did not find wildcard
outputfilename = outputfilename + "_" +
StringUtil.getFormattedDateTimeNow(true);
}
+ // Re-resolve so the transformation writes to the unique name, not
the original file
+ FileObject previousOutput = outputfile;
+ outputfile = HopVfs.getFileObject(outputfilename);
+ try {
+ previousOutput.close();
+ } catch (IOException e) {
+ logError("Unable to close file", e);
+ }
if (isDebug()) {
logDebug(
BaseMessages.getString(PKG,
CONST_ACTION_XSLT_OUPUT_FILE_EXISTS_1_LABEL)
@@ -368,7 +375,7 @@ public class Xslt extends ActionBase implements Cloneable,
IAction {
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInstance();
- if (xsltfactory.equals(FACTORY_SAXON)) {
+ if (FACTORY_SAXON.equals(transformerFactory)) {
// Set the TransformerFactory to the SAXON implementation.
factory = new net.sf.saxon.TransformerFactoryImpl();
}
@@ -404,7 +411,7 @@ public class Xslt extends ActionBase implements Cloneable,
IAction {
// Do we need to set output properties?
if (setOutputProperties) {
- xformer.setOutputProperties(outputProperties);
+ xformer.setOutputProperties(transformerOutputProperties);
}
// Do we need to pass parameters?
@@ -422,7 +429,7 @@ public class Xslt extends ActionBase implements Cloneable,
IAction {
// Apply the xsl file to the source file and write the result to
the output file
xformer.transform(source, resultat);
- if (isAddfiletoresult()) {
+ if (isAddFileToResult()) {
// Add output filename to output files
ResultFile resultFile =
new ResultFile(
@@ -466,7 +473,7 @@ public class Xslt extends ActionBase implements Cloneable,
IAction {
if (!xslfile.exists()) {
logError(
BaseMessages.getString(PKG, "ActionXSLT.FileDoesNotExist1.Label")
- + xmlfilename
+ + xslfilename
+ BaseMessages.getString(PKG,
"ActionXSLT.FileDoesNotExist2.Label"));
}
}
@@ -481,20 +488,7 @@ public class Xslt extends ActionBase implements Cloneable,
IAction {
+ BaseMessages.getString(PKG, "ActionXSLT.ErrorXLSTXSL2.Label")
+ e.getMessage());
} finally {
- try {
- if (xmlfile != null) {
- xmlfile.close();
- }
-
- if (xslfile != null) {
- xslfile.close();
- }
- if (outputfile != null) {
- outputfile.close();
- }
- } catch (IOException e) {
- logError("Unable to close file", e);
- }
+ closeFileStream(xmlfile, xslfile, outputfile);
}
return retval;
@@ -505,47 +499,13 @@ public class Xslt extends ActionBase implements
Cloneable, IAction {
return true;
}
- /*
- public void setXmlFilename(String filename) {
- this.xmlfilename = filename;
- }
-
- public String getXmlFilename() {
- return xmlfilename;
- }
-
- public String getOutputFilename() {
- return outputfilename;
- }
-
- public void setOutputfilename(String outputfilename) {
- this.outputfilename = outputfilename;
- }
-
- public void setxslFilename(String filename) {
- this.xslfilename = filename;
- }
-
- public String getxslFilename() {
- return xslfilename;
- }
-
- public void setAddFileToResult(boolean addfiletoresultin) {
- this.addfiletoresult = addfiletoresultin;
- }
-
- public boolean isAddFileToResult() {
- return addfiletoresult;
- }
- */
-
@Override
public List<ResourceReference> getResourceDependencies(
IVariables variables, WorkflowMeta workflowMeta) {
List<ResourceReference> references =
super.getResourceDependencies(variables, workflowMeta);
- if ((!Utils.isEmpty(xslfilename)) && (!Utils.isEmpty(xmlfilename))) {
- String realXmlFileName = variables.resolve(xmlfilename);
- String realXslFileName = variables.resolve(xslfilename);
+ if ((!Utils.isEmpty(xslFilename)) && (!Utils.isEmpty(xmlFilename))) {
+ String realXmlFileName = variables.resolve(xmlFilename);
+ String realXslFileName = variables.resolve(xslFilename);
ResourceReference reference = new ResourceReference(this);
reference
.getEntries()
@@ -567,58 +527,70 @@ public class Xslt extends ActionBase implements
Cloneable, IAction {
ValidatorContext ctx = new ValidatorContext();
putVariableSpace(ctx, getVariables());
putValidators(ctx, notBlankValidator(), fileExistsValidator());
- andValidator().validate(this, "xmlfilename", remarks, ctx);
- andValidator().validate(this, "xslfilename", remarks, ctx);
+ andValidator().validate(this, "xmlFilename", remarks, ctx);
+ andValidator().validate(this, "xslFilename", remarks, ctx);
- andValidator().validate(this, "outputfilename", remarks,
putValidators(notBlankValidator()));
+ andValidator().validate(this, "outputFilename", remarks,
putValidators(notBlankValidator()));
}
- /**
- * @return Returns the OutputPropertyName.
- */
- public String[] getOutputPropertyName() {
- return outputPropertyName;
- }
+ @Getter
+ @Setter
+ public static class Parameter {
+ @HopMetadataProperty(key = "name")
+ private String name;
- /**
- * @param argumentDirection The OutputPropertyName to set.
- */
- public void setOutputPropertyName(String[] argumentDirection) {
- this.outputPropertyName = argumentDirection;
- }
+ @HopMetadataProperty(key = "field")
+ private String field;
- /**
- * @return Returns the OutputPropertyField.
- */
- public String[] getOutputPropertyValue() {
- return outputPropertyValue;
- }
+ public Parameter() {}
- /**
- * @param argumentDirection The outputPropertyValue to set.
- */
- public void setOutputPropertyValue(String[] argumentDirection) {
- this.outputPropertyValue = argumentDirection;
- }
+ public Parameter(Parameter parameter) {
+ this.name = parameter.name;
+ this.field = parameter.field;
+ }
- /**
- * @return Returns the parameterName.
- */
- public String[] getParameterName() {
- return parameterName;
+ public Parameter(String name, String field) {
+ this.name = name;
+ this.field = field;
+ }
}
- /**
- * @param argumentDirection The parameterName to set.
- */
- public void setParameterName(String[] argumentDirection) {
- this.parameterName = argumentDirection;
+ @Getter
+ @Setter
+ public static class OutputProperty {
+ @HopMetadataProperty(key = "name")
+ private String name;
+
+ @HopMetadataProperty(key = "value")
+ private String value;
+
+ public OutputProperty() {}
+
+ public OutputProperty(OutputProperty outputProperty) {
+ this.name = outputProperty.name;
+ this.value = outputProperty.value;
+ }
+
+ public OutputProperty(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
}
- /**
- * @return Returns the parameterField.
- */
- public String[] getParameterField() {
- return parameterField;
+ private void closeFileStream(FileObject xmlFile, FileObject xslFile,
FileObject outputFile) {
+ try {
+ if (xmlFile != null) {
+ xmlFile.close();
+ }
+
+ if (xslFile != null) {
+ xslFile.close();
+ }
+ if (outputFile != null) {
+ outputFile.close();
+ }
+ } catch (IOException e) {
+ logError("Unable to close file", e);
+ }
}
}
diff --git
a/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/XsltDialog.java
b/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/XsltDialog.java
index ba65b370c8..752516d3f5 100644
---
a/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/XsltDialog.java
+++
b/plugins/actions/xml/src/main/java/org/apache/hop/workflow/actions/xml/xslt/XsltDialog.java
@@ -441,7 +441,7 @@ public class XsltDialog extends ActionDialog {
fdlOutputProperties.top = new FormAttachment(0, margin);
wlOutputProperties.setLayoutData(fdlOutputProperties);
- final int OutputPropertiesRows = action.getOutputPropertyName().length;
+ final int OutputPropertiesRows = action.getOutputPropertyList().size();
ColumnInfo[] colinf =
new ColumnInfo[] {
@@ -484,7 +484,7 @@ public class XsltDialog extends ActionDialog {
fdlFields.top = new FormAttachment(wOutputProperties, margin);
wlFields.setLayoutData(fdlFields);
- final int FieldsRows = action.getParameterField().length;
+ final int FieldsRows = action.getParameters().size();
colinf =
new ColumnInfo[] {
@@ -563,35 +563,37 @@ public class XsltDialog extends ActionDialog {
/** Copy information from the meta-data input to the dialog fields. */
public void getData() {
wName.setText(Const.nullToEmpty(action.getName()));
- wxmlFilename.setText(Const.nullToEmpty(action.getXmlfilename()));
- wxslFilename.setText(Const.nullToEmpty(action.getXslfilename()));
- wOutputFilename.setText(Const.nullToEmpty(action.getoutputfilename()));
+ wxmlFilename.setText(Const.nullToEmpty(action.getXmlFilename()));
+ wxslFilename.setText(Const.nullToEmpty(action.getXslFilename()));
+ wOutputFilename.setText(Const.nullToEmpty(action.getOutputFilename()));
- if (action.ifFileExists >= 0) {
- wIfFileExists.select(action.ifFileExists);
+ if (action.getIfFileExists() >= 0) {
+ wIfFileExists.select(action.getIfFileExists());
} else {
wIfFileExists.select(2); // NOTHING
}
- wAddFileToResult.setSelection(action.isAddfiletoresult());
+ wAddFileToResult.setSelection(action.isAddFileToResult());
wPrevious.setSelection(action.isFilenamesFromPrevious());
- if (action.getXSLTFactory() != null) {
- wXSLTFactory.setText(action.getXSLTFactory());
+ if (action.getTransformerFactory() != null) {
+ wXSLTFactory.setText(action.getTransformerFactory());
} else {
wXSLTFactory.setText("JAXP");
}
- if (action.getParameterName() != null) {
- for (int i = 0; i < action.getParameterName().length; i++) {
+ if (action.getParameters() != null) {
+ for (int i = 0; i < action.getParameters().size(); i++) {
TableItem item = wFields.table.getItem(i);
- item.setText(1, Const.NVL(action.getParameterField()[i], ""));
- item.setText(2, Const.NVL(action.getParameterName()[i], ""));
+ Xslt.Parameter parameter = action.getParameters().get(i);
+ item.setText(1, Const.NVL(parameter.getField(), ""));
+ item.setText(2, Const.NVL(parameter.getName(), ""));
}
}
- if (action.getOutputPropertyName() != null) {
- for (int i = 0; i < action.getOutputPropertyName().length; i++) {
+ if (action.getOutputPropertyList() != null) {
+ for (int i = 0; i < action.getOutputPropertyList().size(); i++) {
TableItem item = wOutputProperties.table.getItem(i);
- item.setText(1, Const.NVL(action.getOutputPropertyName()[i], ""));
- item.setText(2, Const.NVL(action.getOutputPropertyValue()[i], ""));
+ Xslt.OutputProperty outputProperty =
action.getOutputPropertyList().get(i);
+ item.setText(1, Const.NVL(outputProperty.getName(), ""));
+ item.setText(2, Const.NVL(outputProperty.getValue(), ""));
}
}
}
@@ -616,27 +618,26 @@ public class XsltDialog extends ActionDialog {
return;
}
action.setName(wName.getText());
- action.setXmlfilename(wxmlFilename.getText());
- action.setXslfilename(wxslFilename.getText());
- action.setOutputfilename(wOutputFilename.getText());
- action.ifFileExists = wIfFileExists.getSelectionIndex();
+ action.setXmlFilename(wxmlFilename.getText());
+ action.setXslFilename(wxslFilename.getText());
+ action.setOutputFilename(wOutputFilename.getText());
+ action.setIfFileExists(wIfFileExists.getSelectionIndex());
action.setFilenamesFromPrevious(wPrevious.getSelection());
- action.setAddfiletoresult(wAddFileToResult.getSelection());
- action.setXSLTFactory(wXSLTFactory.getText());
+ action.setAddFileToResult(wAddFileToResult.getSelection());
+ action.setTransformerFactory(wXSLTFactory.getText());
int nrparams = wFields.nrNonEmpty();
int nroutputprops = wOutputProperties.nrNonEmpty();
- action.allocate(nrparams, nroutputprops);
+ action.getParameters().clear();
+ action.getOutputPropertyList().clear();
for (int i = 0; i < nrparams; i++) {
TableItem item = wFields.getNonEmpty(i);
- action.getParameterField()[i] = item.getText(1);
- action.getParameterName()[i] = item.getText(2);
+ action.getParameters().add(new Xslt.Parameter(item.getText(2),
item.getText(1)));
}
for (int i = 0; i < nroutputprops; i++) {
TableItem item = wOutputProperties.getNonEmpty(i);
- action.getOutputPropertyName()[i] = item.getText(1);
- action.getOutputPropertyValue()[i] = item.getText(2);
+ action.getOutputPropertyList().add(new
Xslt.OutputProperty(item.getText(1), item.getText(2)));
}
dispose();
diff --git
a/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltCheckTest.java
b/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltCheckTest.java
index f0bbbf42fd..6f9358bdab 100644
---
a/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltCheckTest.java
+++
b/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltCheckTest.java
@@ -44,9 +44,9 @@ class XsltCheckTest {
Path xml = Files.writeString(tempDir.resolve("source.xml"), "<a/>");
Path xsl = Files.writeString(tempDir.resolve("style.xsl"), "<b/>");
Xslt action = new Xslt();
- action.setXmlfilename(xml.toAbsolutePath().toString());
- action.setXslfilename(xsl.toAbsolutePath().toString());
-
action.setOutputfilename(tempDir.resolve("out.xml").toAbsolutePath().toString());
+ action.setXmlFilename(xml.toAbsolutePath().toString());
+ action.setXslFilename(xsl.toAbsolutePath().toString());
+
action.setOutputFilename(tempDir.resolve("out.xml").toAbsolutePath().toString());
List<ICheckResult> remarks = new ArrayList<>();
action.check(remarks, null, new Variables(), null);
diff --git
a/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltRelativeReferencesTest.java
b/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltRelativeReferencesTest.java
index a507e6f902..9a3351af65 100644
---
a/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltRelativeReferencesTest.java
+++
b/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltRelativeReferencesTest.java
@@ -26,8 +26,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
+import org.apache.commons.vfs2.FileObject;
import org.apache.hop.core.HopEnvironment;
import org.apache.hop.core.Result;
+import org.apache.hop.core.vfs.HopVfs;
import org.apache.hop.workflow.WorkflowMeta;
import org.apache.hop.workflow.action.ActionMeta;
import org.apache.hop.workflow.engine.IWorkflowEngine;
@@ -206,21 +208,41 @@ class XsltRelativeReferencesTest {
}
private String transform(String factory, String xml, String xsl) throws
Exception {
- Path output = dir.resolve("output-" + factory + ".txt");
+ Path output = dir.resolve("output-" + factory + "-" + System.nanoTime() +
".txt");
IWorkflowEngine<WorkflowMeta> workflow = new LocalWorkflowEngine(new
WorkflowMeta());
Xslt action = new Xslt("xslt");
workflow.getWorkflowMeta().addAction(new ActionMeta(action));
action.setParentWorkflow(workflow);
action.setParentWorkflowMeta(mock(WorkflowMeta.class));
- action.setXSLTFactory(factory);
- action.setXmlfilename(xml);
- action.setXslfilename(xsl);
- action.setOutputfilename(output.toString());
+ action.setTransformerFactory(factory);
+ action.setXmlFilename(xml);
+ action.setXslFilename(xsl);
+ action.setOutputFilename(output.toString());
- Result result = action.execute(new Result(), 0);
+ try {
+ Result result = action.execute(new Result(), 0);
- assertTrue(result.isResult(), "the XSLT action failed");
- return Files.readString(output);
+ assertTrue(result.isResult(), "the XSLT action failed");
+ return Files.readString(output);
+ } finally {
+ // On Windows the zip archive stays locked until the layered FileSystem
is closed, which
+ // breaks @TempDir cleanup. Do this in the test only — production code
must not close a
+ // shared zip/jar FileSystem while other readers may still be using it.
+ closeLayeredFileSystem(xsl);
+ HopVfs.freeUnusedResources();
+ }
+ }
+
+ private static void closeLayeredFileSystem(String path) {
+ if (path == null || !(path.startsWith("zip:") || path.startsWith("jar:")))
{
+ return;
+ }
+ try (FileObject file = HopVfs.getFileObject(path)) {
+ HopVfs.getFileSystemManager().closeFileSystem(file.getFileSystem());
+ } catch (Exception ignored) {
+ // ignore close failures in test teardown
+ }
+ // ignore
}
}
diff --git
a/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltTest.java
b/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltTest.java
new file mode 100644
index 0000000000..99b7f273c4
--- /dev/null
+++
b/plugins/actions/xml/src/test/java/org/apache/hop/workflow/actions/xml/xslt/XsltTest.java
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.workflow.actions.xml.xslt;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.stream.Stream;
+import org.apache.hop.core.HopEnvironment;
+import org.apache.hop.core.Result;
+import org.apache.hop.core.logging.HopLogStore;
+import org.apache.hop.core.logging.LogChannel;
+import org.apache.hop.core.xml.XmlHandler;
+import org.apache.hop.metadata.serializer.xml.XmlMetadataUtil;
+import org.apache.hop.workflow.WorkflowMeta;
+import org.apache.hop.workflow.action.ActionMeta;
+import org.apache.hop.workflow.action.ActionSerializationTestUtil;
+import org.apache.hop.workflow.engine.IWorkflowEngine;
+import org.apache.hop.workflow.engines.local.LocalWorkflowEngine;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.w3c.dom.Node;
+
+/** Unit tests for {@link Xslt}, covering issue #9172 fixes. */
+class XsltTest {
+
+ private static final String XSL =
+ """
+ <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+ <xsl:output method="text" encoding="UTF-8"/>
+ <xsl:template match="/orders">
+ <xsl:text>id,customer </xsl:text>
+ <xsl:for-each select="order">
+ <xsl:value-of select="@id"/>,<xsl:value-of select="customer"/>
+ <xsl:text> </xsl:text>
+ </xsl:for-each>
+ </xsl:template>
+ </xsl:stylesheet>
+ """;
+
+ private static final String XML =
+ """
+ <orders>
+ <order id="1"><customer>Apache Hop</customer></order>
+ </orders>
+ """;
+
+ @TempDir Path tempDir;
+
+ @BeforeAll
+ static void setUpBeforeClass() throws Exception {
+ HopEnvironment.init();
+ HopLogStore.init();
+ }
+
+ @Test
+ void testSerializationRoundTrip() throws Exception {
+ Xslt action =
ActionSerializationTestUtil.testSerialization("/xslt-action.xml", Xslt.class);
+
+ assertEquals("${Internal.Entry.Current.Folder}/files/in.xml",
action.getXmlFilename());
+ assertEquals("${Internal.Entry.Current.Folder}/files/style.xsl",
action.getXslFilename());
+ assertEquals("${Internal.Entry.Current.Folder}/files/out.csv",
action.getOutputFilename());
+ assertEquals(0, action.getIfFileExists());
+ assertTrue(action.isAddFileToResult());
+ assertTrue(action.isFilenamesFromPrevious());
+ assertEquals(Xslt.FACTORY_SAXON, action.getTransformerFactory());
+ assertEquals(1, action.getParameters().size());
+ assertEquals("paramName", action.getParameters().getFirst().getName());
+ assertEquals("paramField", action.getParameters().getFirst().getField());
+ assertEquals(1, action.getOutputPropertyList().size());
+ assertEquals("encoding",
action.getOutputPropertyList().getFirst().getName());
+ assertEquals("UTF-8",
action.getOutputPropertyList().getFirst().getValue());
+ }
+
+ @Test
+ void testCloneCopiesNestedSettings() throws Exception {
+ Xslt action =
ActionSerializationTestUtil.testSerialization("/xslt-action.xml", Xslt.class);
+
+ Xslt clone = (Xslt) action.clone();
+
+ assertEquals(action.isAddFileToResult(), clone.isAddFileToResult());
+ assertEquals(action.isFilenamesFromPrevious(),
clone.isFilenamesFromPrevious());
+ assertEquals(action.getTransformerFactory(),
clone.getTransformerFactory());
+ assertEquals(action.getParameters().size(), clone.getParameters().size());
+ assertEquals(
+ action.getParameters().getFirst().getName(),
clone.getParameters().getFirst().getName());
+ assertEquals(
+ action.getOutputPropertyList().getFirst().getValue(),
+ clone.getOutputPropertyList().getFirst().getValue());
+ }
+
+ /**
+ * Workflows saved before the XML cleanup still use {@code <iffileexists>}
instead of {@code
+ * <ifFileExists>}.
+ */
+ @Test
+ void convertLegacyXmlReadsIffileexistsTag() throws Exception {
+ String xml =
+ "<xmlfilename>in.xml</xmlfilename>"
+ + "<xslfilename>style.xsl</xslfilename>"
+ + "<outputfilename>out.csv</outputfilename>"
+ + "<iffileexists>2</iffileexists>";
+ String actionXml =
+ XmlHandler.openTag(ActionMeta.XML_TAG) + xml +
XmlHandler.closeTag(ActionMeta.XML_TAG);
+ Node node = XmlHandler.loadXmlString(actionXml, ActionMeta.XML_TAG);
+
+ Xslt action = XmlMetadataUtil.deSerializeFromXml(node, Xslt.class, new
Xslt(), null);
+
+ assertEquals(2, action.getIfFileExists());
+ }
+
+ @Test
+ void createNewWithUniqueNameKeepsExistingFile() throws Exception {
+ Path xml = Files.writeString(tempDir.resolve("in.xml"), XML);
+ Path xsl = Files.writeString(tempDir.resolve("style.xsl"), XSL);
+ Path output = Files.writeString(tempDir.resolve("out.csv"), "old");
+
+ Xslt action = action(xml, xsl, output);
+ action.setIfFileExists(0);
+
+ Result result = action.execute(new Result(), 0);
+
+ assertTrue(result.isResult());
+ assertEquals("old", Files.readString(output));
+ List<Path> uniqueOutputs;
+ try (Stream<Path> paths = Files.list(tempDir)) {
+ uniqueOutputs =
+ paths
+ .filter(path -> path.getFileName().toString().startsWith("out_"))
+ .filter(path -> path.getFileName().toString().endsWith(".csv"))
+ .toList();
+ }
+ assertEquals(1, uniqueOutputs.size());
+ assertTrue(Files.readString(uniqueOutputs.getFirst()).contains("Apache
Hop"));
+ }
+
+ @Test
+ void missingXslLogsTheXslFilename() throws Exception {
+ Path xml = Files.writeString(tempDir.resolve("in.xml"), XML);
+ Path xsl = tempDir.resolve("missing.xsl");
+ Path output = tempDir.resolve("out.csv");
+
+ Xslt action = action(xml, xsl, output);
+
+ Result result = action.execute(new Result(), 0);
+
+ assertFalse(result.isResult());
+ String log =
+ HopLogStore.getAppender()
+ .getBuffer(action.getLogChannel().getLogChannelId(), true)
+ .toString();
+ assertTrue(log.contains(xsl.toString()), log);
+ assertFalse(log.contains("File [" + xml + "] does not exist!"), log);
+ }
+
+ private Xslt action(Path xml, Path xsl, Path output) {
+ IWorkflowEngine<WorkflowMeta> workflow = new LocalWorkflowEngine(new
WorkflowMeta());
+ Xslt action = new Xslt("xslt");
+ workflow.getWorkflowMeta().addAction(new ActionMeta(action));
+ action.setParentWorkflow(workflow);
+ action.setParentWorkflowMeta(mock(WorkflowMeta.class));
+ action.setLog(new LogChannel(action));
+ action.setXmlFilename(xml.toString());
+ action.setXslFilename(xsl.toString());
+ action.setOutputFilename(output.toString());
+ return action;
+ }
+}
diff --git a/plugins/actions/xml/src/test/resources/xslt-action.xml
b/plugins/actions/xml/src/test/resources/xslt-action.xml
new file mode 100644
index 0000000000..7911ccbb83
--- /dev/null
+++ b/plugins/actions/xml/src/test/resources/xslt-action.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ ~
+ -->
+<action>
+ <xmlfilename>${Internal.Entry.Current.Folder}/files/in.xml</xmlfilename>
+ <xslfilename>${Internal.Entry.Current.Folder}/files/style.xsl</xslfilename>
+
<outputfilename>${Internal.Entry.Current.Folder}/files/out.csv</outputfilename>
+ <ifFileExists>0</ifFileExists>
+ <addfiletoresult>Y</addfiletoresult>
+ <filenamesfromprevious>Y</filenamesfromprevious>
+ <xsltfactory>SAXON</xsltfactory>
+ <parameters>
+ <parameter>
+ <name>paramName</name>
+ <field>paramField</field>
+ </parameter>
+ </parameters>
+ <outputproperties>
+ <outputproperty>
+ <name>encoding</name>
+ <value>UTF-8</value>
+ </outputproperty>
+ </outputproperties>
+</action>