View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.util;
26  
27  import java.io.File;
28  import java.nio.charset.Charset;
29  import java.nio.file.Path;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.maven.project.MavenProject;
34  import org.eluder.coveralls.maven.plugin.source.DirectorySourceLoader;
35  import org.eluder.coveralls.maven.plugin.source.MultiSourceLoader;
36  import org.eluder.coveralls.maven.plugin.source.ScanSourceLoader;
37  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
38  
39  /**
40   * A factory for creating SourceLoader objects.
41   */
42  public class SourceLoaderFactory {
43  
44      /** The base dir. */
45      private final File baseDir;
46  
47      /** The project. */
48      private final MavenProject project;
49  
50      /** The source encoding. */
51      private final Charset sourceEncoding;
52  
53      /** The source directories. */
54      private List<File> sourceDirectories;
55  
56      /** The scan for sources. */
57      private boolean scanForSources;
58  
59      /**
60       * Instantiates a new source loader factory.
61       *
62       * @param baseDir
63       *            the base dir
64       * @param project
65       *            the project
66       * @param sourceEncoding
67       *            the source encoding
68       */
69      public SourceLoaderFactory(final File baseDir, final MavenProject project, final Charset sourceEncoding) {
70          this.baseDir = baseDir;
71          this.project = project;
72          this.sourceEncoding = sourceEncoding;
73      }
74  
75      /**
76       * With source directories.
77       *
78       * @param sourceDirectories
79       *            the source directories
80       *
81       * @return the source loader factory
82       */
83      public SourceLoaderFactory withSourceDirectories(final List<File> sourceDirectories) {
84          this.sourceDirectories = sourceDirectories;
85          return this;
86      }
87  
88      /**
89       * With scan for sources.
90       *
91       * @param scanForSources
92       *            the scan for sources
93       *
94       * @return the source loader factory
95       */
96      public SourceLoaderFactory withScanForSources(final boolean scanForSources) {
97          this.scanForSources = scanForSources;
98          return this;
99      }
100 
101     /**
102      * Creates a new SourceLoader object.
103      *
104      * @return the source loader
105      */
106     public SourceLoader createSourceLoader() {
107         final var multiSourceLoader = new MultiSourceLoader();
108         final List<File> directories = new ArrayList<>();
109         final var modules = new MavenProjectCollector(this.project).collect();
110         for (final MavenProject module : modules) {
111             for (final String sourceRoot : module.getCompileSourceRoots()) {
112                 final var sourceDirectory = Path.of(sourceRoot);
113                 directories.add(sourceDirectory.toFile());
114             }
115         }
116         if (this.sourceDirectories != null) {
117             directories.addAll(this.sourceDirectories);
118         }
119         for (final File directory : directories) {
120             if (directory.exists() && directory.isDirectory()) {
121                 final var moduleSourceLoader = new DirectorySourceLoader(this.baseDir, directory, this.sourceEncoding);
122                 multiSourceLoader.add(moduleSourceLoader);
123             }
124         }
125 
126         if (this.scanForSources) {
127             for (final File directory : directories) {
128                 if (directory.exists() && directory.isDirectory()) {
129                     final var scanSourceLoader = new ScanSourceLoader(this.baseDir, directory, this.sourceEncoding);
130                     multiSourceLoader.add(scanSourceLoader);
131                 }
132             }
133         }
134         return multiSourceLoader;
135     }
136 }