net.sourceforge.cobertura.util
Class FileFinder

java.lang.Object
  extended by net.sourceforge.cobertura.util.FileFinder

public class FileFinder
extends java.lang.Object

Maps source file names to existing files. After adding description of places files can be found in, it can be used to localize the files.

FileFinder supports two types of source files locations:

The difference between these two is that in case of the first you add all source files under the specified root directory, and in the second you add exactly one file. In both cases file to be found has to be located under subdirectory that maps to package definition provided with the source file name.

Author:
Jeremy Thomerson

Nested Class Summary
private  class FileFinder.JarZipFilter
          A filter that accepts files that end in .jar or .zip
 
Field Summary
private static org.apache.log4j.Logger LOGGER
           
private  java.util.Set sourceDirectories
           
private  java.util.Map sourceFilesMap
           
 
Constructor Summary
FileFinder()
           
 
Method Summary
 void addSourceDirectory(java.lang.String directory)
          Adds directory that is a root of sources.
 void addSourceFile(java.lang.String baseDir, java.lang.String file)
          Adds file by specifying root directory and relative path to the file in it.
private  java.lang.String getCorrectedPath(java.lang.String path)
           
 java.io.File getFileForSource(java.lang.String fileName)
          Maps source file name to existing file.
 Source getSource(java.lang.String fileName)
          Maps source file name to existing file or source archive.
 java.util.List getSourceDirectoryList()
          Returns a list with string for all source directories.
private  Source searchJarsForSource(java.lang.String fileName)
          Gets a BufferedReader for a file within a jar.
 java.lang.String toString()
          Returns string representation of FileFinder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOGGER

private static org.apache.log4j.Logger LOGGER

sourceDirectories

private java.util.Set sourceDirectories

sourceFilesMap

private java.util.Map sourceFilesMap
Constructor Detail

FileFinder

public FileFinder()
Method Detail

addSourceDirectory

public void addSourceDirectory(java.lang.String directory)
Adds directory that is a root of sources. A source file that is under this directory will be found if relative path to the file from root matches package name.

Example:

 fileFinder.addSourceDirectory( "C:/MyProject/src/main");
 fileFinder.addSourceDirectory( "C:/MyProject/src/test");
 
In path both / and \ can be used.

Parameters:
directory - The root of source files
Throws:
java.lang.NullPointerException - if directory is null

addSourceFile

public void addSourceFile(java.lang.String baseDir,
                          java.lang.String file)
Adds file by specifying root directory and relative path to the file in it. Adds exactly one file, relative path should match package that the source file is in, otherwise it will be not found later.

Example:

 fileFinder.addSourceFile( "C:/MyProject/src/main", "com/app/MyClass.java");
 fileFinder.addSourceFile( "C:/MyProject/src/test", "com/app/MyClassTest.java");
 
In paths both / and \ can be used.

Parameters:
baseDir - sources root directory
file - path to source file relative to baseDir
Throws:
java.lang.NullPointerException - if either baseDir or file is null

getFileForSource

public java.io.File getFileForSource(java.lang.String fileName)
                              throws java.io.IOException
Maps source file name to existing file. When mapping file name first values that were added with addSourceDirectory(java.lang.String) and later added with addSourceFile(java.lang.String, java.lang.String) are checked.

Parameters:
fileName - source file to be mapped
Returns:
existing file that maps to passed sourceFile
Throws:
java.io.IOException - if cannot map source file to existing file
java.lang.NullPointerException - if fileName is null

getSource

public Source getSource(java.lang.String fileName)
Maps source file name to existing file or source archive. When mapping file name first values that were added with addSourceDirectory(java.lang.String) and later added with addSourceFile(java.lang.String, java.lang.String) are checked.

Parameters:
fileName - source file to be mapped
Returns:
Source that maps to passed sourceFile or null if it can't be found
Throws:
java.lang.NullPointerException - if fileName is null

searchJarsForSource

private Source searchJarsForSource(java.lang.String fileName)
Gets a BufferedReader for a file within a jar.

Parameters:
fileName - source file to get an input stream for
Returns:
Source for existing file inside a jar that maps to passed sourceFile or null if cannot map source file to existing file

getSourceDirectoryList

public java.util.List getSourceDirectoryList()
Returns a list with string for all source directories. Example: [C:/MyProject/src/main,C:/MyProject/src/test]

Returns:
list with Strings for all source roots, or empty list if no source roots were specified

getCorrectedPath

private java.lang.String getCorrectedPath(java.lang.String path)

toString

public java.lang.String toString()
Returns string representation of FileFinder.

Overrides:
toString in class java.lang.Object