1 | package ise.antelope.tasks.typedefs.file;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.*;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Copyright 2003
|
---|
8 | *
|
---|
9 | * @version $Revision: 1.2 $
|
---|
10 | */
|
---|
11 | public class FileList implements FileOp {
|
---|
12 |
|
---|
13 | private String what = "files";
|
---|
14 | private String separator = ",";
|
---|
15 | private boolean includepath = true;
|
---|
16 |
|
---|
17 | public void setSeparator(String s) {
|
---|
18 | if (s != null)
|
---|
19 | separator = s;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public void setIncludepath(boolean b) {
|
---|
23 | includepath = b;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public void setWhat(String s) {
|
---|
27 | if (s == null || s.equals("files")) {
|
---|
28 | what = "files";
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | else if (s.equals("dirs")) {
|
---|
32 | what = "dirs";
|
---|
33 | return;
|
---|
34 | }
|
---|
35 | else if (s.equals("all")) {
|
---|
36 | what = "all";
|
---|
37 | return;
|
---|
38 | }
|
---|
39 | else
|
---|
40 | what = "files";
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Lists the files in a directory. Does not recurse. Does not
|
---|
45 | * list subdirectores. Only lists files in the directory.
|
---|
46 | *
|
---|
47 | * @param f a directory
|
---|
48 | * @return a list of files contained in the directory.
|
---|
49 | */
|
---|
50 | public String execute(File f) {
|
---|
51 | if (f == null)
|
---|
52 | throw new IllegalArgumentException("file cannot be null");
|
---|
53 | if (!f.isDirectory())
|
---|
54 | return f.toString();
|
---|
55 | List files = Arrays.asList(f.listFiles());
|
---|
56 | StringBuffer value = new StringBuffer();
|
---|
57 | for (Iterator it = files.iterator(); it.hasNext(); ) {
|
---|
58 | File file = (File)it.next();
|
---|
59 | if ( (what.equals("files") && file.isFile()) ||
|
---|
60 | (what.equals("dirs" ) && file.isDirectory()) ||
|
---|
61 | (what.equals("all" )) ) {
|
---|
62 | String filename = includepath ? file.getAbsolutePath() : file.getName();
|
---|
63 | value.append(filename);
|
---|
64 | if (it.hasNext())
|
---|
65 | value.append(separator);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return value.toString();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|