1 10 package org.mmbase.util.transformers; 11 12 import java.util.Iterator ; 13 import org.mmbase.util.functions.Parameters; 14 import org.mmbase.util.logging.Logger; 15 import org.mmbase.util.logging.Logging; 16 17 23 24 public class Transformers { 25 private static final Logger log = Logging.getLoggerInstance(Transformers.class); 26 27 28 40 41 public static CharTransformer getCharTransformer(String name, String config, String errorId, boolean back) { 42 Class clazz; 43 try { 44 clazz = Class.forName(name); 45 } catch (ClassNotFoundException ex) { 46 log.error("Class " + name + " specified for " + errorId + " could not be found"); 47 return null; 48 } 49 50 if (! Transformer.class.isAssignableFrom(clazz) && 51 ! ParameterizedTransformerFactory.class.isAssignableFrom(clazz)) { 52 log.error("The class " + clazz + " specified for " + errorId + " is not a Transformer"); 53 return null; 54 } 55 Object t; 56 try { 57 t = clazz.newInstance(); 58 } catch (Exception ex) { 59 log.error("Error instantiating a " + clazz + ": " + ex.toString()); 60 return null; 61 } 62 if (t instanceof ParameterizedTransformerFactory) { 63 ParameterizedTransformerFactory pt = (ParameterizedTransformerFactory) t; 64 Parameters params = pt.createParameters(); 65 Iterator it = org.mmbase.util.StringSplitter.split(config).iterator(); 66 int i = 0; 67 while (it.hasNext()) { 68 params.set(i++, it.next()); 69 } 70 config = ""; 71 t = pt.createTransformer(params); 72 } 73 CharTransformer ct; 74 75 if (t instanceof CharTransformer) { 76 ct = (CharTransformer) t; 77 } else if (t instanceof ByteToCharTransformer) { 78 ct = new ByteCharTransformer((ByteToCharTransformer) t); 79 } else { 80 log.error("The class " + clazz + " specified for " + errorId + " is not a CharTransformer or a ByteToCharTransformer"); 81 return null; 82 } 83 84 if (config == null) config = ""; 85 if (ct instanceof ConfigurableTransformer) { 86 log.debug("Trying to configure with '" + config + "'"); 87 if (! config.equals("")) { 88 int conf; 89 try { 90 log.debug("With int"); 91 conf = Integer.parseInt(config); 92 } catch (NumberFormatException nfe) { 93 try { 94 log.debug("With static field"); 95 conf = clazz.getField(config).getInt(null); 96 } catch (Exception nsfe) { 97 log.error("Type " + errorId + " is not well configured : " + nfe.toString() + " and " + nsfe.toString()); 98 return null; 99 } 100 } 101 ((ConfigurableTransformer) ct).configure(conf); 102 } 103 } else { 104 if (! config.equals("")) { 105 log.warn("Tried to configure non-configurable transformer " + name); 106 } 107 } 108 if (back) { 109 ct = new InverseCharTransformer(ct); 110 } 111 return ct; 112 } 113 114 115 116 119 120 public static ParameterizedTransformerFactory getTransformerFactory(String name, String errorId) { 121 Class clazz; 122 try { 123 clazz = Class.forName(name); 124 } catch (ClassNotFoundException ex) { 125 log.error("Class " + name + " specified for " + errorId + " could not be found"); 126 return null; 127 } 128 if (! ParameterizedTransformerFactory.class.isAssignableFrom(clazz)) { 129 log.error("The class " + clazz + " specified for " + errorId + " is not a ParamerizedTransformerFactory"); 130 return null; 131 } 132 ParameterizedTransformerFactory fact; 133 try { 134 fact = (ParameterizedTransformerFactory) clazz.newInstance(); 135 } catch (Exception ex) { 136 log.error("Error instantiating a " + clazz + ": " + ex.toString()); 137 return null; 138 } 139 140 return fact; 141 } 142 143 } 144 | Popular Tags |