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