001    /*
002     * CSVMappingParser.java
003     *
004     * Copyright (C) 2005 Anupam Sengupta ([email protected])
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019     *
020     * Version: $Revision: 1.3 $
021     */
022    
023    package net.sf.anupam.csv.formatters;
024    
025    import org.apache.commons.digester.Digester;
026    import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.xml.sax.InputSource;
030    import org.xml.sax.SAXException;
031    
032    import java.io.BufferedInputStream;
033    import java.io.FileInputStream;
034    import java.io.FileNotFoundException;
035    import java.io.IOException;
036    import java.io.InputStream;
037    import java.util.ArrayList;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    /**
043     * XML Parser (based on Commons Digester) to parse and return the CSV formatter configuration. This is for internal use
044     * within the framwork.
045     *
046     * @author Anupam Sengupta
047     * @version $Revision: 1.3 $
048     * @see org.apache.commons.digester.Digester
049     * @since 1.5
050     */
051    class CSVFormatterConfigParser {
052    
053        /**
054         * The logger to use.
055         */
056        private static final Log LOG = LogFactory
057                .getLog(CSVFormatterConfigParser.class);
058    
059        /**
060         * The rule set for parsing formatter configuration.
061         */
062        private static FromXmlRuleSet ruleSet;
063    
064        private static CSVFormatterConfigParser singleton;
065        private static final Map<String, FormatterConfiguration> formatCfgMapping = new HashMap<String, FormatterConfiguration>();
066        private static boolean isLoaded;
067    
068        /**
069         * Constructor for CSVMappingParser.
070         */
071        private CSVFormatterConfigParser() {
072            super();
073    
074    
075        }
076    
077        /**
078         * Returns the map of parsed format configuration beans.
079         *
080         * @param xmlFileName the XML mapping configuration file
081         * @param inClassPath flag indicating whether the XML file is in the classpath
082         * @return a map of format mappings. An empty map is returned if an error occurs
083         */
084        public synchronized Map<String, FormatterConfiguration> getFormatMappings(
085                final String xmlFileName, final boolean inClassPath) {
086    
087            if (!isLoaded) {
088                loadMappings(xmlFileName, inClassPath);
089                isLoaded = true;
090            }
091    
092    
093            return formatCfgMapping;
094        }
095    
096    
097        /**
098         * Load the formatter mappings.
099         *
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 xmlFileName, final boolean inClassPath) {
104            try {
105                final InputStream xmlStream = (inClassPath)
106                        ? getClass().getClassLoader()
107                        .getResourceAsStream(xmlFileName)
108                        : new BufferedInputStream(new FileInputStream(xmlFileName));
109    
110                final InputSource inputSrc = new InputSource(xmlStream);
111                final Digester digester = new Digester();
112                digester.clear();
113    
114                CSVFormatterConfigParser.ruleSet
115                        .addRuleInstances(digester);
116                digester.push(new ArrayList<FormatterConfiguration>());
117                final List<FormatterConfiguration> formatMappingList = (List<FormatterConfiguration>) digester
118                        .parse(inputSrc);
119    
120                for (FormatterConfiguration formatConfig : formatMappingList) {
121                    formatCfgMapping.put(formatConfig.getFormatterName(), formatConfig);
122                }
123            } catch (final FileNotFoundException e) {
124                LOG.warn("The XML File: "
125                        + xmlFileName + " was not found", e);
126            } catch (final IOException e) {
127                LOG.warn("The XML File: "
128                        + xmlFileName + " could not be read", e);
129            } catch (final SAXException 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 is = CSVFormatterConfigParser.class.getClassLoader()
139                        .getResourceAsStream("net/sf/anupam/csv/formatters/csv-formatter-config-digester-rules.xml");
140                if (is != null) {
141                    final InputSource isrc = new InputSource(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    }