001: /*
002: * CSVMappingParser.java
003: *
004: * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
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: * Returns the map of parsed format configuration beans.
078: *
079: * @param xmlFileName the XML mapping configuration file
080: * @param inClassPath flag indicating whether the XML file is in the classpath
081: * @return a map of format mappings. An empty map is returned if an error occurs
082: */
083: public synchronized Map<String, FormatterConfiguration> getFormatMappings(
084: final String xmlFileName, final boolean inClassPath) {
085:
086: if (!isLoaded) {
087: loadMappings(xmlFileName, inClassPath);
088: isLoaded = true;
089: }
090:
091: return formatCfgMapping;
092: }
093:
094: /**
095: * Load the formatter mappings.
096: *
097: * @param xmlFileName the XML file to load the mappings from
098: * @param inClassPath indicates whether the mapping file is in the classpath
099: */
100: private void loadMappings(final String xmlFileName,
101: final boolean inClassPath) {
102: try {
103: final InputStream xmlStream = (inClassPath) ? getClass()
104: .getClassLoader().getResourceAsStream(xmlFileName)
105: : new BufferedInputStream(new FileInputStream(
106: xmlFileName));
107:
108: final InputSource inputSrc = new InputSource(xmlStream);
109: final Digester digester = new Digester();
110: digester.clear();
111:
112: CSVFormatterConfigParser.ruleSet.addRuleInstances(digester);
113: digester.push(new ArrayList<FormatterConfiguration>());
114: final List<FormatterConfiguration> formatMappingList = (List<FormatterConfiguration>) digester
115: .parse(inputSrc);
116:
117: for (FormatterConfiguration formatConfig : formatMappingList) {
118: formatCfgMapping.put(formatConfig.getFormatterName(),
119: formatConfig);
120: }
121: } catch (final FileNotFoundException e) {
122: LOG.warn("The XML File: " + xmlFileName + " was not found",
123: e);
124: } catch (final IOException e) {
125: LOG.warn("The XML File: " + xmlFileName
126: + " could not be read", e);
127: } catch (final SAXException e) {
128: LOG.warn("The XML File: " + xmlFileName
129: + " could not be parsed", e);
130: }
131: }
132:
133: public synchronized static CSVFormatterConfigParser getConfigParser() {
134:
135: if (singleton == null) {
136: final InputStream is = CSVFormatterConfigParser.class
137: .getClassLoader()
138: .getResourceAsStream(
139: "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: }
|