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 (Iterator<?> iter = rc.iterator(); iter.hasNext(); ) {
52              Resource r = (Resource) iter.next();
53              if (!r.isDirectory()) {
54                  document.setResource(r);
55                  document.getMetaData().clear();
56                  report.report(document);
57              }
58          }
59      }
60      private 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          public Reader reader() throws IOException {
70              final InputStream in = resource.getInputStream();
71              final Reader result = new InputStreamReader(in);
72              return result;
73          }
74  
75          public String getName() {
76              // TODO: reconsider names
77              String result = null;
78              if (resource instanceof FileResource) {
79                  final FileResource fileResource = (FileResource) resource;
80                  final File file = fileResource.getFile();
81                  result = DocumentImplUtils.toName(file);
82              } else {
83                  result = resource.getName();
84              }
85              return result;
86          }
87  
88          public boolean isComposite() {
89              if (resource instanceof FileResource) {
90                  final FileResource fileResource = (FileResource) resource;
91                  final File file = fileResource.getFile();
92                  return DocumentImplUtils.isZip(file);
93              }
94              return false;
95          }
96          
97          public MetaData getMetaData() {
98              return metaData;
99          }
100         
101         public InputStream inputStream() throws IOException {
102             return resource.getInputStream();
103         }
104     }
105 }