KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSVMappingParser.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
23 package net.sf.anupam.csv.formatters;
24
25 import org.apache.commons.digester.Digester;
26 import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * XML Parser (based on Commons Digester) to parse and return the CSV formatter configuration. This is for internal use
44  * within the framwork.
45  *
46  * @author Anupam Sengupta
47  * @version $Revision: 1.3 $
48  * @see org.apache.commons.digester.Digester
49  * @since 1.5
50  */

51 class CSVFormatterConfigParser {
52
53     /**
54      * The logger to use.
55      */

56     private static final Log LOG = LogFactory
57             .getLog(CSVFormatterConfigParser.class);
58
59     /**
60      * The rule set for parsing formatter configuration.
61      */

62     private static FromXmlRuleSet ruleSet;
63
64     private static CSVFormatterConfigParser singleton;
65     private static final Map JavaDoc<String JavaDoc, FormatterConfiguration> formatCfgMapping = new HashMap JavaDoc<String JavaDoc, FormatterConfiguration>();
66     private static boolean isLoaded;
67
68     /**
69      * Constructor for CSVMappingParser.
70      */

71     private CSVFormatterConfigParser() {
72         super();
73
74
75     }
76
77     /**
78      * Returns the map of parsed format configuration beans.
79      *
80      * @param xmlFileName the XML mapping configuration file
81      * @param inClassPath flag indicating whether the XML file is in the classpath
82      * @return a map of format mappings. An empty map is returned if an error occurs
83      */

84     public synchronized Map JavaDoc<String JavaDoc, FormatterConfiguration> getFormatMappings(
85             final String JavaDoc xmlFileName, final boolean inClassPath) {
86
87         if (!isLoaded) {
88             loadMappings(xmlFileName, inClassPath);
89             isLoaded = true;
90         }
91
92
93         return formatCfgMapping;
94     }
95
96
97     /**
98      * Load the formatter mappings.
99      *
100      * @param xmlFileName the XML file to load the mappings from
101      * @param inClassPath indicates whether the mapping file is in the classpath
102      */

103     private void loadMappings(final String JavaDoc xmlFileName, final boolean inClassPath) {
104         try {
105             final InputStream JavaDoc xmlStream = (inClassPath)
106                     ? getClass().getClassLoader()
107                     .getResourceAsStream(xmlFileName)
108                     : new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(xmlFileName));
109
110             final InputSource JavaDoc inputSrc = new InputSource JavaDoc(xmlStream);
111             final Digester digester = new Digester();
112             digester.clear();
113
114             CSVFormatterConfigParser.ruleSet
115                     .addRuleInstances(digester);
116             digester.push(new ArrayList JavaDoc<FormatterConfiguration>());
117             final List JavaDoc<FormatterConfiguration> formatMappingList = (List JavaDoc<FormatterConfiguration>) digester
118                     .parse(inputSrc);
119
120             for (FormatterConfiguration formatConfig : formatMappingList) {
121                 formatCfgMapping.put(formatConfig.getFormatterName(), formatConfig);
122             }
123         } catch (final FileNotFoundException JavaDoc e) {
124             LOG.warn("The XML File: "
125                     + xmlFileName + " was not found", e);
126         } catch (final IOException JavaDoc e) {
127             LOG.warn("The XML File: "
128                     + xmlFileName + " could not be read", e);
129         } catch (final SAXException JavaDoc e) {
130             LOG.warn("The XML File: "
131                     + xmlFileName + " could not be parsed", e);
132         }
133     }
134
135     public synchronized static CSVFormatterConfigParser getConfigParser() {
136
137         if (singleton == null) {
138             final InputStream JavaDoc is = CSVFormatterConfigParser.class.getClassLoader()
139                     .getResourceAsStream("net/sf/anupam/csv/formatters/csv-formatter-config-digester-rules.xml");
140             if (is != null) {
141                 final InputSource JavaDoc isrc = new InputSource JavaDoc(is);
142                 ruleSet = new FromXmlRuleSet(isrc);
143                 LOG.info("Loaded Digester Rules for "
144                         + CSVFormatterConfigParser.class);
145             } else {
146                 LOG
147                         .error("The CSV Formatter Configuration Digester Rules XML was not found");
148             }
149             singleton = new CSVFormatterConfigParser();
150         }
151         return singleton;
152     }
153 }
154
Popular Tags