1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
42  public class CopyTask {
43  
44      public static void copyDirectory(String source, String destination) {
45          copyDirectory(source, destination, null, null);
46      }
47  
48      public static void copyDirectory(
49          String source, String destination, String includes, String excludes) {
50  
51          copyDirectory(
52              new File(source), new File(destination), includes, excludes);
53      }
54  
55      public static void copyDirectory(File source, File destination) {
56          copyDirectory(source, destination, null, null);
57      }
58  
59      public static void copyDirectory(
60          File source, File destination, String includes, String excludes) {
61  
62          copyDirectory(source, destination, includes, excludes, false, true);
63      }
64  
65      public static void copyDirectory(
66          File source, File destination, String includes, String excludes,
67          boolean overwrite, boolean preserveLastModified) {
68  
69          Copy copy = new Copy();
70  
71          FileSet fileSet = new FileSet();
72  
73          fileSet.setDir(source);
74  
75          if (Validator.isNotNull(includes)) {
76              fileSet.setIncludes(includes);
77          }
78  
79          if (Validator.isNotNull(excludes)) {
80              fileSet.setExcludes(excludes);
81          }
82  
83          copy.setProject(AntUtil.getProject());
84          copy.addFileset(fileSet);
85          copy.setTodir(destination);
86          copy.setOverwrite(overwrite);
87          copy.setPreserveLastModified(preserveLastModified);
88  
89          copy.execute();
90      }
91  
92      public static void copyFile(
93          File sourceFile, File destinationDir, boolean overwrite,
94          boolean preserveLastModified) {
95  
96          copyFile(
97              sourceFile, destinationDir, null, overwrite, preserveLastModified);
98      }
99  
100     public static void copyFile(
101         File sourceFile, File destinationDir, Map<String, String> filterMap,
102         boolean overwrite, boolean preserveLastModified) {
103 
104         Copy copy = new Copy();
105 
106         FileSet fileSet = new FileSet();
107 
108         fileSet.setFile(sourceFile);
109 
110         copy.setProject(AntUtil.getProject());
111         copy.setFiltering(true);
112         copy.addFileset(fileSet);
113         copy.setTodir(destinationDir);
114         copy.setOverwrite(overwrite);
115         copy.setPreserveLastModified(preserveLastModified);
116 
117         if (filterMap != null) {
118             FilterSet filterSet = copy.createFilterSet();
119 
120             Iterator<String> itr = filterMap.keySet().iterator();
121 
122             while (itr.hasNext()) {
123                 String token = itr.next();
124 
125                 String replacement = filterMap.get(token);
126 
127                 filterSet.addFilter(token, replacement);
128             }
129         }
130 
131         copy.execute();
132     }
133 
134 }