KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > anupam > csv > formatters > CSVFormatterFactory


1 /*
2  * CSVFormatterFactory.java
3  *
4  * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Version: $Revision: 1.3 $
21  */

22 package net.sf.anupam.csv.formatters;
23
24 import net.sf.anupam.csv.exceptions.CSVOException;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * A singleton factory which creates and caches the
33  * {@link CSVFieldFormatter csv field formatters}. The factory
34  * maintains a cache of CSV formatters that are reentrant (i.e.,
35  * the formatters that do not maintain any instance specific state).
36  *
37  * @author Anupam Sengupta
38  * @version $Revision: 1.3 $
39  * @see CSVFieldFormatter
40  * @since 1.5
41  */

42 public final class CSVFormatterFactory {
43
44     /**
45      * The CSV formatter mapping file name. This file assumed to be present in the
46      * classpath.
47      */

48     private static final String JavaDoc FMT_MAPPING_FILE_NAME = "net/sf/anupam/csv/formatters/csv-formatter-config.xml";
49
50     /**
51      * The singleton instance of the factory.
52      */

53     private static CSVFormatterFactory singleton;
54
55     /**
56      * The generic NO-OP formatter which is used when no explicit formatter is
57      * defined.
58      */

59     private static final CSVFieldFormatter DO_NOTHING_FORMATTER = new DoNothingFormatter();
60
61     /**
62      * The logger to use.
63      */

64     private static final Log LOG = LogFactory
65             .getLog(CSVFormatterFactory.class);
66
67     /**
68      * Mapping of the formatter name and the configuration.
69      */

70     private Map JavaDoc<String JavaDoc, FormatterConfiguration> formatterLookupMap;
71
72     /**
73      * The cached formatters.
74      */

75     private Map JavaDoc<String JavaDoc, CSVFieldFormatter> formatterCache;
76
77
78     /**
79      * Constructor for CSVFormatterFactory. Private to prevent direct
80      * instantiation.
81      */

82     private CSVFormatterFactory() {
83         super();
84         formatterLookupMap = new HashMap JavaDoc<String JavaDoc, FormatterConfiguration>();
85         formatterCache = new HashMap JavaDoc<String JavaDoc, CSVFieldFormatter>();
86     }
87
88     /**
89      * Returns the singleton instance of this factory.
90      *
91      * @return the singleton instance
92      */

93     public synchronized static CSVFormatterFactory getSingleton() {
94         if (singleton == null) {
95             singleton = new CSVFormatterFactory();
96             singleton.loadMappings();
97             LOG.info("Created the CSVFormatter Factory");
98         }
99         return singleton;
100     }
101
102     /**
103      * Loads all mappings from the formatter configuration file.
104      */

105     private void loadMappings() {
106         final CSVFormatterConfigParser parser = CSVFormatterConfigParser.getConfigParser();
107         final FormatterConfiguration doNothingConfiguration = new FormatterConfiguration();
108         doNothingConfiguration.setFormatterName("none");
109         doNothingConfiguration.setFormatterClass("net.sf.anupam.csv.formatters.DoNothingFormatter");
110         doNothingConfiguration.setConstructionNeeded(false);
111         formatterLookupMap.put("none", doNothingConfiguration);
112         formatterLookupMap.putAll(parser.getFormatMappings(FMT_MAPPING_FILE_NAME,
113                 true));
114         createCache();
115         LOG.debug("Loaded the CSV Mapping configuration from "
116                 + FMT_MAPPING_FILE_NAME);
117     }
118
119     /**
120      * Creates the cached formatters for the ones which do not need special
121      * construction.
122      */

123     private void createCache() {
124         for (String JavaDoc formatterName : formatterLookupMap.keySet()) {
125             final FormatterConfiguration currentFormatter = formatterLookupMap
126                     .get(formatterName);
127             // If the formatter does not require special construction,
128
// then create it one time and cache it.
129
if (!currentFormatter.isConstructionNeeded()) {
130
131                 final CSVFieldFormatter formatter = createFormatterForClass(currentFormatter
132                         .getFormatterClass());
133
134                 formatterCache.put(formatterName, formatter);
135             }
136         }
137     }
138
139     /**
140      * Creates a formatter from the specified class.
141      *
142      * @param className the formatter class
143      * @return the created formatter
144      */

145     private CSVFieldFormatter createFormatterForClass(final String JavaDoc className) {
146
147         Object JavaDoc formatter;
148         try {
149             formatter = Class.forName(className.trim()).newInstance();
150         } catch (final InstantiationException JavaDoc e) {
151             LOG.warn("Could not create formatter for class: "
152                     + className, e);
153             formatter = DO_NOTHING_FORMATTER;
154         } catch (final IllegalAccessException JavaDoc e) {
155             LOG.warn("Could not create formatter for class: "
156                     + className, e);
157             formatter = DO_NOTHING_FORMATTER;
158         } catch (final ClassNotFoundException JavaDoc e) {
159             LOG.warn("Could not create formatter for class: "
160                     + className, e);
161             formatter = DO_NOTHING_FORMATTER;
162         }
163         return (CSVFieldFormatter) formatter;
164
165     }
166
167     /**
168      * Creates a new instance of the specified formatter. The cache is used
169      * whenever possible.
170      *
171      * @param formatterName the formatter to return
172      * @return the requested formatter
173      * @throws CSVOException thrown if the formatter cannot be created
174      */

175     public CSVFieldFormatter createFormatterFor(final String JavaDoc formatterName)
176             throws CSVOException {
177
178         // If a cache hit, then return the cached formatter
179
if (formatterCache.containsKey(formatterName)) {
180             return formatterCache.get(formatterName);
181         } else {
182             LOG.warn("Formatter: " + formatterName + " not found");
183             throw new CSVOException("Formatter: " + formatterName + " not found");
184         }
185
186     }
187 }
188
Popular Tags