KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.support.builder;
2
3 import java.io.File JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.IOException JavaDoc;
6
7 import spoon.support.builder.support.CtFileFile;
8 import spoon.support.builder.support.CtFolderFile;
9 import spoon.support.builder.support.CtFolderZip;
10
11 public class FileFactory {
12     public static boolean isArchive(File JavaDoc f) {
13         return f.getName().endsWith(".jar") || f.getName().endsWith(".zip");
14     }
15
16     public static boolean isFile(File JavaDoc f) {
17         return f.isFile() && !isArchive(f);
18     }
19
20     public static CtFile createFile(File JavaDoc f) throws FileNotFoundException JavaDoc {
21         if (!f.exists())
22             throw new FileNotFoundException JavaDoc(f.toString());
23         return new CtFileFile(f);
24     }
25
26     public static CtResource createResource(File JavaDoc f)
27             throws FileNotFoundException JavaDoc {
28         if (f.isFile())
29             return createFile(f);
30         else
31             return createFolder(f);
32     }
33
34     public static CtFolder createFolder(File JavaDoc f) throws FileNotFoundException JavaDoc {
35         if (!f.exists())
36             throw new FileNotFoundException JavaDoc(f.toString() + " not exist");
37         try {
38             if (f.isDirectory())
39                 return new CtFolderFile(f);
40             if (isArchive(f))
41                 return new CtFolderZip(f);
42         } catch (IOException JavaDoc e) {
43             e.printStackTrace();
44         }
45
46         return null;
47     }
48
49 }
50
Popular Tags