Coverage Report - org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
ApacheNoticeResourceTransformer
14%
13/92
7%
4/58
7
 
 1  
 package org.apache.maven.plugins.shade.resource;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.BufferedReader;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.InputStreamReader;
 26  
 import java.io.OutputStreamWriter;
 27  
 import java.io.PrintWriter;
 28  
 import java.io.Writer;
 29  
 import java.text.SimpleDateFormat;
 30  
 import java.util.Date;
 31  
 import java.util.Iterator;
 32  
 import java.util.LinkedHashMap;
 33  
 import java.util.LinkedHashSet;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 import java.util.Set;
 37  
 import java.util.TreeSet;
 38  
 import java.util.jar.JarEntry;
 39  
 import java.util.jar.JarOutputStream;
 40  
 
 41  
 import org.codehaus.plexus.util.StringUtils;
 42  
 
 43  1
 public class ApacheNoticeResourceTransformer
 44  
     implements ResourceTransformer
 45  
 {
 46  1
     Set entries = new LinkedHashSet();
 47  1
     Map organizationEntries = new LinkedHashMap();
 48  
 
 49  
     String projectName;
 50  1
     boolean addHeader = true;
 51  
 
 52  1
     String preamble1 =
 53  
           "// ------------------------------------------------------------------\n"
 54  
         + "// NOTICE file corresponding to the section 4d of The Apache License,\n"
 55  
         + "// Version 2.0, in this case for ";
 56  
 
 57  1
     String preamble2 = "\n// ------------------------------------------------------------------\n";
 58  
 
 59  1
     String preamble3 = "This product includes software developed at\n";
 60  
 
 61  
     //defaults overridable via config in pom
 62  1
     String organizationName = "The Apache Software Foundation";
 63  1
     String organizationURL = "http://www.apache.org/";
 64  1
     String inceptionYear = "2006";
 65  
 
 66  
     String copyright;
 67  
 
 68  
     /**
 69  
      * The file encoding of the <code>NOTICE</code> file.
 70  
      */
 71  
     String encoding;
 72  
 
 73  
     private static final String NOTICE_PATH = "META-INF/NOTICE";
 74  
 
 75  
     private static final String NOTICE_TXT_PATH = "META-INF/NOTICE.txt";
 76  
 
 77  
     public boolean canTransformResource( String resource )
 78  
     {
 79  4
         if ( NOTICE_PATH.equalsIgnoreCase( resource ) || NOTICE_TXT_PATH.equalsIgnoreCase( resource ) )
 80  
         {
 81  3
             return true;
 82  
         }
 83  
 
 84  1
         return false;
 85  
     }
 86  
 
 87  
     public void processResource( String resource, InputStream is, List relocators )
 88  
         throws IOException
 89  
     {
 90  0
         if ( entries.isEmpty() )
 91  
         {
 92  0
             String year = new SimpleDateFormat( "yyyy" ).format( new Date() );
 93  0
             if ( !inceptionYear.equals( year ) )
 94  
             {
 95  0
                 year = inceptionYear + "-" + year;
 96  
             }
 97  
 
 98  
 
 99  
             //add headers
 100  0
             if ( addHeader ) 
 101  
             {
 102  0
                 entries.add( preamble1 + projectName + preamble2 );
 103  
             }
 104  
             else
 105  
             {
 106  0
                 entries.add( "" );
 107  
             }
 108  
             //fake second entry, we'll look for a real one later
 109  0
             entries.add( projectName + "\nCopyright " + year + " " + organizationName + "\n" );
 110  0
             entries.add( preamble3 + organizationName + " (" + organizationURL + ").\n" );
 111  
         }
 112  
 
 113  
 
 114  
         BufferedReader reader;
 115  0
         if ( StringUtils.isNotEmpty( encoding ) )
 116  
         {
 117  0
             reader = new BufferedReader( new InputStreamReader( is, encoding ) );
 118  
         }
 119  
         else
 120  
         {
 121  0
             reader = new BufferedReader( new InputStreamReader( is ) );
 122  
         }
 123  
 
 124  0
         String line = reader.readLine();
 125  0
         StringBuffer sb = new StringBuffer();
 126  0
         Set currentOrg = null;
 127  0
         int lineCount = 0;
 128  0
         while ( line != null )
 129  
         {
 130  0
             String trimedLine = line.trim();
 131  
 
 132  0
             if ( !trimedLine.startsWith( "//" ) )
 133  
             {
 134  0
                 if ( trimedLine.length() > 0 )
 135  
                 {
 136  0
                     if ( trimedLine.startsWith( "- " ) )
 137  
                     {
 138  
                         //resource-bundle 1.3 mode
 139  0
                         if ( lineCount == 1
 140  
                             && sb.toString().indexOf( "This product includes/uses software(s) developed by" ) != -1 )
 141  
                         {
 142  0
                             currentOrg = (Set) organizationEntries.get( sb.toString().trim() );
 143  0
                             if ( currentOrg == null )
 144  
                             {
 145  0
                                 currentOrg = new TreeSet();
 146  0
                                 organizationEntries.put( sb.toString().trim(), currentOrg );
 147  
                             }
 148  0
                             sb = new StringBuffer();
 149  
                         }
 150  0
                         else if ( sb.length() > 0 && currentOrg != null )
 151  
                         {
 152  0
                             currentOrg.add( sb.toString() );
 153  0
                             sb = new StringBuffer();
 154  
                         }
 155  
 
 156  
                     }
 157  0
                     sb.append( line ).append( "\n" );
 158  0
                     lineCount++;
 159  
                 }
 160  
                 else
 161  
                 {
 162  0
                     String ent = sb.toString();
 163  0
                     if ( ent.startsWith( projectName )
 164  
                         && ent.indexOf( "Copyright " ) != -1 )
 165  
                     {
 166  0
                         copyright = ent;
 167  
                     }
 168  0
                     if ( currentOrg == null )
 169  
                     {
 170  0
                         entries.add( ent );
 171  
                     }
 172  
                     else
 173  
                     {
 174  0
                         currentOrg.add( ent );
 175  
                     }
 176  0
                     sb = new StringBuffer();
 177  0
                     lineCount = 0;
 178  0
                     currentOrg = null;
 179  
                 }
 180  
             }
 181  
 
 182  0
             line = reader.readLine();
 183  0
         }
 184  0
         if ( sb.length() > 0 ) 
 185  
         {
 186  0
             if ( currentOrg == null )
 187  
             {
 188  0
                 entries.add( sb.toString() );
 189  
             }
 190  
             else
 191  
             {
 192  0
                 currentOrg.add( sb.toString() );
 193  
             }
 194  
         }
 195  0
     }
 196  
 
 197  
     public boolean hasTransformedResource()
 198  
     {
 199  0
         return true;
 200  
     }
 201  
 
 202  
     public void modifyOutputStream( JarOutputStream jos )
 203  
         throws IOException
 204  
     {
 205  0
         jos.putNextEntry( new JarEntry( NOTICE_PATH ) );
 206  
 
 207  
         Writer pow;
 208  0
         if ( StringUtils.isNotEmpty( encoding ) )
 209  
         {
 210  0
             pow = new OutputStreamWriter( jos, encoding );
 211  
         }
 212  
         else
 213  
         {
 214  0
             pow = new OutputStreamWriter( jos );
 215  
         }
 216  0
         PrintWriter writer = new PrintWriter( pow );
 217  
 
 218  0
         int count = 0;
 219  0
         for ( Iterator itr = entries.iterator() ; itr.hasNext() ; )
 220  
         {
 221  0
             ++count;
 222  0
             String line = (String) itr.next();
 223  0
             if ( line.equals( copyright ) && count != 2 )
 224  
             {
 225  0
                 continue;
 226  
             }
 227  
 
 228  0
             if ( count == 2 && copyright != null )
 229  
             {
 230  0
                 writer.print( copyright );
 231  0
                 writer.print( '\n' );
 232  
             }
 233  
             else
 234  
             {
 235  0
                 writer.print( line );
 236  0
                 writer.print( '\n' );
 237  
             }
 238  0
             if ( count == 3 )
 239  
             {
 240  
                 //do org stuff
 241  0
                 for ( Iterator oit = organizationEntries.entrySet().iterator(); oit.hasNext(); )
 242  
                 {
 243  0
                     Map.Entry entry = (Map.Entry) oit.next();
 244  0
                     writer.print( entry.getKey().toString() );
 245  0
                     writer.print( '\n' );
 246  0
                     Set entrySet = (Set) entry.getValue();
 247  0
                     for ( Iterator eit = entrySet.iterator(); eit.hasNext(); )
 248  
                     {
 249  0
                         writer.print( eit.next().toString() );
 250  
                     }
 251  0
                     writer.print( '\n' );
 252  0
                 }
 253  
             }
 254  0
         }
 255  
 
 256  0
         writer.flush();
 257  
 
 258  0
         entries.clear();
 259  0
     }
 260  
 }