| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. 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 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.sqlparser; |
|---|
| 34 | |
|---|
| 35 | import java.io.File; |
|---|
| 36 | import java.util.ArrayList; |
|---|
| 37 | import java.util.HashSet; |
|---|
| 38 | import java.util.Iterator; |
|---|
| 39 | import java.util.List; |
|---|
| 40 | import java.util.Set; |
|---|
| 41 | |
|---|
| 42 | import org.apache.tools.ant.BuildException; |
|---|
| 43 | import org.apache.tools.ant.DirectoryScanner; |
|---|
| 44 | import org.apache.tools.ant.Project; |
|---|
| 45 | import org.apache.tools.ant.taskdefs.MatchingTask; |
|---|
| 46 | import org.apache.tools.ant.types.FileSet; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Ant task for the SQL Transformer. |
|---|
| 50 | * |
|---|
| 51 | * @author Michael Griffel |
|---|
| 52 | */ |
|---|
| 53 | public class SqlTransformerTask |
|---|
| 54 | extends MatchingTask |
|---|
| 55 | { |
|---|
| 56 | /** list of input files. */ |
|---|
| 57 | private final List mFilesets = new ArrayList(); |
|---|
| 58 | /** the output directory. */ |
|---|
| 59 | private File mOutputDir; |
|---|
| 60 | /** terminate ant build on error. */ |
|---|
| 61 | private boolean mFailOnError = false; |
|---|
| 62 | /** force output of target files even if they already exist. */ |
|---|
| 63 | private boolean mForce = false; |
|---|
| 64 | /** the metaInf file (optional). */ |
|---|
| 65 | private File mMetaInfFile = null; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Specifies the output directory. |
|---|
| 70 | * @param d the output directory. |
|---|
| 71 | */ |
|---|
| 72 | public void setTodir (File d) |
|---|
| 73 | { |
|---|
| 74 | mOutputDir = d; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Set whether we should fail on an error. |
|---|
| 79 | * |
|---|
| 80 | * @param b Whether we should fail on an error. |
|---|
| 81 | */ |
|---|
| 82 | public void setFailonerror (boolean b) |
|---|
| 83 | { |
|---|
| 84 | mFailOnError = b; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Sets the force output of target files flag to the given value. |
|---|
| 89 | * |
|---|
| 90 | * @param b Whether we should force the generation of output files. |
|---|
| 91 | */ |
|---|
| 92 | public void setForce (boolean b) |
|---|
| 93 | { |
|---|
| 94 | mForce = b; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Specifies the MetaInf file (optional parameter). |
|---|
| 99 | * @param f the MetaInf file. |
|---|
| 100 | */ |
|---|
| 101 | public void setMetainf (File f) |
|---|
| 102 | { |
|---|
| 103 | mMetaInfFile = f; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Adds the given files to the file set. |
|---|
| 108 | * @param set the file set to add. |
|---|
| 109 | */ |
|---|
| 110 | public void addFileset (FileSet set) |
|---|
| 111 | { |
|---|
| 112 | mFilesets.add(set); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * |
|---|
| 117 | * Execute this task. |
|---|
| 118 | * |
|---|
| 119 | * @throws BuildException An building exception occurred. |
|---|
| 120 | */ |
|---|
| 121 | public void execute () |
|---|
| 122 | throws BuildException |
|---|
| 123 | { |
|---|
| 124 | try |
|---|
| 125 | { |
|---|
| 126 | checkAttributes(); |
|---|
| 127 | |
|---|
| 128 | final File[] files = getSuiteFiles(); |
|---|
| 129 | int transformedFiles = 0; |
|---|
| 130 | for (int i = 0; i < files.length; i++) |
|---|
| 131 | { |
|---|
| 132 | final File inFile = files[i]; |
|---|
| 133 | final File outFile = new File(mOutputDir, inFile.getName()); |
|---|
| 134 | if (mForce || inFile.lastModified() > outFile.lastModified()) |
|---|
| 135 | { |
|---|
| 136 | log("Transforming file: " + inFile, Project.MSG_INFO); |
|---|
| 137 | final SqlTransformer transformer |
|---|
| 138 | = new SqlTransformer( |
|---|
| 139 | inFile.getAbsolutePath(), |
|---|
| 140 | outFile.getAbsolutePath(), |
|---|
| 141 | mMetaInfFile != null |
|---|
| 142 | ? mMetaInfFile.getAbsolutePath() : null, false); |
|---|
| 143 | transformer.execute(); |
|---|
| 144 | transformedFiles++; |
|---|
| 145 | } |
|---|
| 146 | else |
|---|
| 147 | { |
|---|
| 148 | log(inFile + " omitted as " |
|---|
| 149 | + outFile + " is up to date.", Project.MSG_VERBOSE); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | if (transformedFiles > 0) |
|---|
| 153 | { |
|---|
| 154 | log(transformedFiles + " of " + files.length |
|---|
| 155 | + " files transformed successfully", Project.MSG_INFO); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | catch (BuildException e) |
|---|
| 159 | { |
|---|
| 160 | if (mFailOnError) |
|---|
| 161 | { |
|---|
| 162 | throw e; |
|---|
| 163 | } |
|---|
| 164 | log(e.getMessage(), Project.MSG_ERR); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | private void checkAttributes () |
|---|
| 170 | { |
|---|
| 171 | if (mOutputDir == null) |
|---|
| 172 | { |
|---|
| 173 | throw new BuildException( |
|---|
| 174 | "Missing mandatory attribute 'todir'.", getLocation()); |
|---|
| 175 | } |
|---|
| 176 | ensureDirectoryFor(mOutputDir); |
|---|
| 177 | |
|---|
| 178 | if (mMetaInfFile != null && !mMetaInfFile.exists()) |
|---|
| 179 | { |
|---|
| 180 | throw new BuildException( |
|---|
| 181 | "Cannot find META-INF file '" + mMetaInfFile + "'.", |
|---|
| 182 | getLocation()); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Ensure the directory exists for a given directory name. |
|---|
| 188 | * |
|---|
| 189 | * @param targetFile the directory name that is required. |
|---|
| 190 | * @exception BuildException if the directories cannot be created. |
|---|
| 191 | */ |
|---|
| 192 | private static void ensureDirectoryFor (File directory) |
|---|
| 193 | throws BuildException |
|---|
| 194 | { |
|---|
| 195 | if (!directory.exists()) |
|---|
| 196 | { |
|---|
| 197 | if (!directory.mkdirs()) |
|---|
| 198 | { |
|---|
| 199 | throw new BuildException("Unable to create directory: " |
|---|
| 200 | + directory.getAbsolutePath()); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | private File[] getSuiteFiles () |
|---|
| 206 | { |
|---|
| 207 | final Set set = new HashSet(); |
|---|
| 208 | |
|---|
| 209 | final Iterator iterator = mFilesets.iterator(); |
|---|
| 210 | while (iterator.hasNext()) |
|---|
| 211 | { |
|---|
| 212 | final FileSet fs = (FileSet) iterator.next(); |
|---|
| 213 | |
|---|
| 214 | final DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
|---|
| 215 | final File dir = fs.getDir(getProject()); |
|---|
| 216 | final String[] filenames = ds.getIncludedFiles(); |
|---|
| 217 | for (int i = 0; i < filenames.length; i++) |
|---|
| 218 | { |
|---|
| 219 | set.add(new File(dir, filenames[i])); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | final int size = set.size(); |
|---|
| 224 | log("Found " + size + " files in " + mFilesets.size() + " filesets", |
|---|
| 225 | Project.MSG_VERBOSE); |
|---|
| 226 | |
|---|
| 227 | return (File[]) set.toArray(new File[size]); |
|---|
| 228 | } |
|---|
| 229 | } |
|---|