View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.anttasks;
20  
21  import org.apache.rat.ReportConfiguration;
22  import org.apache.rat.api.Document;
23  import org.apache.rat.api.RatException;
24  import org.apache.rat.document.DocumentName;
25  import org.apache.rat.document.DocumentNameMatcher;
26  import org.apache.rat.document.FileDocument;
27  import org.apache.rat.document.IgnoredDocument;
28  import org.apache.rat.report.RatReport;
29  import org.apache.rat.report.Reportable;
30  import org.apache.tools.ant.types.Resource;
31  import org.apache.tools.ant.types.ResourceCollection;
32  import org.apache.tools.ant.types.resources.FileResource;
33  
34  /**
35   * Implementation of Reportable that traverses over a resource collection
36   * internally.
37   */
38  class ResourceCollectionContainer implements Reportable {
39      /** The resources as collected by Ant. */
40      private final ResourceCollection resources;
41      /** The report configuration being used for the report. */
42      private final ReportConfiguration configuration;
43      /** The document name. */
44      private final DocumentName name;
45  
46      ResourceCollectionContainer(final DocumentName name, final ReportConfiguration configuration, final ResourceCollection resources) {
47          this.resources = resources;
48          this.configuration = configuration;
49          this.name = name;
50      }
51  
52      @Override
53      public void run(final RatReport report) throws RatException {
54          for (Resource r : resources) {
55              if (r.isFilesystemOnly()) {
56                  FileResource fr = (FileResource) r;
57                  DocumentName dirName = DocumentName.builder(fr.getFile()).setBaseName(fr.getProject().getBaseDir()).build();
58                  DocumentNameMatcher excluder = configuration.getDocumentExcluder(dirName);
59                  // unlike a directory walk, where DirectoryWalker#listChildren() applies the excluder to every
60                  // child it finds, a resource collection hands RAT files it already resolved itself, so nothing
61                  // else ever checks them against the excluder (RAT-569); do it here, before reporting.
62                  Document document = excluder.matches(dirName)
63                          ? new FileDocument(dirName, fr.getFile(), excluder)
64                          : new IgnoredDocument(dirName, fr.getFile(), excluder);
65                  report.report(document);
66              }
67          }
68      }
69  
70      @Override
71      public DocumentName name() {
72          return name;
73      }
74  }