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 java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  import java.util.Locale;
28  
29  import javax.annotation.Nonnull;
30  import javax.annotation.Nullable;
31  
32  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
33  import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils;
34  import org.apache.maven.plugin.assembly.utils.LineEndings;
35  import org.apache.maven.plugin.assembly.utils.LineEndingsUtils;
36  import org.apache.maven.shared.filtering.MavenFileFilterRequest;
37  import org.apache.maven.shared.filtering.MavenFilteringException;
38  import org.codehaus.plexus.logging.Logger;
39  import org.codehaus.plexus.util.FileUtils;
40  import org.codehaus.plexus.util.ReaderFactory;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  /**
44   * @version $Id: FileFormatter.java 1633544 2014-10-22 06:58:47Z krosenvold $
45   */
46  public class FileFormatter
47  {
48  
49      private final Logger logger;
50  
51      private final AssemblerConfigurationSource configSource;
52  
53      public FileFormatter( AssemblerConfigurationSource configSource, Logger logger )
54      {
55          this.configSource = configSource;
56          this.logger = logger;
57      }
58  
59      public File format( @Nonnull File source, boolean filter, String lineEnding, String encoding )
60          throws AssemblyFormattingException
61      {
62          File tempRoot = configSource.getTemporaryRootDirectory();
63          AssemblyFileUtils.verifyTempDirectoryAvailability( tempRoot );
64  
65          File result = source;
66  
67          if ( StringUtils.isEmpty( encoding ) && filter )
68          {
69              logger.warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
70                  + ", i.e. build is platform dependent!" );
71          }
72  
73          if ( filter )
74          {
75              result =
76                  doFileFilter( source, tempRoot, encoding, configSource.getEscapeString(), configSource.getDelimiters() );
77          }
78  
79          LineEndings lineEnding1 = LineEndingsUtils.getLineEnding( lineEnding );
80          if ( !LineEndings.keep.equals( lineEnding1 ) )
81          {
82              result = formatLineEndings( lineEnding1, result, tempRoot, encoding );
83          }
84  
85          return result;
86      }
87  
88      private File doFileFilter( @Nonnull File source, @Nullable File tempRoot, String encoding, String escapeString,
89                                 List<String> delimiters )
90          throws AssemblyFormattingException
91      {
92          try
93          {
94              File target = FileUtils.createTempFile( source.getName() + ".", ".filtered", tempRoot );
95  
96              // @todo this test can be improved
97              boolean isPropertiesFile = source.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".properties" );
98  
99              MavenFileFilterRequest filterRequest =
100                 new MavenFileFilterRequest( source, target, true, configSource.getProject(), configSource.getFilters(),
101                                             isPropertiesFile, encoding, configSource.getMavenSession(), null );
102             filterRequest.setEscapeString( escapeString );
103 
104             // if these are NOT set, just use the defaults, which are '${*}' and '@'.
105             if ( delimiters != null && !delimiters.isEmpty() )
106             {
107                 LinkedHashSet<String> delims = new LinkedHashSet<String>();
108                 for ( String delim : delimiters )
109                 {
110                     if ( delim == null )
111                     {
112                         // FIXME: ${filter:*} could also trigger this condition. Need a better long-term solution.
113                         delims.add( "${*}" );
114                     }
115                     else
116                     {
117                         delims.add( delim );
118                     }
119                 }
120 
121                 filterRequest.setDelimiters( delims );
122             }
123             else
124             {
125                 filterRequest.setDelimiters( filterRequest.getDelimiters() );
126             }
127 
128             filterRequest.setInjectProjectBuildFilters( configSource.isIncludeProjectBuildFilters() );
129             configSource.getMavenFileFilter().copyFile( filterRequest );
130 
131             return target;
132         }
133         catch ( MavenFilteringException e )
134         {
135             throw new AssemblyFormattingException( "Error filtering file '" + source + "': " + e.getMessage(), e );
136         }
137     }
138 
139 
140     private File formatLineEndings( LineEndings lineEnding, File source, File tempRoot, String encoding )
141         throws AssemblyFormattingException
142     {
143         try
144         {
145             File target = FileUtils.createTempFile( source.getName() + ".", ".formatted", tempRoot );
146 
147             LineEndingsUtils.convertLineEndings( source, target, lineEnding, null, encoding );
148 
149             return target;
150         }
151         catch ( FileNotFoundException e )
152         {
153             throw new AssemblyFormattingException( "File to filter not found: " + e.getMessage(), e );
154         }
155         catch ( IOException e )
156         {
157             throw new AssemblyFormattingException( "Error line formatting file '" + source + "': " + e.getMessage(), e );
158         }
159     }
160 }