| 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.commons.taskdefs; |
|---|
| 34 | |
|---|
| 35 | import java.io.File; |
|---|
| 36 | import java.io.IOException; |
|---|
| 37 | import java.util.Locale; |
|---|
| 38 | |
|---|
| 39 | import org.apache.tools.ant.BuildException; |
|---|
| 40 | import org.apache.tools.ant.Project; |
|---|
| 41 | import org.apache.tools.ant.Task; |
|---|
| 42 | import org.apache.tools.ant.taskdefs.Execute; |
|---|
| 43 | import org.apache.tools.ant.taskdefs.LogStreamHandler; |
|---|
| 44 | import org.apache.tools.ant.types.Commandline; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Ant task for the 'dot' command from the graphviz package. |
|---|
| 48 | * |
|---|
| 49 | * @author Michael Griffel |
|---|
| 50 | */ |
|---|
| 51 | public class DotTask |
|---|
| 52 | extends Task |
|---|
| 53 | { |
|---|
| 54 | private static final String DEFAULT_OUTPUT_FORMAT = "svg"; |
|---|
| 55 | /** The input files. */ |
|---|
| 56 | private File[] mInFiles; |
|---|
| 57 | /** The output format. */ |
|---|
| 58 | private String mFormat = DEFAULT_OUTPUT_FORMAT; |
|---|
| 59 | /** terminate ant build on error. */ |
|---|
| 60 | private boolean mFailOnError; |
|---|
| 61 | private final Commandline mCommand = new Commandline(); |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Sets the XML input file that contains the document. |
|---|
| 65 | * @param f the XML input file (log message info). |
|---|
| 66 | */ |
|---|
| 67 | public void setIn (File f) |
|---|
| 68 | { |
|---|
| 69 | mInFiles = new File[] {f}; |
|---|
| 70 | |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Set the output format. |
|---|
| 75 | * @param format the output format. |
|---|
| 76 | */ |
|---|
| 77 | public void setFormat (String format) |
|---|
| 78 | { |
|---|
| 79 | mFormat = format; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Set whether we should fail on an error. |
|---|
| 84 | * @param b Whether we should fail on an error. |
|---|
| 85 | */ |
|---|
| 86 | public void setFailonerror (boolean b) |
|---|
| 87 | { |
|---|
| 88 | mFailOnError = b; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Returns the file name extension for the current output format. |
|---|
| 93 | * @return the file name extension for the current output format. |
|---|
| 94 | */ |
|---|
| 95 | public String getFileExtension () |
|---|
| 96 | { |
|---|
| 97 | return mFormat.toLowerCase(Locale.US); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Execute this task. |
|---|
| 102 | * |
|---|
| 103 | * @throws BuildException An building exception occurred. |
|---|
| 104 | */ |
|---|
| 105 | public void execute () |
|---|
| 106 | throws BuildException |
|---|
| 107 | { |
|---|
| 108 | try |
|---|
| 109 | { |
|---|
| 110 | checkAttributes(); |
|---|
| 111 | |
|---|
| 112 | mCommand.setExecutable("dot"); |
|---|
| 113 | mCommand.createArgument().setValue("-Gfontnames=svg"); |
|---|
| 114 | mCommand.createArgument().setValue("-Gcharset=UTF-8"); |
|---|
| 115 | mCommand.createArgument().setValue("-T" + mFormat); |
|---|
| 116 | mCommand.createArgument().setValue("-O"); |
|---|
| 117 | |
|---|
| 118 | for (int i = 0; i < mInFiles.length; i++) |
|---|
| 119 | { |
|---|
| 120 | mCommand.createArgument().setValue(mInFiles[i].getAbsolutePath()); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | final Execute exe = new Execute(new LogStreamHandler( |
|---|
| 124 | this, Project.MSG_VERBOSE, Project.MSG_WARN), null); |
|---|
| 125 | exe.setCommandline(mCommand.getCommandline()); |
|---|
| 126 | log(mCommand.describeCommand(), Project.MSG_VERBOSE); |
|---|
| 127 | try |
|---|
| 128 | { |
|---|
| 129 | exe.execute(); |
|---|
| 130 | } |
|---|
| 131 | catch (IOException e) |
|---|
| 132 | { |
|---|
| 133 | throw new BuildException(e, getLocation()); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | catch (BuildException e) |
|---|
| 137 | { |
|---|
| 138 | if (mFailOnError) |
|---|
| 139 | { |
|---|
| 140 | throw e; |
|---|
| 141 | } |
|---|
| 142 | log(e.getMessage(), Project.MSG_ERR); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Checks the attributes provided by this class. |
|---|
| 148 | * @throws BuildException |
|---|
| 149 | */ |
|---|
| 150 | private void checkAttributes () |
|---|
| 151 | throws BuildException |
|---|
| 152 | { |
|---|
| 153 | checkAttributeInFile(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | private void checkAttributeInFile () |
|---|
| 157 | { |
|---|
| 158 | if (mInFiles == null) |
|---|
| 159 | { |
|---|
| 160 | throw new BuildException( |
|---|
| 161 | "Missing mandatory attribute 'in'.", getLocation()); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | for (int i = 0; i < mInFiles.length; i++) |
|---|
| 165 | { |
|---|
| 166 | if (!mInFiles[i].exists()) |
|---|
| 167 | { |
|---|
| 168 | throw new BuildException( |
|---|
| 169 | "Input file '" + mInFiles[i] + "' not found.", |
|---|
| 170 | getLocation()); |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * Set the input files. |
|---|
| 177 | * |
|---|
| 178 | * @param inFiles the input files. |
|---|
| 179 | */ |
|---|
| 180 | public void setInFiles (File[] inFiles) |
|---|
| 181 | { |
|---|
| 182 | mInFiles = inFiles; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | } |
|---|