1 | package org.greenstone.anttasks;
|
---|
2 |
|
---|
3 | import java.io.BufferedWriter;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileWriter;
|
---|
6 | import java.util.ArrayList;
|
---|
7 |
|
---|
8 | import org.apache.tools.ant.BuildException;
|
---|
9 | import org.apache.tools.ant.DirectoryScanner;
|
---|
10 | import org.apache.tools.ant.Task;
|
---|
11 | import org.apache.tools.ant.types.FileSet;
|
---|
12 | import org.apache.tools.ant.types.PatternSet;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * PatternSetToFile is an Ant task used to take an Ant PatternSet and output to a
|
---|
16 | * file all the files that it encompasses
|
---|
17 | * @author Sam McIntosh
|
---|
18 | */
|
---|
19 | public class PatternSetToFile extends Task
|
---|
20 | {
|
---|
21 | //Sets whether or not only the top-level files and folders
|
---|
22 | //are stored in the output file
|
---|
23 | public boolean _toplevelonly = false;
|
---|
24 |
|
---|
25 | //The file the set will be written to
|
---|
26 | public File _outfile = null;
|
---|
27 |
|
---|
28 | //The directory to check the PatternSet within
|
---|
29 | public File _dir = null;
|
---|
30 |
|
---|
31 | //An array of the child PatternSets of this task
|
---|
32 | public ArrayList _sets = new ArrayList();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * This function performs the main task
|
---|
36 | */
|
---|
37 | public void execute() throws BuildException
|
---|
38 | {
|
---|
39 | try
|
---|
40 | {
|
---|
41 | System.out.println("Creating " + _outfile.getAbsolutePath());
|
---|
42 | BufferedWriter out = new BufferedWriter(new FileWriter(_outfile));
|
---|
43 | for(int i = 0; i < _sets.size(); i++)
|
---|
44 | {
|
---|
45 | PatternSet set = (PatternSet)_sets.get(i);
|
---|
46 | FileSet fs = new FileSet();
|
---|
47 | fs.setDir(_dir);
|
---|
48 | PatternSet ps = fs.createPatternSet();
|
---|
49 | ps.append(set, this.getProject());
|
---|
50 |
|
---|
51 | DirectoryScanner ds = fs.getDirectoryScanner(this.getProject());
|
---|
52 |
|
---|
53 | String[] includedFiles = ds.getIncludedFiles();
|
---|
54 |
|
---|
55 | if(_toplevelonly)
|
---|
56 | {
|
---|
57 | ArrayList toplevel = new ArrayList();
|
---|
58 |
|
---|
59 | for(int j = 0; j < includedFiles.length; j++)
|
---|
60 | {
|
---|
61 | int separatorIndex = includedFiles[j].indexOf(File.separator);
|
---|
62 |
|
---|
63 | if ( separatorIndex == -1 && includedFiles[j].length() > 0 && !toplevel.contains(toplevel)) {
|
---|
64 | toplevel.add(includedFiles[j]);
|
---|
65 | }
|
---|
66 | else if ( separatorIndex > -1 && includedFiles[j].length() > 0) {
|
---|
67 | String path = includedFiles[j].substring(0, separatorIndex);
|
---|
68 | if ( !toplevel.contains(path) ) {
|
---|
69 | toplevel.add(path);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | includedFiles = (String[])toplevel.toArray(new String[0]);
|
---|
74 | }
|
---|
75 |
|
---|
76 | for (int j = 0; j < includedFiles.length; j++)
|
---|
77 | {
|
---|
78 | out.write(includedFiles[j] + "\n");
|
---|
79 | }
|
---|
80 | }
|
---|
81 | out.close();
|
---|
82 | }
|
---|
83 | catch(Exception ex)
|
---|
84 | {
|
---|
85 | ex.printStackTrace();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void setToplevelonly(boolean toplevelonly)
|
---|
90 | {
|
---|
91 | _toplevelonly = toplevelonly;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void setOutfile(File outfile)
|
---|
95 | {
|
---|
96 | _outfile = outfile;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void setDir(File dir)
|
---|
100 | {
|
---|
101 | _dir = dir;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void addConfiguredPatternset(PatternSet patternset)
|
---|
105 | {
|
---|
106 | if (patternset == null)
|
---|
107 | {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | _sets.add(patternset);
|
---|
112 | }
|
---|
113 | }
|
---|