Example usage for org.springframework.web.util HtmlCharacterEntityReferences REFERENCE_END

List of usage examples for org.springframework.web.util HtmlCharacterEntityReferences REFERENCE_END

Introduction

In this page you can find the example usage for org.springframework.web.util HtmlCharacterEntityReferences REFERENCE_END.

Prototype

char REFERENCE_END

To view the source code for org.springframework.web.util HtmlCharacterEntityReferences REFERENCE_END.

Click Source Link

Usage

From source file:org.springframework.web.util.HtmlUtils.java

/**
 * Turn special characters into HTML character references.
 * Handles complete character set defined in HTML 4.01 recommendation.
 * <p>Escapes all special characters to their corresponding numeric
 * reference in decimal format (&#<i>Decimal</i>;) at least as required by the
 * specified encoding. In other words, if a special character does
 * not have to be escaped for the given encoding, it may not be.
 * <p>Reference://w  w  w .j  a va  2 s.  c o m
 * <a href="http://www.w3.org/TR/html4/sgml/entities.html">
 * http://www.w3.org/TR/html4/sgml/entities.html
 * </a>
 * @param input the (unescaped) input string
 * @param encoding The name of a supported {@link java.nio.charset.Charset charset}
 * @return the escaped string
 * @since 4.1.2
 */
public static String htmlEscapeDecimal(String input, String encoding) {
    Assert.notNull(encoding, "Encoding is required");
    if (input == null) {
        return null;
    }
    StringBuilder escaped = new StringBuilder(input.length() * 2);
    for (int i = 0; i < input.length(); i++) {
        char character = input.charAt(i);
        if (characterEntityReferences.isMappedToReference(character, encoding)) {
            escaped.append(HtmlCharacterEntityReferences.DECIMAL_REFERENCE_START);
            escaped.append((int) character);
            escaped.append(HtmlCharacterEntityReferences.REFERENCE_END);
        } else {
            escaped.append(character);
        }
    }
    return escaped.toString();
}

From source file:org.springframework.web.util.HtmlUtils.java

/**
 * Turn special characters into HTML character references.
 * Handles complete character set defined in HTML 4.01 recommendation.
 * <p>Escapes all special characters to their corresponding numeric
 * reference in hex format (&#x<i>Hex</i>;) at least as required by the
 * specified encoding. In other words, if a special character does
 * not have to be escaped for the given encoding, it may not be.
 * <p>Reference:/*from   w  w w. j a v  a  2 s . c  o  m*/
 * <a href="http://www.w3.org/TR/html4/sgml/entities.html">
 * http://www.w3.org/TR/html4/sgml/entities.html
 * </a>
 * @param input the (unescaped) input string
 * @param encoding The name of a supported {@link java.nio.charset.Charset charset}
 * @return the escaped string
 * @since 4.1.2
 */
public static String htmlEscapeHex(String input, String encoding) {
    Assert.notNull(encoding, "Encoding is required");
    if (input == null) {
        return null;
    }
    StringBuilder escaped = new StringBuilder(input.length() * 2);
    for (int i = 0; i < input.length(); i++) {
        char character = input.charAt(i);
        if (characterEntityReferences.isMappedToReference(character, encoding)) {
            escaped.append(HtmlCharacterEntityReferences.HEX_REFERENCE_START);
            escaped.append(Integer.toString(character, 16));
            escaped.append(HtmlCharacterEntityReferences.REFERENCE_END);
        } else {
            escaped.append(character);
        }
    }
    return escaped.toString();
}