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