0

Java can escape html using e.g. Apache CommonsText StringEscapeUtils but it will use html entity names, not numbers. So > becomes > Is there a library or class or some feasible custom approach that will use html entity numbers, so > becomes > and becomes € etc?

(Context: I'm using a platform where escaped html using entity names is no longer accepted in a certain process for security reasons, so I really need the entity numbers).

1
  • There is no internal framework that will do this, you'd need to use the Apache Commons Text. If you implemented this yourself, you'd need to somehow specify which characters should not be escaped. Commented May 23, 2023 at 21:24

2 Answers 2

1

There doesn't appear to be.

The Apache Commons Text library maps the values directly to the names.
Apache Commons Text - EntityArrays.java

You can create your own implementation, using the basic set, ", &, <, and >.

String escape(String string) {
    StringBuilder escaped = new StringBuilder();
    for (char character : string.toCharArray()) {
        switch (character) {
            case '"', '&', '<', '>' -> {
                escaped.append("&#%d;".formatted((int) character));
            }
            default -> escaped.append(character);
        }
    }
    return escaped.toString();
}

Input

<html>stack overflow</html>

Output

&#60;html&#62;stack overflow&#60;/html&#62;

If you're looking to create an escape pattern which will encapsulate a much broader range of values, you can implement a range parameter.
Note, the ranges here are the allowed code-points, not the to-be-escaped code-points.

record Range(int start, int limit) { }

/** @param ranges allowed code-points */
String escape(String string, Range... ranges) {
    StringBuilder escaped = new StringBuilder();
    int codePoint;
    boolean allowed;
    for (char character : string.toCharArray()) {
        codePoint = (int) character;
        allowed = false;
        for (Range range : ranges) {
            if (codePoint >= range.start && codePoint <= range.limit) {
                allowed = true;
                break;
            }
        }
        if (allowed) escaped.append(character);
        else escaped.append("&#%d;".formatted(codePoint));
    }
    return escaped.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use the method present in org.springframework.web.util.HtmlUtils:

//Send a input string in parameter
HtmlUtils.htmlEscapeDecimal(String input)

I hope it will work for you as you expected.

2 Comments

Thanks, it looks like it could do what I need, but it's unwilling to cooperate, throwing an error once I call a method: "java.lang.UnsupportedClassVersionError: org/springframework/web/util/HtmlUtils has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0" My project is built using Java 11 and this spring stuff (version 5) claims to support that, so I'm out, sorry.
@Mko Can you check if your JAVA_HOME variable is set with correct version? HtmlUtils should work fine with jdk 11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.