一、前言
关于flex-webtier源码包中flex.webtier.util.ObjectUtil、flex.webtier.util.StringUtils对象字符串工具类,涉及具体对字符串及对象相关处理内容参见源码说明部分。
二、源码说明
1.ObjectUtil类
package flex.webtier.util;@b@@b@import java.lang.reflect.Field;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.util.Hashtable;@b@@b@public class ObjectUtil@b@{@b@ private static Hashtable primitiveClasses_ = new Hashtable();@b@@b@ public static Field findField(Object instance, String name)@b@ {@b@ try@b@ {@b@ return instance.getClass().getField(name); } catch (NoSuchFieldException e) {@b@ }@b@ return null;@b@ }@b@@b@ public static Method findMethod(Object instance, String name, Class[] params)@b@ {@b@ try@b@ {@b@ return instance.getClass().getMethod(name, params); } catch (NoSuchMethodException e) {@b@ }@b@ return null;@b@ }@b@@b@ public static Object getValue(Object instance, String fieldName)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ Field f = findField(instance, fieldName);@b@ if (f != null)@b@ return f.get(instance);@b@@b@ fieldName = StringUtils.upperCaseFirstInitial(fieldName);@b@ Method get = findMethod(instance, "get".concat(fieldName), new Class[0]);@b@ if (get == null)@b@ {@b@ get = findMethod(instance, "is".concat(fieldName), new Class[0]);@b@ if (get == null)@b@ {@b@ return null;@b@ }@b@ }@b@@b@ return get.invoke(instance, new Object[] { (Object)null });@b@ }@b@@b@ public static void setValue(Object instance, Object value, Field field) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ setValue(instance, field.getName(), field.getType(), value);@b@ }@b@@b@ public static void setValue(Object instance, String fieldName, Class fieldClass, Object value)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ Field f = findField(instance, fieldName);@b@ if (f != null) {@b@ f.set(instance, value);@b@ } else {@b@ Class[] params = new Class[1];@b@ params[0] = fieldClass;@b@@b@ fieldName = StringUtils.upperCaseFirstInitial(fieldName);@b@ Method set = findMethod(instance, "set".concat(fieldName), params);@b@ if (set == null) {@b@ set = findMethod(instance, "set".concat(fieldName), params);@b@ if (set == null)@b@ {@b@ throw new NoSuchMethodException("set".concat(fieldName) + " with args " + params[0].getName()); }@b@ }@b@ Object[] args = { value };@b@ set.invoke(instance, args);@b@ }@b@ }@b@@b@ public static String replaceVars(Object instance, String s)@b@ {@b@ if (s == null)@b@ return null;@b@@b@ StringBuffer out = new StringBuffer(2 * s.length());@b@@b@ int i = 0;@b@ int len = s.length();@b@ do@b@ {@b@ int j = s.indexOf(123, i);@b@ if (j == -1) {@b@ j = len;@b@ out.append(s.substring(i, j));@b@ break;@b@ }@b@@b@ if (j > i) {@b@ out.append(s.substring(i, j));@b@ }@b@@b@ i = s.indexOf(125, j);@b@ if (i == -1) {@b@ i = len;@b@ out.append(s.substring(j, i));@b@ break;@b@ }@b@@b@ String name = s.substring(j + 1, i);@b@ Object value = null;@b@ try {@b@ value = getValue(instance, name);@b@ }@b@ catch (IllegalAccessException ex) {@b@ ex.printStackTrace();@b@ }@b@ catch (InvocationTargetException ex) {@b@ ex.printStackTrace();@b@ }@b@@b@ if (value != null) {@b@ out.append(value);@b@ }@b@ else {@b@ out.append('{').append(name).append('}');@b@ }@b@@b@ ++i; }@b@ while (i < len);@b@@b@ return out.toString();@b@ }@b@@b@ public static String getClassName(Object instance) {@b@ String packageName = instance.getClass().getName();@b@ int index = packageName.lastIndexOf(46);@b@ if (index >= 0)@b@ return packageName.substring(index + 1);@b@@b@ return packageName;@b@ }@b@@b@ static@b@ {@b@ primitiveClasses_.put(Character.TYPE.getName(), Character.TYPE);@b@ primitiveClasses_.put(Boolean.TYPE.getName(), Boolean.TYPE);@b@ primitiveClasses_.put(Byte.TYPE.getName(), Byte.TYPE);@b@ primitiveClasses_.put(Integer.TYPE.getName(), Integer.TYPE);@b@ primitiveClasses_.put(Long.TYPE.getName(), Long.TYPE);@b@ primitiveClasses_.put(Short.TYPE.getName(), Short.TYPE);@b@ primitiveClasses_.put(Float.TYPE.getName(), Float.TYPE);@b@ primitiveClasses_.put(Double.TYPE.getName(), Double.TYPE);@b@ }@b@}
2.StringUtils类
package flex.webtier.util;@b@@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.IOException;@b@import java.util.Vector;@b@@b@public class StringUtils@b@{@b@ public static String quoteIfNotNull(String str)@b@ {@b@ return (str = (str != null) ? (str = "'" + escapeApostrophe(str) + "'") : null);@b@ }@b@@b@ public static String doubleQuoteIfNotNull(String str) {@b@ return (str = (str != null) ? (str = "\"" + str + "\"") : null);@b@ }@b@@b@ public static String escapeApostrophe(String str) {@b@ if (str.indexOf("'") > -1)@b@ return substitute(str, "'", "''");@b@@b@ return str;@b@ }@b@@b@ public static String appendToQuotedString(String str1, String str2) throws Exception {@b@ StringBuffer sb = new StringBuffer();@b@ if ((str1.startsWith("\"")) && (str1.endsWith("\""))) {@b@ sb.append(str1);@b@ sb.insert(sb.length() - 1, str2);@b@ }@b@ else {@b@ sb.append("\"");@b@ sb.append(str1);@b@ sb.append(str2);@b@ sb.append("\"");@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String removeDoubleChar(String str, char c)@b@ {@b@ return removeDuplicates(str, c);@b@ }@b@@b@ public static String removeDuplicates(String str, char c)@b@ {@b@ if (str == null) { return str;@b@ }@b@@b@ int newCharPos = str.indexOf(c);@b@ if ((newCharPos < 0) || (newCharPos + 1 == str.length())) { return str;@b@ }@b@@b@ if ((str == null) || (str.length() < 2)) {@b@ return str;@b@ }@b@@b@ char[] chars = str.toCharArray();@b@ char[] newChars = new char[chars.length];@b@ newCharPos = 0;@b@@b@ boolean lastWasChar = false;@b@@b@ for (int i = 0; i < chars.length; ++i)@b@ {@b@ char thisChar = chars[i];@b@ if (thisChar == c)@b@ {@b@ if (lastWasChar)@b@ break label106:@b@@b@ lastWasChar = true;@b@ }@b@ else {@b@ lastWasChar = false;@b@ }@b@ newChars[(newCharPos++)] = thisChar;@b@ }@b@@b@ label106: return new String(newChars, 0, newCharPos);@b@ }@b@@b@ public static int findSingleChar(String str, int currentIndex, int endIndex, char c)@b@ {@b@ if (str == null) return endIndex;@b@ boolean foundChar = false;@b@ int strIndex = endIndex;@b@ while (true) {@b@ while (true) { if (foundChar) break label59;@b@ strIndex = str.indexOf(c, currentIndex);@b@@b@ if (strIndex == -1) {@b@ return endIndex;@b@ }@b@@b@ if (str.charAt(strIndex + 1) != c) break;@b@ currentIndex = strIndex + 2;@b@ }@b@ foundChar = true;@b@ }@b@@b@ label59: return strIndex;@b@ }@b@@b@ public static String loadFileToString(String filename)@b@ throws IOException@b@ {@b@ if (filename == null) return null;@b@@b@ File f = new File(filename);@b@ int size = (int)f.length();@b@ int bytes_read = 0;@b@ FileInputStream in = new FileInputStream(f);@b@ byte[] data = new byte[size];@b@@b@ while (bytes_read < size)@b@ bytes_read += in.read(data, bytes_read, size - bytes_read);@b@@b@ in.close();@b@@b@ return new String(data);@b@ }@b@@b@ public static String[] parseArguments(String str, Character trimChar)@b@ {@b@ if (str == null) return null;@b@@b@ int quote = 0;@b@ int paren = 0;@b@ boolean done = false;@b@ Vector args = new Vector();@b@@b@ char[] chars = str.toCharArray();@b@@b@ int endIndex = str.length() - 1;@b@ int index = 0;@b@@b@ while (!(done))@b@ {@b@ StringBuffer param = new StringBuffer(255);@b@@b@ int lastIndex = index;@b@@b@ while ((index < endIndex) && (((quote > 0) || (paren > 0) || ((chars[index] != ')') && (((index == 0) || ((chars[index] != ',') && (chars[(index - 1)] != '\\'))))))))@b@ {@b@ if (chars[index] == '\\') {@b@ ++index;@b@ }@b@ else if (chars[index] == '"') {@b@ quote = 1 - quote;@b@ }@b@ else if (chars[index] == '(') {@b@ ++paren;@b@ }@b@ else if (chars[index] == ')') {@b@ --paren;@b@ }@b@@b@ param.append(chars[(index++)]);@b@ }@b@@b@ if (index > lastIndex) {@b@ String s = param.toString().trim();@b@ if ((!(s.equals(""))) && (s.charAt(0) == '"') && (s.charAt(s.length() - 1) == '"'))@b@ s = s.substring(1, s.length() - 1);@b@ args.addElement(s);@b@ }@b@@b@ ++index;@b@@b@ if (index >= endIndex)@b@ done = true;@b@@b@ }@b@@b@ String[] strArgs = new String[args.size()];@b@@b@ for (int i = 0; i < args.size(); ++i) {@b@ String arg = (String)args.elementAt(i);@b@ if ((trimChar != null) && @b@ (arg != null) && (arg.length() > 1) && (arg.charAt(0) == trimChar.charValue())) {@b@ arg = arg.substring(1);@b@ }@b@@b@ strArgs[i] = arg;@b@ }@b@@b@ return strArgs;@b@ }@b@@b@ public static String databaseQuote(String value, boolean wrapQuotes)@b@ {@b@ if (value == null) return value;@b@@b@ String result = "";@b@ if (wrapQuotes) result = "'";@b@@b@ for (int i = 0; i < value.length(); ++i) {@b@ String s = value.substring(i, i + 1);@b@ if (s.equals("'"))@b@ result = result + "''";@b@ else@b@ result = result + s;@b@ }@b@@b@ if (wrapQuotes) result = result + "'";@b@@b@ return result;@b@ }@b@@b@ public static String databaseQuote(String value) {@b@ return databaseQuote(value, true);@b@ }@b@@b@ public static String surroundWithQuotes(String name) {@b@ return "\"" + name + "\"";@b@ }@b@@b@ public static String surroundWithSingleQuotes(String name) {@b@ return "'" + name + "'";@b@ }@b@@b@ public static String escapeQuotes(String s) {@b@ if (s == null) return s;@b@@b@ StringBuffer sb = new StringBuffer();@b@@b@ int i = 0;@b@ int len = s.length();@b@ while (i < len) {@b@ char c = s.charAt(i);@b@ if (c == '"')@b@ sb.append("\\\"");@b@ else@b@ sb.append(c);@b@ ++i;@b@ }@b@@b@ return sb.toString();@b@ }@b@@b@ public static boolean matchString(String pattern, String string, char sep)@b@ {@b@ if ((pattern == null) || (string == null))@b@ return false;@b@@b@ int p = 0;@b@ while (true) { int s = 0;@b@ while (true) { boolean sEnd = s >= string.length();@b@ boolean pEnd = (p >= pattern.length()) || (pattern.charAt(p) == sep);@b@@b@ if ((sEnd) && (pEnd))@b@ return true;@b@ if (sEnd) break; if (pEnd)@b@ break;@b@ if (pattern.charAt(p) == '?')@b@ break label161:@b@ if (pattern.charAt(p) == '*')@b@ {@b@ ++p;@b@ int i = string.length();@b@ while (true) { if (i < s) break label170;@b@ if (matchString(pattern.substring(p), string.substring(i)))@b@ {@b@ return true;@b@ }@b@ --i;@b@ }@b@@b@ }@b@@b@ if (pattern.charAt(p) != string.charAt(s))@b@ break;@b@ label161: ++p; ++s;@b@ }@b@@b@ label170: p = pattern.indexOf(sep, p);@b@ if (p == -1)@b@ return false;@b@ ++p;@b@ }@b@ }@b@@b@ public static boolean matchString(String pattern, String string)@b@ {@b@ return matchString(pattern, string, '|');@b@ }@b@@b@ public static void sortStrings(String[] strings)@b@ {@b@ int i;@b@ if (strings != null)@b@ {@b@ for (i = 0; i < strings.length - 1; ++i)@b@ for (int j = i + 1; j < strings.length; ++j)@b@ if (strings[i].compareTo(strings[j]) > 0) {@b@ String t = strings[i];@b@ strings[i] = strings[j];@b@ strings[j] = t;@b@ }@b@ }@b@ }@b@@b@ public static String leftPad(String str, int pad)@b@ {@b@ if (str == null)@b@ return null;@b@@b@ if (str.length() >= pad)@b@ return str;@b@@b@ int len = pad - str.length();@b@@b@ for (int i = 0; i < len; ++i)@b@ str = " " + str;@b@@b@ return str;@b@ }@b@@b@ public static String substitute(String str, String from, String to)@b@ {@b@ if ((from == null) || (from.equals("")) || (to == null))@b@ return str;@b@@b@ int index = str.indexOf(from);@b@@b@ if (index == -1)@b@ return str;@b@@b@ StringBuffer buf = new StringBuffer(str.length());@b@ int lastIndex = 0;@b@@b@ while (index != -1) {@b@ buf.append(str.substring(lastIndex, index));@b@ buf.append(to);@b@ lastIndex = index + from.length();@b@ index = str.indexOf(from, lastIndex);@b@ }@b@@b@ buf.append(str.substring(lastIndex));@b@@b@ return buf.toString();@b@ }@b@@b@ public static String getHeaderParameter(String header, String parameter)@b@ {@b@ return getHeaderParameter(header, parameter, null);@b@ }@b@@b@ public static String getHeaderParameter(String header, String parameter, String deflt)@b@ {@b@ if ((header == null) || (parameter == null))@b@ return deflt;@b@@b@ int i = header.indexOf(parameter);@b@@b@ if (i == -1)@b@ return deflt;@b@@b@ int start = header.indexOf(61, i) + 1;@b@ int end = header.indexOf(59, start);@b@@b@ if (end == -1)@b@ return header.substring(start).trim();@b@@b@ return header.substring(start, end).trim();@b@ }@b@@b@ public static String stripParamsFromHeader(String header)@b@ {@b@ if (header == null)@b@ return null;@b@@b@ int semi_colon = header.indexOf(59);@b@@b@ if (semi_colon == -1)@b@ return header;@b@@b@ return header.substring(0, semi_colon).trim();@b@ }@b@@b@ public static boolean isJavaEscape(char c)@b@ {@b@ return ((c == 'b') || (c == 't') || (c == 'n') || (c == 'f') || (c == 'r') || (c == '"') || (c == '\'') || (c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') || (c == '5') || (c == '6') || (c == '7') || (c == '\\') || (c == 'u'));@b@ }@b@@b@ public static int occurrences(String s, char c)@b@ {@b@ if (s == null) return 0;@b@@b@ char[] ca = s.toCharArray();@b@ int count = 0;@b@ int i = 0;@b@ for (; i < ca.length; ++i)@b@ if (ca[i] == c)@b@ ++count;@b@@b@ return count;@b@ }@b@@b@ public static String arrayToString(String[] str, char sep)@b@ {@b@ if (str == null) return null;@b@@b@ StringBuffer sb = new StringBuffer();@b@ for (int i = 0; i < str.length; ++i) {@b@ sb.append(str[i]);@b@ sb.append(sep);@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String entitizeHtml(String buffer)@b@ {@b@ if (buffer == null) return buffer;@b@@b@ StringBuffer sb = new StringBuffer();@b@ for (int i = 0; i < buffer.length(); ++i) {@b@ char c = buffer.charAt(i);@b@ switch (c)@b@ {@b@ case '>':@b@ sb.append(">");@b@ break;@b@ case '<':@b@ sb.append("<");@b@ break;@b@ default:@b@ sb.append(c);@b@ }@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String lowerCaseFirstInitial(String str) {@b@ if ((str != null) && (str.length() >= 1))@b@ return str.substring(0, 1).toLowerCase() + str.substring(1);@b@ return str;@b@ }@b@@b@ public static String upperCaseFirstInitial(String str) {@b@ if ((str != null) && (str.length() >= 1))@b@ return str.substring(0, 1).toUpperCase() + str.substring(1);@b@ return str;@b@ }@b@}