Class 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 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 interface AutoCloseable
      • 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 file
        language - 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 using addFile(Path) on all regular files.
        Parameters:
        dir - Directory path
        Returns:
        True if the directory has been added
        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 to addFile(Path) and other overloads using a Path.
        Parameters:
        charset - A charset
      • setFileFilter

        public void setFileFilter​(Predicate<FileId> fileFilter)
        Sets an additional filter that is being called before adding the file to the list.
        Parameters:
        fileFilter - the filter should return true if the file should be collected and analyzed.
        Throws:
        NullPointerException - if fileFilter is null.
      • 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.