KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > build > AntSchemaFix


1 package org.tigris.scarab.util.build;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.File JavaDoc;
50 import java.io.FileWriter JavaDoc;
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 /**
58  * This class is used as ant task backend for the generation
59  * of a property file by use of a template file.
60  *
61  * @author <a HREF="mailto:dabbous@saxess.com">Hussayn Dabbous</a>
62  * @version $Id: AntPropertyFileGenerator.java 9421 2005-02-20 22:32:38Z jorgeuriarte $
63  */

64
65 public class AntSchemaFix extends Task implements PropertyGetter
66 {
67     File JavaDoc sourceFile;
68     File JavaDoc targetFile;
69     String JavaDoc dbtype;
70     
71     /**
72      * Source schema file to be fixed.
73      * @param theSourceFileName
74      */

75     public void setSource(String JavaDoc theSourceFileName)
76     {
77         sourceFile = new File JavaDoc(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     /**
97      * @param theFileName
98      * @return
99      */

100     private String JavaDoc adjust(String JavaDoc theFileName)
101     {
102         String JavaDoc baseDir = this.getProject().getBaseDir().getAbsolutePath();
103         System.setProperty("user.dir",baseDir);
104         
105         String JavaDoc result = theFileName;
106         if( !theFileName.startsWith("/") // this is for unix
107
&& !theFileName.startsWith("\\") // this is for windows
108
&& theFileName.charAt(1)!=(':') // this is for windows
109
)
110         {
111             result = baseDir + File.separator + theFileName;
112         }
113         
114         return result;
115     }
116
117     /**
118      * target schema file to be created.
119      * @param theTargetFileName
120      */

121     public void setTarget(String JavaDoc theTargetFileName)
122     {
123
124         targetFile = new File JavaDoc(adjust(theTargetFileName));
125         if (targetFile.exists())
126         {
127             targetFile.delete();
128         }
129         
130     }
131
132     /**
133      * database type for which to run the fix.
134      * currently only hypersonic needs a fix.
135      * @param theDbtype
136      */

137     public void setDbtype(String JavaDoc theDbtype)
138     {
139         dbtype = theDbtype;
140     }
141
142     /**
143      * fix schema-file if dbtype is hypersonic.
144      * due to an error in the hypersonic-torque generator.
145      */

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     /**
162      * replace "integer(...)" by "integer"
163      * replace "DELETED ... ," by "DELETED integer DEFAULT 0,"
164      * @param sourceFile
165      * @param targetFile
166      */

167     private void fixHsqlSchema()
168     {
169         try
170         {
171             
172             boolean modifySelf = false;
173
174             if(targetFile==null)
175             {
176                 String JavaDoc targetFileName = sourceFile.getPath()+"tmp";
177                 setTarget(targetFileName);
178                 modifySelf=true;
179             }
180             
181             java.io.BufferedReader JavaDoc rdr = new java.io.BufferedReader JavaDoc(new java.io.FileReader JavaDoc(sourceFile));
182             FileWriter JavaDoc fw = new java.io.FileWriter JavaDoc(targetFile);
183             java.io.BufferedWriter JavaDoc wrtr = new java.io.BufferedWriter JavaDoc(fw);
184             
185             String JavaDoc str;
186             RegexProcessor processor = new RegexProcessor();
187             while ((str = rdr.readLine()) != null)
188             {
189                 
190                 String JavaDoc fstr = processor.process(str,"integer \\(\\d+\\)", "integer");
191                 fstr = processor.process(fstr,"DELETED\\s+integer[^,]*,", "DELETED integer DEFAULT 0,");
192
193                 //if(!fstr.equals(str))
194
//{
195
// System.out.println("old: "+str);
196
// System.out.println("new: "+fstr);
197
//}
198

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 JavaDoc 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     /**
231      * dummy method. Not used.
232      * @param name
233      * @return
234      */

235     public Object JavaDoc getProperty(String JavaDoc name, Object JavaDoc def)
236     {
237         throw new BuildException("Tried to getProperty() from non implemented method.");
238     }
239 }
240
Popular Tags