View Javadoc
1   package org.apache.maven.shared.filtering;
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.execution.MavenSession;
23  import org.apache.maven.project.MavenProject;
24  import org.apache.maven.shared.utils.StringUtils;
25  import org.apache.maven.shared.utils.io.FileUtils;
26  import org.apache.maven.shared.utils.io.FileUtils.FilterWrapper;
27  import org.apache.maven.shared.utils.io.IOUtil;
28  import org.sonatype.plexus.build.incremental.BuildContext;
29  
30  import javax.annotation.Nonnull;
31  import javax.annotation.Nullable;
32  import java.io.BufferedReader;
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.FileNotFoundException;
36  import java.io.FileOutputStream;
37  import java.io.FileReader;
38  import java.io.FileWriter;
39  import java.io.IOException;
40  import java.io.InputStreamReader;
41  import java.io.OutputStreamWriter;
42  import java.io.Reader;
43  import java.io.UnsupportedEncodingException;
44  import java.io.Writer;
45  import java.util.Arrays;
46  import java.util.List;
47  
48  /**
49   * @author Olivier Lamy
50   * @plexus.component role="org.apache.maven.shared.filtering.MavenFileFilter"
51   * role-hint="default"
52   */
53  public class DefaultMavenFileFilter
54      extends BaseFilter
55      implements MavenFileFilter
56  {
57  
58      /**
59       * @plexus.requirement
60       */
61      private MavenReaderFilter readerFilter;
62  
63      /**
64       * @plexus.requirement
65       */
66      private BuildContext buildContext;
67  
68      public void copyFile( File from, File to, boolean filtering, MavenProject mavenProject, List<String> filters,
69                            boolean escapedBackslashesInFilePath, String encoding, MavenSession mavenSession )
70          throws MavenFilteringException
71      {
72          MavenResourcesExecution mre = new MavenResourcesExecution();
73          mre.setMavenProject( mavenProject );
74          mre.setFileFilters( filters );
75          mre.setEscapeWindowsPaths( escapedBackslashesInFilePath );
76          mre.setMavenSession( mavenSession );
77          mre.setInjectProjectBuildFilters( true );
78  
79          List<FileUtils.FilterWrapper> filterWrappers = getDefaultFilterWrappers( mre );
80          copyFile( from, to, filtering, filterWrappers, encoding );
81      }
82  
83  
84      public void copyFile( MavenFileFilterRequest mavenFileFilterRequest )
85          throws MavenFilteringException
86      {
87          List<FilterWrapper> filterWrappers = getDefaultFilterWrappers( mavenFileFilterRequest );
88  
89          copyFile( mavenFileFilterRequest.getFrom(), mavenFileFilterRequest.getTo(),
90                    mavenFileFilterRequest.isFiltering(), filterWrappers, mavenFileFilterRequest.getEncoding() );
91      }
92  
93  
94      public void copyFile( File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
95                            String encoding )
96          throws MavenFilteringException
97      {
98          // overwrite forced to false to preserve backward comp
99          copyFile( from, to, filtering, filterWrappers, encoding, false );
100     }
101 
102 
103     public void copyFile( File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers,
104                           String encoding, boolean overwrite )
105         throws MavenFilteringException
106     {
107         try
108         {
109             if ( filtering )
110             {
111                 if ( getLogger().isDebugEnabled() )
112                 {
113                     getLogger().debug( "filtering " + from.getPath() + " to " + to.getPath() );
114                 }
115                 filterFile( from, to, encoding, filterWrappers );
116             }
117             else
118             {
119                 if ( getLogger().isDebugEnabled() )
120                 {
121                     getLogger().debug( "copy " + from.getPath() + " to " + to.getPath() );
122                 }
123                 FileUtils.copyFile( from, to, encoding, new FileUtils.FilterWrapper[0], overwrite );
124             }
125 
126             buildContext.refresh( to );
127         }
128         catch ( IOException e )
129         {
130             throw new MavenFilteringException( e.getMessage(), e );
131         }
132 
133     }
134 
135     private void filterFile( @Nonnull File from, @Nonnull File to, @Nullable String encoding,
136                              @Nullable List<FilterWrapper> wrappers )
137         throws IOException, MavenFilteringException
138     {
139         if ( wrappers != null && wrappers.size() > 0 )
140         {
141             Reader fileReader = null;
142             Writer fileWriter = null;
143             try
144             {
145                 fileReader = getFileReader( encoding, from );
146                 fileWriter = getFileWriter( encoding, to );
147                 Reader src = readerFilter.filter( fileReader, true, wrappers );
148 
149                 IOUtil.copy( src, fileWriter );
150             }
151             finally
152             {
153                 IOUtil.close( fileReader );
154                 IOUtil.close( fileWriter );
155             }
156         }
157         else
158         {
159             if ( to.lastModified() < from.lastModified()  )
160             {
161                 FileUtils.copyFile( from, to );
162             }
163         }
164     }
165 
166     private Writer getFileWriter( String encoding, File to )
167         throws IOException
168     {
169         if ( StringUtils.isEmpty( encoding ) )
170         {
171             return new FileWriter( to );
172         }
173         else
174         {
175             FileOutputStream outstream = new FileOutputStream( to );
176 
177             return new OutputStreamWriter( outstream, encoding );
178         }
179     }
180 
181     private Reader getFileReader( String encoding, File from )
182         throws FileNotFoundException, UnsupportedEncodingException
183     {
184         // buffer so it isn't reading a byte at a time!
185         if ( StringUtils.isEmpty( encoding ) )
186         {
187             return new BufferedReader( new FileReader( from ) );
188         }
189         else
190         {
191             FileInputStream instream = new FileInputStream( from );
192             return new BufferedReader( new InputStreamReader( instream, encoding ) );
193         }
194     }
195 
196 
197 }