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(excludes)) {
053                            fileSet.setExcludes(excludes);
054                    }
055    
056                    if (Validator.isNotNull(includes)) {
057                            fileSet.setIncludes(includes);
058                    }
059    
060                    copy.addFileset(fileSet);
061    
062                    copy.setOverwrite(overwrite);
063                    copy.setPreserveLastModified(preserveLastModified);
064                    copy.setProject(AntUtil.getProject());
065                    copy.setTodir(destination);
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                    copyFile(
103                            sourceFile, destinationDir, null, filterMap, overwrite,
104                            preserveLastModified);
105            }
106    
107            public static void copyFile(
108                    File sourceFile, File destinationDir, String destinationFileName,
109                    Map<String, String> filterMap, boolean overwrite,
110                    boolean preserveLastModified) {
111    
112                    Copy copy = new Copy();
113    
114                    copy.setFile(sourceFile);
115                    copy.setFiltering(true);
116                    copy.setOverwrite(overwrite);
117                    copy.setPreserveLastModified(preserveLastModified);
118                    copy.setProject(AntUtil.getProject());
119    
120                    if (destinationFileName == null) {
121                            copy.setTodir(destinationDir);
122                    }
123                    else {
124                            copy.setTofile(new File(destinationDir, destinationFileName));
125                    }
126    
127                    if (filterMap != null) {
128                            FilterSet filterSet = copy.createFilterSet();
129    
130                            for (Map.Entry<String, String> entry : filterMap.entrySet()) {
131                                    String token = entry.getKey();
132                                    String replacement = entry.getValue();
133    
134                                    filterSet.addFilter(token, replacement);
135                            }
136                    }
137    
138                    copy.execute();
139            }
140    
141    }