11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .web .util ;
1818
19+ import org .springframework .util .Assert ;
20+
1921/**
2022 * Utility class for HTML escaping. Escapes and unescapes
2123 * based on the W3C HTML 4.01 recommendation, handling
@@ -57,13 +59,33 @@ public abstract class HtmlUtils {
5759 * @return the escaped string
5860 */
5961 public static String htmlEscape (String input ) {
62+ return htmlEscape (input , WebUtils .DEFAULT_CHARACTER_ENCODING );
63+ }
64+
65+ /**
66+ * Turn special characters into HTML character references.
67+ * Handles complete character set defined in HTML 4.01 recommendation.
68+ * <p>Escapes all special characters to their corresponding
69+ * entity reference (e.g. {@code <}) at least as required by the
70+ * specified encoding. In other words, if a special character does
71+ * not have to be escaped for the given encoding, it may not be.
72+ * <p>Reference:
73+ * <a href="http://www.w3.org/TR/html4/sgml/entities.html">
74+ * http://www.w3.org/TR/html4/sgml/entities.html
75+ * </a>
76+ * @param input the (unescaped) input string
77+ * @param encoding The name of a supported {@link java.nio.charset.Charset charset}
78+ * @return the escaped string
79+ */
80+ public static String htmlEscape (String input , String encoding ) {
81+ Assert .notNull (encoding , "encoding is required" );
6082 if (input == null ) {
6183 return null ;
6284 }
6385 StringBuilder escaped = new StringBuilder (input .length () * 2 );
6486 for (int i = 0 ; i < input .length (); i ++) {
6587 char character = input .charAt (i );
66- String reference = characterEntityReferences .convertToReference (character );
88+ String reference = characterEntityReferences .convertToReference (character , encoding );
6789 if (reference != null ) {
6890 escaped .append (reference );
6991 }
@@ -87,13 +109,33 @@ public static String htmlEscape(String input) {
87109 * @return the escaped string
88110 */
89111 public static String htmlEscapeDecimal (String input ) {
112+ return htmlEscapeDecimal (input , WebUtils .DEFAULT_CHARACTER_ENCODING );
113+ }
114+
115+ /**
116+ * Turn special characters into HTML character references.
117+ * Handles complete character set defined in HTML 4.01 recommendation.
118+ * <p>Escapes all special characters to their corresponding numeric
119+ * reference in decimal format (&#<i>Decimal</i>;) at least as required by the
120+ * specified encoding. In other words, if a special character does
121+ * not have to be escaped for the given encoding, it may not be.
122+ * <p>Reference:
123+ * <a href="http://www.w3.org/TR/html4/sgml/entities.html">
124+ * http://www.w3.org/TR/html4/sgml/entities.html
125+ * </a>
126+ * @param input the (unescaped) input string
127+ * @param encoding The name of a supported {@link java.nio.charset.Charset charset}
128+ * @return the escaped string
129+ */
130+ public static String htmlEscapeDecimal (String input , String encoding ) {
131+ Assert .notNull (encoding , "encoding is required" );
90132 if (input == null ) {
91133 return null ;
92134 }
93135 StringBuilder escaped = new StringBuilder (input .length () * 2 );
94136 for (int i = 0 ; i < input .length (); i ++) {
95137 char character = input .charAt (i );
96- if (characterEntityReferences .isMappedToReference (character )) {
138+ if (characterEntityReferences .isMappedToReference (character , encoding )) {
97139 escaped .append (HtmlCharacterEntityReferences .DECIMAL_REFERENCE_START );
98140 escaped .append ((int ) character );
99141 escaped .append (HtmlCharacterEntityReferences .REFERENCE_END );
@@ -118,13 +160,33 @@ public static String htmlEscapeDecimal(String input) {
118160 * @return the escaped string
119161 */
120162 public static String htmlEscapeHex (String input ) {
163+ return htmlEscapeHex (input , WebUtils .DEFAULT_CHARACTER_ENCODING );
164+ }
165+
166+ /**
167+ * Turn special characters into HTML character references.
168+ * Handles complete character set defined in HTML 4.01 recommendation.
169+ * <p>Escapes all special characters to their corresponding numeric
170+ * reference in hex format (&#x<i>Hex</i>;) at least as required by the
171+ * specified encoding. In other words, if a special character does
172+ * not have to be escaped for the given encoding, it may not be.
173+ * <p>Reference:
174+ * <a href="http://www.w3.org/TR/html4/sgml/entities.html">
175+ * http://www.w3.org/TR/html4/sgml/entities.html
176+ * </a>
177+ * @param input the (unescaped) input string
178+ * @param encoding The name of a supported {@link java.nio.charset.Charset charset}
179+ * @return the escaped string
180+ */
181+ public static String htmlEscapeHex (String input , String encoding ) {
182+ Assert .notNull (encoding , "encoding is required" );
121183 if (input == null ) {
122184 return null ;
123185 }
124186 StringBuilder escaped = new StringBuilder (input .length () * 2 );
125187 for (int i = 0 ; i < input .length (); i ++) {
126188 char character = input .charAt (i );
127- if (characterEntityReferences .isMappedToReference (character )) {
189+ if (characterEntityReferences .isMappedToReference (character , encoding )) {
128190 escaped .append (HtmlCharacterEntityReferences .HEX_REFERENCE_START );
129191 escaped .append (Integer .toString (character , 16 ));
130192 escaped .append (HtmlCharacterEntityReferences .REFERENCE_END );
0 commit comments