📄 stringpairtokenizer.java
字号:
//String regex="\\s*\"(.*?)\"\\s*|(?<=^|,)[^,]*" StringBuffer sb = new StringBuffer(); sb.append("\\s*"+quotechar+".*?"+quotechar+"\\s*"); //all matches enclosed in double quotes (\s*".*?"\s*) with leading/trailing white spaces sb.append("|"); //or sb.append("(?<=^|"+separator+")"); //beginning of line or comma (^|,) via zero-width positive lookbehind ((?<=)) followed by sb.append("[^"+separator+"]*"); //any character except comma ([^,]) zero or more times (*) String regex = sb.toString(); while (field.startsWith(separator)) field = field.substring(1); // hack // DOES NOT WORK STABLE, for exampel '/node/heron/client/joe' fails // whereas '/node/heron/client/\"joe/xx\"' works //String regex = "(?:[^\",]+?(?=,))|(\".+\")"; //String regex = "(?:[^\""+separator+"]+?(?="+separator+"))|(\".+\")"; //String regex = "(?:[^"+quotechar+separator+"]+?(?="+separator+"))|("+quotechar+".+"+quotechar+")"; Pattern pat = Pattern.compile(regex); Matcher mat = pat.matcher(field); ArrayList list = new ArrayList(); while (mat.find()) { String s = mat.group(); list.add(s); } return (String[])list.toArray(new String[list.size()]); } */ /* public static final char ESCAPE_CHARACTER = '\\'; public static final char DEFAULT_SEPARATOR = ','; public static final char DEFAULT_QUOTE_CHARACTER = '"'; * Writes the next line to the file. * * @param nextLine a string array with each comma-separated element as a separate * entry. * * @throws IOException * if bad things happen during the write public void writeNext(String[] nextLine) throws IOException { StringBuffer sb = new StringBuffer(); for (int i = 0; i < nextLine.length; i++) { String nextElement = nextLine[i]; sb.append(quotechar); for (int j = 0; j < nextElement.length(); j++) { char nextChar = nextElement.charAt(j); if (nextChar == quotechar) { sb.append(ESCAPE_CHARACTER).append(nextChar); } else if (nextChar == ESCAPE_CHARACTER) { sb.append(ESCAPE_CHARACTER).append(nextChar); } else { sb.append(nextChar); } } sb.append(quotechar); if (i != nextLine.length - 1) { sb.append(separator); } } sb.append('\n'); pw.write(sb.toString()); } */ /** * Convert a separator based string to an array of strings. * <p /> * Example:<br /> * NameList=Josua,David,Ken,Abel<br /> * Will return each name separately in the array. * @param key the key to look for * @param defaultVal The default value to return if key is not found * @param separator The separator, typically "," * @return The String array for the given key, the elements are trimmed (no leading/following white spaces), is never null */ public static final String[] toArray(String str, String separator) { if (str == null) { return new String[0]; } if (separator == null) { String[] a = new String[1]; a[0] = str; return a; } StringTokenizer st = new StringTokenizer(str, separator); int num = st.countTokens(); String[] arr = new String[num]; int ii=0; while (st.hasMoreTokens()) { arr[ii++] = st.nextToken().trim(); } return arr; } /** * Dumps the given map to a human readable string. * @param map * @return e.g. "key1=15,name=joe" */ public static String dumpMap(Map map) { StringBuffer buf = new StringBuffer(); if (map != null) { boolean first = true; Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); buf.append(entry.getKey()).append("=").append(entry.getValue()); if (!first) buf.append(","); first = false; } } else buf.append("null"); return buf.toString(); } /** * If a value is missing then a null object will be put into the map as value. * The map returns pairs 'String,ClientProperty' if wantClientProperties is true, * otherwise it returns 'String,String' pairs. * * @param rawString e.g. "org.xmlBlaster.protocol.soap.SoapDriver,classpath=xerces.jar:soap.jar,MAXSIZE=100" * @param outerToken is for example ";" or "," * @param innterToken is for example "=" or " " * @param wantClientProperties if set to <code>true</code> returns pairs 'String,ClientProperty', returns 'String,String' pairs otherwise. */ private static Map parseStringProperties(String rawString, String outerToken, String innerToken, boolean wantClientProperties) { if (rawString==null) throw new IllegalArgumentException("SessionInfo.parsePropertyValue(null)"); Map ret = new HashMap(); StringTokenizer st = new StringTokenizer(rawString, outerToken); while(st.hasMoreTokens()) { String tok = st.nextToken().trim(); int pos = tok.indexOf(innerToken); if (pos < 0) { ret.put(tok.trim(), null); } else { String key = tok.substring(0,pos).trim(); String value = tok.substring(pos+1).trim(); if (wantClientProperties) ret.put(key, new ClientProperty(key, null, null, value)); else ret.put(key, value); } } return ret; } /** * @param rawString e.g. "org.xmlBlaster.protocol.soap.SoapDriver,classpath=xerces.jar:soap.jar,MAXSIZE=100" * @param outerToken is for example ";" or "," * @param innterToken is for example "=" or " " * If a value is missing then a null object as value. * the map returns pairs 'String,ClientProperty'. */ public static Map parseToStringClientPropertyPairs(String rawString, String outerToken, String innerToken) { return parseStringProperties(rawString, outerToken, innerToken, true); } /** * @param rawString e.g. "org.xmlBlaster.protocol.soap.SoapDriver,classpath=xerces.jar:soap.jar,MAXSIZE=100" * @param outerToken is for example ";" or "," * @param innterToken is for example "=" or " " * If a value is missing then a null object as value. * the map returns pairs 'String,String'. */ public static Map parseToStringStringPairs(String rawString, String outerToken, String innerToken) { return parseStringProperties(rawString, outerToken, innerToken, false); } public static String arrayToCSV(String[] strs, String sep) { if (strs == null || strs.length < 1) return ""; StringBuffer buf = new StringBuffer(strs.length*100); for (int i=0; i<strs.length; i++) { if (strs[i] == null) continue; // Ignore if (buf.length() > 0) buf.append(sep); buf.append(strs[i]); } return buf.toString(); } // java org.xmlBlaster.util.StringPairTokenizer public static void main(String[] args) { String test = (args.length > 0) ? args[0] : "/node/heron/client/\"joe/the/great\""; String separator = (args.length > 1) ? args[1] : "/"; String quotechar = (args.length > 2) ? args[2] : "\""; System.out.println("From '" + test + "' we get"); /* { System.out.println("with split():"); String[] result = split(test, separator, quotechar); for (int i=0; i<result.length; i++) System.out.println("'" + result[i] + "'"); } */ { System.out.println("with parseLine():"); String[] result = parseLine(test, separator.charAt(0), quotechar.charAt(0), true); for (int i=0; i<result.length; i++) System.out.println("'" + result[i] + "'"); } { System.out.println("\nTesting with quotes:\n"); String[] nextLines = { "org.xmlBlaster.protocol.soap.SoapDriver,\"classpath=xerces.jar:soap.jar,all\",MAXSIZE=100,a=10,\"b=", "20\",c=30" }; Map map = parseLine(nextLines, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_INNER_SEPARATOR, true, false, true); java.util.Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -