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 Static Methods Instance Methods Concrete Methods Deprecated 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.FileSystem
addZipFile(Path zipFile)
Deprecated.UseaddZipFileWithContent(Path)
instead.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.MessageReporter
getReporter()
Returns the reporter for the file collection phase.static FileCollector
newCollector(LanguageVersionDiscoverer discoverer, MessageReporter reporter)
Internal API: please usePmdAnalysis.files()
instead of creating a collector yourself.FileCollector
newCollector(MessageReporter logger)
Returns a new collector using the configuration except for the logger.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
-
setRecursive
public void setRecursive(boolean collectFilesRecursively)
-
newCollector
@InternalApi public static FileCollector newCollector(LanguageVersionDiscoverer discoverer, MessageReporter reporter)
Internal API: please usePmdAnalysis.files()
instead of creating a collector yourself.
-
newCollector
@InternalApi public FileCollector newCollector(MessageReporter logger)
Returns a new collector using the configuration except for the logger.
-
getCollectedFiles
@InternalApi public List<TextFile> getCollectedFiles()
Returns an unmodifiable list of all files that have been collected.Internal: This might be unstable until PMD 7, but it's internal.
-
getReporter
@InternalApi public MessageReporter 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
-
addZipFile
@Deprecated public FileSystem addZipFile(Path zipFile)
Deprecated.UseaddZipFileWithContent(Path)
instead.Opens a zip file and returns a FileSystem for its contents, so it can be explored with thePath
API. You can then calladdFile(Path)
and such. The zip file is registered as a resource to close at the end of analysis.
-
addZipFileWithContent
@Experimental 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
-
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.
-
-