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