1/*
2 * Copyright 2003 The Apache Software Foundation
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockito.cglib.transform;
17
18import java.io.File;
19import java.util.*;
20import org.apache.tools.ant.BuildException;
21import org.apache.tools.ant.DirectoryScanner;
22import org.apache.tools.ant.Project;
23import org.apache.tools.ant.Task;
24import org.apache.tools.ant.types.FileSet;
25
26abstract public class AbstractProcessTask extends Task {
27    private Vector filesets = new Vector();
28
29    public void addFileset(FileSet set) {
30        filesets.addElement(set);
31    }
32
33    protected Collection getFiles() {
34        Map fileMap = new HashMap();
35        Project p = getProject();
36        for (int i = 0; i < filesets.size(); i++) {
37            FileSet fs = (FileSet)filesets.elementAt(i);
38            DirectoryScanner ds = fs.getDirectoryScanner(p);
39            String[] srcFiles = ds.getIncludedFiles();
40            File dir = fs.getDir(p);
41            for (int j = 0; j < srcFiles.length; j++) {
42                 File src = new File(dir, srcFiles[j]);
43                 fileMap.put(src.getAbsolutePath(), src);
44            }
45        }
46        return fileMap.values();
47    }
48
49
50
51    public void execute() throws BuildException {
52        beforeExecute();
53        for (Iterator it = getFiles().iterator(); it.hasNext();) {
54            try {
55                processFile((File)it.next());
56            } catch (Exception e) {
57                 throw new BuildException(e);
58            }
59        }
60    }
61
62    protected void beforeExecute() throws BuildException { }
63    abstract protected void processFile(File file) throws Exception;
64}
65