001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.DirectoryScanner;
027: import org.apache.tools.ant.Project;
028:
029: /**
030: * Copies a directory.
031: *
032: * @since Ant 1.1
033: *
034: * @deprecated The copydir task is deprecated since Ant 1.2. Use copy instead.
035: */
036:
037: public class Copydir extends MatchingTask {
038:
039: private File srcDir;
040: private File destDir;
041: private boolean filtering = false;
042: private boolean flatten = false;
043: private boolean forceOverwrite = false;
044: private Hashtable filecopyList = new Hashtable();
045:
046: /**
047: * The src attribute
048: *
049: * @param src the source file
050: */
051: public void setSrc(File src) {
052: srcDir = src;
053: }
054:
055: /**
056: * The dest attribute
057: *
058: * @param dest the destination file
059: */
060: public void setDest(File dest) {
061: destDir = dest;
062: }
063:
064: /**
065: * The filtering attribute.
066: * Default is false.
067: * @param filter if true use filtering
068: */
069: public void setFiltering(boolean filter) {
070: filtering = filter;
071: }
072:
073: /**
074: * The flattening attribute.
075: * Default is false.
076: * @param flatten if true use flattening
077: */
078: public void setFlatten(boolean flatten) {
079: this .flatten = flatten;
080: }
081:
082: /**
083: * The forceoverwrite attribute.
084: * Default is false.
085: * @param force if true overwrite even if the destination file
086: * is newer that the source file
087: */
088: public void setForceoverwrite(boolean force) {
089: forceOverwrite = force;
090: }
091:
092: /**
093: * Execute the task.
094: * @throws BuildException on error
095: */
096: public void execute() throws BuildException {
097: log("DEPRECATED - The copydir task is deprecated. Use copy instead.");
098:
099: if (srcDir == null) {
100: throw new BuildException("src attribute must be set!",
101: getLocation());
102: }
103:
104: if (!srcDir.exists()) {
105: throw new BuildException("srcdir " + srcDir.toString()
106: + " does not exist!", getLocation());
107: }
108:
109: if (destDir == null) {
110: throw new BuildException("The dest attribute must be set.",
111: getLocation());
112: }
113:
114: if (srcDir.equals(destDir)) {
115: log("Warning: src == dest", Project.MSG_WARN);
116: }
117:
118: DirectoryScanner ds = super .getDirectoryScanner(srcDir);
119:
120: try {
121: String[] files = ds.getIncludedFiles();
122: scanDir(srcDir, destDir, files);
123: if (filecopyList.size() > 0) {
124: log("Copying " + filecopyList.size() + " file"
125: + (filecopyList.size() == 1 ? "" : "s")
126: + " to " + destDir.getAbsolutePath());
127: Enumeration e = filecopyList.keys();
128: while (e.hasMoreElements()) {
129: String fromFile = (String) e.nextElement();
130: String toFile = (String) filecopyList.get(fromFile);
131: try {
132: getProject().copyFile(fromFile, toFile,
133: filtering, forceOverwrite);
134: } catch (IOException ioe) {
135: String msg = "Failed to copy " + fromFile
136: + " to " + toFile + " due to "
137: + ioe.getMessage();
138: throw new BuildException(msg, ioe,
139: getLocation());
140: }
141: }
142: }
143: } finally {
144: filecopyList.clear();
145: }
146: }
147:
148: private void scanDir(File from, File to, String[] files) {
149: for (int i = 0; i < files.length; i++) {
150: String filename = files[i];
151: File srcFile = new File(from, filename);
152: File destFile;
153: if (flatten) {
154: destFile = new File(to, new File(filename).getName());
155: } else {
156: destFile = new File(to, filename);
157: }
158: if (forceOverwrite
159: || (srcFile.lastModified() > destFile
160: .lastModified())) {
161: filecopyList.put(srcFile.getAbsolutePath(), destFile
162: .getAbsolutePath());
163: }
164: }
165: }
166: }
|