hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1027755 - in /httpcomponents/maven-notice-plugin/trunk: pom.xml src/main/java/org/apache/httpcomponents/maven/plugin/notice/NoticeMojo.java
Date Tue, 26 Oct 2010 22:01:24 GMT
Author: olegk
Date: Tue Oct 26 22:01:23 2010
New Revision: 1027755

URL: http://svn.apache.org/viewvc?rev=1027755&view=rev
Log:
Rewrite NOTICE header based on the POM properties

Modified:
    httpcomponents/maven-notice-plugin/trunk/pom.xml
    httpcomponents/maven-notice-plugin/trunk/src/main/java/org/apache/httpcomponents/maven/plugin/notice/NoticeMojo.java

Modified: httpcomponents/maven-notice-plugin/trunk/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/maven-notice-plugin/trunk/pom.xml?rev=1027755&r1=1027754&r2=1027755&view=diff
==============================================================================
--- httpcomponents/maven-notice-plugin/trunk/pom.xml (original)
+++ httpcomponents/maven-notice-plugin/trunk/pom.xml Tue Oct 26 22:01:23 2010
@@ -58,6 +58,7 @@
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <maven.version>2.2.0</maven.version>
+    <commons-io.version>2.0</commons-io.version>
   </properties>
 
   <dependencies>
@@ -77,6 +78,11 @@
         </exclusion>
       </exclusions>
     </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>${commons-io.version}</version>
+    </dependency>
   </dependencies>
 
   <build>

Modified: httpcomponents/maven-notice-plugin/trunk/src/main/java/org/apache/httpcomponents/maven/plugin/notice/NoticeMojo.java
URL: http://svn.apache.org/viewvc/httpcomponents/maven-notice-plugin/trunk/src/main/java/org/apache/httpcomponents/maven/plugin/notice/NoticeMojo.java?rev=1027755&r1=1027754&r2=1027755&view=diff
==============================================================================
--- httpcomponents/maven-notice-plugin/trunk/src/main/java/org/apache/httpcomponents/maven/plugin/notice/NoticeMojo.java
(original)
+++ httpcomponents/maven-notice-plugin/trunk/src/main/java/org/apache/httpcomponents/maven/plugin/notice/NoticeMojo.java
Tue Oct 26 22:01:23 2010
@@ -25,15 +25,20 @@
  */
 package org.apache.httpcomponents.maven.plugin.notice;
 
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Calendar;
 import java.util.List;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.maven.model.License;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
@@ -47,6 +52,8 @@ import org.apache.maven.project.MavenPro
  */
 public final class NoticeMojo extends AbstractMojo {
 
+    private final static String UTF_8 = "UTF-8";
+    
     /**
      * The Maven Project Object
      *
@@ -83,26 +90,39 @@ public final class NoticeMojo extends Ab
         List licenses = this.project.getLicenses();
         for (int i = 0; i < licenses.size(); i++) {
             License license = (License) licenses.get(i);
-            URI uri = createURI(license.getUrl());
-            if (!uri.isAbsolute()) {
-                File file = findFile(license.getUrl());
-                if (file != null) {
-                    uri = createURI("file://" + file.getAbsolutePath());
+            try {
+                URI uri = new URI(license.getUrl());
+                String s = "LICENSE";
+                if (i > 0) {
+                    s = s + i;
                 }
+                s = s + ".txt";
+                File out = new File(nalDir, s);
+                if (!uri.isAbsolute()) {
+                    File file = findFile(license.getUrl());
+                    if (file != null) {
+                        FileUtils.copyFile(file, out);
+                    }
+                } else {
+                    FileUtils.copyURLToFile(uri.toURL(), out);
+                }
+            } catch (URISyntaxException ex) {
+                throw new MojoFailureException("Invalid URI: " + 
+                        ex.getMessage(), ex);
+            } catch (IOException ex) {
+                throw new MojoFailureException("I/O error copying LICENSE file: " + 
+                        ex.getMessage(), ex);
             }
-            String s = "LICENSE";
-            if (i > 0) {
-                s = s + i;
-            }
-            s = s + ".txt";
-            File out = new File(nalDir, s);
-            copy(uri, out);
         }
         File file = findFile("NOTICE.txt");
         if (file != null) {
             File out = new File(nalDir, "NOTICE.txt");
-            URI uri = createURI("file://" + file.getAbsolutePath());
-            copy(uri, out);
+            try {
+                rewriteNotice(file, out);
+            } catch (IOException ex) {
+                throw new MojoFailureException("I/O error copying NOTICE file: " + 
+                        ex.getMessage(), ex);
+            }
         }
 
         Resource nalRes = new Resource();
@@ -126,33 +146,42 @@ public final class NoticeMojo extends Ab
         return null;
     }
 
-    private URI createURI(final String s) throws MojoExecutionException {
+    private void rewriteNotice(final File in, final File out) throws IOException  {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(
+                new FileInputStream(in), UTF_8));
         try {
-            return new URI(s);
-        } catch (URISyntaxException ex) {
-            throw new MojoExecutionException("Invalid URL: " + s);
-        }
-    }
-
-    private void copy(final URI in, final File out) throws MojoExecutionException {
-        try {
-            InputStream instream = in.toURL().openStream();
+            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+                    new FileOutputStream(out), UTF_8));
             try {
-                OutputStream outstream = new FileOutputStream(out);
-                try {
-                    byte[] tmp = new byte[1024 * 8];
-                    int l;
-                    while ((l = instream.read(tmp)) != -1) {
-                        outstream.write(tmp, 0, l);
+                writer.write("Apache HttpComponent ");
+                writer.write(this.project.getName());
+                writer.write("\r\n");
+                writer.write("Copyright ");
+                String fromYear = this.project.getInceptionYear();
+                if (fromYear != null && fromYear.length() > 0) {
+                    writer.write(fromYear);
+                    writer.write("-");
+                }
+                Calendar cal = Calendar.getInstance();
+                String toYear = Integer.toString(cal.get(Calendar.YEAR));
+                writer.write(toYear);
+                writer.write(" The Apache Software Foundation\r\n");
+                
+                int count = 0;
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    count++;
+                    if (count > 2) {
+                        writer.write(line);
+                        writer.write("\r\n");
                     }
-                } finally {
-                    outstream.close();
                 }
+                writer.flush();
             } finally {
-                instream.close();
+                writer.close();
             }
-        } catch (IOException ex) {
-            throw new MojoExecutionException("I/O error: " + ex.getMessage(), ex);
+        } finally {
+            reader.close();
         }
     }
 



Mime
View raw message