From 1d6bca2ad4dab8401e86ae7ff3d770aa61b66ba8 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Wed, 5 Jul 2023 15:18:07 -0700 Subject: [PATCH] Fix for the issue reported in PR #715 - Do not replace a single whitespace character with non-breaking whitespace ( ) - Replace multiple whitespace characters occurring next to each other with non-breaking whitespace ( ) characters to preserve the whitespace when displayed in a browser. --- .../action/visitor/DAOLoadVisitor.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java b/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java index 70a56d8b..0793e5d4 100644 --- a/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java +++ b/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java @@ -30,6 +30,7 @@ import java.util.*; import com.salesforce.dataloader.model.Row; +import com.salesforce.dataloader.util.AppUtil; import com.salesforce.dataloader.util.DAORowUtil; import org.apache.commons.beanutils.*; @@ -317,17 +318,31 @@ private static String escapeHTMLChars(String input) { if (input == null) { return null; } - input = StringEscapeUtils.escapeHtml4(input); - - // space characters need further handling StringBuffer htmlFormattedStr = new StringBuffer(""); for (int i = 0, len = input.length(); i < len; i++) { char c = input.charAt(i); int cval = c; - if (Character.isWhitespace(c) || cval == NONBREAKING_SPACE_ASCII_VAL) { - htmlFormattedStr.append(" "); + char nextChar = 0; + if (i+1 < input.length()) { + nextChar = input.charAt(i+1); + } + char prevChar = 0; + if (i > 0) { + prevChar = input.charAt(i-1); + } + + boolean isCharWhitespace = Character.isWhitespace(c) || cval == NONBREAKING_SPACE_ASCII_VAL; + boolean isNextCharWhitespace = Character.isWhitespace(nextChar) || nextChar == NONBREAKING_SPACE_ASCII_VAL; + boolean isPrevCharWhitespace = Character.isWhitespace(prevChar) || prevChar == NONBREAKING_SPACE_ASCII_VAL; + //only occurrences of multiple w + if (isCharWhitespace) { + if (isNextCharWhitespace || isPrevCharWhitespace) { + htmlFormattedStr.append(" "); + } else { + htmlFormattedStr.append(c); + } } else { - htmlFormattedStr.append(c); + htmlFormattedStr.append(StringEscapeUtils.escapeHtml4(Character.toString(c))); } } return htmlFormattedStr.toString();