001: /**
002: *
003: */package clime.messadmin.utils;
004:
005: /**
006: * Copy of org.apache.commons.lang.StringUtils 2.1
007: * @author Cédrik LIME
008: */
009: public class StringUtils {
010:
011: private StringUtils() {
012: }
013:
014: /**
015: * Checks if a String is not empty ("") and not null.
016: *
017: * @param str the String to check, may be null
018: * @return <code>true</code> if the String is not empty or null
019: */
020: public static boolean isEmpty(String str) {
021: return str == null || str.length() == 0;
022: }
023:
024: /**
025: * Checks if a String is not empty ("") and not null.
026: *
027: * @param str the String to check, may be null
028: * @return <code>true</code> if the String is not empty and not null
029: */
030: public static boolean isNotEmpty(String str) {
031: return str != null && str.length() > 0;
032: }
033:
034: /**
035: * Checks if a String is whitespace, empty ("") or null.
036: *
037: * @param str the String to check, may be null
038: * @return <code>true</code> if the String is null, empty or whitespace
039: */
040: public static boolean isBlank(String str) {
041: int strLen;
042: if (str == null || (strLen = str.length()) == 0) {
043: return true;
044: }
045: for (int i = 0; i < strLen; ++i) {
046: if (!Character.isWhitespace(str.charAt(i))) {
047: return false;
048: }
049: }
050: return true;
051: }
052:
053: /**
054: * Checks if a String is not empty (""), not null and not whitespace only.
055: *
056: * @param str the String to check, may be null
057: * @return <code>true</code> if the String is not empty and not null and not whitespace
058: */
059: public static boolean isNotBlank(String str) {
060: int strLen;
061: if (str == null || (strLen = str.length()) == 0) {
062: return false;
063: }
064: for (int i = 0; i < strLen; ++i) {
065: if (!Character.isWhitespace(str.charAt(i))) {
066: return true;
067: }
068: }
069: return false;
070: }
071:
072: private static final int HIGHEST_SPECIAL = '>';
073: private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][];
074: static {
075: specialCharactersRepresentation['&'] = "&".toCharArray();//$NON-NLS-1$
076: specialCharactersRepresentation['<'] = "<".toCharArray();//$NON-NLS-1$
077: specialCharactersRepresentation['>'] = ">".toCharArray();//$NON-NLS-1$
078: specialCharactersRepresentation['"'] = """.toCharArray();//$NON-NLS-1$
079: specialCharactersRepresentation['\''] = "'".toCharArray();//$NON-NLS-1$
080: }
081:
082: /**
083: * Copied and adapted from org.apache.taglibs.standard.tag.common.core.Util v1.1.2
084: *
085: * Performs the following substring replacements
086: * (to facilitate output to XML/HTML pages):
087: *
088: * & -> &
089: * < -> <
090: * > -> >
091: * " -> "
092: * ' -> '
093: *
094: * See also OutSupport.writeEscapedXml().
095: */
096: public static String escapeXml(String buffer) {
097: if (null == buffer) {
098: return null;
099: }
100: int start = 0;
101: int length = buffer.length();
102: char[] arrayBuffer = buffer.toCharArray();
103: StringBuffer escapedBuffer = null;
104:
105: for (int i = 0; i < length; ++i) {
106: char c = arrayBuffer[i];
107: if (c <= HIGHEST_SPECIAL) {
108: char[] escaped = specialCharactersRepresentation[c];
109: if (escaped != null) {
110: // create StringBuffer to hold escaped xml string
111: if (start == 0) {
112: escapedBuffer = new StringBuffer(length + 5);
113: }
114: // add unescaped portion
115: if (start < i) {
116: escapedBuffer.append(arrayBuffer, start, i
117: - start);
118: }
119: start = i + 1;
120: // add escaped xml
121: escapedBuffer.append(escaped);
122: }
123: }
124: }
125: // no xml escaping was necessary
126: if (start == 0) {
127: return buffer;
128: }
129: // add rest of unescaped portion
130: if (start < length) {
131: escapedBuffer.append(arrayBuffer, start, length - start);
132: }
133: return escapedBuffer.toString();
134: }
135: }
|