View Javadoc
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.MavenFileFilterRequest;
25  import org.apache.maven.shared.filtering.MavenFilteringException;
26  import org.codehaus.plexus.logging.Logger;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.codehaus.plexus.util.IOUtil;
29  import org.codehaus.plexus.util.ReaderFactory;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  import javax.annotation.Nonnull;
33  import javax.annotation.Nullable;
34  
35  import java.io.File;
36  import java.io.FileNotFoundException;
37  import java.io.IOException;
38  import java.io.Reader;
39  import java.util.Locale;
40  
41  /**
42   * @version $Id: FileFormatter.java 1506484 2013-07-24 10:23:42Z olamy $
43   */
44  public class FileFormatter
45  {
46  
47      private final Logger logger;
48  
49      private final AssemblerConfigurationSource configSource;
50  
51      public FileFormatter( AssemblerConfigurationSource configSource, Logger logger )
52      {
53          this.configSource = configSource;
54          this.logger = logger;
55      }
56  
57      public File format( File source, boolean filter, String lineEnding, String encoding )
58          throws AssemblyFormattingException
59      {
60          return format( source, filter, lineEnding, configSource.getTemporaryRootDirectory(), encoding );
61      }
62  
63      public File format( @Nonnull File source, boolean filter, String lineEnding, @Nullable File tempRoot,
64                          String encoding )
65          throws AssemblyFormattingException
66      {
67          AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot );
68  
69          File result = source;
70  
71          if ( StringUtils.isEmpty( encoding ) && filter )
72          {
73              logger.warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
74                               + ", i.e. build is platform dependent!" );
75          }
76  
77          if ( filter )
78          {
79              result = doFileFilter( source, tempRoot, encoding, configSource.getEscapeString() );
80          }
81  
82          String lineEndingChars = AssemblyFileUtils.getLineEndingCharacters( lineEnding );
83          if ( lineEndingChars != null )
84          {
85              result = formatLineEndings( lineEndingChars, result, tempRoot, encoding );
86          }
87  
88          return result;
89      }
90  
91      private File doFileFilter( @Nonnull File source, @Nullable File tempRoot, String encoding, String escapeString )
92          throws AssemblyFormattingException
93      {
94          try
95          {
96              File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot );
97  
98              //@todo this test can be improved
99              boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
100 
101             MavenFileFilterRequest filterRequest =
102                 new MavenFileFilterRequest( source, target, true, configSource.getProject(), configSource.getFilters(),
103                                             isPropertiesFile, encoding, configSource.getMavenSession(), null );
104             filterRequest.setEscapeString( escapeString );
105             filterRequest.setInjectProjectBuildFilters( true );
106             configSource.getMavenFileFilter().copyFile( filterRequest );
107 
108             return target;
109         }
110         catch ( MavenFilteringException e )
111         {
112             throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e );
113         }
114     }
115 
116     private File formatLineEndings( String lineEndingChars, File source, File tempRoot, String encoding )
117         throws AssemblyFormattingException
118     {
119         Reader contentReader = null;
120         try
121         {
122             File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot );
123 
124             AssemblyFileUtils.convertLineEndings( source, target, lineEndingChars, null, encoding );
125 
126             return target;
127         }
128         catch ( FileNotFoundException e )
129         {
130             throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e );
131         }
132         catch ( IOException e )
133         {
134             throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(),
135                                                    e );
136         }
137         finally
138         {
139             IOUtil.close( contentReader );
140         }
141     }
142 }