001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.transformers;
011:
012: import java.util.Iterator;
013: import org.mmbase.util.functions.Parameters;
014: import org.mmbase.util.logging.Logger;
015: import org.mmbase.util.logging.Logging;
016:
017: /**
018: * Utitilies related to the tranformers of this package.
019: *
020: * @author Michiel Meeuwissen
021: * @since MMBase-1.7
022: */
023:
024: public class Transformers {
025: private static final Logger log = Logging
026: .getLoggerInstance(Transformers.class);
027:
028: /**
029: * This method instatiates a CharTransformer by use of reflection.
030: * @param name The class name for the CharTransformer to be returned
031: * @param config A configuration string for this transformer. At the moment this can be parsed
032: * as an integer, or the name of a integer constant of the transformer's class.
033: * Likely, other ways to configure a transformer will be available.
034: * @param errorId If something goes wrong, an error message is logged, in which this String is
035: * used, to clear things up.
036: * @param back If true, the Transformer will be wrapped in a InverseCharTransformer, so the
037: * transformation will do the inverse thing.
038: * @return A CharTransformer instance or null in case of an error.
039: */
040:
041: public static CharTransformer getCharTransformer(String name,
042: String config, String errorId, boolean back) {
043: Class<?> clazz;
044: try {
045: clazz = Class.forName(name);
046: } catch (ClassNotFoundException ex) {
047: log.error("Class " + name + " specified for " + errorId
048: + " could not be found");
049: return null;
050: }
051:
052: if (!Transformer.class.isAssignableFrom(clazz)
053: && !ParameterizedTransformerFactory.class
054: .isAssignableFrom(clazz)) {
055: log.error("The class " + clazz + " specified for "
056: + errorId + " is not a Transformer");
057: return null;
058: }
059: Object t;
060: try {
061: t = clazz.newInstance();
062: } catch (Exception ex) {
063: log.error("Error instantiating a " + clazz + ": "
064: + ex.toString());
065: return null;
066: }
067: if (t instanceof ParameterizedTransformerFactory) {
068: ParameterizedTransformerFactory pt = (ParameterizedTransformerFactory) t;
069: Parameters params = pt.createParameters();
070: Iterator<String> it = org.mmbase.util.StringSplitter.split(
071: config).iterator();
072: int i = 0;
073: while (it.hasNext()) {
074: params.set(i++, it.next());
075: }
076: config = "";
077: t = pt.createTransformer(params);
078: }
079: CharTransformer ct;
080:
081: if (t instanceof CharTransformer) {
082: ct = (CharTransformer) t;
083: } else if (t instanceof ByteToCharTransformer) {
084: log.debug("making a ByteCharTransformer");
085: ct = new ByteCharTransformer((ByteToCharTransformer) t);
086: } else {
087: log
088: .error("The class "
089: + clazz
090: + " specified for "
091: + errorId
092: + " is not a CharTransformer or a ByteToCharTransformer");
093: return null;
094: }
095:
096: if (config == null)
097: config = "";
098: if (ct instanceof ConfigurableTransformer) {
099: log.debug("Trying to configure with '" + config + "'");
100: if (!config.equals("")) {
101: int conf;
102: try {
103: log.debug("With int");
104: conf = Integer.parseInt(config);
105: } catch (NumberFormatException nfe) {
106: try {
107: log.debug("With static field");
108: conf = clazz.getField(config).getInt(null);
109: } catch (Exception nsfe) {
110: log.error("Type " + errorId
111: + " is not well configured : "
112: + nfe.toString() + " and "
113: + nsfe.toString());
114: return null;
115: }
116: }
117: ((ConfigurableTransformer) ct).configure(conf);
118: }
119: } else {
120: if (!config.equals("")) {
121: log
122: .warn("Tried to configure non-configurable transformer "
123: + name);
124: }
125: }
126: if (back) {
127: ct = new InverseCharTransformer(ct);
128: }
129: return ct;
130: }
131:
132: /**
133: * @since MMBase-1.8
134: */
135:
136: public static ParameterizedTransformerFactory getTransformerFactory(
137: String name, String errorId) {
138: Class<?> clazz;
139: try {
140: clazz = Class.forName(name);
141: } catch (ClassNotFoundException ex) {
142: log.error("Class " + name + " specified for " + errorId
143: + " could not be found");
144: return null;
145: }
146: if (!ParameterizedTransformerFactory.class
147: .isAssignableFrom(clazz)) {
148: log.error("The class " + clazz + " specified for "
149: + errorId
150: + " is not a ParamerizedTransformerFactory");
151: return null;
152: }
153: ParameterizedTransformerFactory fact;
154: try {
155: fact = (ParameterizedTransformerFactory) clazz
156: .newInstance();
157: } catch (Exception ex) {
158: log.error("Error instantiating a " + clazz + ": "
159: + ex.toString());
160: return null;
161: }
162:
163: return fact;
164: }
165:
166: }
|