Commits

Anonymous committed 6ae862b

Remove TextUtils and add commons-lang StringEscapeUtils

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1946e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (7)

                                     <artifact>commons-lang:commons-lang</artifact>
                                     <includes>
                                         <include>org/apache/commons/lang/StringUtils.class</include>
+                                        <include>org/apache/commons/lang/StringEscapeUtils.class</include>
                                     </includes>
                                 </filter>
                             </filters>

src/java/com/opensymphony/xwork2/util/TextUtils.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork2.util;
-
-import org.apache.commons.lang.StringUtils;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
-
-
-/**
- * Utilities for common String manipulations.
- *
- * This is a class contains static methods only and is not meant to be instantiated.
- * It was brought in from oscore trunk revision 147, and trimmed to only contain
- * methods used by XWork.
- *
- * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
- * @author <a href="mailto:pkan@internet.com">Patrick Kan</a>
- * @author <a href="mailto:mcannon@internet.com">Mike Cannon-Brookes</a>
- * @author <a href="mailto:hani@fate.demon.co.uk">Hani Suleiman</a>
- * @author <a href="mailto:joeo@adjacency.org">Joseph B. Ottinger</a>
- * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
- *
- * @version $Revision: 147 $
- */
-public class TextUtils {
-
-    public final static String htmlEncode(String s) {
-        return htmlEncode(s, true);
-    }
-
-    /**
-     * Escape html entity characters and high characters (eg "curvy" Word quotes).
-     * Note this method can also be used to encode XML.
-     * @param s the String to escape.
-     * @param encodeSpecialChars if true high characters will be encode other wise not.
-     * @return the escaped string
-     */
-    public final static String htmlEncode(String s, boolean encodeSpecialChars) {
-        s = StringUtils.defaultString(s);
-
-        StringBuilder str = new StringBuilder();
-
-        for (int j = 0; j < s.length(); j++) {
-            char c = s.charAt(j);
-
-            // encode standard ASCII characters into HTML entities where needed
-            if (c < '\200') {
-                switch (c) {
-                case '"':
-                    str.append("&quot;");
-
-                    break;
-
-                case '&':
-                    str.append("&amp;");
-
-                    break;
-
-                case '<':
-                    str.append("&lt;");
-
-                    break;
-
-                case '>':
-                    str.append("&gt;");
-
-                    break;
-
-                default:
-                    str.append(c);
-                }
-            }
-            // encode 'ugly' characters (ie Word "curvy" quotes etc)
-            else if (encodeSpecialChars && (c < '\377')) {
-                String hexChars = "0123456789ABCDEF";
-                int a = c % 16;
-                int b = (c - a) / 16;
-                String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
-                str.append("&#x" + hex + ";");
-            }
-            //add other characters back in - to handle charactersets
-            //other than ascii
-            else {
-                str.append(c);
-            }
-        }
-
-        return str.toString();
-    }
-
-    /**
-     * Escape a String into a JavaScript-compatible String.
-     *
-     * @param s the String to escape.
-     * @return the escaped string
-     */
-    public final static String escapeJavaScript(String s) {
-        s = StringUtils.defaultString(s);
-        StringBuffer str = new StringBuffer();
-
-        for (int j = 0; j < s.length(); j++) {
-            char c = s.charAt(j);
-            switch (c) {
-                case '\t':
-                    str.append("\\t");
-                    break;
-                case '\b':
-                    str.append("\\b");
-                    break;
-                case '\n':
-                    str.append("\\n");
-                    break;
-                case '\f':
-                    str.append("\\f");
-                    break;
-                case '\r':
-                    str.append("\\r");
-                    break;
-                case '\\':
-                    str.append("\\\\");
-                    break;
-                case '"':
-                    str.append("\\\"");
-                    break;
-                case '\'':
-                    str.append("\\'");
-                    break;
-                case '/':
-                    str.append("\\/");
-                    break;
-                default:
-                    str.append(c);
-            }
-        }
-        return str.toString();
-    }
-
-    /**
-     * Verify That the given String is in valid URL format.
-     * @param url The url string to verify.
-     * @return a boolean indicating whether the URL seems to be incorrect.
-     */
-    public final static boolean verifyUrl(String url) {
-        if (url == null) {
-            return false;
-        }
-
-        if (url.startsWith("https://")) {
-            // URL doesn't understand the https protocol, hack it
-            url = "http://" + url.substring(8);
-        }
-
-        try {
-            new URL(url);
-
-            return true;
-        } catch (MalformedURLException e) {
-            return false;
-        }
-    }
-}

src/java/com/opensymphony/xwork2/util/URLUtil.java

             return null;
         }
     }
+
+    /**
+     * Verify That the given String is in valid URL format.
+     * @param url The url string to verify.
+     * @return a boolean indicating whether the URL seems to be incorrect.
+     */
+    public final static boolean verifyUrl(String url) {
+        if (url == null) {
+            return false;
+        }
+
+        if (url.startsWith("https://")) {
+            // URL doesn't understand the https protocol, hack it
+            url = "http://" + url.substring(8);
+        }
+
+        try {
+            new URL(url);
+
+            return true;
+        } catch (MalformedURLException e) {
+            return false;
+        }
+    }
 }

src/java/com/opensymphony/xwork2/util/finder/UrlSet.java

  */
 package com.opensymphony.xwork2.util.finder;
 
-import com.opensymphony.xwork2.util.TextUtils;
-
 import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;

src/java/com/opensymphony/xwork2/validator/validators/URLValidator.java

  */
 package com.opensymphony.xwork2.validator.validators;
 
-import com.opensymphony.xwork2.util.TextUtils;
 import com.opensymphony.xwork2.validator.ValidationException;
+import com.opensymphony.xwork2.util.URLUtil;
 
 
 /**
             return;
         }
 
-        if (!(value.getClass().equals(String.class)) || !TextUtils.verifyUrl((String) value)) {
+        if (!(value.getClass().equals(String.class)) || !URLUtil.verifyUrl((String) value)) {
             addFieldError(fieldName, object);
         }
     }

src/test/com/opensymphony/xwork2/util/TextUtilsTest.java

-/*
- * Copyright (c) 2002-2007 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork2.util;
-
-import com.opensymphony.xwork2.XWorkTestCase;
-
-import java.util.Arrays;
-
-/**
- * Unit test for {@link com.opensymphony.xwork2.util.TextUtils}.
- */
-public class TextUtilsTest extends XWorkTestCase {
-
-	public void testJavaScriptEscapeNoEscapes() {
-        assertEquals("", TextUtils.escapeJavaScript(null));
-        assertEquals("", TextUtils.escapeJavaScript(""));
-        assertEquals("   ", TextUtils.escapeJavaScript("   "));
-        assertEquals("Hello World", TextUtils.escapeJavaScript("Hello World"));
-	}
-	
-	public void testJavaScriptEscape() {
-		assertEquals("\\t", TextUtils.escapeJavaScript("\t"));
-		assertEquals("\\b", TextUtils.escapeJavaScript("\b"));
-		assertEquals("\\n", TextUtils.escapeJavaScript("\n"));
-		assertEquals("\\f", TextUtils.escapeJavaScript("\f"));
-		assertEquals("\\r", TextUtils.escapeJavaScript("\r"));
-		assertEquals("\\\\", TextUtils.escapeJavaScript("\\"));
-		assertEquals("\\\"", TextUtils.escapeJavaScript("\""));
-		assertEquals("\\'", TextUtils.escapeJavaScript("'"));
-		assertEquals("<\\/script", TextUtils.escapeJavaScript("</script"));
-	}
-	
-    public void testHtmlEncodeNoHTML() {
-        assertEquals("", TextUtils.htmlEncode(null));
-        assertEquals("", TextUtils.htmlEncode(""));
-        assertEquals("   ", TextUtils.htmlEncode("   "));
-        assertEquals("Hello World", TextUtils.htmlEncode("Hello World"));
-    }
-
-    public void testHtmlEncodeNoHTMLNoSpecial() {
-        assertEquals("", TextUtils.htmlEncode(null, false));
-        assertEquals("", TextUtils.htmlEncode("", false));
-        assertEquals("   ", TextUtils.htmlEncode("   ", false));
-        assertEquals("Hello World", TextUtils.htmlEncode("Hello World", false));
-    }
-
-    public void testHtmlEncodeSimple() {
-        assertEquals("Me &amp; My", TextUtils.htmlEncode("Me & My"));
-        assertEquals("Me &amp; My &amp; You", TextUtils.htmlEncode("Me & My & You"));
-        assertEquals("Me &amp; &quot;Special one&quot;", TextUtils.htmlEncode("Me & \"Special one\""));
-        assertEquals("100 &lt; 200", TextUtils.htmlEncode("100 < 200"));
-        assertEquals("200 &gt; 100", TextUtils.htmlEncode("200 > 100"));
-    }
-
-    public void testHtmlEncodeSimpleNoSpecial() {
-        assertEquals("Me &amp; My", TextUtils.htmlEncode("Me & My", false));
-        assertEquals("Me &amp; My &amp; You", TextUtils.htmlEncode("Me & My & You", false));
-        assertEquals("Me &amp; &quot;Special one&quot;", TextUtils.htmlEncode("Me & \"Special one\"", false));
-        assertEquals("100 &lt; 200", TextUtils.htmlEncode("100 < 200", false));
-        assertEquals("200 &gt; 100", TextUtils.htmlEncode("200 > 100", false));
-    }
-
-    public void testSpecialChars() {
-        assertEquals("Sp&#xE9;cial", TextUtils.htmlEncode("Sp\u00e9cial"));
-    }
-
-    public void testVerifyUrl() {
-        assertEquals(false, TextUtils.verifyUrl(null));
-        assertEquals(false, TextUtils.verifyUrl(""));
-        assertEquals(false, TextUtils.verifyUrl("   "));
-        assertEquals(false, TextUtils.verifyUrl("no url"));
-
-        assertEquals(true, TextUtils.verifyUrl("http://www.opensymphony.com"));
-        assertEquals(true, TextUtils.verifyUrl("https://www.opensymphony.com"));
-        assertEquals(true, TextUtils.verifyUrl("https://www.opensymphony.com:443/login"));
-        assertEquals(true, TextUtils.verifyUrl("http://localhost:8080/myapp"));
-    }
-}

src/test/com/opensymphony/xwork2/util/URLUtilTest.java

             //the factory cant be set multiple times..just ignore exception no biggie
         }
     }
+
+    public void testVerifyUrl() {
+        assertEquals(false, URLUtil.verifyUrl(null));
+        assertEquals(false, URLUtil.verifyUrl(""));
+        assertEquals(false, URLUtil.verifyUrl("   "));
+        assertEquals(false, URLUtil.verifyUrl("no url"));
+
+        assertEquals(true, URLUtil.verifyUrl("http://www.opensymphony.com"));
+        assertEquals(true, URLUtil.verifyUrl("https://www.opensymphony.com"));
+        assertEquals(true, URLUtil.verifyUrl("https://www.opensymphony.com:443/login"));
+        assertEquals(true, URLUtil.verifyUrl("http://localhost:8080/myapp"));
+    }
 }