| 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.commons.taskdefs; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FilenameFilter; |
| 38 | | | import java.util.ArrayList; |
| 39 | | | import java.util.Iterator; |
| 40 | | | import java.util.List; |
| 41 | | | |
| 42 | | | import org.apache.tools.ant.BuildException; |
| 43 | | | import org.apache.tools.ant.Project; |
| 44 | | | import org.apache.tools.ant.Task; |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | @author |
| 51 | | | |
| 52 | | | public final class AntTaskUtil |
| 53 | | | { |
| 54 | | | private static final String FORMAT_SVG = "svg"; |
| 55 | | | |
| 56 | | | private static final int PACKET_SIZE = 1; |
| 57 | | | |
| 58 | | | private AntTaskUtil () |
| 59 | 0 | | { |
| 60 | | | |
| 61 | 0 | | } |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | @param |
| 67 | | | @exception |
| 68 | | | |
| 69 | | | public static void ensureDirectory (File directory) |
| 70 | | | throws BuildException |
| 71 | | | { |
| 72 | 100 | | if (!directory.exists()) |
| 73 | | | { |
| 74 | 0 | | if (!directory.mkdirs()) |
| 75 | | | { |
| 76 | 0 | | throw new BuildException("Unable to create directory: " |
| 77 | | | + directory.getAbsolutePath()); |
| 78 | | | } |
| 79 | | | } |
| 80 | 100 | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | @param |
| 86 | | | |
| 87 | | | @exception |
| 88 | | | |
| 89 | | | public static void ensureDirectoryForFile (File targetFile) |
| 90 | | | throws BuildException |
| 91 | | | { |
| 92 | 100 | | ensureDirectory(targetFile.getParentFile()); |
| 93 | 100 | | } |
| 94 | | | |
| 95 | | | |
| 96 | | | |
| 97 | | | |
| 98 | | | @param |
| 99 | | | @return |
| 100 | | | |
| 101 | | | public static String stripFileExtension (String fileWithExtension) |
| 102 | | | { |
| 103 | 0 | | final int lastIndexOfDot = fileWithExtension.lastIndexOf('.'); |
| 104 | | | final String result; |
| 105 | 0 | | if (lastIndexOfDot != -1) |
| 106 | | | { |
| 107 | 0 | | result = fileWithExtension.substring(0, lastIndexOfDot); |
| 108 | | | } |
| 109 | | | else |
| 110 | | | { |
| 111 | 0 | | result = fileWithExtension; |
| 112 | | | } |
| 113 | 0 | | return result; |
| 114 | | | } |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | @param |
| 120 | | | @param |
| 121 | | | @param |
| 122 | | | |
| 123 | | | public static void renderDotFiles (Task task, File dotDir, |
| 124 | | | boolean failOnError) |
| 125 | | | { |
| 126 | 0 | | final File[] dotFiles = dotDir.listFiles(new FilenameFilter() |
| 127 | 0 | | { |
| 128 | | | public boolean accept (File dir, String name) |
| 129 | | | { |
| 130 | | | final boolean result; |
| 131 | 0 | | if (name.endsWith(".dot")) |
| 132 | | | { |
| 133 | 0 | | result = true; |
| 134 | | | } |
| 135 | | | else |
| 136 | | | { |
| 137 | 0 | | result = false; |
| 138 | | | } |
| 139 | 0 | | return result; |
| 140 | | | } |
| 141 | | | }); |
| 142 | 0 | | if (dotFiles != null) |
| 143 | | | { |
| 144 | 0 | | dots2svgs(task, failOnError, dotFiles); |
| 145 | | | } |
| 146 | | | else |
| 147 | | | { |
| 148 | 0 | | task.log("No .dot files found to render", Project.MSG_VERBOSE); |
| 149 | | | } |
| 150 | 0 | | } |
| 151 | | | |
| 152 | | | |
| 153 | | | |
| 154 | | | |
| 155 | | | @param |
| 156 | | | @param |
| 157 | | | @param |
| 158 | | | |
| 159 | | | public static void renderGnuplotFiles (Task task, File gnuplotDir, |
| 160 | | | boolean failOnError) |
| 161 | | | { |
| 162 | 0 | | final File[] gnuplotFiles = gnuplotDir.listFiles(new FilenameFilter() |
| 163 | 0 | | { |
| 164 | | | public boolean accept (File dir, String name) |
| 165 | | | { |
| 166 | | | final boolean result; |
| 167 | 0 | | if (name.endsWith(".gnuplot")) |
| 168 | | | { |
| 169 | 0 | | result = true; |
| 170 | | | } |
| 171 | | | else |
| 172 | | | { |
| 173 | 0 | | result = false; |
| 174 | | | } |
| 175 | 0 | | return result; |
| 176 | | | } |
| 177 | | | }); |
| 178 | 0 | | if (gnuplotFiles != null) |
| 179 | | | { |
| 180 | 0 | | gnuplots2svgs(task, failOnError, gnuplotFiles); |
| 181 | | | } |
| 182 | | | else |
| 183 | | | { |
| 184 | 0 | | task.log("No .gnuplot files found to render", Project.MSG_VERBOSE); |
| 185 | | | } |
| 186 | 0 | | } |
| 187 | | | |
| 188 | | | private static void dots2svgs (Task task, boolean failOnError, |
| 189 | | | final File[] dotFiles) |
| 190 | | | { |
| 191 | | | |
| 192 | | | |
| 193 | | | |
| 194 | | | |
| 195 | 0 | | List dotPackets = createPackets(dotFiles); |
| 196 | 0 | | for (Iterator packetIter = dotPackets.iterator(); packetIter.hasNext();) |
| 197 | | | { |
| 198 | 0 | | File[] dotPacket = (File[]) packetIter.next(); |
| 199 | 0 | | final DotTask dot = new DotTask(); |
| 200 | 0 | | dot.setProject(task.getProject()); |
| 201 | 0 | | dot.setTaskName("dot"); |
| 202 | 0 | | dot.setFailonerror(failOnError); |
| 203 | 0 | | dot.setFormat(FORMAT_SVG); |
| 204 | 0 | | dot.setInFiles(dotPacket); |
| 205 | 0 | | dot.execute(); |
| 206 | 0 | | } |
| 207 | | | |
| 208 | 0 | | for (int i = 0; i < dotFiles.length; i++) |
| 209 | | | { |
| 210 | 0 | | File generatedFile = new File(dotFiles[i].getParentFile(), |
| 211 | | | dotFiles[i].getName() + "." + FORMAT_SVG); |
| 212 | 0 | | File targetFile = new File(dotFiles[i].getParentFile(), |
| 213 | | | stripFileExtension(dotFiles[i].getName()) + "." + FORMAT_SVG); |
| 214 | 0 | (1) | targetFile.delete(); |
| 215 | 0 | (2) | generatedFile.renameTo(targetFile); |
| 216 | | | } |
| 217 | 0 | | } |
| 218 | | | |
| 219 | | | private static void gnuplots2svgs (Task task, boolean failOnError, |
| 220 | | | final File[] dotFiles) |
| 221 | | | { |
| 222 | | | |
| 223 | | | |
| 224 | | | |
| 225 | | | |
| 226 | 0 | | List gnuplotPackets = createPackets(dotFiles); |
| 227 | 0 | (3) | for (Iterator packetIter = gnuplotPackets.iterator(); packetIter.hasNext();) |
| 228 | | | { |
| 229 | 0 | | File[] gnuplotPacket = (File[]) packetIter.next(); |
| 230 | 0 | | final GnuplotTask dot = new GnuplotTask(); |
| 231 | 0 | | dot.setProject(task.getProject()); |
| 232 | 0 | | dot.setTaskName("gnuplot"); |
| 233 | 0 | | dot.setFailonerror(failOnError); |
| 234 | 0 | | dot.setInFiles(gnuplotPacket); |
| 235 | 0 | | dot.execute(); |
| 236 | 0 | | } |
| 237 | 0 | | } |
| 238 | | | |
| 239 | | | private static List createPackets (File[] dotFiles) |
| 240 | | | { |
| 241 | 0 | | List result = new ArrayList(); |
| 242 | 0 | | for (int i = 0; i < (dotFiles.length / PACKET_SIZE) + 1; i++) |
| 243 | | | { |
| 244 | | | final File[] packet; |
| 245 | 0 | | int remaining = dotFiles.length - (i * PACKET_SIZE); |
| 246 | 0 | | if (remaining < PACKET_SIZE) |
| 247 | | | { |
| 248 | 0 | | packet = new File[remaining]; |
| 249 | | | } |
| 250 | | | else |
| 251 | | | { |
| 252 | 0 | | packet = new File[PACKET_SIZE]; |
| 253 | | | } |
| 254 | 0 | | for (int j = 0; j < packet.length; j++) |
| 255 | | | { |
| 256 | 0 | | int index = (i * PACKET_SIZE) + j; |
| 257 | 0 | | if (index < dotFiles.length) |
| 258 | | | { |
| 259 | 0 | | packet[j] = dotFiles[index]; |
| 260 | | | } |
| 261 | | | } |
| 262 | 0 | | result.add(packet); |
| 263 | | | } |
| 264 | 0 | | return result; |
| 265 | | | } |
| 266 | | | } |