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.Map;
022    
023    import org.apache.tools.ant.taskdefs.Copy;
024    import org.apache.tools.ant.types.FileSet;
025    import org.apache.tools.ant.types.FilterSet;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class CopyTask {
031    
032            public static void copyDirectory(File source, File destination) {
033                    copyDirectory(source, destination, null, null);
034            }
035    
036            public static void copyDirectory(
037                    File source, File destination, String includes, String excludes) {
038    
039                    copyDirectory(source, destination, includes, excludes, false, true);
040            }
041    
042            public static void copyDirectory(
043                    File source, File destination, String includes, String excludes,
044                    boolean overwrite, boolean preserveLastModified) {
045    
046                    Copy copy = new Copy();
047    
048                    FileSet fileSet = new FileSet();
049    
050                    fileSet.setDir(source);
051    
052                    if (Validator.isNotNull(includes)) {
053                            fileSet.setIncludes(includes);
054                    }
055    
056                    if (Validator.isNotNull(excludes)) {
057                            fileSet.setExcludes(excludes);
058                    }
059    
060                    copy.setProject(AntUtil.getProject());
061                    copy.addFileset(fileSet);
062                    copy.setTodir(destination);
063                    copy.setOverwrite(overwrite);
064                    copy.setPreserveLastModified(preserveLastModified);
065    
066                    copy.execute();
067            }
068    
069            public static void copyDirectory(String source, String destination) {
070                    copyDirectory(source, destination, null, null);
071            }
072    
073            public static void copyDirectory(
074                    String source, String destination, String includes, String excludes) {
075    
076                    copyDirectory(
077                            new File(source), new File(destination), includes, excludes);
078            }
079    
080            public static void copyDirectory(
081                    String source, String destination, String includes, String excludes,
082                    boolean overwrite, boolean preserveLastModified) {
083    
084                    copyDirectory(
085                            new File(source), new File(destination), includes, excludes,
086                            overwrite, preserveLastModified);
087            }
088    
089            public static void copyFile(
090                    File sourceFile, File destinationDir, boolean overwrite,
091                    boolean preserveLastModified) {
092    
093                    copyFile(
094                            sourceFile, destinationDir, null, overwrite, preserveLastModified);
095            }
096    
097            public static void copyFile(
098                    File sourceFile, File destinationDir, Map<String, String> filterMap,
099                    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                            for (Map.Entry<String, String> entry : filterMap.entrySet()) {
118                                    String token = entry.getKey();
119                                    String replacement = entry.getValue();
120    
121                                    filterSet.addFilter(token, replacement);
122                            }
123                    }
124    
125                    copy.execute();
126            }
127    
128    }