Class FileCollector
- java.lang.Object
-
- net.sourceforge.pmd.lang.document.FileCollector
-
- All Implemented Interfaces:
AutoCloseable
public final class FileCollector extends Object implements AutoCloseable
Collects files to analyse before a PMD run. This API allows opening zip files and makes sure they will be closed at the end of a run.- Author:
- Clément Fournier
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
absorb(FileCollector otherCollector)
Add all files collected in the other collector into this one.boolean
addDirectory(Path dir)
Add a directory recursively usingaddFile(Path)
on all regular files.boolean
addDirectory(Path dir, boolean recurse)
boolean
addFile(Path file)
Add a file, language is determined automatically from the extension/file patterns.boolean
addFile(Path file, Language language)
Add a file with the given language (which overrides the file patterns).boolean
addFile(TextFile textFile)
Add a pre-configured text file.boolean
addFileOrDirectory(Path file)
Add a file or directory recursively.boolean
addFileOrDirectory(Path file, boolean recurseIfDirectory)
Add a file or directory recursively.boolean
addSourceFile(FileId fileId, String sourceContents)
Add a text file given its contents and a name.boolean
addZipFileWithContent(Path zipFile)
Opens a zip file and adds all files of the zip file to the list of files to be processed.void
close()
Close registered resources like zip files.void
exclude(FileCollector excludeCollector)
Remove all files collected by the given collector from this one.void
filterLanguages(Set<Language> languages)
Exclude all collected files whose language is not part of the given collection.List<TextFile>
getCollectedFiles()
Returns an unmodifiable list of all files that have been collected.PmdReporter
getReporter()
Returns the reporter for the file collection phase.void
setCharset(Charset charset)
Sets the charset to use for subsequent calls toaddFile(Path)
and other overloads using aPath
.void
setRecursive(boolean collectFilesRecursively)
String
toString()
-
-
-
Method Detail
-
getCollectedFiles
public List<TextFile> getCollectedFiles()
Returns an unmodifiable list of all files that have been collected.- Throws:
IllegalStateException
- if the collector was already closed
-
getReporter
public PmdReporter getReporter()
Returns the reporter for the file collection phase.
-
close
public void close()
Close registered resources like zip files.- Specified by:
close
in interfaceAutoCloseable
-
addFile
public boolean addFile(Path file)
Add a file, language is determined automatically from the extension/file patterns. The encoding is the current encoding (setCharset(Charset)
).- Parameters:
file
- File to add- Returns:
- True if the file has been added
-
addFile
public boolean addFile(Path file, Language language)
Add a file with the given language (which overrides the file patterns). The encoding is the current encoding (setCharset(Charset)
).- Parameters:
file
- Path to a filelanguage
- A language. The language version will be taken to be the contextual default version.- Returns:
- True if the file has been added
-
addFile
public boolean addFile(TextFile textFile)
Add a pre-configured text file. The language version will be checked to match the contextual default for the language (the file cannot be added if it has a different version).- Returns:
- True if the file has been added
-
addSourceFile
public boolean addSourceFile(FileId fileId, String sourceContents)
Add a text file given its contents and a name. The language version will be determined from the name as usual.- Returns:
- True if the file has been added
-
addDirectory
public boolean addDirectory(Path dir) throws IOException
Add a directory recursively usingaddFile(Path)
on all regular files.- Parameters:
dir
- Directory path- Returns:
- True if the directory has been added
- Throws:
IOException
-
addDirectory
public boolean addDirectory(Path dir, boolean recurse) throws IOException
- Throws:
IOException
-
addFileOrDirectory
public boolean addFileOrDirectory(Path file) throws IOException
Add a file or directory recursively. Language is determined automatically from the extension/file patterns.- Returns:
- True if the file or directory has been added
- Throws:
IOException
-
addFileOrDirectory
public boolean addFileOrDirectory(Path file, boolean recurseIfDirectory) throws IOException
Add a file or directory recursively. Language is determined automatically from the extension/file patterns.- Returns:
- True if the file or directory has been added
- Throws:
IOException
-
addZipFileWithContent
public boolean addZipFileWithContent(Path zipFile) throws IOException
Opens a zip file and adds all files of the zip file to the list of files to be processed.The zip file is registered as a resource to close at the end of analysis.
- Returns:
- True if the zip file including its content has been added without errors
- Throws:
IOException
-
setRecursive
public void setRecursive(boolean collectFilesRecursively)
-
setCharset
public void setCharset(Charset charset)
Sets the charset to use for subsequent calls toaddFile(Path)
and other overloads using aPath
.- Parameters:
charset
- A charset
-
exclude
public void exclude(FileCollector excludeCollector)
Remove all files collected by the given collector from this one.
-
absorb
public void absorb(FileCollector otherCollector)
Add all files collected in the other collector into this one. Transfers resources to close as well. The parameter is left empty.
-
filterLanguages
public void filterLanguages(Set<Language> languages)
Exclude all collected files whose language is not part of the given collection.
-
-