Coverage Report - org.apache.maven.plugin.assembly.filter.AbstractLineAggregatingHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractLineAggregatingHandler
0%
0/54
0%
0/18
2,182
 
 1  
 package org.apache.maven.plugin.assembly.filter;
 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 org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
 23  
 import org.codehaus.plexus.archiver.Archiver;
 24  
 import org.codehaus.plexus.archiver.ArchiverException;
 25  
 import org.codehaus.plexus.archiver.ResourceIterator;
 26  
 import org.codehaus.plexus.archiver.UnArchiver;
 27  
 import org.codehaus.plexus.components.io.fileselectors.FileInfo;
 28  
 import org.codehaus.plexus.util.IOUtil;
 29  
 
 30  
 import java.io.BufferedReader;
 31  
 import java.io.File;
 32  
 import java.io.FileOutputStream;
 33  
 import java.io.IOException;
 34  
 import java.io.InputStreamReader;
 35  
 import java.io.OutputStreamWriter;
 36  
 import java.io.PrintWriter;
 37  
 import java.util.ArrayList;
 38  
 import java.util.HashMap;
 39  
 import java.util.List;
 40  
 import java.util.Map;
 41  
 
 42  0
 public abstract class AbstractLineAggregatingHandler
 43  
     implements ContainerDescriptorHandler
 44  
 {
 45  
 
 46  0
     private Map<String, List<String>> catalog = new HashMap<String, List<String>>();
 47  
 
 48  0
     private boolean excludeOverride = false;
 49  
 
 50  
     protected abstract String getOutputPathPrefix( final FileInfo fileInfo );
 51  
 
 52  
     protected abstract boolean fileMatches( final FileInfo fileInfo );
 53  
 
 54  
     protected String getEncoding()
 55  
     {
 56  0
         return "UTF-8";
 57  
     }
 58  
 
 59  
     public void finalizeArchiveCreation( final Archiver archiver )
 60  
         throws ArchiverException
 61  
     {
 62  
         // this will prompt the isSelected() call, below, for all resources added to the archive.
 63  
         // FIXME: This needs to be corrected in the AbstractArchiver, where
 64  
         // runArchiveFinalizers() is called before regular resources are added...
 65  
         // which is done because the manifest needs to be added first, and the
 66  
         // manifest-creation component is a finalizer in the assembly plugin...
 67  0
         for ( final ResourceIterator it = archiver.getResources(); it.hasNext(); )
 68  
         {
 69  0
             it.next();
 70  
         }
 71  
 
 72  0
         addToArchive( archiver );
 73  0
     }
 74  
 
 75  
     protected void addToArchive( final Archiver archiver )
 76  
         throws ArchiverException
 77  
     {
 78  0
         for ( final Map.Entry<String, List<String>> entry : catalog.entrySet() )
 79  
         {
 80  0
             final String name = entry.getKey();
 81  0
             final String fname = new File( name ).getName();
 82  
 
 83  0
             PrintWriter writer = null;
 84  
             File f;
 85  
             try
 86  
             {
 87  0
                 f = File.createTempFile( "assembly-" + fname, ".tmp" );
 88  0
                 f.deleteOnExit();
 89  
 
 90  0
                 writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) );
 91  0
                 for ( final String line : entry.getValue() )
 92  
                 {
 93  0
                     writer.println( line );
 94  
                 }
 95  
             }
 96  0
             catch ( final IOException e )
 97  
             {
 98  0
                 throw new ArchiverException( "Error adding aggregated content for: " + fname
 99  
                                 + " to finalize archive creation. Reason: " + e.getMessage(), e );
 100  
             }
 101  
             finally
 102  
             {
 103  0
                 IOUtil.close( writer );
 104  0
             }
 105  
 
 106  0
             excludeOverride = true;
 107  0
             archiver.addFile( f, name );
 108  0
             excludeOverride = false;
 109  0
         }
 110  0
     }
 111  
 
 112  
     public void finalizeArchiveExtraction( final UnArchiver unArchiver )
 113  
         throws ArchiverException
 114  
     {
 115  0
     }
 116  
 
 117  
     public List<String> getVirtualFiles()
 118  
     {
 119  0
         return new ArrayList<String>( catalog.keySet() );
 120  
     }
 121  
 
 122  
     public boolean isSelected( final FileInfo fileInfo )
 123  
         throws IOException
 124  
     {
 125  0
         if ( excludeOverride )
 126  
         {
 127  0
             return true;
 128  
         }
 129  
 
 130  0
         String name = fileInfo.getName();
 131  0
         name = AssemblyFileUtils.normalizePath( name );
 132  0
         name = name.replace( File.separatorChar, '/' );
 133  
 
 134  0
         if ( fileInfo.isFile() && fileMatches( fileInfo ) )
 135  
         {
 136  0
             name = getOutputPathPrefix( fileInfo ) + new File( name ).getName();
 137  
 
 138  0
             List<String> lines = catalog.get( name );
 139  0
             if ( lines == null )
 140  
             {
 141  0
                 lines = new ArrayList<String>();
 142  0
                 catalog.put( name, lines );
 143  
             }
 144  
 
 145  0
             readLines( fileInfo, lines );
 146  
 
 147  0
             return false;
 148  
         }
 149  
 
 150  0
         return true;
 151  
     }
 152  
 
 153  
     protected void readLines( final FileInfo fileInfo, final List<String> lines )
 154  
         throws IOException
 155  
     {
 156  0
         BufferedReader reader = null;
 157  
         try
 158  
         {
 159  0
             reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );
 160  
 
 161  0
             String line = null;
 162  0
             while ( ( line = reader.readLine() ) != null )
 163  
             {
 164  0
                 if ( !lines.contains( line ) )
 165  
                 {
 166  0
                     lines.add( line );
 167  
                 }
 168  
             }
 169  
         }
 170  
         finally
 171  
         {
 172  0
             IOUtil.close( reader );
 173  0
         }
 174  0
     }
 175  
 
 176  
     protected final Map<String, List<String>> getCatalog()
 177  
     {
 178  0
         return catalog;
 179  
     }
 180  
 
 181  
     protected final void setCatalog( final Map<String, List<String>> catalog )
 182  
     {
 183  0
         this.catalog = catalog;
 184  0
     }
 185  
 
 186  
 }