Author: vsiveton
Date: Thu Oct 18 06:55:15 2007
New Revision: 585963

URL: http://svn.apache.org/viewvc?rev=585963&view=rev
Log:
o added GenerateUMLDoc class that implements the (old) java.xml logic and added 
the associated Ant task
o added some ant-trax dependencies
o added test cases

Added:
    
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java
   (with props)
    
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java
   (with props)
    
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/test/java/org/apache/maven/jxr/java/doc/UmlDocTaskTest.java
   (with props)
Modified:
    maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/pom.xml

Modified: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/pom.xml?rev=585963&r1=585962&r2=585963&view=diff
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/pom.xml (original)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/pom.xml Thu Oct 18 
06:55:15 2007
@@ -50,6 +50,12 @@
       <artifactId>ant</artifactId>
       <version>1.6.5</version>
     </dependency>
+    <!-- for XSLT task -->
+    <dependency>
+      <groupId>ant</groupId>
+      <artifactId>ant-trax</artifactId>
+      <version>1.6.5</version>
+    </dependency>
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
@@ -59,6 +65,11 @@
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
       <version>1.4.6</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.jxr</groupId>
+      <artifactId>maven-jxr-utils</artifactId>
+      <version>3.0-SNAPSHOT</version>
     </dependency>
 
     <dependency>

Added: 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java?rev=585963&view=auto
==============================================================================
--- 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java
 (added)
+++ 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java
 Thu Oct 18 06:55:15 2007
@@ -0,0 +1,303 @@
+package org.apache.maven.jxr.java.doc;
+
+/*
+ * 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 java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.maven.jxr.util.DotTask;
+import org.apache.maven.jxr.util.DotTask.DotNotPresentInPathBuildException;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.taskdefs.XSLTProcess;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+
+import com.sun.tools.javadoc.Main;
+
+/**
+ * Generate UML diagram from Java source directory.
+ * <br/>
+ * <b>Note</b>: <a href="http://www.graphviz.org/";>Graphviz</a> program should 
be in the path.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class GenerateUMLDoc
+{
+    /** Source directory */
+    private File srcDir;
+
+    /** Output file of the diagram */
+    private File out;
+
+    /** Temp javadoc xml file */
+    private File javadocXml;
+
+    /** Temp xsl file */
+    private File xml2dot;
+
+    /** Temp generated dot file */
+    private File dot;
+
+    /**
+     * Default constructor.
+     *
+     * @param srcDir not null
+     * @param out not null
+     * @throws IllegalArgumentException if any
+     */
+    public GenerateUMLDoc( File srcDir, File out )
+        throws IllegalArgumentException
+    {
+        if ( srcDir == null )
+        {
+            throw new IllegalArgumentException( "Missing mandatory attribute 
'srcDir'." );
+        }
+        if ( !srcDir.exists() || srcDir.isFile() )
+        {
+            throw new IllegalArgumentException( "Input '" + srcDir + "' not 
found or not a directory." );
+        }
+
+        if ( out == null )
+        {
+            throw new IllegalArgumentException( "Missing mandatory attribute 
'out'." );
+        }
+        if ( out.exists() && out.isDirectory() )
+        {
+            throw new IllegalArgumentException( out + " is a directory." );
+        }
+        if ( !out.exists() && !out.getParentFile().exists() && 
!out.getParentFile().mkdirs() )
+        {
+            throw new IllegalArgumentException( "Cannot create the parent 
directory of " + out );
+        }
+
+        this.srcDir = srcDir;
+        this.out = out;
+    }
+
+    /**
+     * Generate the documentation
+     *
+     * @throws IOException if any
+     * @throws BuildException if any
+     * @throws DotNotPresentInPathBuildException if any
+     */
+    public void generateUML()
+        throws IOException, BuildException, DotNotPresentInPathBuildException
+    {
+        // 1. Generate Javadoc xml
+        generateJavadocXML();
+
+        // 2. Generate dot image
+        generateJavadocDot();
+
+        // 3. Generate UML image
+        generateUmlImage();
+    }
+
+    // ----------------------------------------------------------------------
+    // Private
+    // ----------------------------------------------------------------------
+
+    /**
+     * @return the dest dir
+     */
+    private File getOut()
+    {
+        return this.out;
+    }
+
+    /**
+     * @return the javadoc output xml file
+     */
+    private File getJavadocXml()
+    {
+        if ( this.javadocXml == null )
+        {
+            this.javadocXml = FileUtils.createTempFile( "javadoc", ".xml", 
null );
+            this.javadocXml.deleteOnExit();
+        }
+
+        return this.javadocXml;
+    }
+
+    /**
+     * @return a temp file for dot file.
+     */
+    private File getDot()
+    {
+        if ( this.dot == null )
+        {
+            this.dot = FileUtils.createTempFile( "javadoc", ".dot", null );
+            this.dot.deleteOnExit();
+        }
+
+        return this.dot;
+    }
+
+    /**
+     * @return a minimal Ant project.
+     */
+    private Project getAntProject()
+    {
+        Project antProject = new Project();
+        antProject.setBasedir( new File( "" ).getAbsolutePath() );
+
+        return antProject;
+    }
+
+    /**
+     * @return xsl temp file.
+     * @throws IOException if any
+     */
+    private File getXml2dot()
+        throws IOException
+    {
+        if ( this.xml2dot == null )
+        {
+            this.xml2dot = FileUtils.createTempFile( "xml2dot", ".xsl", null );
+            this.xml2dot.deleteOnExit();
+
+            InputStream is = getClass().getClassLoader().getResourceAsStream(
+                                                                              
GenerateUMLDoc.class.getPackage()
+                                                                               
   .getName().replace( ".", "/" )
+                                                                               
   + "/xml2dot.xsl" );
+            if ( is == null )
+            {
+                throw new IOException( "This resource doesn't exist." );
+            }
+
+            FileOutputStream w = new FileOutputStream( this.xml2dot );
+
+            IOUtil.copy( is, w );
+
+            IOUtil.close( is );
+
+            IOUtil.close( w );
+        }
+
+        return this.xml2dot;
+    }
+
+    /**
+     * Call javadoc tool with the XMLDoclet
+     *
+     * @throws IOException if Javadoc error
+     */
+    private void generateJavadocXML()
+        throws IOException
+    {
+        final String defaultExcludes = 
"**/*~,**/#*#,**/.#*,**/%*%,**/._*,**/CVS,**/CVS/**,"
+            + 
"**/.cvsignore,**/SCCS,**/SCCS/**,**/vssver.scc,**/.svn,**/.svn/**,**/.DS_Store";
+
+        List args = new LinkedList();
+        args.add( "-package" );
+        args.add( "-sourcepath" );
+        args.add( srcDir.getAbsolutePath() );
+        args.add( "-o" );
+        args.add( getJavadocXml().getAbsolutePath() );
+        List packages = FileUtils.getDirectoryNames( srcDir, null, 
defaultExcludes, false );
+        for ( Iterator it = packages.iterator(); it.hasNext(); )
+        {
+            String p = (String) it.next();
+
+            if ( StringUtils.isEmpty( p ) )
+            {
+                continue;
+            }
+
+            if ( FileUtils.getFileNames( new File( srcDir, p ), "*.java", "", 
false ).isEmpty() )
+            {
+                continue;
+            }
+
+            args.add( StringUtils.replace( p, File.separator, "." ) );
+        }
+
+        StringWriter err = new StringWriter();
+        StringWriter warn = new StringWriter();
+        StringWriter notice = new StringWriter();
+        int exit = Main.execute( "javadoc", new PrintWriter( err ), new 
PrintWriter( warn ), new PrintWriter( notice ),
+                                 XMLDoclet.class.getName(), (String[]) 
args.toArray( new String[0] ) );
+
+        if ( exit != 0 )
+        {
+            throw new IOException( "Error when calling Javadoc: " + err );
+        }
+    }
+
+    /**
+     * Apply XSLT to generate dot file from the Javadoc xml
+     *
+     * @throws BuildException if any
+     * @throws IOException if any
+     */
+    private void generateJavadocDot()
+        throws BuildException, IOException
+    {
+        XSLTProcess xsltTask = new XSLTProcess();
+        xsltTask.setProject( getAntProject() );
+        xsltTask.setTaskName( "xslt" );
+        xsltTask.init();
+        xsltTask.setIn( getJavadocXml() );
+        xsltTask.setOut( getDot() );
+        xsltTask.setStyle( getXml2dot().getAbsolutePath() );
+        xsltTask.execute();
+    }
+
+    /**
+     * Call Graphviz dot to generate images.
+     *
+     * @throws BuildException if any
+     * @throws DotNotPresentInPathBuildException if any
+     */
+    private void generateUmlImage()
+        throws BuildException, DotNotPresentInPathBuildException
+    {
+        String outputPath = getOut().getAbsolutePath();
+        String format;
+        if ( outputPath.lastIndexOf( "." ) != -1 )
+        {
+            format = outputPath.substring( outputPath.lastIndexOf( "." ) + 1 );
+        }
+        else
+        {
+            format = "png";
+        }
+
+        DotTask dotTask = new DotTask();
+        dotTask.setProject( getAntProject() );
+        dotTask.setTaskName( "dot" );
+        dotTask.init();
+        dotTask.setIn( getDot() );
+        dotTask.setOut( getOut() );
+        dotTask.setFormat( format );
+        dotTask.execute();
+    }
+}

Propchange: 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Added: 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java?rev=585963&view=auto
==============================================================================
--- 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java
 (added)
+++ 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java
 Thu Oct 18 06:55:15 2007
@@ -0,0 +1,161 @@
+package org.apache.maven.jxr.java.doc;
+
+/*
+ * 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 java.io.IOException;
+
+import org.apache.maven.jxr.util.DotTask.DotNotPresentInPathBuildException;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+
+/**
+ * <a href="http://ant.apache.org/";>Ant</a> task to generate UML diagram.
+ * <br/>
+ * <b>Note</b>: <a href="http://www.graphviz.org/";>Graphviz</a> program should 
be in the path.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class UmlDocTask
+    extends Task
+{
+    /** Java source directory */
+    private File srcDir;
+
+    /** Output file of the diagram*/
+    private File out;
+
+    /** Terminate Ant build */
+    private boolean failOnError;
+
+    /**
+     * Set the Java source directory.
+     *
+     * @param d Path to the directory.
+     */
+    public void setSrcDir( File d )
+    {
+        this.srcDir = d;
+    }
+
+    /**
+     * Set the destination file.
+     *
+     * @param f Path to the generated UML file.
+     */
+    public void setOut( File f )
+    {
+        this.out = f;
+    }
+
+    /**
+     * Set fail on an error.
+     *
+     * @param b true to fail on an error.
+     */
+    public void setFailonerror( boolean b )
+    {
+        this.failOnError = b;
+    }
+
+    /** [EMAIL PROTECTED] */
+    public void init()
+        throws BuildException
+    {
+        super.init();
+    }
+
+    /** [EMAIL PROTECTED] */
+    public String getTaskName()
+    {
+        return "umldoc";
+    }
+
+    /** [EMAIL PROTECTED] */
+    public String getDescription()
+    {
+        return "Generate UML documentation";
+    }
+
+    /** [EMAIL PROTECTED] */
+    public void execute()
+        throws BuildException
+    {
+        try
+        {
+            GenerateUMLDoc generator = new GenerateUMLDoc( getSrcDir(), 
getOut() );
+            generator.generateUML();
+        }
+        catch ( IllegalArgumentException e )
+        {
+            if ( !failOnError )
+            {
+                throw new BuildException( "IllegalArgumentException: " + 
e.getMessage(), e, getLocation() );
+            }
+
+            log( "IllegalArgumentException: " + e.getMessage(), 
Project.MSG_ERR );
+        }
+        catch ( IOException e )
+        {
+            if ( !failOnError )
+            {
+                throw new BuildException( "IOException: " + e.getMessage(), e, 
getLocation() );
+            }
+
+            log( "IOException: " + e.getMessage(), Project.MSG_ERR );
+        }
+        catch ( DotNotPresentInPathBuildException e )
+        {
+            log( "Dot is not present in the path: " + e.getMessage(), 
Project.MSG_ERR );
+        }
+        catch ( BuildException e )
+        {
+            e.printStackTrace();
+            if ( !failOnError )
+            {
+                throw new BuildException( "RuntimeException: " + 
e.getMessage(), e, getLocation() );
+            }
+
+            log( e.getMessage(), Project.MSG_ERR );
+        }
+    }
+
+    // ----------------------------------------------------------------------
+    // Private
+    // ----------------------------------------------------------------------
+
+    /**
+     * @return the source dir.
+     */
+    private File getSrcDir()
+    {
+        return this.srcDir;
+    }
+
+    /**
+     * @return the output file.
+     */
+    private File getOut()
+    {
+        return this.out;
+    }
+}

Propchange: 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Added: 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/test/java/org/apache/maven/jxr/java/doc/UmlDocTaskTest.java
URL: 
http://svn.apache.org/viewvc/maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/test/java/org/apache/maven/jxr/java/doc/UmlDocTaskTest.java?rev=585963&view=auto
==============================================================================
--- 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/test/java/org/apache/maven/jxr/java/doc/UmlDocTaskTest.java
 (added)
+++ 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/test/java/org/apache/maven/jxr/java/doc/UmlDocTaskTest.java
 Thu Oct 18 06:55:15 2007
@@ -0,0 +1,61 @@
+package org.apache.maven.jxr.java.doc;
+
+/*
+ * 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.apache.tools.ant.Project;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class UmlDocTaskTest
+    extends TestCase
+{
+    /**
+     * Call UMLdoc task
+     *
+     * @throws Exception if any.
+     */
+    public void testDefaultExecute()
+        throws Exception
+    {
+        final String basedir = new File( "" ).getAbsolutePath();
+
+        File out = new File( basedir, "target/unit/umldoc-default/uml.svg" );
+        File srcDir = new File( basedir, "src/test/resources/javasrc" );
+
+        Project antProject = new Project();
+        antProject.setBasedir( basedir );
+
+        UmlDocTask task = new UmlDocTask();
+        task.setProject( antProject );
+        task.setSrcDir( srcDir );
+        task.setOut( out );
+        task.execute();
+
+        // Generated files
+        assertTrue( out.exists() );
+        assertTrue( out.length() > 0 );
+    }
+}

Propchange: 
maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/test/java/org/apache/maven/jxr/java/doc/UmlDocTaskTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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


Reply via email to