KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > anupam > csv > mapping > CSVMappingParser


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.2 $
21  */

22 package net.sf.anupam.csv.mapping;
23
24 import org.apache.commons.digester.Digester;
25 import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.WeakHashMap JavaDoc;
40
41 /**
42  * XML Parser (based on Commons Digester) to parse and return the mapping
43  * configuration.
44  *
45  * @author Anupam Sengupta
46  * @version $Revision: 1.2 $
47  * @see org.apache.commons.digester.Digester
48  * @since 1.5
49  */

50 public class CSVMappingParser {
51
52     /**
53      * The logger to use.
54      */

55     private static final Log LOG = LogFactory
56             .getLog(CSVMappingParser.class);
57
58     /**
59      * The digester rule set for parsing the mapping file.
60      */

61     private static FromXmlRuleSet ruleSet;
62
63     /**
64      * The digester to use for parsing the mapping file.
65      */

66     private Digester digester = new Digester();
67
68     static {
69
70         final InputStream JavaDoc is = ClassLoader
71                 .getSystemResourceAsStream("net/sf/anupam/csv/mapping/csv-mapping-digester-rules.xml");
72         if (is != null) {
73             final InputSource JavaDoc isrc = new InputSource JavaDoc(is);
74             ruleSet = new FromXmlRuleSet(isrc);
75             LOG.info("Loaded Digester Rules for "
76                     + CSVMappingParser.class);
77         } else {
78             LOG.error("The CSV Mapping Digester Rules XML was not found");
79         }
80
81     }
82
83     /**
84      * Constructor for CSVMappingParser.
85      */

86     public CSVMappingParser() {
87         super();
88         digester.clear();
89
90         CSVMappingParser.ruleSet.addRuleInstances(digester);
91         digester.push(new ArrayList JavaDoc<CSVBeanMapping>());
92
93     }
94
95     /**
96      * Finalizes this mapping parser.
97      *
98      * @throws Throwable thrown if the finalization fails
99      * @see Object#finalize()
100      */

101     @Override JavaDoc
102     protected void finalize() throws Throwable JavaDoc {
103         super.finalize();
104         if (digester != null) {
105             digester.clear();
106             digester = null;
107         }
108     }
109
110     /**
111      * Returns the map of parsed mapping configuration beans.
112      *
113      * @param xmlFileName the XML mapping configuration file
114      * @param inClassPath flag indicating whether the XML file is in the classpath
115      * @return a map of CSV bean mappings. An empty map is returned if an error
116      * occurs
117      */

118     public Map JavaDoc<String JavaDoc, CSVBeanMapping> getMappings(final String JavaDoc xmlFileName,
119                                                    final boolean inClassPath) {
120
121         final Map JavaDoc<String JavaDoc, CSVBeanMapping> beanMap = new WeakHashMap JavaDoc<String JavaDoc, CSVBeanMapping>();
122
123         try {
124             final InputStream JavaDoc xmlStream = (inClassPath)
125                     ? ClassLoader.getSystemResourceAsStream(xmlFileName)
126                     : new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(xmlFileName));
127
128             final InputSource JavaDoc inputSrc = new InputSource JavaDoc(xmlStream);
129
130             final List JavaDoc<CSVBeanMapping> mappingList = (List JavaDoc<CSVBeanMapping>) digester
131                     .parse(inputSrc);
132
133             for (CSVBeanMapping mappedBean : mappingList) {
134                 beanMap.put(mappedBean.getBeanName(), mappedBean);
135             }
136         } catch (final FileNotFoundException JavaDoc e) {
137             LOG.warn("The XML File: "
138                     + xmlFileName + " was not found", e);
139         } catch (final IOException JavaDoc e) {
140             LOG.warn("The XML File: "
141                     + xmlFileName + " could not be read", e);
142         } catch (final SAXException JavaDoc e) {
143             LOG.warn("The XML File: "
144                     + xmlFileName + " could not be parsed", e);
145         }
146         return beanMap;
147     }
148
149 }
150
Popular Tags