1 4 package com.tc.test; 5 6 import org.apache.commons.io.FileUtils; 7 8 import com.tc.util.Assert; 9 10 import java.io.File ; 11 import java.io.FileNotFoundException ; 12 import java.io.IOException ; 13 14 17 public class ClassBasedDirectoryTree { 18 19 private final File root; 20 21 public ClassBasedDirectoryTree(File root) throws FileNotFoundException { 22 Assert.assertNotNull(root); 23 if ((!root.exists()) || (!root.isDirectory())) throw new FileNotFoundException ( 24 "Root '" 25 + root 26 .getAbsolutePath() 27 + "' does not exist, or is not a directory.."); 28 this.root = root; 29 } 30 31 public File getDirectory(Class theClass) throws IOException { 32 String [] parts = theClass.getName().split("\\."); 33 File destFile = buildFile(this.root, parts, 0); 34 35 if (destFile.exists() && (!destFile.isDirectory())) throw new FileNotFoundException ( 36 "'" 37 + destFile 38 + "' exists, but is not a directory."); 39 return destFile; 40 } 41 42 public File getOrMakeDirectory(Class theClass) throws IOException { 43 File destFile = getDirectory(theClass); 44 if (!destFile.exists()) FileUtils.forceMkdir(destFile); 45 return destFile; 46 } 47 48 private File buildFile(File base, String [] parts, int startWhere) { 49 if (startWhere >= parts.length) return base; 50 else return buildFile(new File (base, parts[startWhere]), parts, startWhere + 1); 51 } 52 53 } | Popular Tags |