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

        @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.

      • 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​(String sourceContents,
                                     String pathId)
        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
      • 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 to addFile(Path) and other overloads using a Path.
        Parameters:
        charset - A charset
      • relativizeWith

        @Deprecated
        public void relativizeWith​(String prefix)
        Deprecated.
        Add a prefix that is used to relativize file paths as their display name. For instance, when adding a file /tmp/src/main/java/org/foo.java, and relativizing with /tmp/src/, the registered TextFile will have a path id of /tmp/src/main/java/org/foo.java, and a display name of main/java/org/foo.java.

        This only matters for files added from a Path object.

        Parameters:
        prefix - Prefix to relativize (if a directory, include a trailing slash)
      • relativizeWith

        public void relativizeWith​(Path path)
        Add a prefix that is used to relativize file paths as their display name. For instance, when adding a file /tmp/src/main/java/org/foo.java, and relativizing with /tmp/src/, the registered TextFile will have a path id of /tmp/src/main/java/org/foo.java, and a display name of main/java/org/foo.java.

        This only matters for files added from a Path object.

        Parameters:
        path - Path with which to relativize
      • 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.