KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > Transformers


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.transformers;
11
12 import java.util.Iterator JavaDoc;
13 import org.mmbase.util.functions.Parameters;
14 import org.mmbase.util.logging.Logger;
15 import org.mmbase.util.logging.Logging;
16
17 /**
18  * Utitilies related to the tranformers of this package.
19  *
20  * @author Michiel Meeuwissen
21  * @since MMBase-1.7
22  */

23
24 public class Transformers {
25     private static final Logger log = Logging.getLoggerInstance(Transformers.class);
26
27
28     /**
29      * This method instatiates a CharTransformer by use of reflection.
30      * @param name The class name for the CharTransformer to be returned
31      * @param config A configuration string for this transformer. At the moment this can be parsed
32      * as an integer, or the name of a integer constant of the transformer's class.
33      * Likely, other ways to configure a transformer will be available.
34      * @param errorId If something goes wrong, an error message is logged, in which this String is
35      * used, to clear things up.
36      * @param back If true, the Transformer will be wrapped in a InverseCharTransformer, so the
37      * transformation will do the inverse thing.
38      * @return A CharTransformer instance or null in case of an error.
39      */

40
41     public static CharTransformer getCharTransformer(String JavaDoc name, String JavaDoc config, String JavaDoc errorId, boolean back) {
42         Class JavaDoc clazz;
43         try {
44             clazz = Class.forName(name);
45         } catch (ClassNotFoundException JavaDoc 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 JavaDoc t;
56         try {
57             t = clazz.newInstance();
58         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc nfe) {
93                     try {
94                         log.debug("With static field");
95                         conf = clazz.getField(config).getInt(null);
96                     } catch (Exception JavaDoc 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     /**
117      * @since MMBase-1.8
118      */

119
120     public static ParameterizedTransformerFactory getTransformerFactory(String JavaDoc name, String JavaDoc errorId) {
121         Class JavaDoc clazz;
122         try {
123             clazz = Class.forName(name);
124         } catch (ClassNotFoundException JavaDoc 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 JavaDoc ex) {
136             log.error("Error instantiating a " + clazz + ": " + ex.toString());
137             return null;
138         }
139
140         return fact;
141     }
142
143 }
144
Popular Tags