Index: lutinutil/src/java/org/codelutin/util/StringUtil.java diff -u lutinutil/src/java/org/codelutin/util/StringUtil.java:1.19 lutinutil/src/java/org/codelutin/util/StringUtil.java:1.20 --- lutinutil/src/java/org/codelutin/util/StringUtil.java:1.19 Tue Jun 12 12:07:50 2007 +++ lutinutil/src/java/org/codelutin/util/StringUtil.java Sat Nov 17 10:35:35 2007 @@ -22,10 +22,10 @@ * * @author POUSSIN Benjamin * Copyright Code Lutin - * @version $Revision: 1.19 $ + * @version $Revision: 1.20 $ * - * Mise a jour: $Date: 2007-06-12 12:07:50 $ - * par : $Author: thimel $ + * Mise a jour: $Date: 2007-11-17 10:35:35 $ + * par : $Author: tchemit $ */ package org.codelutin.util; @@ -35,14 +35,15 @@ import java.text.MessageFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; /** - * Classe contenant un ensemle de m?thode static utiles pour la manipulation des - * cha?ne de caract?re mais qui ne sont pas d?fini dans la classe String de + * Classe contenant un ensemle de methode static utiles pour la manipulation des + * chaine de caractere mais qui ne sont pas defini dans la classe String de * Java. - * + * * @author poussin * @created 21 octobre 2003 */ @@ -118,11 +119,10 @@ } /** - * Met en majuscule le premier caract?re de la cha?ne pass? en param?tre - * - * @param word - * La cha?ne ? capitaliser - * @return La cha?ne capitalis?e + * Met en majuscule le premier caractere de la chaine passee en parametre + * + * @param word La chaine a capitaliser + * @return La chaine capitalisee * @deprecated you must use * org.apache.commons.lang.StringUtils.capitalise(String) */ @@ -131,11 +131,10 @@ } /** - * Met en minuscule le premier caract?re de la cha?ne pass? en param?tre - * - * @param word - * La cha?ne ? decapitaliser - * @return La cha?ne decapitalis?e + * Met en minuscule le premier caractere de la chaine passe en parametre + * + * @param word La chaine a decapitaliser + * @return La chaine decapitalisee * @deprecated you must use * org.apache.commons.lang.StringUtils.uncapitalise(String) */ @@ -144,14 +143,12 @@ } /** - * Concat?ne un tableau de cha?ne de caract?res en une seul cha?ne. - * - * @param s - * le tableau de cha?nes - * @param sep - * le s?parateur ? utiliser pour s?parer chaque cha?ne. - * @return la cha?ne r?sultante de la concat?nation. Si le tableau est null - * ou vide, la cha?ne r?sultante est une chaine vide. + * Concatène un tableau de chaine de caracteres en une seul chaine. + * + * @param s le tableau de chaines + * @param sep le separateur a utiliser pour separer chaque chaine. + * @return la chaine resultante de la concatenation. Si le tableau est null + * ou vide, la chaine resultante est une chaine vide. * @deprecated you must use org.apache.commons.lang.StringUtils.join */ public static String unsplit(String[] s, String sep) { @@ -161,23 +158,108 @@ StringBuffer result = new StringBuffer(s[0]); for (int i = 1; i < s.length; i++) { - result.append(sep + s[i]); + result.append(sep).append(s[i]); } return result.toString(); } + private static final Character[] openingChars = {'(', '{', '['}; + + private static final Character[] closingChars = {')', '}', ']'}; + /** * Split string use 'separator' as separator. If String contains "'()[]{} * this method count the number of open char end close char to split * correctly argument - * - * @param args - * string to split - * @param separator - * separator use to split string + * + * @param args string to split + * @param separator separator use to split string * @return array of string */ static public String[] split(String args, String separator) { + //TODO test + return split(openingChars,closingChars,args,separator); +// if (args == null) { +// return new String[0]; +// } +// +// List result = new ArrayList(); +// +// int start = 0; +// int end = 0; +// StringBuffer op = new StringBuffer(); // stack of {([ currently open +// char last = '\0'; // contains " or ' if string is openned +// +// for (int i = 0; i < args.length(); i++) { +// char c = args.charAt(i); +// if (c == '\\') { +// // pass next char +// i++; +// } else if (last != '"' && last != '\'') { +// if (c == '(' || c == '{' || c == '[') { +// // open ({| +// op.append(c); +// } else if (c == ')' || c == '}' || c == ']') { +// // close ({[ +// op.deleteCharAt(op.length() - 1); +// } else if (c == '"' || c == '\'') { +// // open string " or ' +// last = c; +// } else if (op.length() == 0 +// && args.regionMatches(i, separator, 0, separator +// .length())) { +// // end of one arguement +// end = i; +// // pass separator +// i += separator.length() - 1; +// +// String a = args.substring(start, end); +// result.add(a); +// // start of next argument +// start = end + separator.length(); +// } +// } else if (c == last) { +// // close string " or ' +// last = '\0'; +// } +// } +// +// if (start < args.length()) { +// String a = args.substring(start, args.length()); +// result.add(a); +// } +// return result.toArray(new String[result.size()]); + } + + + /** + * Use to split string array representation in array according with swixat + * seperator list + * + * @param stringList string that represent array + * @return array with length > 0 if listAsString != null or null + */ + static public String[] split(String stringList) { + String[] result = split(stringList, ","); + return result; + } + + /** + * Split string use 'separator' as separator. If String contains "' + * and openingChar closingChars + *

+ * this method count the number of open char end close char to split correctly + * argument + * + * @param openingChars list of opening caracteres + * @param closingChars list of closing caracteres + * @param args string to split + * @param separator separator use to split string + * @return array of string + */ + static public String[] split(Character[] openingChars, + Character[] closingChars, + String args, String separator) { if (args == null) { return new String[0]; } @@ -185,28 +267,29 @@ List result = new ArrayList(); int start = 0; - int end = 0; - StringBuffer op = new StringBuffer(); // stack of {([ currently open + int end; + StringBuffer op = new StringBuffer(); // stack of {([< currently open char last = '\0'; // contains " or ' if string is openned + List opening = Arrays.asList(openingChars); + + List closing = Arrays.asList(closingChars); + for (int i = 0; i < args.length(); i++) { char c = args.charAt(i); - if (c == '\\') { + if (c == '/') { // pass next char i++; } else if (last != '"' && last != '\'') { - if (c == '(' || c == '{' || c == '[') { - // open ({| + if (opening.contains(c)) { op.append(c); - } else if (c == ')' || c == '}' || c == ']') { - // close ({[ + } else if (closing.contains(c)) { op.deleteCharAt(op.length() - 1); } else if (c == '"' || c == '\'') { // open string " or ' last = c; - } else if (op.length() == 0 - && args.regionMatches(i, separator, 0, separator - .length())) { + } else if (op.length() == 0 && + args.regionMatches(i, separator, 0, separator.length())) { // end of one arguement end = i; // pass separator @@ -228,20 +311,7 @@ result.add(a); } - return (String[]) result.toArray(new String[result.size()]); - } - - /** - * Use to split string array representation in array according with swixat - * seperator list - * - * @param listAsString - * string that represent array - * @return array with length > 0 if listAsString != null or null - */ - static public String[] split(String stringList) { - String[] result = split(stringList, ","); - return result; + return result.toArray(new String[result.size()]); } public static boolean toBoolean(String s) { @@ -345,14 +415,13 @@ /** * Essai de convertir une chaine de caractere en une couleur si possible si * ce n'est pas possible retourne null. - * - * @param s - * la couleur sous la forme de string, par exemple "red", - * "yellow" ou bien en RGB "#FFAA99", et avec un canal alpha - * "#FFAA3366" + * + * @param s la couleur sous la forme de string, par exemple "red", + * "yellow" ou bien en RGB "#FFAA99", et avec un canal alpha + * "#FFAA3366" * @return la couleur demandé si possible sinon null - * @throws IllegalAccessException * @throws IllegalArgumentException + * @throws StringUtilException */ public static Color toColor(String s) throws StringUtilException { try {