KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > relique > jdbc > csv > CsvDriver


1 /**
2     Copyright (C) 2002-2003 Together
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 */

19
20 package org.relique.jdbc.csv;
21
22 import java.sql.*;
23 import java.util.Properties JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.io.File JavaDoc;
26
27 /**
28  * This class implements the Driver interface for the CsvJdbc driver.
29  *
30  * @author Zoran Milakovic
31  */

32
33 public class CsvDriver implements Driver
34 {
35
36 //PARAMETER NAMES
37
public static final String JavaDoc FILE_EXTENSION="fileExtension";
38   public static final String JavaDoc SEPARATOR="separator";
39   public static final String JavaDoc MAXFILESIZE = "maxFileSize";
40   public static final String JavaDoc CREATE="create";
41   public static final String JavaDoc SUPPRESS_HEADERS="suppressHeaders";
42   public static final String JavaDoc CHARSET = "charset";
43   public static final String JavaDoc LINE_BREAK_ESCAPE = "lineBreakEscape";
44   public static final String JavaDoc CARRIAGE_RETURN_ESCAPE = "carriageReturnEscape";
45   public static final String JavaDoc USE_QUOTES = "useQuotes";
46   public static final String JavaDoc USE_QUOTES_ESCAPE = "useQuotesEscape";
47   
48 //DEFAULT VALUES
49
public static final String JavaDoc DEFAULT_EXTENSION = ".csv";
50   public static final char DEFAULT_SEPARATOR = ',';
51   public static final boolean DEFAULT_SUPPRESS = false;
52   public static final boolean DEFAULT_CREATE = false;
53   public static final long DEFAULT_FILE_MAXSIZE = 1500000000;
54   public static final String JavaDoc DEFAULT_LINE_BREAK_ESCAPE = "~CSVLB~";
55   public static final String JavaDoc DEFAULT_DOUBLE_QUOTE_ESCAPE = "\"\"";
56   public static final String JavaDoc DEFAULT_CARRIAGE_RETURN_ESCAPE = "~CSVCR~";
57   public static final boolean DEFAULT_USE_QUOTES = true;
58   public static final boolean DEFAULT_USE_QUOTES_ESCAPE = true;
59
60 //data types
61
public static final String JavaDoc BINARY_TYPE = "BINARY";
62   public static final String JavaDoc VARCHAR_TYPE = "VARCHAR";
63
64
65   public static String JavaDoc FILE_NAME_EXT = "extension";
66   private final static String JavaDoc URL_PREFIX = "jdbc:relique:csv:";
67   private Properties JavaDoc info = null;
68
69   /* If set to true, driver will log into csvdriver.log file, in working directory */
70   private static boolean ENABLE_LOG = false;
71   
72   /* If set to true, driver will show stack traces and other debug info */
73     public static boolean DEBUG = false;
74
75   /**
76    *Gets the propertyInfo attribute of the CsvDriver object
77    *
78    * @param url Description of Parameter
79    * @param info Description of Parameter
80    * @return The propertyInfo value
81    * @exception SQLException Description of Exception
82    * @since
83    */

84   public DriverPropertyInfo[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
85        throws SQLException
86   {
87     return new DriverPropertyInfo[0];
88   }
89
90
91   /**
92    *Gets the majorVersion attribute of the CsvDriver object
93    *
94    * @return The majorVersion value
95    * @since
96    */

97   public int getMajorVersion()
98   {
99     return 1;
100   }
101
102
103   /**
104    *Gets the minorVersion attribute of the CsvDriver object
105    *
106    * @return The minorVersion value
107    * @since
108    */

109   public int getMinorVersion()
110   {
111     return 0;
112   }
113
114
115   /**
116    *Description of the Method
117    *
118    * @param url Description of Parameter
119    * @param info Description of Parameter
120    * @return Description of the Returned Value
121    * @exception SQLException Description of Exception
122    * @since
123    */

124   public Connection connect(String JavaDoc url, Properties JavaDoc info) throws SQLException
125   {
126     DriverManager.println("CsvJdbc - CsvDriver:connect() - url=" + url);
127     // check for correct url
128
if (!url.startsWith(URL_PREFIX))
129     {
130       return null;
131     }
132     // get filepath from url
133
String JavaDoc filePath = url.substring(URL_PREFIX.length());
134     String JavaDoc filePathAll = filePath;
135     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( filePath , ";" );
136     filePath = st.nextToken();
137     if (!filePath.endsWith(File.separator))
138     {
139       filePath += File.separator;
140     }
141     DriverManager.println("CsvJdbc - CsvDriver:connect() - filePath=" + filePath);
142     return new CsvConnection(filePathAll, info);
143   }
144
145
146   /**
147    * Description of the Method
148    *
149    * @param url Description of Parameter
150    * @return Description of the Returned Value
151    * @exception SQLException Description of Exception
152    * @since
153    */

154   public boolean acceptsURL(String JavaDoc url) throws SQLException
155   {
156     DriverManager.println("CsvJdbc - CsvDriver:accept() - url=" + url);
157     return url.startsWith(URL_PREFIX);
158   }
159
160
161   /**
162    *Description of the Method
163    *
164    * @return Description of the Returned Value
165    * @since
166    */

167   public boolean jdbcCompliant()
168   {
169     return false;
170   }
171   // This static block inits the driver when the class is loaded by the JVM.
172
static
173   {
174     try
175     {
176       java.sql.DriverManager.registerDriver(new CsvDriver());
177     }
178     catch (SQLException e)
179     {
180       throw new RuntimeException JavaDoc(
181           "FATAL ERROR: Could not initialise CSV driver ! Message was: "
182            + e.getMessage());
183     }
184   }
185
186   public static void log( String JavaDoc message) {
187     if ( CsvDriver.ENABLE_LOG ) {
188       try {
189         File JavaDoc file = new File JavaDoc("csvdriver.log");
190         if (!file.exists())
191           file.createNewFile();
192         java.io.RandomAccessFile JavaDoc fileLogr = new java.io.RandomAccessFile JavaDoc(file,
193             "rw");
194         fileLogr.seek(fileLogr.length());
195         fileLogr.writeBytes("CsvJdbc, "+message + "\r\n");
196         fileLogr.close();
197       }
198       catch (Exception JavaDoc ex) {
199         ex.printStackTrace();
200       }
201     }
202   }
203
204 }
205
206
Popular Tags