root/trunk/src/java/org/jcoderz/commons/taskdefs/AntTaskUtil.java

Revision 1011, 8.2 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 */
33package org.jcoderz.commons.taskdefs;
34
35
36import java.io.File;
37import java.io.FilenameFilter;
38import java.util.ArrayList;
39import java.util.Iterator;
40import java.util.List;
41
42import org.apache.tools.ant.BuildException;
43import org.apache.tools.ant.Project;
44import org.apache.tools.ant.Task;
45
46
47/**
48 * Provides utility functions for Ant task.
49 *
50 * @author Michael Griffel
51 */
52public 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    {
60        // no instances allowed -- provides only static helper methods.
61    }
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    {
72        if (!directory.exists())
73        {
74            if (!directory.mkdirs())
75            {
76                throw new BuildException("Unable to create directory: "
77                    + directory.getAbsolutePath());
78            }
79        }
80    }
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    {
92        ensureDirectory(targetFile.getParentFile());
93    }
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    {
103        final int lastIndexOfDot = fileWithExtension.lastIndexOf('.');
104        final String result;
105        if (lastIndexOfDot != -1)
106        {
107            result = fileWithExtension.substring(0, lastIndexOfDot);
108        }
109        else
110        {
111            result = fileWithExtension;
112        }
113        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    {
126        final File[] dotFiles = dotDir.listFiles(new FilenameFilter()
127        {
128            public boolean accept (File dir, String name)
129            {
130                final boolean result;
131                if (name.endsWith(".dot"))
132                {
133                    result = true;
134                }
135                else
136                {
137                    result = false;
138                }
139                return result;
140            }
141        });
142        if (dotFiles != null)
143        {
144            dots2svgs(task, failOnError, dotFiles);
145        }
146        else
147        {
148            task.log("No .dot files found to render", Project.MSG_VERBOSE);
149        }
150    }
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    {
162        final File[] gnuplotFiles = gnuplotDir.listFiles(new FilenameFilter()
163        {
164            public boolean accept (File dir, String name)
165            {
166                final boolean result;
167                if (name.endsWith(".gnuplot"))
168                {
169                    result = true;
170                }
171                else
172                {
173                    result = false;
174                }
175                return result;
176            }
177        });
178        if (gnuplotFiles != null)
179        {
180            gnuplots2svgs(task, failOnError, gnuplotFiles);
181        }
182        else
183        {
184            task.log("No .gnuplot files found to render", Project.MSG_VERBOSE);
185        }
186    }
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         */
195        List dotPackets = createPackets(dotFiles);
196        for (Iterator packetIter = dotPackets.iterator(); packetIter.hasNext();)
197        {
198            File[] dotPacket = (File[]) packetIter.next();
199            final DotTask dot = new DotTask();
200            dot.setProject(task.getProject());
201            dot.setTaskName("dot");
202            dot.setFailonerror(failOnError);
203            dot.setFormat(FORMAT_SVG);
204            dot.setInFiles(dotPacket);
205            dot.execute();
206        }
207        // Silly Graphviz always appends the new extension.
208        for (int i = 0; i < dotFiles.length; i++)
209        {
210            File generatedFile = new File(dotFiles[i].getParentFile(),
211                dotFiles[i].getName() + "." + FORMAT_SVG);
212            File targetFile = new File(dotFiles[i].getParentFile(),
213                stripFileExtension(dotFiles[i].getName()) + "." + FORMAT_SVG);
214            targetFile.delete();
215            generatedFile.renameTo(targetFile);
216        }
217    }
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         */
226        List gnuplotPackets = createPackets(dotFiles);
227        for (Iterator packetIter = gnuplotPackets.iterator(); packetIter.hasNext();)
228        {
229            File[] gnuplotPacket = (File[]) packetIter.next();
230            final GnuplotTask dot = new GnuplotTask();
231            dot.setProject(task.getProject());
232            dot.setTaskName("gnuplot");
233            dot.setFailonerror(failOnError);
234            dot.setInFiles(gnuplotPacket);
235            dot.execute();
236        }
237    }
238
239    private static List createPackets (File[] dotFiles)
240    {
241        List result = new ArrayList();
242        for (int i = 0; i < (dotFiles.length / PACKET_SIZE) + 1; i++)
243        {
244            final File[] packet;
245            int remaining = dotFiles.length - (i * PACKET_SIZE);
246            if (remaining < PACKET_SIZE)
247            {
248                packet = new File[remaining];
249            }
250            else
251            {
252                packet = new File[PACKET_SIZE];
253            }
254            for (int j = 0; j < packet.length; j++)
255            {
256                int index = (i * PACKET_SIZE) + j;
257                if (index < dotFiles.length)
258                {
259                    packet[j] = dotFiles[index];
260                }
261            }
262            result.add(packet);
263        }
264        return result;
265    }
266}
Note: See TracBrowser for help on using the browser.