001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.ant;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.io.File;
020    
021    import java.util.Iterator;
022    import java.util.Map;
023    
024    import org.apache.tools.ant.taskdefs.Copy;
025    import org.apache.tools.ant.types.FileSet;
026    import org.apache.tools.ant.types.FilterSet;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class CopyTask {
032    
033            public static void copyDirectory(File source, File destination) {
034                    copyDirectory(source, destination, null, null);
035            }
036    
037            public static void copyDirectory(
038                    File source, File destination, String includes, String excludes) {
039    
040                    copyDirectory(source, destination, includes, excludes, false, true);
041            }
042    
043            public static void copyDirectory(
044                    File source, File destination, String includes, String excludes,
045                    boolean overwrite, boolean preserveLastModified) {
046    
047                    Copy copy = new Copy();
048    
049                    FileSet fileSet = new FileSet();
050    
051                    fileSet.setDir(source);
052    
053                    if (Validator.isNotNull(includes)) {
054                            fileSet.setIncludes(includes);
055                    }
056    
057                    if (Validator.isNotNull(excludes)) {
058                            fileSet.setExcludes(excludes);
059                    }
060    
061                    copy.setProject(AntUtil.getProject());
062                    copy.addFileset(fileSet);
063                    copy.setTodir(destination);
064                    copy.setOverwrite(overwrite);
065                    copy.setPreserveLastModified(preserveLastModified);
066    
067                    copy.execute();
068            }
069    
070            public static void copyDirectory(String source, String destination) {
071                    copyDirectory(source, destination, null, null);
072            }
073    
074            public static void copyDirectory(
075                    String source, String destination, String includes, String excludes) {
076    
077                    copyDirectory(
078                            new File(source), new File(destination), includes, excludes);
079            }
080    
081            public static void copyDirectory(
082                    String source, String destination, String includes, String excludes,
083                    boolean overwrite, boolean preserveLastModified) {
084    
085                    copyDirectory(
086                            new File(source), new File(destination), includes, excludes,
087                            overwrite, preserveLastModified);
088            }
089    
090            public static void copyFile(
091                    File sourceFile, File destinationDir, boolean overwrite,
092                    boolean preserveLastModified) {
093    
094                    copyFile(
095                            sourceFile, destinationDir, null, overwrite, preserveLastModified);
096            }
097    
098            public static void copyFile(
099                    File sourceFile, File destinationDir, Map<String, String> filterMap,
100                    boolean overwrite, boolean preserveLastModified) {
101    
102                    Copy copy = new Copy();
103    
104                    FileSet fileSet = new FileSet();
105    
106                    fileSet.setFile(sourceFile);
107    
108                    copy.setProject(AntUtil.getProject());
109                    copy.setFiltering(true);
110                    copy.addFileset(fileSet);
111                    copy.setTodir(destinationDir);
112                    copy.setOverwrite(overwrite);
113                    copy.setPreserveLastModified(preserveLastModified);
114    
115                    if (filterMap != null) {
116                            FilterSet filterSet = copy.createFilterSet();
117    
118                            Iterator<String> itr = filterMap.keySet().iterator();
119    
120                            while (itr.hasNext()) {
121                                    String token = itr.next();
122    
123                                    String replacement = filterMap.get(token);
124    
125                                    filterSet.addFilter(token, replacement);
126                            }
127                    }
128    
129                    copy.execute();
130            }
131    
132    }