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