001: /**
002: Copyright (C) 2002-2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: */package org.webdocwf.util.i18njdbc;
019:
020: import java.sql.*;
021: import java.util.Properties;
022: import java.util.StringTokenizer;
023: import java.io.File;
024:
025: /**
026: * This class implements the Driver interface for the I18nJdbc driver.
027: *
028: * @author Zoran Milakovic
029: * @author Zeljko Kovacevic
030: */
031:
032: public class I18nDriver implements Driver {
033:
034: //PARAMETER NAMES
035:
036: //ZK added properties for i18nJdbc driver
037: public static final String CHARSET = "charset";
038: public static final String NAMECOLUMN = "nameColumn";
039: public static final String VALUECOLUMN = "valueColumn";
040: public static final String CREATE = "create";
041: public static final String FILE_EXTENSION = "fileExtension";
042: //DEFAULT VALUES
043:
044: //ZK added this
045: public static final String DEFAULT_CHARSET = "UTF-8";
046: public static final String DEFAULT_NAMECOLUMN = "name";
047: public static final String DEFAULT_VALUECOLUMN = "value";
048: public static final boolean DEFAULT_CREATE = false;
049: public static final String DEFAULT_EXTENSION = ".properties";
050: //data types
051:
052: public static final String VARCHAR_TYPE = "VARCHAR";
053: public static String FILE_NAME_EXT = "extension";
054: private final static String URL_PREFIX = "jdbc:webdocwf:i18n:";
055: private Properties info = null;
056:
057: /* If set to true, driver will log into i18ndriver.log file, in working directory */
058: private static boolean ENABLE_LOG = false;
059:
060: /* If set to true, driver will show stack traces and other debug info */
061: public static boolean DEBUG = false;
062:
063: /**
064: *Gets the propertyInfo attribute of the I18nJdbc object
065: *
066: * @param url Description of Parameter
067: * @param info Description of Parameter
068: * @return The propertyInfo value
069: * @exception SQLException Description of Exception
070: * @since
071: */
072: public DriverPropertyInfo[] getPropertyInfo(String url,
073: Properties info) throws SQLException {
074: return new DriverPropertyInfo[0];
075: }
076:
077: /**
078: *Gets the majorVersion attribute of the I18nJdbc object
079: *
080: * @return The majorVersion value
081: * @since
082: */
083: public int getMajorVersion() {
084: return 1;
085: }
086:
087: /**
088: *Gets the minorVersion attribute of the I18nJdbc object
089: *
090: * @return The minorVersion value
091: * @since
092: */
093: public int getMinorVersion() {
094: return 0;
095: }
096:
097: /**
098: *Description of the Method
099: *
100: * @param url Description of Parameter
101: * @param info Description of Parameter
102: * @return Description of the Returned Value
103: * @exception SQLException Description of Exception
104: * @since
105: */
106: public Connection connect(String url, Properties info)
107: throws SQLException {
108: DriverManager.println("i18nJdbc - I18nDriver:connect() - url="
109: + url);
110: // check for correct url
111: if (!url.startsWith(URL_PREFIX)) {
112: return null;
113: }
114: // get filepath from url
115: String filePath = url.substring(URL_PREFIX.length());
116: String filePathAll = filePath;
117: StringTokenizer st = new StringTokenizer(filePath, ";");
118:
119: //System.out.println("filePathAll="+filePathAll);
120:
121: filePath = st.nextToken();
122: if (!filePath.endsWith(File.separator)) {
123: filePath += File.separator;
124: }
125: DriverManager
126: .println("i18nJdbc - i18nDriver:connect() - filePath="
127: + filePath);
128: return new I18nConnection(filePathAll, info);
129: }
130:
131: /**
132: * Description of the Method
133: *
134: * @param url Description of Parameter
135: * @return Description of the Returned Value
136: * @exception SQLException Description of Exception
137: * @since
138: */
139: public boolean acceptsURL(String url) throws SQLException {
140: DriverManager.println("I18nJdbc - I18nDriver:accept() - url="
141: + url);
142: return url.startsWith(URL_PREFIX);
143: }
144:
145: /**
146: *Description of the Method
147: *
148: * @return Description of the Returned Value
149: * @since
150: */
151: public boolean jdbcCompliant() {
152: return false;
153: }
154:
155: // This static block inits the driver when the class is loaded by the JVM.
156: static {
157: try {
158: java.sql.DriverManager.registerDriver(new I18nDriver());
159: } catch (SQLException e) {
160: throw new RuntimeException(
161: "FATAL ERROR: Could not initialise i18n driver ! Message was: "
162: + e.getMessage());
163: }
164: }
165:
166: public static void log(String message) {
167: if (I18nDriver.ENABLE_LOG) {
168: try {
169: File file = new File("i18ndriver.log");
170: if (!file.exists())
171: file.createNewFile();
172: java.io.RandomAccessFile fileLogr = new java.io.RandomAccessFile(
173: file, "rw");
174: fileLogr.seek(fileLogr.length());
175: fileLogr.writeBytes("I18nJdbc, " + message + "\r\n");
176: fileLogr.close();
177: } catch (Exception ex) {
178: ex.printStackTrace();
179: }
180: }
181: }
182:
183: }
|