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 IElement 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                  // do nothing
55              } else {
56                  document.setResource(r);
57                  document.getMetaData().clear();
58                  report.report(document);
59              }
60          }
61      }
62      private class ResourceDocument implements Document {
63  
64          private Resource resource;
65          private final MetaData metaData = new MetaData();
66          
67          public Resource getResource() {
68              return resource;
69          }
70  
71          public void setResource(Resource resource) {
72              this.resource = resource;
73          }
74          
75          public Reader reader() throws IOException {
76              final InputStream in = resource.getInputStream();
77              final Reader result = new InputStreamReader(in);
78              return result;
79          }
80  
81          public String getName() {
82              // TODO: reconsider names
83              String result = null;
84              if (resource instanceof FileResource) {
85                  final FileResource fileResource = (FileResource) resource;
86                  final File file = fileResource.getFile();
87                  result = DocumentImplUtils.toName(file);
88              } else {
89                  result = resource.getName();
90              }
91              return result;
92          }
93  
94          public boolean isComposite() {
95              if (resource instanceof FileResource) {
96                  final FileResource fileResource = (FileResource) resource;
97                  final File file = fileResource.getFile();
98                  return DocumentImplUtils.isZip(file);
99              }
100             return false;
101         }
102         
103         public MetaData getMetaData() {
104             return metaData;
105         }
106         
107         public InputStream inputStream() throws IOException {
108             return resource.getInputStream();
109         }
110     }
111 }