001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.ant;
022:
023: import com.liferay.portal.kernel.util.Validator;
024:
025: import java.io.File;
026:
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import org.apache.tools.ant.taskdefs.Copy;
031: import org.apache.tools.ant.types.FileSet;
032: import org.apache.tools.ant.types.FilterSet;
033:
034: /**
035: * <a href="CopyTask.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class CopyTask {
041:
042: public static void copyDirectory(String source, String destination) {
043: copyDirectory(source, destination, null, null);
044: }
045:
046: public static void copyDirectory(String source, String destination,
047: String includes, String excludes) {
048:
049: copyDirectory(new File(source), new File(destination),
050: includes, excludes);
051: }
052:
053: public static void copyDirectory(File source, File destination) {
054: copyDirectory(source, destination, null, null);
055: }
056:
057: public static void copyDirectory(File source, File destination,
058: String includes, String excludes) {
059:
060: copyDirectory(source, destination, includes, excludes, false,
061: true);
062: }
063:
064: public static void copyDirectory(File source, File destination,
065: String includes, String excludes, boolean overwrite,
066: boolean preserveLastModified) {
067:
068: Copy copy = new Copy();
069:
070: FileSet fileSet = new FileSet();
071:
072: fileSet.setDir(source);
073:
074: if (Validator.isNotNull(includes)) {
075: fileSet.setIncludes(includes);
076: }
077:
078: if (Validator.isNotNull(excludes)) {
079: fileSet.setExcludes(excludes);
080: }
081:
082: copy.setProject(AntUtil.getProject());
083: copy.addFileset(fileSet);
084: copy.setTodir(destination);
085: copy.setOverwrite(overwrite);
086: copy.setPreserveLastModified(preserveLastModified);
087:
088: copy.execute();
089: }
090:
091: public static void copyFile(File sourceFile, File destinationDir,
092: boolean overwrite, boolean preserveLastModified) {
093:
094: copyFile(sourceFile, destinationDir, null, overwrite,
095: preserveLastModified);
096: }
097:
098: public static void copyFile(File sourceFile, File destinationDir,
099: Map filterMap, boolean overwrite,
100: 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 itr = filterMap.keySet().iterator();
119:
120: while (itr.hasNext()) {
121: String token = (String) itr.next();
122:
123: String replacement = (String) filterMap.get(token);
124:
125: filterSet.addFilter(token, replacement);
126: }
127: }
128:
129: copy.execute();
130: }
131:
132: }
|