001: package org.tigris.scarab.util.build;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.io.File;
050: import java.io.FileWriter;
051:
052: import org.apache.oro.text.regex.MalformedPatternException;
053: import org.apache.tools.ant.BuildException;
054: import org.apache.tools.ant.Task;
055: import org.tigris.scarab.util.RegexProcessor;
056:
057: /**
058: * This class is used as ant task backend for the generation
059: * of a property file by use of a template file.
060: *
061: * @author <a href="mailto:dabbous@saxess.com">Hussayn Dabbous</a>
062: * @version $Id: AntPropertyFileGenerator.java 9421 2005-02-20 22:32:38Z jorgeuriarte $
063: */
064:
065: public class AntSchemaFix extends Task implements PropertyGetter {
066: File sourceFile;
067: File targetFile;
068: String dbtype;
069:
070: /**
071: * Source schema file to be fixed.
072: * @param theSourceFileName
073: */
074: public void setSource(String theSourceFileName) {
075: sourceFile = new File(adjust(theSourceFileName));
076: if (!sourceFile.exists()) {
077: System.out.println("wd=[" + System.getProperty("user.dir")
078: + "]");
079: System.out.println("bd=[" + this .getProject().getBaseDir()
080: + "]");
081: throw new BuildException("the source file ["
082: + theSourceFileName + "] does not exist.");
083: }
084:
085: if (!sourceFile.canWrite()) {
086: throw new BuildException("the source file["
087: + theSourceFileName + "] is not writable.");
088: }
089:
090: }
091:
092: /**
093: * @param theFileName
094: * @return
095: */
096: private String adjust(String theFileName) {
097: String baseDir = this .getProject().getBaseDir()
098: .getAbsolutePath();
099: System.setProperty("user.dir", baseDir);
100:
101: String result = theFileName;
102: if (!theFileName.startsWith("/") // this is for unix
103: && !theFileName.startsWith("\\") // this is for windows
104: && theFileName.charAt(1) != (':') // this is for windows
105: ) {
106: result = baseDir + File.separator + theFileName;
107: }
108:
109: return result;
110: }
111:
112: /**
113: * target schema file to be created.
114: * @param theTargetFileName
115: */
116: public void setTarget(String theTargetFileName) {
117:
118: targetFile = new File(adjust(theTargetFileName));
119: if (targetFile.exists()) {
120: targetFile.delete();
121: }
122:
123: }
124:
125: /**
126: * database type for which to run the fix.
127: * currently only hypersonic needs a fix.
128: * @param theDbtype
129: */
130: public void setDbtype(String theDbtype) {
131: dbtype = theDbtype;
132: }
133:
134: /**
135: * fix schema-file if dbtype is hypersonic.
136: * due to an error in the hypersonic-torque generator.
137: */
138: public void execute() {
139: if (dbtype.equals("hypersonic")) {
140: System.out.println("dbtype \"" + dbtype
141: + "\" needs fixes ...");
142: fixHsqlSchema();
143: } else {
144: System.out.println("dbtype \"" + dbtype + "\" is clean.");
145: }
146: }
147:
148: /**
149: * replace "integer(...)" by "integer"
150: * replace "DELETED ... ," by "DELETED integer DEFAULT 0,"
151: * @param sourceFile
152: * @param targetFile
153: */
154: private void fixHsqlSchema() {
155: try {
156:
157: boolean modifySelf = false;
158:
159: if (targetFile == null) {
160: String targetFileName = sourceFile.getPath() + "tmp";
161: setTarget(targetFileName);
162: modifySelf = true;
163: }
164:
165: java.io.BufferedReader rdr = new java.io.BufferedReader(
166: new java.io.FileReader(sourceFile));
167: FileWriter fw = new java.io.FileWriter(targetFile);
168: java.io.BufferedWriter wrtr = new java.io.BufferedWriter(fw);
169:
170: String str;
171: RegexProcessor processor = new RegexProcessor();
172: while ((str = rdr.readLine()) != null) {
173:
174: String fstr = processor.process(str,
175: "integer \\(\\d+\\)", "integer");
176: fstr = processor.process(fstr,
177: "DELETED\\s+integer[^,]*,",
178: "DELETED integer DEFAULT 0,");
179:
180: //if(!fstr.equals(str))
181: //{
182: // System.out.println("old: "+str);
183: // System.out.println("new: "+fstr);
184: //}
185:
186: wrtr.write(fstr);
187: wrtr.newLine();
188: }
189: rdr.close();
190: wrtr.close();
191:
192: if (modifySelf) {
193: sourceFile.delete();
194: targetFile.renameTo(sourceFile);
195: System.out.println("replaced [" + sourceFile.getPath()
196: + "]...");
197: } else {
198: System.out.println("created [" + targetFile.getPath()
199: + "]...");
200: }
201:
202: }
203:
204: catch (java.io.IOException e1) {
205: throw new BuildException(
206: "IOException while processing Hsql-schema.["
207: + e1.getMessage() + "]");
208: }
209:
210: catch (MalformedPatternException e2) {
211: throw new BuildException(
212: "Regex Error while processing Hsql-schema.["
213: + e2.getMessage() + "]");
214: }
215:
216: }
217:
218: /**
219: * dummy method. Not used.
220: * @param name
221: * @return
222: */
223: public Object getProperty(String name, Object def) {
224: throw new BuildException(
225: "Tried to getProperty() from non implemented method.");
226: }
227: }
|