Skip to content

Commit

Permalink
Merge pull request #14 from arunans23/master
Browse files Browse the repository at this point in the history
Introduce new parameter to suppress Escape Characters in CSV output
  • Loading branch information
arunans23 authored Sep 22, 2023
2 parents 103f88e + a9a7cef commit 4b989c2
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 51 deletions.
1 change: 1 addition & 0 deletions docs/csv_to_csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Specify columns to skip from the CSV payload. You can specify the columns as com
* If `headerPresent` is `Present` and `skipHeader` is set as `false`, output CSV would have the header of the input
CSV.
* customValueSeparator (Separator): Values separator to use in the output CSV. Default is `,` (comma)
* suppressEscaping (Suppress Escaping): Specify whether to suppress all escaping in the output Csv payload. Default is false. (ie. Escape characters will be present)

### Sample configuration

Expand Down
1 change: 1 addition & 0 deletions docs/json_to_csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can use the jsonToCsv operation to transform a JSON payload in to an CSV pay
* customHeader (CSV Header):
Set a custom header to the output CSV payload. If this property is not specified, Key values of the input would be
used as the output CSV headers.
* suppressEscaping (Suppress Escaping): Specify whether to suppress all escaping in the output Csv payload. Default is false. (ie. Escape characters will be present)


### Sample configuration
Expand Down
2 changes: 1 addition & 1 deletion docs/xml_to_csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ You can use the xmlToCsv operation to transform an XML payload in to an CSV payl
**Properties**
* customHeader (CSV Header):
Set a custom header to the output CSV payload. If this property is not specified, Key values of the input would be used as the output CSV headers.

* suppressEscaping (Suppress Escaping): Specify whether to suppress all escaping in the output Csv payload. Default is false. (ie. Escape characters will be present)

### Sample configuration

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.wso2.carbon.module</groupId>
<artifactId>mediation-csv-module</artifactId>
<version>1.0.5-SNAPSHOT</version>
<version>1.0.5</version>
<packaging>jar</packaging>
<name>WSO2 Carbon - Connector For csvConnector</name>
<url>http://wso2.org</url>
Expand All @@ -43,7 +43,7 @@
<bouncycastle.version>1.55</bouncycastle.version>
<rampart.apache.version>1.6.1-wso2v18</rampart.apache.version>
<rampart.wso2.version>1.6.1.wso2v18</rampart.wso2.version>
<module.core.version>1.0.1</module.core.version>
<module.core.version>1.0.2</module.core.version>
<junit-jupiter.version>5.6.2</junit-jupiter.version>
<mockito.version>2.21.0</mockito.version>
<mockito-junit.version>2.23.0</mockito-junit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void mediate(SimpleMessageContext mc) {
getEnumParam(mc, ParameterKey.SORT_COLUMNS_BY_ORDERING, OrderingType.class, OrderingType.ASCENDING);
final Optional<String> customHeader = getStringParam(mc, ParameterKey.CUSTOM_HEADER);
final Optional<Character> customValueSeparator = getCharParam(mc, ParameterKey.CUSTOM_VALUE_SEPARATOR);
final boolean suppressEscapeCharacters = getBooleanParam(mc, ParameterKey.SUPPRESS_ESCAPE_CHARACTERS);

CsvPayloadInfo payloadInfo = new CsvPayloadInfo();
if (headerAvailability == HeaderAvailability.PRESENT || customHeader.isPresent()) {
Expand Down Expand Up @@ -89,10 +90,8 @@ public void mediate(SimpleMessageContext mc) {
resultHeader = skipColumnsSingleRow(payloadInfo.getNumberOfColumns(), columnsToSkipQuery.get(),
payloadInfo.getFirstRow(), resultHeader);
}

csvArrayStream
.collect(mc.collectToCsv(resultHeader, customValueSeparator.orElse(Constants.DEFAULT_CSV_SEPARATOR)));

.collect(mc.collectToCsv(resultHeader, customValueSeparator.orElse(Constants.DEFAULT_CSV_SEPARATOR), suppressEscapeCharacters));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Set;

import static org.wso2.carbon.module.csv.constant.Constants.DEFAULT_EXPRESSION_SPLITTER;
import static org.wso2.carbon.module.csv.util.PropertyReader.getBooleanParam;

/**
* Transformer to transform JSON content to a CSV content.
Expand All @@ -41,6 +42,7 @@ public class JsonToCsvTransformer extends SimpleMediator {

@Override
public void mediate(SimpleMessageContext mc) {
final boolean suppressEscapeCharacters = getBooleanParam(mc, ParameterKey.SUPPRESS_ESCAPE_CHARACTERS);

String[] header = getHeader(mc);
mc.getJsonArrayStream()
Expand All @@ -58,7 +60,7 @@ public void mediate(SimpleMessageContext mc) {

return csvEntry.toArray(new String[]{});
})
.collect(mc.collectToCsv(header));
.collect(mc.collectToCsv(header, suppressEscapeCharacters));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;

import static org.wso2.carbon.module.csv.constant.Constants.DEFAULT_EXPRESSION_SPLITTER;
import static org.wso2.carbon.module.csv.util.PropertyReader.getBooleanParam;

/**
* Transformer to transform an XML content to a CSV content.
Expand All @@ -37,7 +38,7 @@ public class XmlToCsvTransformer extends SimpleMediator {

@Override
public void mediate(SimpleMessageContext mc) {

final boolean suppressEscapeCharacter = getBooleanParam(mc, ParameterKey.SUPPRESS_ESCAPE_CHARACTERS);
String[] header = getHeader(mc);
mc.getXmlChildElementsStream()
.map(omElement -> {
Expand All @@ -49,7 +50,7 @@ public void mediate(SimpleMessageContext mc) {
csvEntry.add(childText);
}
return csvEntry.toArray(new String[0]);
}).collect(mc.collectToCsv(header));
}).collect(mc.collectToCsv(header, suppressEscapeCharacter));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ParameterKey {
public static final String GROUP_ELEMENT_NAMESPACE = "groupElementNamespace";
public static final String GROUP_ELEMENT_NAMESPACE_URI = "groupElementNamespaceURI";

public static final String SUPPRESS_ESCAPE_CHARACTERS = "suppressEscaping";

private ParameterKey() {

}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/csvToCsv/csv_to_csv_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<parameter name="customHeader" description="Comma separated string of header to set as CSV payload header"/>
<parameter name="customValueSeparator"
description="Specify a value separator for CSV output. Default is comma ( , )"/>
<parameter name="suppressEscaping" description="Whether to suppress Escape Characters in the output Csv payload"/>
<sequence>

<property name="headerPresent" expression="$func:headerPresent"/>
Expand All @@ -38,6 +39,7 @@
<property name="orderByColumn" expression="$func:orderByColumn"/>
<property name="columnOrdering" expression="$func:columnOrdering"/>
<property name="customValueSeparator" expression="$func:customValueSeparator"/>
<property name="suppressEscaping" expression="$func:suppressEscaping"/>
<class name="org.wso2.carbon.module.csv.CsvToCsvTransformer"/>
</sequence>
</template>
2 changes: 2 additions & 0 deletions src/main/resources/jsonToCsv/json_to_csv_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
-->
<template xmlns="http://ws.apache.org/ns/synapse" name="jsonToCsv">
<parameter name="customHeader" description="Comma separated string of header for Csv payload"/>
<parameter name="suppressEscaping" description="Whether to suppress Escape Characters in the output Csv payload"/>
<sequence>
<property name="customHeader" expression="$func:customHeader"/>
<property name="suppressEscaping" expression="$func:suppressEscaping"/>
<class name="org.wso2.carbon.module.csv.JsonToCsvTransformer"/>
</sequence>
</template>
11 changes: 11 additions & 0 deletions src/main/resources/uischema/csvToCsv.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@
"required": "false",
"helpTip": "Specify a value separator for CSV output. Default is comma ( , )"
}
},
{
"type": "attribute",
"value": {
"name": "suppressEscaping",
"displayName": "Suppress Escaping",
"inputType": "booleanOrExpression",
"defaultValue": "false",
"required": "false",
"helpTip": "Specify whether to suppress all escaping in the output Csv payload. Default is false. (ie. Escape characters will be present)"
}
}
]
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/uischema/jsonToCsv.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
"required": "false",
"helpTip": "Comma separated strings to set as the CSV header"
}
},
{
"type": "attribute",
"value": {
"name": "suppressEscaping",
"displayName": "Suppress Escaping",
"inputType": "booleanOrExpression",
"defaultValue": "false",
"required": "false",
"helpTip": "Specify whether to suppress all escaping in the output Csv payload. Default is false. (ie. Escape characters will be present)"
}
}
]
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/uischema/xmlToCsv.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
"required": "false",
"helpTip": "Comma separated strings to set as the CSV header"
}
},
{
"type": "attribute",
"value": {
"name": "suppressEscaping",
"displayName": "Suppress Escaping",
"inputType": "booleanOrExpression",
"defaultValue": "false",
"required": "false",
"helpTip": "Specify whether to suppress all escaping in the output Csv payload. Default is false. (ie. Escape characters will be present)"
}
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/xmlToCsv/xml_to_csv_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
-->
<template xmlns="http://ws.apache.org/ns/synapse" name="xmlToCsv">
<parameter name="customHeader" description="Comma separated string of header for Csv payload."/>
<parameter name="suppressEscaping" description="Whether to suppress Escape Characters in the output Csv payload"/>
<sequence>
<property name="customHeader" expression="$func:customHeader"/>
<property name="suppressEscaping" expression="$func:suppressEscaping"/>
<class name="org.wso2.carbon.module.csv.XmlToCsvTransformer"/>
</sequence>
</template>
Loading

0 comments on commit 4b989c2

Please sign in to comment.