Coverage Report - org.apache.maven.plugin.assembly.format.FileFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
FileFormatter
79%
23/29
100%
4/4
3
 
 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.filtering.MavenFilteringException;
 25  
 import org.codehaus.plexus.logging.Logger;
 26  
 import org.codehaus.plexus.util.FileUtils;
 27  
 import org.codehaus.plexus.util.IOUtil;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.FileNotFoundException;
 31  
 import java.io.FileReader;
 32  
 import java.io.IOException;
 33  
 import java.io.Reader;
 34  
 import java.util.Locale;
 35  
 
 36  
 /**
 37  
  * @version $Id: FileFormatter.java 965106 2010-07-17 16:44:18Z bentmann $
 38  
  */
 39  
 public class FileFormatter
 40  
 {
 41  
 
 42  
     private final Logger logger;
 43  
 
 44  
     private final AssemblerConfigurationSource configSource;
 45  
 
 46  
     public FileFormatter( AssemblerConfigurationSource configSource, Logger logger )
 47  25
     {
 48  25
         this.configSource = configSource;
 49  25
         this.logger = logger;
 50  25
     }
 51  
 
 52  
     public File format( File source, boolean filter, String lineEnding )
 53  
         throws AssemblyFormattingException
 54  
     {
 55  25
         return format ( source, filter, lineEnding, configSource.getTemporaryRootDirectory() );
 56  
     }
 57  
 
 58  
     public File format( File source, boolean filter, String lineEnding, File tempRoot )
 59  
         throws AssemblyFormattingException
 60  
     {
 61  32
         AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot, logger );
 62  
 
 63  32
         File result = source;
 64  
 
 65  32
         if ( filter )
 66  12
             result = doFileFilter( source, tempRoot );
 67  
 
 68  32
         String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding );
 69  32
         if ( lineEndingChars != null )
 70  
         {
 71  8
             result = formatLineEndings( lineEndingChars, result, tempRoot );
 72  
         }
 73  
 
 74  32
         return result;
 75  
     }
 76  
 
 77  
     private File doFileFilter( File source, File tempRoot )
 78  
         throws AssemblyFormattingException
 79  
     {
 80  
         try
 81  
         {
 82  12
             File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot );
 83  
 
 84  
             //@todo this test can be improved
 85  12
             boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
 86  
 
 87  12
             configSource.getMavenFileFilter().copyFile( source, target, true, configSource.getProject(),
 88  
                     configSource.getFilters(), isPropertiesFile, null, configSource.getMavenSession() );
 89  
 
 90  12
             return target;
 91  
         }
 92  0
         catch (MavenFilteringException e)
 93  
         {
 94  0
             throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e );
 95  
         }
 96  
     }
 97  
 
 98  
     private File formatLineEndings( String lineEndingChars, File source, File tempRoot )
 99  
         throws AssemblyFormattingException
 100  
     {
 101  8
         Reader contentReader = null;
 102  
         try
 103  
         {
 104  8
             contentReader = new FileReader( source );
 105  
 
 106  8
             File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot );
 107  
 
 108  8
             AssemblyFileUtils.convertLineEndings( contentReader, target, lineEndingChars );
 109  
 
 110  8
             return target;
 111  
         }
 112  0
         catch ( FileNotFoundException e )
 113  
         {
 114  0
             throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e );
 115  
         }
 116  0
         catch ( IOException e )
 117  
         {
 118  0
             throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e );
 119  
         }
 120  
         finally
 121  
         {
 122  8
             IOUtil.close( contentReader );
 123  
         }
 124  
     }
 125  
 }