1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.ant;
24  
25  import com.liferay.portal.kernel.util.Validator;
26  
27  import java.io.File;
28  
29  import java.util.Iterator;
30  import java.util.Map;
31  
32  import org.apache.tools.ant.taskdefs.Copy;
33  import org.apache.tools.ant.types.FileSet;
34  import org.apache.tools.ant.types.FilterSet;
35  
36  /**
37   * <a href="CopyTask.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class CopyTask {
42  
43      public static void copyDirectory(String source, String destination) {
44          copyDirectory(source, destination, null, null);
45      }
46  
47      public static void copyDirectory(
48          String source, String destination, String includes, String excludes) {
49  
50          copyDirectory(
51              new File(source), new File(destination), includes, excludes);
52      }
53  
54      public static void copyDirectory(File source, File destination) {
55          copyDirectory(source, destination, null, null);
56      }
57  
58      public static void copyDirectory(
59          File source, File destination, String includes, String excludes) {
60  
61          copyDirectory(source, destination, includes, excludes, false, true);
62      }
63  
64      public static void copyDirectory(
65          File source, File destination, String includes, String excludes,
66          boolean overwrite, boolean preserveLastModified) {
67  
68          Copy copy = new Copy();
69  
70          FileSet fileSet = new FileSet();
71  
72          fileSet.setDir(source);
73  
74          if (Validator.isNotNull(includes)) {
75              fileSet.setIncludes(includes);
76          }
77  
78          if (Validator.isNotNull(excludes)) {
79              fileSet.setExcludes(excludes);
80          }
81  
82          copy.setProject(AntUtil.getProject());
83          copy.addFileset(fileSet);
84          copy.setTodir(destination);
85          copy.setOverwrite(overwrite);
86          copy.setPreserveLastModified(preserveLastModified);
87  
88          copy.execute();
89      }
90  
91      public static void copyFile(
92          File sourceFile, File destinationDir, boolean overwrite,
93          boolean preserveLastModified) {
94  
95          copyFile(
96              sourceFile, destinationDir, null, overwrite, preserveLastModified);
97      }
98  
99      public static void copyFile(
100         File sourceFile, File destinationDir, Map<String, String> filterMap,
101         boolean overwrite, boolean preserveLastModified) {
102 
103         Copy copy = new Copy();
104 
105         FileSet fileSet = new FileSet();
106 
107         fileSet.setFile(sourceFile);
108 
109         copy.setProject(AntUtil.getProject());
110         copy.setFiltering(true);
111         copy.addFileset(fileSet);
112         copy.setTodir(destinationDir);
113         copy.setOverwrite(overwrite);
114         copy.setPreserveLastModified(preserveLastModified);
115 
116         if (filterMap != null) {
117             FilterSet filterSet = copy.createFilterSet();
118 
119             Iterator<String> itr = filterMap.keySet().iterator();
120 
121             while (itr.hasNext()) {
122                 String token = itr.next();
123 
124                 String replacement = filterMap.get(token);
125 
126                 filterSet.addFilter(token, replacement);
127             }
128         }
129 
130         copy.execute();
131     }
132 
133 }