You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2007/11/05 23:56:29 UTC

svn commit: r592176 - in /maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils: ./ src/main/java/org/apache/maven/jxr/util/ src/test/java/org/apache/maven/jxr/util/

Author: vsiveton
Date: Mon Nov  5 14:56:29 2007
New Revision: 592176

URL: http://svn.apache.org/viewvc?rev=592176&view=rev
Log:
o removed ant dependency
o replaced DotTask to DotUtil

Added:
    maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java   (with props)
    maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java   (with props)
Removed:
    maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotTask.java
    maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotTaskTest.java
Modified:
    maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/pom.xml

Modified: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/pom.xml
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/pom.xml?rev=592176&r1=592175&r2=592176&view=diff
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/pom.xml (original)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/pom.xml Mon Nov  5 14:56:29 2007
@@ -33,12 +33,11 @@
 
   <artifactId>maven-jxr-utils</artifactId>
   <name>Utilities classes for source code documentation</name>
-  
+
   <dependencies>
     <dependency>
-      <groupId>ant</groupId>
-      <artifactId>ant</artifactId>
-      <version>1.6.5</version>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
     </dependency>
   </dependencies>
 </project>

Added: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java?rev=592176&view=auto
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java (added)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java Mon Nov  5 14:56:29 2007
@@ -0,0 +1,334 @@
+package org.apache.maven.jxr.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+import org.codehaus.plexus.util.cli.DefaultConsumer;
+
+/**
+ * Utilities methods to play with <a href="http://www.graphviz.org/">Graphviz</a> program.
+ * <br/>
+ * <b>Note:</b> Graphviz <code>dot</code> executable should be in the path for the success of these methods.
+ *
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class DotUtil
+{
+    /** Default output format when calling dot executable */
+    public static final String DEFAULT_OUTPUT_FORMAT = "svg";
+
+    /**
+     * Execute Graphviz dot.
+     *
+     * @param input the input dot file, not null.
+     * @param output the output generated file, could be null.
+     * @throws CommandLineException if any.
+     * @throws IllegalArgumentException if any.
+     * @throws DotNotPresentInPathException if any.
+     */
+    public static void executeDot( File input, File output )
+        throws CommandLineException, DotNotPresentInPathException
+    {
+        if ( output == null )
+        {
+            throw new IllegalArgumentException( "Output parameters could not be null" );
+        }
+        if ( output.exists() && output.isDirectory() )
+        {
+            throw new IllegalArgumentException( "Output file '" + output + "' is a dir." );
+        }
+
+        if ( !output.getParentFile().exists() && !output.getParentFile().mkdirs() )
+        {
+            throw new IllegalArgumentException( "Parent output file '" + output.getParentFile()
+                + "' could not be created." );
+        }
+
+        process( null, input, null, output );
+    }
+
+    /**
+     * Execute Graphviz dot.
+     *
+     * @param input the input dot file, not null.
+     * @param format the wanted format, could be null.
+     * @param output the output generated file, could be null.
+     * @throws CommandLineException if any.
+     * @throws IllegalArgumentException if any.
+     * @throws DotNotPresentInPathException if any.
+     */
+    public static void executeDot( File input, String format, File output )
+        throws CommandLineException, DotNotPresentInPathException
+    {
+        if ( output == null )
+        {
+            throw new IllegalArgumentException( "Output parameters could not be null" );
+        }
+        if ( output.exists() && output.isDirectory() )
+        {
+            throw new IllegalArgumentException( "Output file '" + output + "' is a dir." );
+        }
+
+        if ( !output.getParentFile().exists() && !output.getParentFile().mkdirs() )
+        {
+            throw new IllegalArgumentException( "Parent output file '" + output.getParentFile()
+                + "' could not be created." );
+        }
+
+        process( null, input, format, output );
+    }
+
+    /**
+     * Execute Graphviz dot.
+     *
+     * @param exe the dot executable, could be null.
+     * @param input the input dot file, not null.
+     * @param output the output generated file, could be null.
+     * @throws CommandLineException if any.
+     * @throws IllegalArgumentException if any.
+     * @throws DotNotPresentInPathException if any.
+     */
+    public static void executeDot( File exe, File input, File output )
+        throws CommandLineException, DotNotPresentInPathException
+    {
+        if ( exe == null )
+        {
+            throw new IllegalArgumentException( "Exe parameters could not be null" );
+        }
+        if ( !exe.exists() || !exe.isFile() )
+        {
+            throw new IllegalArgumentException( "Exe file '" + input + "' not found or not a file." );
+        }
+
+        if ( output == null )
+        {
+            throw new IllegalArgumentException( "Output parameters could not be null" );
+        }
+        if ( output.exists() && output.isDirectory() )
+        {
+            throw new IllegalArgumentException( "Output file '" + output + "' is a dir." );
+        }
+
+        if ( !output.getParentFile().exists() && !output.getParentFile().mkdirs() )
+        {
+            throw new IllegalArgumentException( "Parent output file '" + output.getParentFile()
+                + "' could not be created." );
+        }
+
+        process( exe, input, null, output );
+    }
+
+    /**
+     * Execute Graphviz dot.
+     *
+     * @param exe the dot executable, could be null.
+     * @param input the input dot file, not null.
+     * @param format the wanted format, could be null.
+     * @param output the output generated file, could be null.
+     * @throws CommandLineException if any.
+     * @throws IllegalArgumentException if any.
+     * @throws DotNotPresentInPathException if any.
+     */
+    public static void executeDot( File exe, File input, String format, File output )
+        throws CommandLineException, DotNotPresentInPathException
+    {
+        if ( exe == null )
+        {
+            throw new IllegalArgumentException( "Exe parameters could not be null" );
+        }
+        if ( !exe.exists() || !exe.isFile() )
+        {
+            throw new IllegalArgumentException( "Exe file '" + input + "' not found or not a file." );
+        }
+
+        if ( output == null )
+        {
+            throw new IllegalArgumentException( "Output parameters could not be null" );
+        }
+        if ( output.exists() && output.isDirectory() )
+        {
+            throw new IllegalArgumentException( "Output file '" + output + "' is a dir." );
+        }
+
+        if ( !output.getParentFile().exists() && !output.getParentFile().mkdirs() )
+        {
+            throw new IllegalArgumentException( "Parent output file '" + output.getParentFile()
+                + "' could not be created." );
+        }
+
+        process( exe, input, format, output );
+    }
+
+    // ----------------------------------------------------------------------
+    // Private
+    // ----------------------------------------------------------------------
+
+    /**
+     * Execute Graphviz dot.
+     *
+     * @param exe the dot executable, could be null.
+     * @param input the input dot file, not null.
+     * @param format the wanted format, could be null.
+     * @param output the output generated file, could be null.
+     * @throws CommandLineException if any.
+     * @throws IllegalArgumentException if any.
+     * @throws DotNotPresentInPathException if any.
+     */
+    private static void process( File exe, File input, String format, File output )
+        throws CommandLineException, DotNotPresentInPathException
+    {
+        // Input checks
+        if ( input == null )
+        {
+            throw new IllegalArgumentException( "Input parameter is mandatory." );
+        }
+        if ( !input.exists() || !input.isFile() )
+        {
+            throw new IllegalArgumentException( "Input file '" + input + "' not found or not a file." );
+        }
+
+        Commandline cmd = new Commandline();
+        cmd.setWorkingDirectory( input.getParentFile() );
+        if ( exe == null )
+        {
+            // in the path
+            verifyDotInPath();
+            cmd.setExecutable( "dot" );
+        }
+        else
+        {
+            cmd.setExecutable( exe.getAbsolutePath() );
+        }
+
+        if ( StringUtils.isEmpty( format ) )
+        {
+            if ( output.getAbsolutePath().lastIndexOf( "." ) != -1 )
+            {
+                format = output.getAbsolutePath().substring( output.getAbsolutePath().lastIndexOf( "." ) + 1 );
+            }
+            else
+            {
+                format = DEFAULT_OUTPUT_FORMAT;
+            }
+        }
+        format = format.toLowerCase();
+
+        if ( output == null )
+        {
+            output = new File( input.getParentFile(), input.getName() + "." + format );
+        }
+        else
+        {
+            String ouputPath = output.getAbsolutePath();
+            if ( ( ouputPath.lastIndexOf( "." ) != -1 )
+                && ( !format.equals( ouputPath.substring( ouputPath.lastIndexOf( "." ) + 1 ).toLowerCase() ) ) )
+            {
+                output = new File( ouputPath + "." + format );
+            }
+        }
+
+        cmd.createArg().setValue( "-T" + format );
+        cmd.createArg().setValue( "-o" );
+        cmd.createArg().setValue( output.getAbsolutePath() );
+        cmd.createArg().setValue( input.getAbsolutePath() );
+
+        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
+
+        int exitCode = CommandLineUtils.executeCommandLine( cmd, new DefaultConsumer(), err );
+
+        if ( exitCode != 0 )
+        {
+            String cmdLine = CommandLineUtils.toString( cmd.getCommandline() ).replaceAll( "'", "" );
+
+            StringBuffer msg = new StringBuffer( "Exit code: " + exitCode + " - " + err.getOutput() );
+            msg.append( '\n' );
+            msg.append( "Command line was:" + cmdLine );
+            throw new CommandLineException( msg.toString() );
+        }
+    }
+
+    /**
+     * Call <code>dot -V</code> to see if <code>dot</code> executable is in the path.
+     *
+     * @throws CommandLineException if any
+     * @throws DotNotPresentInPathException if any.
+     */
+    private static void verifyDotInPath()
+        throws CommandLineException, DotNotPresentInPathException
+    {
+        Commandline cmd = new Commandline();
+        cmd.setExecutable( "dot" );
+        cmd.createArg().setValue( "-V" );
+
+        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
+
+        int exitCode = CommandLineUtils.executeCommandLine( cmd, new DefaultConsumer(), err );
+
+        if ( exitCode != 0 )
+        {
+            throw new DotNotPresentInPathException( "Dot is not present in the PATH. Install it first" );
+        }
+    }
+
+    /**
+     * Signals that the dot executable is not present in the path.
+     */
+    public static class DotNotPresentInPathException
+        extends RuntimeException
+    {
+        static final long serialVersionUID = 2381457101638341000L;
+
+        /**
+         * Constructs am exception with no descriptive information.
+         */
+        public DotNotPresentInPathException()
+        {
+            super();
+        }
+
+        /**
+         * Constructs an exception with the given descriptive message.
+         *
+         * @param message
+         */
+        public DotNotPresentInPathException( String message )
+        {
+            super( message );
+        }
+
+        /**
+         * Constructs an exception with the given message and exception as
+         * a root cause.
+         *
+         * @param message
+         * @param cause
+         */
+        public DotNotPresentInPathException( String message, Throwable cause )
+        {
+            super( message, cause );
+        }
+    }
+}

Propchange: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/main/java/org/apache/maven/jxr/util/DotUtil.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java?rev=592176&view=auto
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java (added)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java Mon Nov  5 14:56:29 2007
@@ -0,0 +1,84 @@
+package org.apache.maven.jxr.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.jxr.util.DotUtil.DotNotPresentInPathException;
+
+/**
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class DotUtilTest
+    extends TestCase
+{
+    public void testDefaultExecuteDot()
+        throws Exception
+    {
+        final String basedir = new File( "" ).getAbsolutePath();
+
+        File in = new File( basedir, "src/test/resources/dot/target.dot" );
+        File out = new File( basedir, "target/unit/dot-default/output.png" );
+
+        try
+        {
+            DotUtil.executeDot( in, out );
+            assertTrue( "DOT exists in the path", true );
+        }
+        catch ( DotNotPresentInPathException e )
+        {
+            assertTrue( "DOT doesnt exist in the path. Ignored test", true );
+            return;
+        }
+
+        // Generated files
+        assertTrue( out.exists() );
+        assertTrue( out.length() > 0 );
+    }
+
+    public void testFormatExecuteDot()
+        throws Exception
+    {
+        final String basedir = new File( "" ).getAbsolutePath();
+
+        File in = new File( basedir, "src/test/resources/dot/target.dot" );
+        File out = new File( basedir, "target/unit/dot-default/output.png" );
+
+        String format = DotUtil.DEFAULT_OUTPUT_FORMAT;
+        try
+        {
+            DotUtil.executeDot( in, format, out );
+            assertTrue( "DOT exists in the path", true );
+        }
+        catch ( DotNotPresentInPathException e )
+        {
+            assertTrue( "DOT doesnt exist in the path. Ignored test", true );
+            return;
+        }
+
+        // Generated files
+        File generated = new File( out.getParentFile(), "output.png." + DotUtil.DEFAULT_OUTPUT_FORMAT );
+        assertTrue( generated.exists() );
+        assertTrue( generated.length() > 0 );
+    }
+}

Propchange: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-utils/src/test/java/org/apache/maven/jxr/util/DotUtilTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"