Coverage Report - org.apache.maven.plugin.assembly.format.FileSetFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
FileSetFormatter
77%
35/45
90%
9/10
4,667
 
 1  
 package org.apache.maven.plugin.assembly.format;
 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.AssemblerConfigurationSource;
 23  
 import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
 24  
 import org.apache.maven.shared.model.fileset.FileSet;
 25  
 import org.apache.maven.shared.model.fileset.util.FileSetManager;
 26  
 import org.codehaus.plexus.logging.Logger;
 27  
 import org.codehaus.plexus.util.FileUtils;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.IOException;
 31  
 
 32  
 /**
 33  
  * @version $Id: FileSetFormatter.java 1163853 2011-08-31 22:42:32Z jdcasey $
 34  
  */
 35  
 public class FileSetFormatter
 36  
 {
 37  
 
 38  
     private final AssemblerConfigurationSource configSource;
 39  
 
 40  
     private final Logger logger;
 41  
 
 42  
     public FileSetFormatter( AssemblerConfigurationSource configSource, Logger logger )
 43  39
     {
 44  39
         this.configSource = configSource;
 45  39
         this.logger = logger;
 46  39
     }
 47  
 
 48  
     public File formatFileSetForAssembly( File fileSetDir, org.apache.maven.plugin.assembly.model.FileSet set )
 49  
         throws AssemblyFormattingException, IOException
 50  
     {
 51  30
         String lineEndingHint = set.getLineEnding();
 52  
 
 53  30
         String lineEnding = AssemblyFileUtils.getLineEndingCharacters( lineEndingHint );
 54  
 
 55  30
         if ( ( lineEnding != null ) || set.isFiltered() )
 56  
         {
 57  
 
 58  18
             FileSet fileSet = new FileSet();
 59  18
             fileSet.setLineEnding( lineEnding );
 60  
             
 61  18
             fileSet.setDirectory(fileSetDir.getAbsolutePath());
 62  
                         
 63  18
             fileSet.setIncludes( set.getIncludes() );
 64  
 
 65  18
             fileSet.setExcludes( set.getExcludes() );
 66  18
             fileSet.setUseDefaultExcludes( true );
 67  
 
 68  18
             FileSetManager fsm = new FileSetManager( logger );
 69  18
             String[] files = fsm.getIncludedFiles( fileSet );
 70  
 
 71  
             // if we don't have anything to process, let's just skip all of this mess.
 72  18
             if ( ( files == null ) || ( files.length == 0 ) )
 73  
             {
 74  3
                 logger.info( "No files selected for line-ending conversion or filtering. Skipping: " + fileSet.getDirectory() );
 75  
             }
 76  
             else
 77  
             {
 78  15
                 File formattedDir =
 79  
                     FileUtils.createTempFile( "fileSetFormatter.", ".tmp", configSource.getTemporaryRootDirectory() );
 80  
                 
 81  15
                 logger.debug( "Filtering files from: " + fileSetDir + " into temp dir: " + formattedDir );
 82  
 
 83  15
                 formattedDir.delete();
 84  15
                 formattedDir.mkdirs();
 85  
 
 86  15
                 FileFormatter fileFormatter = new FileFormatter( configSource, logger );
 87  36
                 for ( int i = 0; i < files.length; i++ )
 88  
                 {
 89  21
                     String file = files[i];
 90  
                     
 91  21
                     logger.debug( "Filtering: " + file );
 92  
 
 93  21
                     File targetFile = new File( formattedDir, file );
 94  
 
 95  21
                     targetFile.getParentFile().mkdirs();
 96  
 
 97  21
                     File sourceFile = new File( fileSetDir, file );
 98  
                     try
 99  
                     {
 100  21
                         sourceFile = fileFormatter.format( sourceFile, set.isFiltered(), lineEndingHint, formattedDir, configSource.getEncoding() );
 101  21
                         AssemblyFileUtils.copyFile( sourceFile, targetFile );
 102  
                     }
 103  0
                     catch ( AssemblyFormattingException e )
 104  
                     {
 105  0
                         deleteDirectory( formattedDir );
 106  0
                         throw e;
 107  
                     }
 108  0
                     catch ( IOException e )
 109  
                     {
 110  0
                         deleteDirectory( formattedDir );
 111  0
                         throw e;
 112  21
                     }
 113  
                 }
 114  15
                 return formattedDir;
 115  
             }
 116  3
         }
 117  
         else
 118  
         {
 119  12
             logger.debug( "NOT reformatting any files in " + fileSetDir );
 120  
         }
 121  
 
 122  15
         return fileSetDir;
 123  
     }
 124  
 
 125  
     private static void deleteDirectory( File formattedDir )
 126  
     {
 127  
         try
 128  
         {
 129  0
             FileUtils.deleteDirectory( formattedDir );
 130  
         }
 131  0
         catch ( IOException e1 )
 132  
         {
 133  
             // ignore
 134  0
         }
 135  0
     }
 136  
 
 137  
 }