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