1 19 20 package org.webdocwf.util.i18njdbc; 21 22 import java.io.File ; 23 import java.sql.CallableStatement ; 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.sql.SQLWarning ; 29 import java.sql.Savepoint ; 30 import java.sql.Statement ; 31 import java.util.Enumeration ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 import java.util.Vector ; 36 37 import org.webdocwf.util.i18njdbc.I18nDriver; 38 39 40 46 public class I18nConnection 47 implements Connection { 48 49 50 private String path; 51 52 53 private String nameColumn = I18nDriver.DEFAULT_NAMECOLUMN; 55 private String valueColumn = I18nDriver.DEFAULT_VALUECOLUMN; 56 private boolean create = I18nDriver.DEFAULT_CREATE; 57 private String extension = I18nDriver.DEFAULT_EXTENSION; 58 private I18nProperties prop; 59 private String currentTableName = null; 60 61 62 private Vector statements = new Vector (); 63 64 65 67 68 private boolean closed; 69 70 71 private boolean autoCommit=true; 72 73 74 78 protected I18nConnection(String path) throws SQLException { 79 init(path); 80 this.prop = new I18nProperties(); 81 } 82 83 84 89 protected I18nConnection(String path, Properties info) throws SQLException { 90 91 if (info != null) { 93 if (info.getProperty(I18nDriver.FILE_EXTENSION) != null) { 95 extension = info.getProperty(I18nDriver.FILE_EXTENSION); 96 if (!extension.startsWith(".")) 97 this.extension = "." + extension; 98 } 99 if (info.getProperty(I18nDriver.NAMECOLUMN) != null) { 101 this.nameColumn = info.getProperty(I18nDriver.NAMECOLUMN); 102 } 103 if (info.getProperty(I18nDriver.VALUECOLUMN) != null) { 105 this.valueColumn = info.getProperty(I18nDriver.VALUECOLUMN); 106 } 107 if (info.getProperty(I18nDriver.CREATE) != null) { 113 this.create = Boolean.valueOf(info.getProperty( 114 I18nDriver.CREATE)).booleanValue(); 115 } 116 } 117 init(path); 118 this.prop = new I18nProperties(); 119 } 120 125 protected void init(String path) throws SQLException { 126 if (path == null || path.length() == 0) { 127 throw new IllegalArgumentException ( 128 "'path' argument may not be empty or null"); 129 } 130 StringTokenizer st = new StringTokenizer (path, ";"); 132 this.path = st.nextToken(); 133 if (!this.path.endsWith(File.separator)) { 134 this.path += File.separator; 135 } 136 while (st.hasMoreTokens()) { 137 String next = st.nextToken(); 138 if (!this.setProperty(next)) { 139 throw new IllegalArgumentException ( 140 "unknown property " + next); 141 } 142 } 143 File filePath = new File (this.path); 144 145 if (!this.create && !filePath.exists()) { 146 throw new SQLException ( 147 "Specified path '" + filePath.getAbsolutePath() + 148 "' does not exist !"); 149 } 150 151 if (this.create && !filePath.exists()) 152 filePath.mkdirs(); 153 } 154 155 160 private boolean setProperty(String propString) { 161 boolean retVal = true; 162 StringTokenizer st = new StringTokenizer (propString, "="); 163 String name = st.nextToken(); 164 String value = st.nextToken(); 165 166 if (name.equals(I18nDriver.FILE_EXTENSION)) { 167 if (!value.startsWith(".")) 168 value = "." + value; 169 this.extension = value; 170 } 171 else if (name.equals(I18nDriver.CREATE)) { 175 this.create = Boolean.valueOf(value).booleanValue(); 176 } 177 else if (name.equals(I18nDriver.NAMECOLUMN)) { 178 this.nameColumn = value; 179 } 180 else if (name.equals(I18nDriver.VALUECOLUMN)) { 181 this.valueColumn = value; 182 } 183 else 184 retVal = false; 185 return retVal; 186 } 187 188 203 public Statement createStatement() throws SQLException { 204 I18nStatement statement = new I18nStatement(this); 205 statements.add(statement); 206 return statement; 207 } 208 209 238 public PreparedStatement prepareStatement(String sql) throws SQLException { 239 int index = sql.indexOf("?"); 240 while (index != -1) { 241 sql = sql.substring(0, index) + I18nPreparedStatement.PREPARE_SEPARATOR + sql.substring(index + 1); 242 index = sql.indexOf("?"); 243 } 244 I18nPreparedStatement statement = new I18nPreparedStatement(this, sql); 245 statements.add(statement); 246 return statement; 247 248 } 249 250 277 public CallableStatement prepareCall(String sql) throws SQLException { 278 throw new UnsupportedOperationException ( 279 "Connection.prepareCall(String) unsupported"); 280 } 281 282 293 public String nativeSQL(String sql) throws SQLException { 294 throw new UnsupportedOperationException ( 295 "Connection.nativeSQL(String) unsupported"); 296 } 297 298 326 public void setAutoCommit(boolean autoCommit) throws SQLException { 327 this.autoCommit = autoCommit; 328 } 329 330 339 public boolean getAutoCommit() throws SQLException { 340 return this.autoCommit; 341 } 342 343 354 public void commit() throws SQLException { 355 for (int i = 0; i < this.statements.size(); i++) { 356 ( (Statement ) statements.get(i)).close(); 357 } 358 try { 359 if(getCurrentTableName() != null) { 360 this.prop.store(new File (getPath() + getCurrentTableName() + getExtension())); 361 this.prop = new I18nProperties(); 362 this.currentTableName = null; 363 } 364 } catch (Exception e) { 365 e.printStackTrace(); 366 throw new SQLException (e.getMessage()); 367 } 368 } 369 370 380 public void rollback() throws SQLException { 381 throw new UnsupportedOperationException ( 382 "Connection.rollback() unsupported"); 383 } 384 385 399 public void close() throws SQLException { 400 for (Enumeration i = statements.elements(); i.hasMoreElements(); ) { 402 Statement statement = (Statement ) i.nextElement(); 403 statement.close(); 404 } 405 closed = true; 407 } 408 409 426 public boolean isClosed() throws SQLException { 427 return closed; 428 } 429 430 442 public DatabaseMetaData getMetaData() throws SQLException { 443 throw new UnsupportedOperationException ( 444 "Connection.getMetaData() unsupported"); 445 } 446 447 458 public void setReadOnly(boolean readOnly) throws SQLException { 459 throw new UnsupportedOperationException ( 460 "Connection.setReadOnly(boolean) unsupported"); 461 } 462 463 471 public boolean isReadOnly() throws SQLException { 472 return true; 473 } 474 475 488 public void setCatalog(String catalog) throws SQLException { 489 } 491 492 499 public String getCatalog() throws SQLException { 500 return null; 501 } 502 503 525 public void setTransactionIsolation(int level) throws SQLException { 526 throw new UnsupportedOperationException ( 527 "Connection.setTransactionIsolation(int) unsupported"); 528 } 529 530 544 public int getTransactionIsolation() throws SQLException { 545 return Connection.TRANSACTION_NONE; 546 } 547 548 569 public SQLWarning getWarnings() throws SQLException { 570 throw new UnsupportedOperationException ( 571 "Connection.getWarnings() unsupported"); 572 } 573 574 582 public void clearWarnings() throws SQLException { 583 throw new UnsupportedOperationException ( 584 "Connection.getWarnings() unsupported"); 585 } 586 587 589 610 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws 611 SQLException { 612 throw new UnsupportedOperationException ( 613 "Connection.createStatement(int, int) unsupported"); 614 } 615 616 640 public PreparedStatement prepareStatement(String sql, int resultSetType, 641 int resultSetConcurrency) throws 642 SQLException { 643 throw new UnsupportedOperationException ( 644 "Connection.prepareStatement(String, int, int) unsupported"); 645 } 646 647 670 public CallableStatement prepareCall(String sql, int resultSetType, 671 int resultSetConcurrency) throws 672 SQLException { 673 throw new UnsupportedOperationException ( 674 "Connection.prepareCall(String, int, int) unsupported"); 675 } 676 677 688 public Map getTypeMap() throws SQLException { 689 throw new UnsupportedOperationException ( 690 "Connection.getTypeMap() unsupported"); 691 } 692 693 706 public void setTypeMap(Map map) throws SQLException { 707 throw new UnsupportedOperationException ( 708 "Connection.setTypeMap(Map) unsupported"); 709 } 710 711 727 public void setHoldability(int holdability) throws SQLException { 728 throw new UnsupportedOperationException ( 729 "Connection.setHoldability(int) unsupported"); 730 } 731 732 743 public int getHoldability() throws SQLException { 744 throw new UnsupportedOperationException ( 745 "Connection.getHoldability() unsupported"); 746 } 747 748 public Savepoint setSavepoint() throws SQLException { 750 throw new UnsupportedOperationException ( 751 "Connection.setSavepoint() unsupported"); 752 } 753 754 public Savepoint setSavepoint(String name) throws SQLException { 755 throw new UnsupportedOperationException ( 756 "Connection.setSavepoint(String) unsupported"); 757 } 758 759 public void rollback(Savepoint savepoint) throws SQLException { 760 throw new UnsupportedOperationException ( 761 "Connection.rollback(Savepoint) unsupported"); 762 } 763 764 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 765 throw new UnsupportedOperationException ( 766 "Connection.releaseSavepoint(Savepoint) unsupported"); 767 } 768 769 public Statement createStatement(int resultSetType, 770 int resultSetConcurrency, 771 int resultSetHoldability) throws 772 SQLException { 773 throw new UnsupportedOperationException ( 774 "Connection.createStatement(int,int,int) unsupported"); 775 } 776 777 public PreparedStatement prepareStatement(String sql, 778 int resultSetType, 779 int resultSetConcurrency, 780 int resultSetHoldability) throws 781 SQLException { 782 throw new UnsupportedOperationException ( 783 "Connection.prepareStatement(String,int,int,int) unsupported"); 784 } 785 786 public CallableStatement prepareCall(String sql, 787 int resultSetType, 788 int resultSetConcurrency, 789 int resultSetHoldability) throws 790 SQLException { 791 throw new UnsupportedOperationException ( 792 "Connection.prepareCall(String,int,int,int) unsupported"); 793 } 794 795 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws 796 SQLException { 797 throw new UnsupportedOperationException ( 798 "Connection.prepareStatement(String,int) unsupported"); 799 } 800 801 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws 802 SQLException { 803 throw new UnsupportedOperationException ( 804 "Connection.prepareStatement(String,int[]) unsupported"); 805 } 806 807 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws 808 SQLException { 809 throw new UnsupportedOperationException ( 810 "Connection.prepareStatement(String,String[]) unsupported"); 811 } 812 813 817 821 protected String getPath() { 822 return path; 823 } 824 825 829 protected String getExtension() { 830 return extension; 831 } 832 833 834 838 842 854 858 861 protected String getNameColumn() { 862 return this.nameColumn; 863 } 864 865 868 protected String getValueColumn() { 869 return this.valueColumn; 870 } 871 872 875 protected String [] getColumnNames() { 876 String [] retString = new String [2]; 877 retString[0] = this.nameColumn; 878 retString[1] = this.valueColumn; 879 return retString; 880 } 881 882 public I18nProperties getProperties() { 883 return prop; 884 } 885 886 public void setProperties(I18nProperties prop) { 887 this.prop = prop; 888 } 889 890 public String getCurrentTableName() { 891 return currentTableName; 892 } 893 894 public void setCurrentTableName(String currentFileName) throws SQLException { 895 if( !this.getAutoCommit() && (this.currentTableName != null) && !this.currentTableName.equals(currentFileName) ) { 896 this.commit(); 897 } 898 this.currentTableName = currentFileName; 899 } 900 } 901 | Popular Tags |