Index: lutinutil/src/java/org/codelutin/util/ConverterUtil.java
diff -u lutinutil/src/java/org/codelutin/util/ConverterUtil.java:1.4 lutinutil/src/java/org/codelutin/util/ConverterUtil.java:1.5
--- lutinutil/src/java/org/codelutin/util/ConverterUtil.java:1.4 Wed Feb 13 15:22:43 2008
+++ lutinutil/src/java/org/codelutin/util/ConverterUtil.java Wed Feb 13 20:54:34 2008
@@ -21,63 +21,60 @@
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
-import java.net.URL;
-import java.net.URI;
-import java.util.Arrays;
-
/**
- * Une classe contenant des méthodes utiles sur les converters
+ * Une classe contenant des méthodes utiles sur les converters et les conversions
*
* @author tony
*/
public class ConverterUtil {
- private static final Class[] AUTO_REGISTRED_CONVERTER = new Class[]{URL.class, URI.class,VersionNumber.class};
+ protected static final String CONVERTER_PACKAGE = "org.codelutin.util";
+
/**
* Cherche un converter pour un type donné.
*
+ * Recherche dans un premier temps dans les converteurs déjà connus.
+ *
* Si le type est une énum et qu'aucun converter, n'a été trouvé, on
* enregistre un nouveau convert d'enum.
- *
- * Si le type est URL, et qu'aucun converter n'a été trouvé,
- * on enregistre un URLConverter.
- *
- * Si le type est VersionNumber, et qu'aucun converter n'a été trouvé,
- * on enregistre un VersionNumberConverter.
+ *
+ * Sinon on tente d'instancier un converteur dans le paquetage dédié aux
+ * converteurs {@link #CONVERTER_PACKAGE}.
*
* @param type le type à convertir
* @return le converter trouvé, ou null si non trouvé
- * @see Converter
- * @see EnumConverter
- * @see URLConverter
- * @see VersionNumberConverter
*/
public static Converter getConverter(Class type) {
Converter converter = ConvertUtils.lookup(type);
- if (converter!=null) {
+ if (converter != null) {
return converter;
}
if (type.isEnum()) {
registerEnumConverter(type);
+ return ConvertUtils.lookup(type);
+ }
+ // on essaye de trouver un converter dans le paquetage des converters
+ try {
+ registerConverter0(type);
converter = ConvertUtils.lookup(type);
- } else if (Arrays.asList(AUTO_REGISTRED_CONVERTER).contains(type)) {
- try {
- registerConverter(type);
- converter = ConvertUtils.lookup(type);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
return converter;
}
- public static void registerConverter(Class type) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
- if (ConvertUtils.lookup(type)== null) {
- Converter converter = (Converter) Class.forName("org.codelutin.util."+type.getSimpleName()+"Converter").newInstance();
- ConvertUtils.register(converter, type);
+ public static void registerConverter(Class type) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
+ if (ConvertUtils.lookup(type) == null) {
+ registerConverter0(type);
}
}
+ protected static void registerConverter0(Class type) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
+ Class> aClass = Class.forName(CONVERTER_PACKAGE + "." + type.getSimpleName() + "Converter");
+ Converter converter = (Converter) aClass.newInstance();
+ ConvertUtils.register(converter, type);
+ }
+
/**
* Enregistre un nouveau converter pour un type d'enum donné, avec une
* valeur par defaut.
@@ -101,4 +98,14 @@
public static void registerEnumConverter(Class> type) {
registerEnumConverter(type, null);
}
+
+ public static byte[] convert(char[] chars) {
+ byte[] bytes = new byte[chars.length];
+ for (int i = 0; i < chars.length; i++) {
+ bytes[i] = (byte) (chars[i] & 0xff);
+ }
+ return bytes;
+ }
+
+
}