01: /*
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use, modify
07: * and/or redistribute the software under the terms of the CeCILL-C license as
08: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13: *
14: * The fact that you are presently reading this means that you have had
15: * knowledge of the CeCILL-C license and that you accept its terms.
16: */
17:
18: package spoon.support.builder;
19:
20: import java.io.File;
21: import java.io.FileNotFoundException;
22: import java.io.IOException;
23:
24: import spoon.support.builder.support.CtFileFile;
25: import spoon.support.builder.support.CtFolderFile;
26: import spoon.support.builder.support.CtFolderZip;
27:
28: public class FileFactory {
29: public static boolean isArchive(File f) {
30: return f.getName().endsWith(".jar")
31: || f.getName().endsWith(".zip");
32: }
33:
34: public static boolean isFile(File f) {
35: return f.isFile() && !isArchive(f);
36: }
37:
38: public static CtFile createFile(File f)
39: throws FileNotFoundException {
40: if (!f.exists()) {
41: throw new FileNotFoundException(f.toString());
42: }
43: return new CtFileFile(f);
44: }
45:
46: public static CtResource createResource(File f)
47: throws FileNotFoundException {
48: if (f.isFile()) {
49: return createFile(f);
50: }
51: return createFolder(f);
52: }
53:
54: public static CtFolder createFolder(File f)
55: throws FileNotFoundException {
56: if (!f.exists()) {
57: throw new FileNotFoundException(f.toString()
58: + " does not exist");
59: }
60: try {
61: if (f.isDirectory()) {
62: return new CtFolderFile(f);
63: }
64: if (isArchive(f)) {
65: return new CtFolderZip(f);
66: }
67: } catch (IOException e) {
68: e.printStackTrace();
69: }
70:
71: return null;
72: }
73:
74: }
|