SEC-812: Added missing TextUtils file

This commit is contained in:
Luke Taylor 2008-05-05 19:09:09 +00:00
parent fa44c74993
commit fca3a2a709
2 changed files with 47 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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>\"'"));
}
}