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.2 $
021: */
022: package net.sf.anupam.csv.mapping;
023:
024: import org.apache.commons.digester.Digester;
025: import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.SAXException;
030:
031: import java.io.BufferedInputStream;
032: import java.io.FileInputStream;
033: import java.io.FileNotFoundException;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.util.ArrayList;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.WeakHashMap;
040:
041: /**
042: * XML Parser (based on Commons Digester) to parse and return the mapping
043: * configuration.
044: *
045: * @author Anupam Sengupta
046: * @version $Revision: 1.2 $
047: * @see org.apache.commons.digester.Digester
048: * @since 1.5
049: */
050: public class CSVMappingParser {
051:
052: /**
053: * The logger to use.
054: */
055: private static final Log LOG = LogFactory
056: .getLog(CSVMappingParser.class);
057:
058: /**
059: * The digester rule set for parsing the mapping file.
060: */
061: private static FromXmlRuleSet ruleSet;
062:
063: /**
064: * The digester to use for parsing the mapping file.
065: */
066: private Digester digester = new Digester();
067:
068: static {
069:
070: final InputStream is = ClassLoader
071: .getSystemResourceAsStream("net/sf/anupam/csv/mapping/csv-mapping-digester-rules.xml");
072: if (is != null) {
073: final InputSource isrc = new InputSource(is);
074: ruleSet = new FromXmlRuleSet(isrc);
075: LOG.info("Loaded Digester Rules for "
076: + CSVMappingParser.class);
077: } else {
078: LOG
079: .error("The CSV Mapping Digester Rules XML was not found");
080: }
081:
082: }
083:
084: /**
085: * Constructor for CSVMappingParser.
086: */
087: public CSVMappingParser() {
088: super ();
089: digester.clear();
090:
091: CSVMappingParser.ruleSet.addRuleInstances(digester);
092: digester.push(new ArrayList<CSVBeanMapping>());
093:
094: }
095:
096: /**
097: * Finalizes this mapping parser.
098: *
099: * @throws Throwable thrown if the finalization fails
100: * @see Object#finalize()
101: */
102: @Override
103: protected void finalize() throws Throwable {
104: super .finalize();
105: if (digester != null) {
106: digester.clear();
107: digester = null;
108: }
109: }
110:
111: /**
112: * Returns the map of parsed mapping configuration beans.
113: *
114: * @param xmlFileName the XML mapping configuration file
115: * @param inClassPath flag indicating whether the XML file is in the classpath
116: * @return a map of CSV bean mappings. An empty map is returned if an error
117: * occurs
118: */
119: public Map<String, CSVBeanMapping> getMappings(
120: final String xmlFileName, final boolean inClassPath) {
121:
122: final Map<String, CSVBeanMapping> beanMap = new WeakHashMap<String, CSVBeanMapping>();
123:
124: try {
125: final InputStream xmlStream = (inClassPath) ? ClassLoader
126: .getSystemResourceAsStream(xmlFileName)
127: : new BufferedInputStream(new FileInputStream(
128: xmlFileName));
129:
130: final InputSource inputSrc = new InputSource(xmlStream);
131:
132: final List<CSVBeanMapping> mappingList = (List<CSVBeanMapping>) digester
133: .parse(inputSrc);
134:
135: for (CSVBeanMapping mappedBean : mappingList) {
136: beanMap.put(mappedBean.getBeanName(), mappedBean);
137: }
138: } catch (final FileNotFoundException e) {
139: LOG.warn("The XML File: " + xmlFileName + " was not found",
140: e);
141: } catch (final IOException e) {
142: LOG.warn("The XML File: " + xmlFileName
143: + " could not be read", e);
144: } catch (final SAXException e) {
145: LOG.warn("The XML File: " + xmlFileName
146: + " could not be parsed", e);
147: }
148: return beanMap;
149: }
150:
151: }
|