View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package dev.aherscu.qa.s3.publisher.maven.plugin.config;
18  
19  import java.io.*;
20  import java.util.*;
21  
22  public class IncludedFilesListBuilder extends AbstractFilesListBuilder {
23  
24      private final List<Include>               includes;
25      private final ConstrainedFilesListBuilder constrainedFilesListBuilder;
26  
27      public IncludedFilesListBuilder(final File inputDirectory,
28          final List<Include> includes,
29          final List<String> excludes, final List<Metadata> metadatas) {
30          super(inputDirectory, excludes, metadatas);
31          this.includes = includes;
32          constrainedFilesListBuilder = new ConstrainedFilesListBuilder(
33              inputDirectory, excludes, metadatas);
34      }
35  
36      public List<ManagedFile> build() throws IOException {
37          for (final Include include : includes) {
38              collectIncludedFiles(include);
39              replaceConstrainedFiles(include);
40          }
41          return new ArrayList<>(fileMap.values());
42      }
43  
44      private void collectIncludedFiles(final Include include)
45          throws IOException {
46          final List<String> includedFileNames =
47              getMatchingFilesNames(include.getBind().getPattern());
48          for (final String fileName : includedFileNames) {
49              final ManagedFile managedFile =
50                  new ManagedFile(fileName, getMetadata(include.getBind()));
51              fileMap.put(fileName, managedFile);
52          }
53      }
54  
55      private void replaceConstrainedFiles(final Include include)
56          throws IOException {
57          final List<ManagedFile> constrainedFiles =
58              constrainedFilesListBuilder.build(include);
59          for (final ManagedFile managedFile : constrainedFiles) {
60              fileMap.put(managedFile.getFilename(), managedFile);
61          }
62      }
63  }