Author: jleroux
Date: Fri Sep 29 00:34:32 2006
New Revision: 451145
URL:
http://svn.apache.org/viewvc?view=rev&rev=451145Log:
Added a new method htmlSpecialChars into the StringUtil class from John Martin.
htmlSpecialChars may be used in
http://issues.apache.org/jira/browse/OFBIZ-178 &
http://issues.apache.org/jira/browse/OFBIZ-260 issues
Modified:
incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
Modified: incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
URL:
http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java?view=diff&rev=451145&r1=451144&r2=451145==============================================================================
--- incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java (original)
+++ incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java Fri Sep 29 00:34:32 2006
@@ -367,4 +367,41 @@
}
return outStrBfr.toString();
}
+
+ /**
+ * Translates various HTML characters in a string so that the string can be displayed in a browser safely
+ * <p>
+ * This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or
+ * guest book application. The optional arguments doubleQuotes and singleQuotes allow the control of the substitution of
+ * the quote characters. The default is to translate them with the HTML equivalent.
+ * </p>
+ * The translations performed are: <ol>
+ * <li>'&' (ampersand) becomes '&'
+ * <li>'"' (double quote) becomes '"' when doubleQuotes is true.
+ * <li>''' (single quote) becomes ''' when singleQuotes is true.
+ * <li>'<' (less than) becomes '<'
+ * <li>'>' (greater than) becomes '>'
+ * <li>\n (Carriage Return) becomes '<br>gt;'
+ * </ol>
+ */
+ public static String htmlSpecialChars(String html, boolean doubleQuotes, boolean singleQuotes, boolean insertBR) {
+ html = StringUtil.replaceString(html, "&", "&s;");
+ html = StringUtil.replaceString(html, "<", "<");
+ html = StringUtil.replaceString(html, ">", ">");
+ if (doubleQuotes) {
+ html = StringUtil.replaceString(html, "\"", """);
+ }
+ if (singleQuotes) {
+ html = StringUtil.replaceString(html, "'", "'");
+ }
+ if (insertBR) {
+ html = StringUtil.replaceString(html, "\n", "<br>");
+ }
+
+ return html;
+ }
+ public static String htmlSpecialChars(String html) {
+ return htmlSpecialChars(html, true, true, true);
+ }
+
}