svn commit: r451145 - /incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r451145 - /incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java

jleroux@apache.org
Author: jleroux
Date: Fri Sep 29 00:34:32 2006
New Revision: 451145

URL: http://svn.apache.org/viewvc?view=rev&rev=451145
Log:
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 '&amp;'
+     *    <li>'"' (double quote) becomes '&quot;' when doubleQuotes is true.
+     *    <li>''' (single quote) becomes '&#039;' when singleQuotes is true.
+     *    <li>'<' (less than) becomes '&lt;'
+     *    <li>'>' (greater than) becomes '&gt;'
+     *    <li>\n (Carriage Return) becomes '&lt;br&gt;gt;'
+     * </ol>
+     */
+    public static String htmlSpecialChars(String html, boolean doubleQuotes, boolean singleQuotes, boolean insertBR) {
+        html = StringUtil.replaceString(html, "&", "&amps;");
+        html = StringUtil.replaceString(html, "<", "&lt;");
+        html = StringUtil.replaceString(html, ">", "&gt;");
+        if (doubleQuotes) {
+            html = StringUtil.replaceString(html, "\"", "&quot;");
+        }
+        if (singleQuotes) {
+            html = StringUtil.replaceString(html, "'", "&#039");
+        }
+        if (insertBR) {
+            html = StringUtil.replaceString(html, "\n", "<br>");
+        }
+
+        return html;
+    }
+    public static String htmlSpecialChars(String html) {
+        return htmlSpecialChars(html, true, true, true);
+    }    
+    
 }