KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > csv > CsvConnection


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.csv;
17
18 import au.com.bytecode.opencsv.CSVReader;
19 import au.com.bytecode.opencsv.CSVWriter;
20 import scriptella.core.EtlCancelledException;
21 import scriptella.driver.text.AbstractTextConnection;
22 import scriptella.expression.PropertiesSubstitutor;
23 import scriptella.spi.ConnectionParameters;
24 import scriptella.spi.ParametersCallback;
25 import scriptella.spi.ProviderException;
26 import scriptella.spi.QueryCallback;
27 import scriptella.spi.Resource;
28 import scriptella.util.IOUtils;
29 import scriptella.util.StringUtils;
30
31 import java.io.IOException JavaDoc;
32 import java.io.Reader JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 /**
38  * Represents a connection to CSV file.
39  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
40  *
41  * @author Fyodor Kupolov
42  * @version 1.0
43  */

44 public class CsvConnection extends AbstractTextConnection {
45     private static final Logger JavaDoc LOG = Logger.getLogger(CsvConnection.class.getName());
46     private CSVWriter out;
47
48
49     /**
50      * Name of the <code>separator</code> connection property.
51      * The delimiter to use for separating entries when reading from or writing to files.
52      */

53     public static final String JavaDoc SEPARATOR = "separator";
54     /**
55      * Name of the <code>quote</code> connection property.
56      * The character to use for quoted elements when reading from or writing to files. Use empty string to suppress
57      * quoting.
58      */

59     public static final String JavaDoc QUOTE = "quote";
60
61     /**
62      * Name of the <code>suppressHeaders</code> connection property.
63      * true means the first line contains headers. Default value is true.
64      * <p>Only valid for &lt;query&gt; elements.
65      */

66     public static final String JavaDoc HEADERS = "headers";
67
68     private final char separator;
69     private final char quote;
70     private final boolean headers;
71
72
73     public CsvConnection(ConnectionParameters parameters) {
74         super(Driver.DIALECT, parameters);
75         String JavaDoc sep = parameters.getStringProperty(SEPARATOR);
76         if (sep != null && sep.length() > 0) {
77             separator = sep.charAt(0);
78         } else {
79             separator = ',';
80         }
81         String JavaDoc q = parameters.getStringProperty(QUOTE);
82         if (q == null) {
83             quote = '\"'; //default value
84
} else if (q.length() > 0) {
85             quote = q.charAt(0);
86         } else { //otherwise no quoting
87
quote = CSVWriter.NO_QUOTE_CHARACTER;
88         }
89
90         headers = parameters.getBooleanProperty(HEADERS, true);
91     }
92
93     public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException {
94         try {
95             executeScript(scriptContent.open(), parametersCallback);
96         } catch (IOException JavaDoc e) {
97             throw new CsvProviderException("Cannot read script.", e);
98         }
99     }
100
101
102      void executeScript(Reader reader, ParametersCallback parametersCallback) throws IOException JavaDoc {
103
104         CSVReader r = new CSVReader(reader);//Parsing rules of script in XML is always standard
105
CSVWriter out = getOut();
106         PropertiesSubstitutor ps = new PropertiesSubstitutor(parametersCallback);
107         for (String JavaDoc[] row; (row = r.readNext()) != null;) {
108             EtlCancelledException.checkEtlCancelled();
109             for (int i = 0; i < row.length; i++) {
110                 String JavaDoc field = row[i];
111                 if (field != null) {
112                     if (trim) {//removing extra whitespaces by default
113
field = field.trim();
114                     }
115                     row[i] = ps.substitute(field);
116                 }
117             }
118             //If only one column and empty - skip this row
119
if (row.length==1 && StringUtils.isAsciiWhitespacesOnly(row[0])) {
120                 continue;
121             }
122
123             if (isReadonly()) {
124                 if (LOG.isLoggable(Level.INFO)) {
125                     LOG.info("Readonly Mode - " + Arrays.deepToString(row) + " has been skipped.");
126                 }
127             } else {
128                 try {
129                     out.writeNext(row);
130                     counter.statements++;
131                 } catch (Exception JavaDoc e) {
132                     throw new CsvProviderException("Failed to write CSV row ", e);
133                 }
134             }
135         }
136     }
137
138     public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException {
139         if (out != null) {
140             throw new CsvProviderException("Cannot query and update a CSV file simultaneously.");
141         }
142         try {
143             CSVReader in = new CSVReader(IOUtils.getReader(url.openStream(), encoding), separator, quote);
144             CsvQuery q = new CsvQuery(in, headers, trim);
145             try {
146                 q.execute(new CSVReader(queryContent.open()), parametersCallback, queryCallback, counter);
147             } catch (IOException JavaDoc e) {
148                 throw new CsvProviderException("Cannot read query " + queryContent, e);
149             } finally {
150                 IOUtils.closeSilently(q);
151             }
152         } catch (IOException JavaDoc e) {
153             throw new CsvProviderException("Unable to open URL " + url + " for input", e);
154         }
155     }
156
157     /**
158      * @return lazily intialized writer.
159      */

160     protected CSVWriter getOut() {
161         if (out == null) {
162             try {
163                 out = new CSVWriter(IOUtils.getWriter(IOUtils.getOutputStream(url), encoding), separator, quote, eol);
164             } catch (IOException JavaDoc e) {
165                 throw new CsvProviderException("Unable to open URL " + url + " for output", e);
166             }
167         }
168         return out;
169     }
170
171
172     public void close() throws ProviderException {
173         if (out != null) {
174             try {
175                 out.close();
176             } catch (Exception JavaDoc e) {
177                 LOG.log(Level.INFO, "A problem occured while trying to close CSV writer", e);
178             }
179         }
180     }
181 }
182
Popular Tags