Index: lutinutil/src/java/org/codelutin/util/StringUtil.java diff -u lutinutil/src/java/org/codelutin/util/StringUtil.java:1.22 lutinutil/src/java/org/codelutin/util/StringUtil.java:1.23 --- lutinutil/src/java/org/codelutin/util/StringUtil.java:1.22 Wed Nov 28 01:21:37 2007 +++ lutinutil/src/java/org/codelutin/util/StringUtil.java Sun Dec 2 05:16:20 2007 @@ -22,9 +22,9 @@ * * @author POUSSIN Benjamin * Copyright Code Lutin - * @version $Revision: 1.22 $ + * @version $Revision: 1.23 $ * - * Mise a jour: $Date: 2007-11-28 01:21:37 $ + * Mise a jour: $Date: 2007-12-02 05:16:20 $ * par : $Author: tchemit $ */ package org.codelutin.util; @@ -57,6 +57,7 @@ * Cette methode retire les accents. Quand le caractere est connu, elle le * remplace par une version sans accent sinon, elle le supprime. * Les caracteres non-alphanumeriques sont supprimes. + * * @param s la chaine a unaccentuer * @return la chaine sans accent */ @@ -424,7 +425,7 @@ * "#FFAA3366" * @return la couleur demandé si possible sinon null * @throws IllegalArgumentException - * @throws StringUtilException + * @throws StringUtilException if any problem while conversion */ public static Color toColor(String s) throws StringUtilException { try { @@ -507,23 +508,22 @@ return result; } - static public void printCardinalite(StringBuilder sb, String txt, int min, int max, boolean mandatory) { + static public void printCardinalite(StringBuilder sb, String txt, int min, int max, boolean mandatory, String mo, String mc, String oo, String oc) { boolean print = false; if (!mandatory) { if (max == -1) { - sb.append('(').append(txt).append(')'); + sb.append(oo).append(txt).append(oc); sb.append('*'); } else { if (max == 1 && min == 0) { - sb.append('[').append(txt).append(']'); + sb.append(oo).append(txt).append(oc); } else { - sb.append('(').append(txt).append(')'); + sb.append(mo).append(txt).append(mc); print = true; } - } } else { - sb.append('(').append(txt).append(')'); + sb.append(mo).append(txt).append(mc); if (max == -1 && min == 1) { sb.append('+'); } else { @@ -533,7 +533,16 @@ } } if (print) { - sb.append('{').append(min).append(',').append(max).append('}'); + sb.append('{'); + sb.append(min); + if (max != min) { + if (max==-1) { + sb.append('+'); + } else { + sb.append(',').append(max); + } + } + sb.append('}'); } } @@ -576,6 +585,11 @@ return opens.isEmpty(); } + /** + * Convertir un nom en une constante Java + * @param name le nom à convertir + * @return la constante générée + */ public static String convertToConstantName(String name) { StringBuilder sb = new StringBuilder(); for (int i = 0, j = name.length(); i < j; i++) { @@ -595,6 +609,67 @@ } } String result = sb.toString(); - return result.startsWith("_")?result.substring(1):result; + return result.startsWith("_") ? result.substring(1) : result; + } + + /** + * Parse la cardinalite à la fin d'un texte. + * + * TODO A améliorer. + * + * @param key la valeur dont on cherche la cardinalité + * @param mandatory flag to say if this is a mandatory element or not. + * @return un tableau contenant 3 object : le texte donné sans les + * informations de cardinalité, la répétitionMin, la répétitionMax. + */ + public static Object[] parseCardinalite(String key, boolean mandatory) { + Object[] result = new Object[3]; + int repetitionMin; + int repetitionMax; + String cleanKey; + + if (key.endsWith("+")) { + // cardinalité + = {1,-1} + repetitionMin = 1; + repetitionMax = -1; + cleanKey = key.substring(0, key.length() - 1); + } else { + if (key.endsWith("*")) { + // cardinalité * = {0,-1} + repetitionMin = 0; + repetitionMax = -1; + cleanKey = key.substring(0, key.length() - 1); + } else { + if (key.endsWith("}")) { + // cardinalité spécifiée + int index = key.indexOf("{"); + cleanKey = key.substring(0, index); + String tmp = key.substring(index + 1, key.length() - 1); + int comaIndex = tmp.indexOf(','); + if (comaIndex == -1) { + // cardinalité unique + repetitionMax = repetitionMin = Integer.valueOf(tmp); + } else { + repetitionMin = Integer.valueOf(tmp.substring(0, comaIndex)); + repetitionMax = Integer.valueOf(tmp.substring(comaIndex + 1)); + } + } else { + // pas de cardinalité + // rien à faire (on prend les valeurs par defaut) + cleanKey = key; + if (mandatory) { + repetitionMin = 1; + repetitionMax = 1; + } else { + repetitionMin = 0; + repetitionMax = 1; + } + } + } + } + result[0] = cleanKey.trim().toLowerCase(); + result[1] = repetitionMin; + result[2] = repetitionMax; + return result; } }