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(File source, File destination) {
44          copyDirectory(source, destination, null, null);
45      }
46  
47      public static void copyDirectory(
48          File source, File destination, String includes, String excludes) {
49  
50          copyDirectory(source, destination, includes, excludes, false, true);
51      }
52  
53      public static void copyDirectory(
54          File source, File destination, String includes, String excludes,
55          boolean overwrite, boolean preserveLastModified) {
56  
57          Copy copy = new Copy();
58  
59          FileSet fileSet = new FileSet();
60  
61          fileSet.setDir(source);
62  
63          if (Validator.isNotNull(includes)) {
64              fileSet.setIncludes(includes);
65          }
66  
67          if (Validator.isNotNull(excludes)) {
68              fileSet.setExcludes(excludes);
69          }
70  
71          copy.setProject(AntUtil.getProject());
72          copy.addFileset(fileSet);
73          copy.setTodir(destination);
74          copy.setOverwrite(overwrite);
75          copy.setPreserveLastModified(preserveLastModified);
76  
77          copy.execute();
78      }
79  
80      public static void copyDirectory(String source, String destination) {
81          copyDirectory(source, destination, null, null);
82      }
83  
84      public static void copyDirectory(
85          String source, String destination, String includes, String excludes) {
86  
87          copyDirectory(
88              new File(source), new File(destination), includes, excludes);
89      }
90  
91      public static void copyDirectory(
92          String source, String destination, String includes, String excludes,
93          boolean overwrite, boolean preserveLastModified) {
94  
95          copyDirectory(
96              new File(source), new File(destination), includes, excludes,
97              overwrite, preserveLastModified);
98      }
99  
100     public static void copyFile(
101         File sourceFile, File destinationDir, boolean overwrite,
102         boolean preserveLastModified) {
103 
104         copyFile(
105             sourceFile, destinationDir, null, overwrite, preserveLastModified);
106     }
107 
108     public static void copyFile(
109         File sourceFile, File destinationDir, Map<String, String> filterMap,
110         boolean overwrite, boolean preserveLastModified) {
111 
112         Copy copy = new Copy();
113 
114         FileSet fileSet = new FileSet();
115 
116         fileSet.setFile(sourceFile);
117 
118         copy.setProject(AntUtil.getProject());
119         copy.setFiltering(true);
120         copy.addFileset(fileSet);
121         copy.setTodir(destinationDir);
122         copy.setOverwrite(overwrite);
123         copy.setPreserveLastModified(preserveLastModified);
124 
125         if (filterMap != null) {
126             FilterSet filterSet = copy.createFilterSet();
127 
128             Iterator<String> itr = filterMap.keySet().iterator();
129 
130             while (itr.hasNext()) {
131                 String token = itr.next();
132 
133                 String replacement = filterMap.get(token);
134 
135                 filterSet.addFilter(token, replacement);
136             }
137         }
138 
139         copy.execute();
140     }
141 
142 }