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 java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.Reader;
26  import java.nio.charset.StandardCharsets;
27  import java.util.Collections;
28  import java.util.SortedSet;
29  
30  import org.apache.rat.api.Document;
31  import org.apache.rat.api.RatException;
32  import org.apache.rat.document.impl.FileDocument;
33  import org.apache.rat.report.IReportable;
34  import org.apache.rat.report.RatReport;
35  import org.apache.tools.ant.types.Resource;
36  import org.apache.tools.ant.types.ResourceCollection;
37  import org.apache.tools.ant.types.resources.FileResource;
38  
39  /**
40   * Implementation of IReportable that traverses over a resource collection
41   * internally.
42   */
43  class ResourceCollectionContainer implements IReportable {
44      private final ResourceCollection rc;
45  
46      ResourceCollectionContainer(ResourceCollection rc) {
47          this.rc = rc;
48      }
49  
50      @Override
51      public void run(RatReport report) throws RatException {
52          ResourceDocument document = null;
53          for (Resource r : rc) {
54              if (!r.isDirectory()) {
55                  document = new ResourceDocument(r);
56                  report.report(document);
57              }
58          }
59      }
60  
61      private static class ResourceDocument extends Document {
62  
63          private final Resource resource;
64  
65          private static String asName(Resource resource) {
66              return resource instanceof FileResource ?
67                      FileDocument.normalizeFileName(((FileResource) resource).getFile())
68                      : resource.getName();
69          }
70  
71  
72          private ResourceDocument(Resource resource) {
73              super(asName(resource));
74              this.resource = resource;
75          }
76  
77          @Override
78          public Reader reader() throws IOException {
79              final InputStream in = resource.getInputStream();
80              return new InputStreamReader(in, StandardCharsets.UTF_8);
81          }
82  
83          @Override
84          public boolean isDirectory() {
85              if (resource instanceof FileResource) {
86                  final FileResource fileResource = (FileResource) resource;
87                  final File file = fileResource.getFile();
88                  return file.isDirectory();
89              }
90              return false;
91          }
92  
93          @Override
94          public SortedSet<Document> listChildren() {
95              if (resource instanceof FileResource) {
96                  final FileResource fileResource = (FileResource) resource;
97                  return new FileDocument(fileResource.getFile()).listChildren();
98              }
99              return Collections.emptySortedSet();
100         }
101 
102         @Override
103         public InputStream inputStream() throws IOException {
104             return resource.getInputStream();
105         }
106     }
107 }