KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > builder > support > CtFolderFile


1 package spoon.support.builder.support;
2
3 import java.io.File JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 import spoon.support.builder.CtFile;
10 import spoon.support.builder.CtFolder;
11 import spoon.support.builder.FileFactory;
12
13 public class CtFolderFile implements CtFolder {
14
15     File JavaDoc file;
16
17     List JavaDoc<CtFile> files;
18
19     List JavaDoc<CtFolder> subFolder;
20
21     public CtFolderFile(File JavaDoc file) throws IOException JavaDoc {
22         super();
23         if (!file.isDirectory())
24             throw new IOException JavaDoc("Not a directory");
25         this.file = file;
26     }
27
28     public List JavaDoc<CtFile> getAllFile() {
29         List JavaDoc<CtFile> all = new ArrayList JavaDoc<CtFile>(getFile());
30         for (CtFolder f : getSubFolder()) {
31             all.addAll(f.getAllFile());
32         }
33         return all;
34     }
35
36     public List JavaDoc<CtFile> getFile() {
37         if (files == null) {
38             files = new ArrayList JavaDoc<CtFile>();
39             for (File JavaDoc f : file.listFiles()) {
40                 if (FileFactory.isFile(f))
41                     files.add(new CtFileFile(f));
42             }
43         }
44         return files;
45     }
46
47     public String JavaDoc getName() {
48         return file.getName();
49     }
50
51     public CtFolder getParent() {
52         try {
53             return FileFactory.createFolder(file.getParentFile());
54         } catch (FileNotFoundException JavaDoc e) {
55             e.printStackTrace();
56         }
57         return null;
58     }
59
60     public List JavaDoc<CtFolder> getSubFolder() {
61         if (subFolder == null) {
62             subFolder = new ArrayList JavaDoc<CtFolder>();
63             for (File JavaDoc f : file.listFiles()) {
64                 if (!FileFactory.isFile(f))
65                     try {
66                         subFolder.add(FileFactory.createFolder(f));
67                     } catch (FileNotFoundException JavaDoc e) {
68                         e.printStackTrace();
69                     }
70             }
71         }
72         return subFolder;
73     }
74
75     public boolean isFile() {
76         return false;
77     }
78
79     @Override JavaDoc
80     public String JavaDoc toString() {
81         return file.toString();
82     }
83
84     public List JavaDoc<CtFile> getAllJavaFile() {
85         List JavaDoc<CtFile> files = new ArrayList JavaDoc<CtFile>();
86         for (CtFile f : getFile())
87             if (f.isJava())
88                 files.add(f);
89         for (CtFolder fol : getSubFolder())
90             files.addAll(fol.getAllJavaFile());
91         return files;
92     }
93
94 }
95
Popular Tags