1 19 20 package edu.umd.cs.findbugs.classfile.impl; 21 22 import java.io.BufferedInputStream ; 23 import java.io.File ; 24 import java.io.FileFilter ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 32 import edu.umd.cs.findbugs.RecursiveFileSearch; 33 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry; 34 import edu.umd.cs.findbugs.classfile.ICodeBaseIterator; 35 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator; 36 import edu.umd.cs.findbugs.classfile.IScannableCodeBase; 37 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException; 38 39 45 public class DirectoryCodeBase extends AbstractScannableCodeBase implements IScannableCodeBase { 46 private class DirectoryCodeBaseIterator implements ICodeBaseIterator { 47 48 Iterator <String > fileNameIterator = rfs.fileNameIterator(); 49 50 53 public boolean hasNext() throws InterruptedException { 54 return fileNameIterator.hasNext(); 55 } 56 57 60 public ICodeBaseEntry next() throws InterruptedException { 61 final String fileName = fileNameIterator.next(); 62 63 String resourceName = getResourceName(fileName); 65 66 File file = new File (fileName); 68 long modTime = file.lastModified(); 69 if (modTime > 0 && modTime > getLastModifiedTime()) { 70 setLastModifiedTime(modTime); 71 } 72 73 return new DirectoryCodeBaseEntry(DirectoryCodeBase.this, resourceName); 74 } 75 } 76 77 private File directory; 78 private RecursiveFileSearch rfs; 79 private boolean searchPerformed; 80 81 87 public DirectoryCodeBase(ICodeBaseLocator codeBaseLocator, File directory) { 88 super(codeBaseLocator); 89 if (!directory.isDirectory()) throw new IllegalArgumentException (); 90 this.directory = directory; 91 this.rfs = new RecursiveFileSearch(directory.getPath(), new FileFilter (){ 92 95 public boolean accept(File pathname) { 96 return true; 97 } 98 }); 99 this.searchPerformed = false; 100 } 101 102 105 public ICodeBaseIterator iterator() throws InterruptedException { 106 if (!searchPerformed) { 107 rfs.search(); 108 searchPerformed = true; 109 } 110 111 return new DirectoryCodeBaseIterator(); 112 } 113 114 117 public String getPathName() { 118 return directory.getPath(); 119 } 120 121 124 public void close() { 125 } 127 128 131 public ICodeBaseEntry lookupResource(String resourceName) throws ResourceNotFoundException { 132 resourceName = translateResourceName(resourceName); 136 137 File file = getFullPathOfResource(resourceName); 138 if (!file.exists()) { 139 throw new ResourceNotFoundException(resourceName); 140 } 141 return new DirectoryCodeBaseEntry(this, resourceName); 142 } 143 144 InputStream openFile(String resourceName) throws FileNotFoundException , IOException { 145 File path = getFullPathOfResource(resourceName); 146 return new BufferedInputStream (new FileInputStream (path)); 147 } 148 149 155 File getFullPathOfResource(String resourceName) { 156 return new File (directory, resourceName); 157 } 158 159 165 String getResourceName(String fileName) { 166 168 String dirPath = directory.getPath(); 170 if (!fileName.startsWith(dirPath)) { 171 throw new IllegalStateException ("Filename " + fileName + " not inside directory "+ dirPath); 172 } 173 174 179 String relativeFileName = fileName.substring(dirPath.length()); 180 File file = new File (relativeFileName); 181 LinkedList <String > partList = new LinkedList <String >(); 182 do { 183 partList.addFirst(file.getName()); 184 } while ((file = file.getParentFile()) != null); 185 186 StringBuffer buf = new StringBuffer (); 187 for (String part : partList) { 188 if (buf.length() > 0) { 189 buf.append('/'); 190 } 191 buf.append(part); 192 } 193 194 return buf.toString(); 195 } 196 197 200 @Override 201 public String toString() { 202 return directory.getPath(); 203 } 204 } 205 | Popular Tags |