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