You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by br...@apache.org on 2005/10/03 06:58:22 UTC

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

Author: brett
Date: Sun Oct  2 21:58:16 2005
New Revision: 293240

URL: http://svn.apache.org/viewcvs?rev=293240&view=rev
Log:
PR: MNG-903
Submitted by: Matthew Pocock
Reviewed by: Brett Porter
correctly include HTML licenses in the page

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

Modified: maven/components/trunk/maven-plugins/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugins/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java?rev=293240&r1=293239&r2=293240&view=diff
==============================================================================
--- maven/components/trunk/maven-plugins/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java (original)
+++ maven/components/trunk/maven-plugins/maven-project-info-reports-plugin/src/main/java/org/apache/maven/report/projectinfo/LicenseReport.java Sun Oct  2 21:58:16 2005
@@ -37,21 +37,22 @@
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Generates the Project License report.
- * 
- * @goal license
- * 
+ *
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
  * @version $Id$
+ * @goal license
  */
 public class LicenseReport
     extends AbstractMavenReport
 {
     /**
      * Report output directory.
-     * 
+     *
      * @parameter expression="${project.build.directory}/site"
      * @required
      */
@@ -59,7 +60,7 @@
 
     /**
      * Doxia Site Renderer.
-     * 
+     *
      * @parameter expression="${component.org.codehaus.doxia.site.renderer.SiteRenderer}"
      * @required
      * @readonly
@@ -68,7 +69,7 @@
 
     /**
      * The Maven Project.
-     * 
+     *
      * @parameter expression="${project}"
      * @required
      * @readonly
@@ -215,8 +216,8 @@
                     }
                     catch ( MalformedURLException e )
                     {
-                        throw new MissingResourceException( "The license url [" + url + "] seems to be invalid: "
-                            + e.getMessage(), null, null );
+                        throw new MissingResourceException(
+                            "The license url [" + url + "] seems to be invalid: " + e.getMessage(), null, null );
                     }
                 }
                 else
@@ -224,8 +225,8 @@
                     File licenseFile = new File( project.getBasedir(), url );
                     if ( !licenseFile.exists() )
                     {
-                        throw new MissingResourceException( "Maven can't find the file " + licenseFile
-                            + " on the system.", null, null );
+                        throw new MissingResourceException(
+                            "Maven can't find the file " + licenseFile + " on the system.", null, null );
                     }
                     try
                     {
@@ -233,8 +234,8 @@
                     }
                     catch ( MalformedURLException e )
                     {
-                        throw new MissingResourceException( "The license url [" + url + "] seems to be invalid: "
-                            + e.getMessage(), null, null );
+                        throw new MissingResourceException(
+                            "The license url [" + url + "] seems to be invalid: " + e.getMessage(), null, null );
                     }
                 }
 
@@ -262,8 +263,26 @@
                     paragraph( comments );
                 }
 
-                verbatimText( licenseContent );
+                // TODO: we should check for a text/html mime type instead, and possibly use a html parser to do this a bit more cleanly/reliably.
+                String licenseContentLC = licenseContent.toLowerCase();
+                int bodyStart = licenseContentLC.indexOf( "<body" );
+                int bodyEnd = licenseContentLC.indexOf( "</body>" );
+                if ( ( licenseContentLC.startsWith( "<!doctype html" ) || licenseContentLC.startsWith( "<html>" ) ) &&
+                    bodyStart >= 0 && bodyEnd >= 0 )
+                {
+                    bodyStart = licenseContentLC.indexOf( ">", bodyStart ) + 1;
+                    String body = licenseContent.substring( bodyStart, bodyEnd );
+
+                    link( "[Original text]", licenseUrl.toExternalForm() );
+                    paragraph( "Copy of the license follows." );
 
+                    body = replaceRelativeLinks( body, baseURL( licenseUrl ).toExternalForm() );
+                    sink.rawText( body );
+                }
+                else
+                {
+                    verbatimText( licenseContent );
+                }
                 endSection();
             }
 
@@ -274,5 +293,82 @@
     private static ResourceBundle getBundle( Locale locale )
     {
         return ResourceBundle.getBundle( "project-info-report", locale, LicenseReport.class.getClassLoader() );
+    }
+
+    private static URL baseURL( URL aUrl )
+    {
+        String urlTxt = aUrl.toExternalForm();
+        int lastSlash = urlTxt.lastIndexOf( '/' );
+        if ( lastSlash > -1 )
+        {
+            try
+            {
+                return new URL( urlTxt.substring( 0, lastSlash + 1 ) );
+            }
+            catch ( MalformedURLException e )
+            {
+                throw new AssertionError( e );
+            }
+        }
+        else
+        {
+            return aUrl;
+        }
+    }
+
+    private static String replaceRelativeLinks( String html, String baseURL )
+    {
+        if ( !baseURL.endsWith( "/" ) )
+        {
+            baseURL += "/";
+        }
+
+        String serverURL = baseURL.substring( 0, baseURL.indexOf( '/', baseURL.indexOf( "//" ) + 2 ) );
+
+        html = replaceParts( html, baseURL, serverURL, "[aA]", "[hH][rR][eE][fF]" );
+        html = replaceParts( html, baseURL, serverURL, "[iI][mM][gG]", "[sS][rR][cC]" );
+        return html;
+    }
+
+    private static String replaceParts( String html, String baseURL, String serverURL, String tagPattern,
+                                        String attributePattern )
+    {
+        Pattern anchor = Pattern
+            .compile( "(<\\s*" + tagPattern + "\\s+[^>]*" + attributePattern + "\\s*=\\s*\")([^\"]*)\"([^>]*>)" );
+        StringBuilder sb = new StringBuilder( html );
+
+        int indx = 0;
+        do
+        {
+            Matcher mAnchor = anchor.matcher( sb );
+            mAnchor.region( indx, sb.length() );
+            if ( !mAnchor.find() )
+            {
+                System.err.println( "No more matches" );
+                break; // no more matches
+            }
+
+            indx = mAnchor.end( 3 );
+
+            if ( mAnchor.group( 2 ).startsWith( "#" ) )
+            {
+                // relative link - don't want to alter this one!
+            }
+            if ( mAnchor.group( 2 ).startsWith( "/" ) )
+            {
+                // root link
+                sb.insert( mAnchor.start( 2 ), serverURL );
+                indx += serverURL.length();
+            }
+            else if ( mAnchor.group( 2 ).indexOf( ':' ) < 0 )
+            {
+                // relative link
+                sb.insert( mAnchor.start( 2 ), baseURL );
+                indx += baseURL.length();
+            }
+        }
+        while ( true );
+
+        return sb.toString();
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org