Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.AntTaskUtil

LineHitsNoteSource
1  /*
2   * $Id: AntTaskUtil.java 1011 2008-06-16 17:57:36Z amandel $
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.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   * Provides utility functions for Ant task.
49   *
50   * @author Michael Griffel
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 ()
590     {
60          // no instances allowed -- provides only static helper methods.
610     }
62  
63      /**
64       * Ensure the directory exists for a given directory name.
65       *
66       * @param directory the directory name that is required.
67       * @exception BuildException if the directories cannot be created.
68       */
69      public static void ensureDirectory (File directory)
70          throws BuildException
71      {
72100         if (!directory.exists())
73          {
740             if (!directory.mkdirs())
75              {
760                 throw new BuildException("Unable to create directory: "
77                      + directory.getAbsolutePath());
78              }
79          }
80100     }
81  
82      /**
83       * Ensure the directory exists for a given file.
84       *
85       * @param targetFile the file for which the directories are
86       *        required.
87       * @exception BuildException if the directories cannot be created.
88       */
89      public static void ensureDirectoryForFile (File targetFile)
90          throws BuildException
91      {
92100         ensureDirectory(targetFile.getParentFile());
93100     }
94  
95      /**
96       * Strip file extension.
97       *
98       * @param fileWithExtension the file with extension
99       * @return the string
100       */
101      public static String stripFileExtension (String fileWithExtension)
102      {
1030         final int lastIndexOfDot = fileWithExtension.lastIndexOf('.');
104          final String result;
1050         if (lastIndexOfDot != -1)
106          {
1070             result = fileWithExtension.substring(0, lastIndexOfDot);
108          }
109          else
110          {
1110             result = fileWithExtension;
112          }
1130         return result;
114      }
115  
116      /**
117       * Render dot files.
118       *
119       * @param task the task
120       * @param dotDir the dot dir
121       * @param failOnError the fail on error
122       */
123      public static void renderDotFiles (Task task, File dotDir,
124          boolean failOnError)
125      {
1260         final File[] dotFiles = dotDir.listFiles(new FilenameFilter()
1270         {
128              public boolean accept (File dir, String name)
129              {
130                  final boolean result;
1310                 if (name.endsWith(".dot"))
132                  {
1330                     result = true;
134                  }
135                  else
136                  {
1370                     result = false;
138                  }
1390                 return result;
140              }
141          });
1420         if (dotFiles != null)
143          {
1440             dots2svgs(task, failOnError, dotFiles);
145          }
146          else
147          {
1480             task.log("No .dot files found to render", Project.MSG_VERBOSE);
149          }
1500     }
151      
152      /**
153       * Render .gnuplot files.
154       *
155       * @param task the task
156       * @param gnuplotDir the gnuplot dir
157       * @param failOnError the fail on error
158       */
159      public static void renderGnuplotFiles (Task task, File gnuplotDir,
160          boolean failOnError)
161      {
1620         final File[] gnuplotFiles = gnuplotDir.listFiles(new FilenameFilter()
1630         {
164              public boolean accept (File dir, String name)
165              {
166                  final boolean result;
1670                 if (name.endsWith(".gnuplot"))
168                  {
1690                     result = true;
170                  }
171                  else
172                  {
1730                     result = false;
174                  }
1750                 return result;
176              }
177          });
1780         if (gnuplotFiles != null)
179          {
1800             gnuplots2svgs(task, failOnError, gnuplotFiles);
181          }
182          else
183          {
1840             task.log("No .gnuplot files found to render", Project.MSG_VERBOSE);
185          }
1860     }
187  
188      private static void dots2svgs (Task task, boolean failOnError,
189          final File[] dotFiles)
190      {
191          /*
192           * Windows command line size is limited, so we render up to
193           * PACKET_SIZE files at once.
194           */
1950         List dotPackets = createPackets(dotFiles);
1960         for (Iterator packetIter = dotPackets.iterator(); packetIter.hasNext();)
197          {
1980             File[] dotPacket = (File[]) packetIter.next();
1990             final DotTask dot = new DotTask();
2000             dot.setProject(task.getProject());
2010             dot.setTaskName("dot");
2020             dot.setFailonerror(failOnError);
2030             dot.setFormat(FORMAT_SVG);
2040             dot.setInFiles(dotPacket);
2050             dot.execute();
2060         }
207          // Silly Graphviz always appends the new extension.
2080         for (int i = 0; i < dotFiles.length; i++)
209          {
2100             File generatedFile = new File(dotFiles[i].getParentFile(),
211                  dotFiles[i].getName() + "." + FORMAT_SVG);
2120             File targetFile = new File(dotFiles[i].getParentFile(),
213                  stripFileExtension(dotFiles[i].getName()) + "." + FORMAT_SVG);
2140(1)            targetFile.delete();
2150(2)            generatedFile.renameTo(targetFile);
216          }
2170     }
218      
219      private static void gnuplots2svgs (Task task, boolean failOnError,
220          final File[] dotFiles)
221      {
222          /*
223           * Windows command line size is limited, so we render up to
224           * PACKET_SIZE files at once.
225           */
2260         List gnuplotPackets = createPackets(dotFiles);
2270(3)        for (Iterator packetIter = gnuplotPackets.iterator(); packetIter.hasNext();)
228          {
2290             File[] gnuplotPacket = (File[]) packetIter.next();
2300             final GnuplotTask dot = new GnuplotTask();
2310             dot.setProject(task.getProject());
2320             dot.setTaskName("gnuplot");
2330             dot.setFailonerror(failOnError);
2340             dot.setInFiles(gnuplotPacket);
2350             dot.execute();
2360         }
2370     }
238  
239      private static List createPackets (File[] dotFiles)
240      {
2410         List result = new ArrayList();
2420         for (int i = 0; i < (dotFiles.length / PACKET_SIZE) + 1; i++)
243          {
244              final File[] packet;
2450             int remaining = dotFiles.length - (i * PACKET_SIZE);
2460             if (remaining < PACKET_SIZE)
247              {
2480                 packet = new File[remaining];
249              }
250              else
251              {
2520                 packet = new File[PACKET_SIZE];
253              }
2540             for (int j = 0; j < packet.length; j++)
255              {
2560                 int index = (i * PACKET_SIZE) + j;
2570                 if (index < dotFiles.length)
258                  {
2590                     packet[j] = dotFiles[index];
260                  }
261              }
2620             result.add(packet);
263          }
2640         return result;
265      }
266  }

Findings in this File

c (4) Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag 'BuildException'.
w (1) 214 : 0 org.jcoderz.commons.taskdefs.AntTaskUtil.dots2svgs(Task, boolean, File[]) ignores exceptional return value of java.io.File.delete()
w (2) 215 : 0 org.jcoderz.commons.taskdefs.AntTaskUtil.dots2svgs(Task, boolean, File[]) ignores exceptional return value of java.io.File.renameTo(File)
c (3) 227 : 0 Line is longer than 80 characters.