| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 50 | | | |
| 51 | | | @author |
| 52 | | | |
| 53 | 0 | | public class SqlTransformerTask |
| 54 | | | extends MatchingTask |
| 55 | | | { |
| 56 | | | |
| 57 | 0 | | private final List mFilesets = new ArrayList(); |
| 58 | | | |
| 59 | | | private File mOutputDir; |
| 60 | | | |
| 61 | 0 | | private boolean mFailOnError = false; |
| 62 | | | |
| 63 | 0 | | private boolean mForce = false; |
| 64 | | | |
| 65 | 0 | | private File mMetaInfFile = null; |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | @param |
| 71 | | | |
| 72 | | | public void setTodir (File d) |
| 73 | | | { |
| 74 | 0 | | mOutputDir = d; |
| 75 | 0 | | } |
| 76 | | | |
| 77 | | | |
| 78 | | | |
| 79 | | | |
| 80 | | | @param |
| 81 | | | |
| 82 | | | public void setFailonerror (boolean b) |
| 83 | | | { |
| 84 | 0 | | mFailOnError = b; |
| 85 | 0 | | } |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | @param |
| 91 | | | |
| 92 | | | public void setForce (boolean b) |
| 93 | | | { |
| 94 | 0 | | mForce = b; |
| 95 | 0 | | } |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | @param |
| 100 | | | |
| 101 | | | public void setMetainf (File f) |
| 102 | | | { |
| 103 | 0 | | mMetaInfFile = f; |
| 104 | 0 | | } |
| 105 | | | |
| 106 | | | |
| 107 | | | |
| 108 | | | @param |
| 109 | | | |
| 110 | | | public void addFileset (FileSet set) |
| 111 | | | { |
| 112 | 0 | | mFilesets.add(set); |
| 113 | 0 | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | @throws |
| 120 | | | |
| 121 | | | public void execute () |
| 122 | | | throws BuildException |
| 123 | | | { |
| 124 | | | try |
| 125 | | | { |
| 126 | 0 | | checkAttributes(); |
| 127 | | | |
| 128 | 0 | | final File[] files = getSuiteFiles(); |
| 129 | 0 | | int transformedFiles = 0; |
| 130 | 0 | | for (int i = 0; i < files.length; i++) |
| 131 | | | { |
| 132 | 0 | | final File inFile = files[i]; |
| 133 | 0 | | final File outFile = new File(mOutputDir, inFile.getName()); |
| 134 | 0 | | if (mForce || inFile.lastModified() > outFile.lastModified()) |
| 135 | | | { |
| 136 | 0 | | log("Transforming file: " + inFile, Project.MSG_INFO); |
| 137 | 0 | | final SqlTransformer transformer |
| 138 | | | = new SqlTransformer( |
| 139 | | | inFile.getAbsolutePath(), |
| 140 | | | outFile.getAbsolutePath(), |
| 141 | | | mMetaInfFile != null |
| 142 | | | ? mMetaInfFile.getAbsolutePath() : null, false); |
| 143 | 0 | | transformer.execute(); |
| 144 | 0 | | transformedFiles++; |
| 145 | 0 | | } |
| 146 | | | else |
| 147 | | | { |
| 148 | 0 | | log(inFile + " omitted as " |
| 149 | | | + outFile + " is up to date.", Project.MSG_VERBOSE); |
| 150 | | | } |
| 151 | | | } |
| 152 | 0 | | if (transformedFiles > 0) |
| 153 | | | { |
| 154 | 0 | | log(transformedFiles + " of " + files.length |
| 155 | | | + " files transformed successfully", Project.MSG_INFO); |
| 156 | | | } |
| 157 | | | } |
| 158 | 0 | | catch (BuildException e) |
| 159 | | | { |
| 160 | 0 | | if (mFailOnError) |
| 161 | | | { |
| 162 | 0 | | throw e; |
| 163 | | | } |
| 164 | 0 | | log(e.getMessage(), Project.MSG_ERR); |
| 165 | 0 | | } |
| 166 | 0 | | } |
| 167 | | | |
| 168 | | | |
| 169 | | | private void checkAttributes () |
| 170 | | | { |
| 171 | 0 | | if (mOutputDir == null) |
| 172 | | | { |
| 173 | 0 | | throw new BuildException( |
| 174 | | | "Missing mandatory attribute 'todir'.", getLocation()); |
| 175 | | | } |
| 176 | 0 | | ensureDirectoryFor(mOutputDir); |
| 177 | | | |
| 178 | 0 | | if (mMetaInfFile != null && !mMetaInfFile.exists()) |
| 179 | | | { |
| 180 | 0 | | throw new BuildException( |
| 181 | | | "Cannot find META-INF file '" + mMetaInfFile + "'.", |
| 182 | | | getLocation()); |
| 183 | | | } |
| 184 | 0 | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | @param |
| 190 | | | @exception |
| 191 | | | |
| 192 | | | private static void ensureDirectoryFor (File directory) |
| 193 | | | throws BuildException |
| 194 | | | { |
| 195 | 0 | | if (!directory.exists()) |
| 196 | | | { |
| 197 | 0 | | if (!directory.mkdirs()) |
| 198 | | | { |
| 199 | 0 | | throw new BuildException("Unable to create directory: " |
| 200 | | | + directory.getAbsolutePath()); |
| 201 | | | } |
| 202 | | | } |
| 203 | 0 | | } |
| 204 | | | |
| 205 | | | private File[] getSuiteFiles () |
| 206 | | | { |
| 207 | 0 | | final Set set = new HashSet(); |
| 208 | | | |
| 209 | 0 | | final Iterator iterator = mFilesets.iterator(); |
| 210 | 0 | | while (iterator.hasNext()) |
| 211 | | | { |
| 212 | 0 | | final FileSet fs = (FileSet) iterator.next(); |
| 213 | | | |
| 214 | 0 | | final DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
| 215 | 0 | | final File dir = fs.getDir(getProject()); |
| 216 | 0 | | final String[] filenames = ds.getIncludedFiles(); |
| 217 | 0 | | for (int i = 0; i < filenames.length; i++) |
| 218 | | | { |
| 219 | 0 | | set.add(new File(dir, filenames[i])); |
| 220 | | | } |
| 221 | 0 | | } |
| 222 | | | |
| 223 | 0 | | final int size = set.size(); |
| 224 | 0 | | log("Found " + size + " files in " + mFilesets.size() + " filesets", |
| 225 | | | Project.MSG_VERBOSE); |
| 226 | | | |
| 227 | 0 | | return (File[]) set.toArray(new File[size]); |
| 228 | | | } |
| 229 | | | } |