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 2008/07/25 14:23:42 UTC

svn commit: r679777 - /maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java

Author: vsiveton
Date: Fri Jul 25 05:23:42 2008
New Revision: 679777

URL: http://svn.apache.org/viewvc?rev=679777&view=rev
Log:
o LicenseReport#canGenerateReport() should take care of file licenses

Modified:
    maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java

Modified: maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java?rev=679777&r1=679776&r2=679777&view=diff
==============================================================================
--- maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java (original)
+++ maven/plugins/trunk/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java Fri Jul 25 05:23:42 2008
@@ -96,7 +96,39 @@
     /** {@inheritDoc} */
     public boolean canGenerateReport()
     {
-        return !offline;
+        if ( !offline )
+        {
+            return true;
+        }
+
+        List licenses = project.getModel().getLicenses();
+        for ( Iterator i = licenses.iterator(); i.hasNext(); )
+        {
+            License license = (License) i.next();
+
+            String url = license.getUrl();
+
+            URL licenseUrl = null;
+            try
+            {
+                licenseUrl = getLicenseURL( project, url );
+            }
+            catch ( MalformedURLException e )
+            {
+                getLog().error( e.getMessage() );
+            }
+            catch ( IOException e )
+            {
+                getLog().error( e.getMessage() );
+            }
+
+            if ( licenseUrl != null && licenseUrl.getProtocol().equals( "file" ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
     }
 
     /** {@inheritDoc} */
@@ -105,6 +137,61 @@
         return "license";
     }
 
+    /**
+     * @param project not null
+     * @param url not null
+     * @return a valid URL object from the url string
+     * @throws MalformedURLException if any
+     * @throws IOException if any
+     */
+    protected static URL getLicenseURL( MavenProject project, String url )
+        throws MalformedURLException, IOException
+    {
+        URL licenseUrl = null;
+        UrlValidator urlValidator = new UrlValidator( UrlValidator.ALLOW_ALL_SCHEMES );
+        // UrlValidator does not accept file URLs because the file
+        // URLs do not contain a valid authority (no hostname).
+        // As a workaround accept license URLs that start with the
+        // file scheme.
+        if ( urlValidator.isValid( url ) || url.startsWith( "file://" ) )
+        {
+            try
+            {
+                licenseUrl = new URL( url );
+            }
+            catch ( MalformedURLException e )
+            {
+                throw new MalformedURLException( "The license url '" + url + "' seems to be invalid: "
+                    + e.getMessage() );
+            }
+        }
+        else
+        {
+            File licenseFile = new File( project.getBasedir(), url );
+            if ( !licenseFile.exists() )
+            {
+                // Workaround to allow absolute path names while
+                // staying compatible with the way it was...
+                licenseFile = new File( url );
+            }
+            if ( !licenseFile.exists() )
+            {
+                throw new IOException( "Maven can't find the file '" + licenseFile + "' on the system." );
+            }
+            try
+            {
+                licenseUrl = licenseFile.toURL();
+            }
+            catch ( MalformedURLException e )
+            {
+                throw new MalformedURLException( "The license url '" + url + "' seems to be invalid: "
+                    + e.getMessage() );
+            }
+        }
+
+        return licenseUrl;
+    }
+
     // ----------------------------------------------------------------------
     // Private
     // ----------------------------------------------------------------------
@@ -186,43 +273,19 @@
                 if ( url != null )
                 {
                     URL licenseUrl = null;
-                    UrlValidator urlValidator = new UrlValidator( UrlValidator.ALLOW_ALL_SCHEMES );
-                    // UrlValidator does not accept file URLs because the file
-                    // URLs do not contain a valid authority (no hostname).
-                    // As a workaround accept license URLs that start with the
-                    // file scheme.
-                    if ( urlValidator.isValid( url ) || url.startsWith( "file://" ) )
+                    try
                     {
-                        try
-                        {
-                            licenseUrl = new URL( url );
-                        }
-                        catch ( MalformedURLException e )
-                        {
-                            paragraph( "The license url [" + url + "] seems to be invalid: " + e.getMessage() );
-                        }
+                        licenseUrl = getLicenseURL( project, url );
                     }
-                    else
+                    catch ( MalformedURLException e )
                     {
-                        File licenseFile = new File( project.getBasedir(), url );
-                        if ( !licenseFile.exists() )
-                        {
-                            // Workaround to allow absolute path names while
-                            // staying compatible with the way it was...
-                            licenseFile = new File( url );
-                        }
-                        if ( !licenseFile.exists() )
-                        {
-                            paragraph( "Maven can't find the file " + licenseFile + " on the system." );
-                        }
-                        try
-                        {
-                            licenseUrl = licenseFile.toURL();
-                        }
-                        catch ( MalformedURLException e )
-                        {
-                            paragraph( "The license url [" + url + "] seems to be invalid: " + e.getMessage() );
-                        }
+                        // I18N message
+                        paragraph( e.getMessage() );
+                    }
+                    catch ( IOException e )
+                    {
+                        // I18N message
+                        paragraph( e.getMessage() );
                     }
 
                     if ( licenseUrl != null )