1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.ant;
16  
17  import com.liferay.portal.kernel.util.Validator;
18  
19  import java.io.File;
20  
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.apache.tools.ant.taskdefs.Copy;
25  import org.apache.tools.ant.types.FileSet;
26  import org.apache.tools.ant.types.FilterSet;
27  
28  /**
29   * <a href="CopyTask.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class CopyTask {
34  
35      public static void copyDirectory(File source, File destination) {
36          copyDirectory(source, destination, null, null);
37      }
38  
39      public static void copyDirectory(
40          File source, File destination, String includes, String excludes) {
41  
42          copyDirectory(source, destination, includes, excludes, false, true);
43      }
44  
45      public static void copyDirectory(
46          File source, File destination, String includes, String excludes,
47          boolean overwrite, boolean preserveLastModified) {
48  
49          Copy copy = new Copy();
50  
51          FileSet fileSet = new FileSet();
52  
53          fileSet.setDir(source);
54  
55          if (Validator.isNotNull(includes)) {
56              fileSet.setIncludes(includes);
57          }
58  
59          if (Validator.isNotNull(excludes)) {
60              fileSet.setExcludes(excludes);
61          }
62  
63          copy.setProject(AntUtil.getProject());
64          copy.addFileset(fileSet);
65          copy.setTodir(destination);
66          copy.setOverwrite(overwrite);
67          copy.setPreserveLastModified(preserveLastModified);
68  
69          copy.execute();
70      }
71  
72      public static void copyDirectory(String source, String destination) {
73          copyDirectory(source, destination, null, null);
74      }
75  
76      public static void copyDirectory(
77          String source, String destination, String includes, String excludes) {
78  
79          copyDirectory(
80              new File(source), new File(destination), includes, excludes);
81      }
82  
83      public static void copyDirectory(
84          String source, String destination, String includes, String excludes,
85          boolean overwrite, boolean preserveLastModified) {
86  
87          copyDirectory(
88              new File(source), new File(destination), includes, excludes,
89              overwrite, preserveLastModified);
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 }