1 22 package net.sourceforge.cobertura.util; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import org.apache.log4j.Logger; 35 36 37 56 public class FileFinder { 57 58 private static Logger LOGGER = Logger.getLogger(FileFinder.class); 59 60 private Set sourceDirectories = new HashSet (); 62 63 private Map sourceFilesMap = new HashMap (); 65 66 82 public void addSourceDirectory( String directory) { 83 if( LOGGER.isDebugEnabled()) 84 LOGGER.debug( "Adding sourceDirectory=[" + directory + "]"); 85 86 directory = getCorrectedPath(directory); 88 sourceDirectories.add(directory); 89 } 90 91 109 public void addSourceFile( String baseDir, String file) { 110 if( LOGGER.isDebugEnabled()) 111 LOGGER.debug( "Adding sourceFile baseDir=[" + baseDir + "] file=[" + file + "]"); 112 113 if( baseDir==null || file==null) 114 throw new NullPointerException (); 115 116 file = getCorrectedPath( file); 118 baseDir = getCorrectedPath( baseDir); 119 120 Set container = (Set ) sourceFilesMap.get(baseDir); 122 if( container==null) { 123 container = new HashSet (); 124 sourceFilesMap.put( baseDir, container); 125 } 126 container.add( file); 127 } 128 129 139 public File getFileForSource(String fileName) throws IOException { 140 if( LOGGER.isDebugEnabled()) 142 LOGGER.debug( "Searching for file, name=[" + fileName + "]"); 143 fileName = getCorrectedPath( fileName); 144 145 for( Iterator it=sourceDirectories.iterator(); it.hasNext();) { 147 String directory = (String )it.next(); 148 File file = new File ( directory, fileName); 149 if( file.isFile()) { 150 LOGGER.debug( "Found inside sourceDirectories"); 151 return file; 152 } 153 } 154 155 for( Iterator it=sourceFilesMap.keySet().iterator(); it.hasNext();) { 157 String directory = (String )it.next(); 158 Set container = (Set ) sourceFilesMap.get(directory); 159 if( !container.contains( fileName)) 160 continue; 161 File file = new File ( directory, fileName); 162 if( file.isFile()) { 163 LOGGER.debug( "Found inside sourceFilesMap"); 164 return file; 165 } 166 } 167 168 LOGGER.debug( "File not found"); 170 throw new IOException ( "Cannot find source file, name=["+fileName+"]"); 171 } 172 173 179 public List getSourceDirectoryList() { 180 List result = new ArrayList (); 182 for( Iterator it=sourceDirectories.iterator(); it.hasNext();) { 183 result.add( it.next()); 184 } 185 186 for( Iterator it=sourceFilesMap.keySet().iterator(); it.hasNext();) { 188 result.add(it.next()); 189 } 190 191 return result; 193 } 194 195 private String getCorrectedPath(String path) { 196 return path.replace('\\', '/'); 197 } 198 199 202 public String toString() { 203 return "FileFinder, source directories: " + getSourceDirectoryList().toString(); 204 } 205 } 206 | Popular Tags |