commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r643953 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/StringEscapeUtils.java test/org/apache/commons/lang/StringEscapeUtilsTest.java
Date Wed, 02 Apr 2008 16:08:24 GMT
Author: ggregory
Date: Wed Apr  2 09:08:21 2008
New Revision: 643953

URL: http://svn.apache.org/viewvc?rev=643953&view=rev
Log:
(LANG-421) StringEscapeUtils.escapeJava(String) escapes '/' characters

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java?rev=643953&r1=643952&r2=643953&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringEscapeUtils.java Wed
Apr  2 09:08:21 2008
@@ -83,7 +83,7 @@
      * @return String with escaped values, <code>null</code> if null string input
      */
     public static String escapeJava(String str) {
-        return escapeJavaStyleString(str, false);
+        return escapeJavaStyleString(str, false, false);
     }
 
     /**
@@ -99,7 +99,7 @@
      * @throws IOException if error occurs on underlying Writer
      */
     public static void escapeJava(Writer out, String str) throws IOException {
-        escapeJavaStyleString(out, str, false);
+        escapeJavaStyleString(out, str, false, false);
     }
 
     /**
@@ -124,7 +124,7 @@
      * @return String with escaped values, <code>null</code> if null string input
      */
     public static String escapeJavaScript(String str) {
-        return escapeJavaStyleString(str, true);
+        return escapeJavaStyleString(str, true, true);
     }
 
     /**
@@ -140,7 +140,7 @@
      * @throws IOException if error occurs on underlying Writer
      **/
     public static void escapeJavaScript(Writer out, String str) throws IOException {
-        escapeJavaStyleString(out, str, true);
+        escapeJavaStyleString(out, str, true, true);
     }
 
     /**
@@ -148,15 +148,16 @@
      * 
      * @param str String to escape values in, may be null
      * @param escapeSingleQuotes escapes single quotes if <code>true</code>
+     * @param escapeForwardSlash TODO
      * @return the escaped string
      */
-    private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) {
+    private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes, boolean
escapeForwardSlash) {
         if (str == null) {
             return null;
         }
         try {
             StringWriter writer = new StringWriter(str.length() * 2);
-            escapeJavaStyleString(writer, str, escapeSingleQuotes);
+            escapeJavaStyleString(writer, str, escapeSingleQuotes, escapeForwardSlash);
             return writer.toString();
         } catch (IOException ioe) {
             // this should never ever happen while writing to a StringWriter
@@ -171,9 +172,11 @@
      * @param out write to receieve the escaped string
      * @param str String to escape values in, may be null
      * @param escapeSingleQuote escapes single quotes if <code>true</code>
+     * @param escapeForwardSlash TODO
      * @throws IOException if an IOException occurs
      */
-    private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote)
throws IOException {
+    private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
+            boolean escapeForwardSlash) throws IOException {
         if (out == null) {
             throw new IllegalArgumentException("The Writer must not be null");
         }
@@ -194,23 +197,23 @@
                 out.write("\\u00" + hex(ch));
             } else if (ch < 32) {
                 switch (ch) {
-                    case '\b':
+                    case '\b' :
                         out.write('\\');
                         out.write('b');
                         break;
-                    case '\n':
+                    case '\n' :
                         out.write('\\');
                         out.write('n');
                         break;
-                    case '\t':
+                    case '\t' :
                         out.write('\\');
                         out.write('t');
                         break;
-                    case '\f':
+                    case '\f' :
                         out.write('\\');
                         out.write('f');
                         break;
-                    case '\r':
+                    case '\r' :
                         out.write('\\');
                         out.write('r');
                         break;
@@ -224,22 +227,24 @@
                 }
             } else {
                 switch (ch) {
-                    case '\'':
+                    case '\'' :
                         if (escapeSingleQuote) {
-                          out.write('\\');
+                            out.write('\\');
                         }
                         out.write('\'');
                         break;
-                    case '"':
+                    case '"' :
                         out.write('\\');
                         out.write('"');
                         break;
-                    case '\\':
+                    case '\\' :
                         out.write('\\');
                         out.write('\\');
                         break;
-                    case '/':
-                        out.write('\\');
+                    case '/' :
+                        if (escapeForwardSlash) {
+                            out.write('\\');
+                        }
                         out.write('/');
                         break;
                     default :

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java?rev=643953&r1=643952&r2=643953&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
Wed Apr  2 09:08:21 2008
@@ -97,6 +97,22 @@
                 "\uABCD\u1234\u012C");
     }
 
+    /**
+     * https://issues.apache.org/jira/browse/LANG-421
+     */
+    public void testEscapeJavaWithSlash() {
+        final String input = "String with a slash (/) in it";
+
+        final String expected = input;
+        final String actual = StringEscapeUtils.escapeJava(input);
+
+        /**
+         * In 2.4 StringEscapeUtils.escapeJava(String) escapes '/' characters, which are
not a valid character to escape
+         * in a Java string.
+         */
+        assertEquals(expected, actual);
+    }
+    
     private void assertEscapeJava(String escaped, String original) throws IOException {
         assertEscapeJava(null, escaped, original);
     }



Mime
View raw message