Search code examples
javastringescapeutils

String contents are same but equals method returns false


I am using StringEscapeUtils to escape and unescape html. I have following code

import org.apache.commons.lang.StringEscapeUtils;

public class EscapeUtils {

    public static void main(String args[]) {

        String string = "    4-Spaces    ,\"Double Quote\", 'Single Quote', \\Back-Slash\\, /Forward Slash/ ";

        String escaped = StringEscapeUtils.escapeHtml(string);
        String myEscaped = escapeHtml(string);

        String unescaped = StringEscapeUtils.unescapeHtml(escaped);
        String myUnescaped = StringEscapeUtils.unescapeHtml(myEscaped);

        System.out.println("Real String: " + string);
        System.out.println();
        System.out.println("Escaped String: " + escaped);
        System.out.println("My Escaped String: " + myEscaped);
        System.out.println();
        System.out.println("Unescaped String: " + unescaped);
        System.out.println("My Unescaped String: " + myUnescaped);
        System.out.println();
        System.out.println("Comparison:");
        System.out.println("Real String == Unescaped String: " + string.equals(unescaped));
        System.out.println("Real String == My Unescaped String: " + string.equals(myUnescaped));
        System.out.println("Unescaped String == My Unescaped String: " + unescaped.equals(myUnescaped));

    }

    public static String escapeHtml(String s) {
        String escaped = "";
        if(null != s) {
            escaped = StringEscapeUtils.escapeHtml(s);
            escaped = escaped.replaceAll(" "," ");
            escaped = escaped.replaceAll("'","'");
            escaped = escaped.replaceAll("\\\\","\");
            escaped = escaped.replaceAll("/","/");
        }
        return escaped;
    }

}

Output:

Real String:     4-Spaces    ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/ 

Escaped String:     4-Spaces    ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/ 
My Escaped String:     4-Spaces    ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/ 

Unescaped String:     4-Spaces    ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/ 
My Unescaped String:     4-Spaces    ,"Double Quote", 'Single Quote', \Back-Slash\, /Forward Slash/ 

Comparison:
Real String == Unescaped String: true
Real String == My Unescaped String: false
Unescaped String == My Unescaped String: false

I escaped the real string and then unescaped it. but myEsceped is first escaped with same process, and then some more html characters are replaced with their html codes. myUnescaped is actually unescape of myEscaped which has same contents as that of real string.

Output shows that real string, unescaped, and myUnescaped contents are same. But, as in Comparison section, myUnescaped is not equal to string and unescaped.

I don't understand it yet what is actually happening here. Can anyone explain it?


Solution

  • This due to while escaping HTML, you are replacing ' ' with  

    public static String escapeHtml(String s) {
            String escaped = "";
            if(null != s) {
                escaped = StringEscapeUtils.escapeHtml(s);
                escaped = escaped.replaceAll(" "," "); // HERE
                escaped = escaped.replaceAll("'","'");
                escaped = escaped.replaceAll("\\\\","\");
                escaped = escaped.replaceAll("/","/");
            }
            return escaped;
        }
    

    While StringEscapeUtils.escapeHtml does not escape ' ', below is the example on their site:

    "bread" & "butter" 
    

    becomes

    "bread" & "butter"
    

    Which means StringEscapeUtils.escapeHtml preserves spaces

    If from escapeHtml you remove escaped = escaped.replaceAll(" "," ");, unescaped and myUnescaped match !