001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.builder.support;
019:
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import spoon.support.builder.CtFile;
027: import spoon.support.builder.CtFolder;
028: import spoon.support.builder.FileFactory;
029:
030: public class CtFolderFile implements CtFolder {
031:
032: File file;
033:
034: List<CtFile> files;
035:
036: List<CtFolder> subFolders;
037:
038: public CtFolderFile(File file) throws IOException {
039: super ();
040: if (!file.isDirectory())
041: throw new IOException("Not a directory");
042: this .file = file;
043: }
044:
045: public List<CtFile> getAllFiles() {
046: List<CtFile> all = new ArrayList<CtFile>(getFiles());
047: for (CtFolder f : getSubFolder()) {
048: all.addAll(f.getAllFiles());
049: }
050: return all;
051: }
052:
053: public List<CtFile> getFiles() {
054: if (files == null) {
055: files = new ArrayList<CtFile>();
056: for (File f : file.listFiles()) {
057: if (FileFactory.isFile(f))
058: files.add(new CtFileFile(f));
059: }
060: }
061: return files;
062: }
063:
064: public String getName() {
065: return file.getName();
066: }
067:
068: public CtFolder getParent() {
069: try {
070: return FileFactory.createFolder(file.getParentFile());
071: } catch (FileNotFoundException e) {
072: e.printStackTrace();
073: }
074: return null;
075: }
076:
077: public List<CtFolder> getSubFolder() {
078: if (subFolders == null) {
079: subFolders = new ArrayList<CtFolder>();
080: for (File f : file.listFiles()) {
081: if (!FileFactory.isFile(f))
082: try {
083: subFolders.add(FileFactory.createFolder(f));
084: } catch (FileNotFoundException e) {
085: e.printStackTrace();
086: }
087: }
088: }
089: return subFolders;
090: }
091:
092: public boolean isFile() {
093: return false;
094: }
095:
096: @Override
097: public String toString() {
098: return file.toString();
099: }
100:
101: public List<CtFile> getAllJavaFiles() {
102: List<CtFile> files = new ArrayList<CtFile>();
103: for (CtFile f : getFiles())
104: if (f.isJava())
105: files.add(f);
106: for (CtFolder fol : getSubFolder())
107: files.addAll(fol.getAllJavaFiles());
108: return files;
109: }
110:
111: public String getPath() {
112: return toString();
113: }
114:
115: }
|