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/12 14:49:29 UTC

svn commit: r594131 - in /maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc: GenerateUMLDoc.java UmlDoc.java UmlDocTask.java

Author: vsiveton
Date: Mon Nov 12 05:49:23 2007
New Revision: 594131

URL: http://svn.apache.org/viewvc?rev=594131&view=rev
Log:
o using the Plexus way

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

Modified: 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=594131&r1=594130&r2=594131&view=diff
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java (original)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/GenerateUMLDoc.java Mon Nov 12 05:49:23 2007
@@ -19,12 +19,16 @@
  * under the License.
  */
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.util.GregorianCalendar;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -41,6 +45,7 @@
 
 import org.apache.maven.jxr.util.DotUtil;
 import org.apache.maven.jxr.util.DotUtil.DotNotPresentInPathException;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
@@ -56,8 +61,11 @@
  *
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  * @version $Id$
+ * @plexus.component role="org.apache.maven.jxr.java.doc.UmlDoc" role-hint="default"
  */
 public class GenerateUMLDoc
+    extends AbstractLogEnabled
+    implements UmlDoc
 {
     private static final String NOW = new GregorianCalendar( TimeZone.getDefault() ).getTime().toString();
 
@@ -112,70 +120,77 @@
     /** Specify verbose information */
     private boolean verbose;
 
-    /**
-     * Default constructor.
-     *
-     * @param srcDir not null
-     * @param out not null
-     * @throws IllegalArgumentException if any
-     */
-    public GenerateUMLDoc( File srcDir, File out )
-        throws IllegalArgumentException
+    // ----------------------------------------------------------------------
+    // Public
+    // ----------------------------------------------------------------------
+
+    /** {@inheritDoc} */
+    public void generate( File aSrcDir, File theOut )
+        throws IllegalArgumentException, DotNotPresentInPathException, IOException, UmlDocException
     {
-        if ( srcDir == null )
+        generate( aSrcDir, null, theOut, null );
+    }
+
+    /** {@inheritDoc} */
+    public void generate( File aSrcDir, String srcEncoding, File theOut, String outEncoding )
+        throws IllegalArgumentException, DotNotPresentInPathException, IOException, UmlDocException
+    {
+        if ( aSrcDir == null )
         {
             throw new IllegalArgumentException( "Missing mandatory attribute 'srcDir'." );
         }
-        if ( !srcDir.exists() || srcDir.isFile() )
+        if ( !aSrcDir.exists() || aSrcDir.isFile() )
         {
-            throw new IllegalArgumentException( "Input '" + srcDir + "' not found or not a directory." );
+            throw new IOException( "Input '" + aSrcDir + "' not found or not a directory." );
         }
 
-        if ( out == null )
+        if ( theOut == null )
         {
             throw new IllegalArgumentException( "Missing mandatory attribute 'out'." );
         }
-        if ( out.exists() && out.isDirectory() )
+        if ( theOut.exists() && theOut.isDirectory() )
         {
-            throw new IllegalArgumentException( out + " is a directory." );
+            throw new IOException( theOut + " is a directory." );
         }
-        if ( !out.exists() && !out.getParentFile().exists() && !out.getParentFile().mkdirs() )
+        if ( !theOut.exists() && !theOut.getParentFile().exists() && !theOut.getParentFile().mkdirs() )
         {
-            throw new IllegalArgumentException( "Cannot create the parent directory of " + out );
+            throw new IOException( "Cannot create the parent directory of " + theOut );
         }
 
-        this.srcDir = srcDir;
-        this.out = out;
-    }
+        this.srcDir = aSrcDir;
+        this.out = theOut;
 
-    /**
-     * Generate the documentation
-     *
-     * @throws UmlDocException if any
-     * @throws DotNotPresentInPathException if any
-     */
-    public void generateUML()
-        throws UmlDocException, DotNotPresentInPathException
-    {
-        // 1. Generate Javadoc xml
-        try
+        if ( !StringUtils.isEmpty( srcEncoding ) )
         {
-            generateJavadocXML();
+            if ( validateEncoding( srcEncoding ) )
+            {
+                this.encoding = srcEncoding;
+            }
+            else
+            {
+                getLogger().info( "Unsupported Character Encoding for " + srcEncoding + ". IGNORED" );
+            }
         }
-        catch ( IOException e )
+        if ( !StringUtils.isEmpty( outEncoding ) )
         {
-            throw new UmlDocException( "IOException: " + e.getMessage() );
+            if ( validateEncoding( outEncoding ) )
+            {
+                this.diagramEncoding = outEncoding;
+            }
+            else
+            {
+                getLogger().info( "Unsupported Character Encoding for " + outEncoding + ". IGNORED" );
+            }
         }
 
+        // 1. Generate Javadoc xml
+        generateJavadocXML();
+
         // 2. Generate dot image
         try
         {
             generateJavadocDot();
         }
-        catch ( IOException e )
-        {
-            throw new UmlDocException( "IOException: " + e.getMessage() );
-        }
         catch ( TransformerException e )
         {
             throw new UmlDocException( "TransformerException: " + e.getMessage() );
@@ -197,17 +212,76 @@
             File dtd = new File( getJavadocXml().getParentFile(), XMLDoclet.XMLDOCLET_DTD );
             if ( !dtd.delete() )
             {
-                throw new UmlDocException( "IOException: can't delete the generated DTD file: " + dtd );
+                throw new IOException( "Can't delete the generated DTD file: " + dtd );
             }
         }
     }
 
+    /** {@inheritDoc} */
+    public void generate( File graphExecutable, File aSrcDir, String srcEncoding, File theOut, String outEncoding )
+        throws IllegalArgumentException, DotNotPresentInPathException, IOException, UmlDocException
+    {
+        if ( dotExecutable == null )
+        {
+            throw new IllegalArgumentException( "Missing mandatory attribute 'dotExecutable'." );
+        }
+        if ( !dotExecutable.exists() || !dotExecutable.isFile() )
+        {
+            throw new IOException( "Input '" + dotExecutable + "' not found or not a file." );
+        }
+        this.dotExecutable = graphExecutable;
+
+        generate( aSrcDir, srcEncoding, theOut, outEncoding );
+    }
+
+    /** {@inheritDoc} */
+    public void setDiagramLabel( String diagramLabel )
+    {
+        this.diagramLabel = diagramLabel;
+    }
+
+    /** {@inheritDoc} */
+    public void setJavadocPath( String javadocPath )
+    {
+        this.javadocPath = javadocPath;
+    }
+
+    /** {@inheritDoc} */
+    public void setJavasrcPath( String javasrcPath )
+    {
+        this.javasrcPath = javasrcPath;
+    }
+
+    /** {@inheritDoc} */
+    public void setShow( String show )
+    {
+        this.show = show;
+    }
+
+    // ----------------------------------------------------------------------
+    // Protected
+    // ----------------------------------------------------------------------
+
+    /**
+     * Setter for the verbose. Used by Ant task
+     *
+     * @param verbose the verbose to set
+     */
+    protected void setVerbose( boolean verbose )
+    {
+        this.verbose = verbose;
+    }
+
+    // ----------------------------------------------------------------------
+    // Private
+    // ----------------------------------------------------------------------
+
     /**
      * Getter for the diagramEncoding
      *
      * @return the diagramEncoding
      */
-    public String getDiagramEncoding()
+    private String getDiagramEncoding()
     {
         return this.diagramEncoding;
     }
@@ -217,7 +291,7 @@
      *
      * @return the diagramLabel
      */
-    public String getDiagramLabel()
+    private String getDiagramLabel()
     {
         return this.diagramLabel;
     }
@@ -227,7 +301,7 @@
      *
      * @return the dotExecutable
      */
-    public File getDotExecutable()
+    private File getDotExecutable()
     {
         return this.dotExecutable;
     }
@@ -237,7 +311,7 @@
      *
      * @return the encoding
      */
-    public String getEncoding()
+    private String getEncoding()
     {
         return this.encoding;
     }
@@ -247,7 +321,7 @@
      *
      * @return the relative path or URI to the generated javadoc directory
      */
-    public String getJavadocPath()
+    private String getJavadocPath()
     {
         return this.javadocPath;
     }
@@ -257,7 +331,7 @@
      *
      * @return the relative path or URI to the generated javasrc directory
      */
-    public String getJavasrcPath()
+    private String getJavasrcPath()
     {
         return this.javasrcPath;
     }
@@ -267,7 +341,7 @@
      *
      * @return the destDir
      */
-    public File getOut()
+    private File getOut()
     {
         return this.out;
     }
@@ -284,143 +358,27 @@
      *
      * @return the show
      */
-    public String getShow()
+    private String getShow()
     {
         return this.show;
     }
 
     /**
-     * Getter for the srcDir
-     *
-     * @return the srcDir
-     */
-    public File getSrcDir()
-    {
-        return this.srcDir;
-    }
-
-    /**
      * Getter for the verbose
      *
      * @return the verbose
      */
-    public boolean isVerbose()
-    {
-        return this.verbose;
-    }
-
-    /**
-     * Setter for the diagramEncoding
-     *
-     * @param diagramEncoding the diagramEncoding to set
-     */
-    public void setDiagramEncoding( String diagramEncoding )
-    {
-        this.diagramEncoding = diagramEncoding;
-    }
-
-    /**
-     * Setter for the diagramLabel
-     *
-     * @param diagramLabel the diagramLabel to set
-     */
-    public void setDiagramLabel( String diagramLabel )
+    private boolean isVerbose()
     {
-        this.diagramLabel = diagramLabel;
-    }
-
-    /**
-     * Setter for the dotExecutable
-     *
-     * @param dotExecutable the dotExecutable to set
-     */
-    public void setDotExecutable( File dotExecutable )
-    {
-        this.dotExecutable = dotExecutable;
-    }
-
-    /**
-     * Setter for the encoding
-     *
-     * @param encoding the encoding to set
-     */
-    public void setEncoding( String encoding )
-    {
-        this.encoding = encoding;
-    }
-
-    /**
-     * Setter for the javadocPath
-     *
-     * @param javadocPath the relative path or URI to the generated javadoc directory
-     */
-    public void setJavadocPath( String javadocPath )
-    {
-        this.javadocPath = javadocPath;
-    }
-
-    /**
-     * Setter for the javasrcPath
-     *
-     * @param javasrcPath the relative path or URI to the generated javasrc directory
-     */
-    public void setJavasrcPath( String javasrcPath )
-    {
-        this.javasrcPath = javasrcPath;
-    }
-
-    /**
-     * Setter for the destDir
-     *
-     * @param destDir the destDir to set
-     */
-    public void setOut( File destDir )
-    {
-        this.out = destDir;
-    }
-
-    /**
-     * Setter for the show.  Possible values are:
-     * <ul>
-     * <li>public: shows only public classes and members</li>
-     * <li>protected: shows only public and protected classes and members</li>
-     * <li>package: shows all classes and members not marked private</li>
-     * <li>private: shows all classes and members</li>
-     * <li>"" (i.e. empty): nothing</li>
-     * </ul>
-     *
-     * @param show the show to set
-     */
-    public void setShow( String show )
-    {
-        this.show = show;
-    }
+        if ( getLogger() != null ) // for Ant tasks
+        {
+            return !getLogger().isInfoEnabled();
+        }
 
-    /**
-     * Setter for the srcDir
-     *
-     * @param srcDir the srcDir to set
-     */
-    public void setSrcDir( File srcDir )
-    {
-        this.srcDir = srcDir;
+        return this.verbose;
     }
 
     /**
-     * Setter for the verbose
-     *
-     * @param verbose the verbose to set
-     */
-    public void setVerbose( boolean verbose )
-    {
-        this.verbose = verbose;
-    }
-
-    // ----------------------------------------------------------------------
-    // Private
-    // ----------------------------------------------------------------------
-
-    /**
      * @return the javadoc output xml file
      */
     private File getJavadocXml()
@@ -631,5 +589,44 @@
         {
             DotUtil.executeDot( getDotExecutable(), getDot(), getOut() );
         }
+    }
+
+    /**
+     * Validate if a charset is supported on this platform.
+     *
+     * @param charsetName the charsetName to be check.
+     */
+    private static boolean validateEncoding( String charsetName )
+    {
+        if ( StringUtils.isEmpty( charsetName ) )
+        {
+            return false;
+        }
+
+        OutputStream ost = new ByteArrayOutputStream();
+        OutputStreamWriter osw = null;
+        try
+        {
+            osw = new OutputStreamWriter( ost, charsetName );
+        }
+        catch ( UnsupportedEncodingException exc )
+        {
+            return false;
+        }
+        finally
+        {
+            try
+            {
+                if ( osw != null )
+                {
+                    osw.close();
+                }
+            }
+            catch ( IOException exc )
+            {
+                //nop
+            }
+        }
+        return true;
     }
 }

Added: maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDoc.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/UmlDoc.java?rev=594131&view=auto
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDoc.java (added)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDoc.java Mon Nov 12 05:49:23 2007
@@ -0,0 +1,122 @@
+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;
+
+/**
+ * Interface to generate <a href="http://www.uml.org/>UML</a> class diagram.
+ *
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public interface UmlDoc
+{
+    /** The Plexus lookup role. */
+    String ROLE = UmlDoc.class.getName();
+
+    /**
+     * Generate the UML class diagram as image.
+     * <br/>
+     * The image format is based on the out extension, or <a href="http://www.w3.org/Graphics/SVG/">SVG</a> if none.
+     *
+     * @param srcDir the source directory, should be not null
+     * @param srcEncoding the source encoding, could be null
+     * @param out the generated class diagram file, should be not null
+     * @param outEncoding the output encoding, could be null
+     * @throws IOException if any
+     * @throws UmlDocException if any
+     */
+    void generate( File srcDir, File out )
+        throws IOException, UmlDocException;
+
+    /**
+     * Generate the UML class diagram as image.
+     * <br/>
+     * The image format is based on the out extension, or <a href="http://www.w3.org/Graphics/SVG/">SVG</a> if none.
+     *
+     * @param srcDir the source directory, should be not null
+     * @param srcEncoding the source encoding, could be null
+     * @param out the generated class diagram file, should be not null
+     * @param outEncoding the output encoding, could be null
+     * @throws IOException if any
+     * @throws UmlDocException if any
+     */
+    void generate( File srcDir, String srcEncoding, File out, String outEncoding )
+        throws IOException, UmlDocException;
+
+    /**
+     * Generate the UML class diagram as image.
+     * <br/>
+     * The image format is based on the out extension, or <a href="http://www.w3.org/Graphics/SVG/">SVG</a> if none.
+     *
+     * @param graphExecutable the graph executable to use, for instance <a href="http://www.graphviz.org/">Graphviz</a>, should be not null.
+     * @param srcDir the source directory, should be not null
+     * @param srcEncoding the source encoding, could be null
+     * @param out the generated class diagram file, should be not null
+     * @param outEncoding the output encoding, could be null
+     * @throws IOException if any
+     * @throws UmlDocException if any
+     */
+    void generate( File graphExecutable, File srcDir, String srcEncoding, File out, String outEncoding )
+        throws IOException, UmlDocException;
+
+    /**
+     * Setter for the UML class diagram label
+     *
+     * @param diagramLabel the UML class diagram label to set
+     */
+    public void setDiagramLabel( String diagramLabel );
+
+    /**
+     * Setter for the javadoc relative path.
+     * <br/>
+     * Will be used only if the format of the generated UML class image supports links, like
+     * <a href="http://www.w3.org/Graphics/SVG/">SVG</a>.
+     *
+     * @param javadocPath the relative path or URI to the generated javadoc directory
+     */
+    public void setJavadocPath( String javadocPath );
+
+    /**
+     * Setter for the java source cross relative path.
+     * <br/>
+     * Will be used only if the format of the generated UML class image supports links, like
+     * <a href="http://www.w3.org/Graphics/SVG/">SVG</a>.
+     *
+     * @param javasrcPath the relative path or URI to the generated java source cross directory
+     */
+    public void setJavasrcPath( String javasrcPath );
+
+    /**
+     * Specify the visibility members to include in the UML class diagram. Possible values are:
+     * <ul>
+     * <li>public: shows only public classes and members</li>
+     * <li>protected: shows only public and protected classes and members</li>
+     * <li>package: shows all classes and members not marked private</li>
+     * <li>private: shows all classes and members</li>
+     * <li>"" (i.e. empty): nothing</li>
+     * </ul>
+     *
+     * @param show the show to set
+     */
+    public void setShow( String show );
+}

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

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

Modified: 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=594131&r1=594130&r2=594131&view=diff
==============================================================================
--- maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java (original)
+++ maven/sandbox/trunk/jxr/maven-jxr/maven-jxr-java/src/main/java/org/apache/maven/jxr/java/doc/UmlDocTask.java Mon Nov 12 05:49:23 2007
@@ -20,6 +20,7 @@
  */
 
 import java.io.File;
+import java.io.IOException;
 
 import org.apache.maven.jxr.util.DotUtil.DotNotPresentInPathException;
 import org.apache.tools.ant.BuildException;
@@ -225,15 +226,7 @@
     {
         try
         {
-            GenerateUMLDoc generator = new GenerateUMLDoc( getSrcDir(), getOut() );
-            if ( this.dotExecutable != null )
-            {
-                generator.setDotExecutable( dotExecutable );
-            }
-            if ( StringUtils.isNotEmpty( this.encoding ) )
-            {
-                generator.setEncoding( this.encoding );
-            }
+            GenerateUMLDoc generator = new GenerateUMLDoc();
             generator.setVerbose( this.verbose );
             if ( this.show != null )
             {
@@ -247,15 +240,18 @@
             {
                 generator.setJavasrcPath( this.javasrcPath );
             }
-            if ( StringUtils.isNotEmpty( this.diagramEncoding ) )
-            {
-                generator.setDiagramEncoding( this.diagramEncoding );
-            }
             if ( StringUtils.isNotEmpty( this.diagramLabel ) )
             {
                 generator.setDiagramLabel( this.diagramLabel );
             }
-            generator.generateUML();
+            if ( this.dotExecutable != null )
+            {
+                generator.generate( this.dotExecutable, getSrcDir(), this.encoding, getOut(), this.diagramEncoding );
+            }
+            else
+            {
+                generator.generate( getSrcDir(), this.encoding, getOut(), this.diagramEncoding );
+            }
         }
         catch ( IllegalArgumentException e )
         {
@@ -274,6 +270,15 @@
             }
 
             log( "Dot is not present in the path: " + 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 ( UmlDocException e )
         {