Ver Fonte

SEC-812: Added missing TextUtils file

Luke Taylor há 17 anos atrás
pai
commit
fca3a2a709

+ 33 - 0
core/src/main/java/org/springframework/security/util/TextUtils.java

@@ -0,0 +1,33 @@
+package org.springframework.security.util;
+
+/**
+ * Utilities for working with Strings and text.
+ * 
+ * @author Luke Taylor
+ * @version $Id$
+ */
+public abstract class TextUtils {
+
+    public static String escapeEntities(String s) {
+        StringBuffer sb = new StringBuffer();
+        
+        for (int i=0; i < s.length(); i++) {
+            char c = s.charAt(i);
+            
+            if(c == '<') {
+                sb.append("&lt;");
+            } else if (c == '>') {
+                sb.append("&gt;");
+            } else if (c == '"') {
+                sb.append("&#034;");
+            } else if (c == '\'') {
+                sb.append("&#039;");
+            } else {
+                sb.append(c);
+            }
+        }
+        
+        return sb.toString();
+    }
+    
+}

+ 14 - 0
core/src/test/java/org/springframework/security/util/TextUtilsTests.java

@@ -0,0 +1,14 @@
+package org.springframework.security.util;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TextUtilsTests {
+
+    @Test
+    public void charactersAreEscapedCorrectly() {
+        assertEquals("a&lt;script&gt;&#034;&#039;", TextUtils.escapeEntities("a<script>\"'"));
+    }
+    
+}