1 package org.tigris.scarab.util.build; 2 3 48 49 import java.io.File ; 50 import java.io.FileWriter ; 51 52 import org.apache.oro.text.regex.MalformedPatternException; 53 import org.apache.tools.ant.BuildException; 54 import org.apache.tools.ant.Task; 55 import org.tigris.scarab.util.RegexProcessor; 56 57 64 65 public class AntSchemaFix extends Task implements PropertyGetter 66 { 67 File sourceFile; 68 File targetFile; 69 String dbtype; 70 71 75 public void setSource(String theSourceFileName) 76 { 77 sourceFile = new File (adjust(theSourceFileName)); 78 if(!sourceFile.exists()) 79 { 80 System.out.println("wd=["+System.getProperty("user.dir")+"]"); 81 System.out.println("bd=["+this.getProject().getBaseDir()+"]"); 82 throw new BuildException("the source file [" 83 + theSourceFileName 84 + "] does not exist."); 85 } 86 87 if(!sourceFile.canWrite()) 88 { 89 throw new BuildException("the source file[" 90 + theSourceFileName 91 + "] is not writable."); 92 } 93 94 } 95 96 100 private String adjust(String theFileName) 101 { 102 String baseDir = this.getProject().getBaseDir().getAbsolutePath(); 103 System.setProperty("user.dir",baseDir); 104 105 String result = theFileName; 106 if( !theFileName.startsWith("/") && !theFileName.startsWith("\\") && theFileName.charAt(1)!=(':') ) 110 { 111 result = baseDir + File.separator + theFileName; 112 } 113 114 return result; 115 } 116 117 121 public void setTarget(String theTargetFileName) 122 { 123 124 targetFile = new File (adjust(theTargetFileName)); 125 if (targetFile.exists()) 126 { 127 targetFile.delete(); 128 } 129 130 } 131 132 137 public void setDbtype(String theDbtype) 138 { 139 dbtype = theDbtype; 140 } 141 142 146 public void execute() 147 { 148 if(dbtype.equals("hypersonic")) 149 { 150 System.out.println("dbtype \""+dbtype+"\" needs fixes ..."); 151 fixHsqlSchema(); 152 } 153 else 154 { 155 System.out.println("dbtype \""+dbtype+"\" is clean."); 156 } 157 } 158 159 160 161 167 private void fixHsqlSchema() 168 { 169 try 170 { 171 172 boolean modifySelf = false; 173 174 if(targetFile==null) 175 { 176 String targetFileName = sourceFile.getPath()+"tmp"; 177 setTarget(targetFileName); 178 modifySelf=true; 179 } 180 181 java.io.BufferedReader rdr = new java.io.BufferedReader (new java.io.FileReader (sourceFile)); 182 FileWriter fw = new java.io.FileWriter (targetFile); 183 java.io.BufferedWriter wrtr = new java.io.BufferedWriter (fw); 184 185 String str; 186 RegexProcessor processor = new RegexProcessor(); 187 while ((str = rdr.readLine()) != null) 188 { 189 190 String fstr = processor.process(str,"integer \\(\\d+\\)", "integer"); 191 fstr = processor.process(fstr,"DELETED\\s+integer[^,]*,", "DELETED integer DEFAULT 0,"); 192 193 199 wrtr.write(fstr); 200 wrtr.newLine(); 201 } 202 rdr.close(); 203 wrtr.close(); 204 205 if(modifySelf) 206 { 207 sourceFile.delete(); 208 targetFile.renameTo(sourceFile); 209 System.out.println("replaced ["+sourceFile.getPath()+"]..."); 210 } 211 else 212 { 213 System.out.println("created ["+targetFile.getPath()+"]..."); 214 } 215 216 } 217 218 catch (java.io.IOException e1) 219 { 220 throw new BuildException("IOException while processing Hsql-schema.["+e1.getMessage()+"]"); 221 } 222 223 catch (MalformedPatternException e2) 224 { 225 throw new BuildException("Regex Error while processing Hsql-schema.["+e2.getMessage()+"]"); 226 } 227 228 } 229 230 235 public Object getProperty(String name, Object def) 236 { 237 throw new BuildException("Tried to getProperty() from non implemented method."); 238 } 239 } 240 | Popular Tags |