source: main/trunk/ant-tasks/src/org/greenstone/anttasks/PatternSetToFile.java@ 23242

Last change on this file since 23242 was 23242, checked in by sjm84, 13 years ago

A few minor updates to the uninstaller

File size: 2.8 KB
Line 
1package org.greenstone.anttasks;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.util.ArrayList;
7
8import org.apache.tools.ant.BuildException;
9import org.apache.tools.ant.DirectoryScanner;
10import org.apache.tools.ant.Task;
11import org.apache.tools.ant.types.FileSet;
12import 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 */
19public 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}
Note: See TracBrowser for help on using the repository browser.