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