001    /**
002     * Copyright (c) 2000-2013 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                    copyFile(
102                            sourceFile, destinationDir, null, filterMap, overwrite,
103                            preserveLastModified);
104            }
105    
106            public static void copyFile(
107                    File sourceFile, File destinationDir, String destinationFileName,
108                    Map<String, String> filterMap, boolean overwrite,
109                    boolean preserveLastModified) {
110    
111                    Copy copy = new Copy();
112    
113                    copy.setFile(sourceFile);
114                    copy.setFiltering(true);
115                    copy.setOverwrite(overwrite);
116                    copy.setPreserveLastModified(preserveLastModified);
117                    copy.setProject(AntUtil.getProject());
118    
119                    if (destinationFileName == null) {
120                            copy.setTodir(destinationDir);
121                    }
122                    else {
123                            copy.setTofile(new File(destinationDir, destinationFileName));
124                    }
125    
126                    if (filterMap != null) {
127                            FilterSet filterSet = copy.createFilterSet();
128    
129                            for (Map.Entry<String, String> entry : filterMap.entrySet()) {
130                                    String token = entry.getKey();
131                                    String replacement = entry.getValue();
132    
133                                    filterSet.addFilter(token, replacement);
134                            }
135                    }
136    
137                    copy.execute();
138            }
139    
140    }