Skip to content

Commit e580e52

Browse files
authored
Merge pull request #402 from thuva9872/cdata
Wrap value with special characters in CDATA for connectors
2 parents 545075d + 1ad193a commit e580e52

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/mediatorService/MediatorHandler.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ private SynapseConfigResponse generateConnectorSynapseConfig(STNode node, String
196196
protected Map<String, Object> processConnectorParameter(Object data) {
197197

198198
Map<String, Object> dataValue = new HashMap<>();
199+
boolean isExpressionField = false;
199200
if (data instanceof String) {
200201
dataValue.put(Constant.VALUE, String.format("%s", data));
201202
} else if (data instanceof Boolean) {
@@ -205,7 +206,9 @@ protected Map<String, Object> processConnectorParameter(Object data) {
205206
Object isExpressionObj = dataValue.get(Constant.IS_EXPRESSION);
206207
boolean isExpression = isExpressionObj == null ? false : (boolean) isExpressionObj;
207208
if (isExpression) {
209+
isExpressionField = true;
208210
dataValue.put(Constant.VALUE, String.format("{%s}", dataValue.get(Constant.VALUE)));
211+
dataValue.put(Constant.IS_EXPRESSION, true);
209212
} else {
210213
if (dataValue.get(Constant.VALUE).toString().startsWith("{") &&
211214
dataValue.get(Constant.VALUE).toString().endsWith("}")) {
@@ -232,16 +235,28 @@ protected Map<String, Object> processConnectorParameter(Object data) {
232235
dataValue.put(Constant.VALUE, String.format("%s", dataValueStr));
233236
}
234237
if (dataValue.get(Constant.VALUE) != null && dataValue.get(Constant.VALUE).toString().startsWith("<![CDATA[")) {
235-
dataValue.put("isCDATA", true);
238+
dataValue.put(Constant.IS_CDATA, true);
236239
String value = dataValue.get(Constant.VALUE).toString().substring(9); // Remove <![CDATA[
237240
if (value.endsWith("]]>")) {
238241
value = value.substring(0, value.length() - 3); // Remove ]]>
239242
}
240243
dataValue.put(Constant.VALUE, value);
241244
}
245+
if (!isExpressionField && hasSpecialXmlCharacter(dataValue.get(Constant.VALUE))) {
246+
dataValue.put(Constant.IS_CDATA, true);
247+
}
242248
return dataValue;
243249
}
244250

251+
private boolean hasSpecialXmlCharacter(Object o) {
252+
253+
if (!(o instanceof String)) {
254+
return false;
255+
}
256+
String value = (String) o;
257+
return value.contains("&") || value.contains("<") || value.contains(">");
258+
}
259+
245260
protected ConnectorAction getConnectorOperation(STNode node, String mediator) {
246261

247262
String connectorName;

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/syntaxTree/factory/mediators/ConnectorFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ private void addConnectorParameters(Connector connector, DOMElement element) {
7575
if (isExpression) {
7676
parameter.setExpression(inline);
7777
} else {
78-
parameter.setValue(inline);
78+
parameter.setValue(Utils.removeCDATATag(inline));
7979
}
80-
parameter.setValue(Utils.getInlineString(childElement.getFirstChild()));
8180
parameters.add(parameter);
8281
}
8382
}

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/utils/Constant.java

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ public class Constant {
604604
public static final String REQUESTS = "requests";
605605
public static final String DEFAULT_REQUEST = "defaultRequest";
606606
public static final String CONTENT = "content";
607+
public static final String IS_CDATA = "isCDATA";
607608

608609
static {
609610
// AI Connection to Display Name bi-Mapping

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/utils/UISchemaMapper.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.gson.JsonElement;
2424
import com.google.gson.JsonObject;
2525
import com.google.gson.JsonPrimitive;
26+
import org.apache.commons.lang3.StringUtils;
2627
import org.eclipse.lemminx.customservice.synapse.connectors.entity.Connection;
2728
import org.eclipse.lemminx.customservice.synapse.mediatorService.MediatorUtils;
2829
import org.eclipse.lemminx.customservice.synapse.mediatorService.pojo.Namespace;
@@ -85,6 +86,9 @@ private static void processElement(JsonObject elementData, JsonObject elementObj
8586
currentValue.getAsString().substring(1, currentValue.getAsString().length() - 1));
8687
} else if (isCheckBox(value)) {
8788
currentValue = new JsonPrimitive(currentValue.getAsBoolean());
89+
} else if (currentValue.isJsonPrimitive()) {
90+
String sanitizedValue = Utils.removeCDATATag(currentValue.getAsString());
91+
currentValue = new JsonPrimitive(sanitizedValue);
8892
}
8993
value.add("currentValue", currentValue);
9094
}
@@ -100,8 +104,9 @@ private static boolean isCheckBox(JsonObject value) {
100104
return false;
101105
}
102106

103-
private static JsonArray generateTableDataForConnector(String tableFieldValue) {
107+
private static JsonArray generateTableDataForConnector(String tableFieldCDATA) {
104108

109+
String tableFieldValue = Utils.removeCDATATag(tableFieldCDATA);
105110
JsonArray result = new JsonArray();
106111
JSONArray tableValues = new JSONArray(tableFieldValue);
107112
for (int i = 0; i < tableValues.length(); i++) {

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/utils/Utils.java

+18
Original file line numberDiff line numberDiff line change
@@ -1391,4 +1391,22 @@ public static boolean isValidProject(String projectRoot) {
13911391
}
13921392
return true;
13931393
}
1394+
1395+
/**
1396+
* Remove CDATA tags from the given xml string.
1397+
* <p>
1398+
* * Example:
1399+
* {@code <![CDATA[<xml/>]]>} will be converted to {@code <xml/>}
1400+
* </p>
1401+
*
1402+
* @param value the xml string
1403+
* @return the xml string without CDATA tags
1404+
*/
1405+
public static String removeCDATATag(String value) {
1406+
1407+
if (StringUtils.isEmpty(value)) {
1408+
return StringUtils.EMPTY;
1409+
}
1410+
return value.replaceAll("<!\\[CDATA\\[", "").replaceAll("]]>", "");
1411+
}
13941412
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<{{tag}} {{#configKey}}configKey="{{{configKey}}}"{{/configKey}}>
22
{{#parameters}}
3-
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{value}}{{/isCDATA}}{{/value}}</{{{name}}}>
3+
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{#isExpression}}{{value}}{{/isExpression}}{{^isExpression}}{{{value}}}{{/isExpression}}{{/isCDATA}}{{/value}}</{{{name}}}>
44
{{/parameters}}
55
</{{tag}}>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<{{tag}} {{#configKey}}configKey="{{{configKey}}}"{{/configKey}}>
22
{{#parameters}}
3-
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{value}}{{/isCDATA}}{{/value}}</{{{name}}}>
3+
<{{{name}}} {{#value}}{{#namespaces}} xmlns:{{{prefix}}}="{{{uri}}}"{{/namespaces}}>{{#isCDATA}}<![CDATA[{{{value}}}]]>{{/isCDATA}}{{^isCDATA}}{{#isExpression}}{{value}}{{/isExpression}}{{^isExpression}}{{{value}}}{{/isExpression}}{{/isCDATA}}{{/value}}</{{{name}}}>
44
{{/parameters}}
55
</{{tag}}>

0 commit comments

Comments
 (0)