View Javadoc
1   package org.apache.rat.mp;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.io.Reader;
8   import java.nio.file.Files;
9   
10  /*
11   * Licensed to the Apache Software Foundation (ASF) under one
12   * or more contributor license agreements.  See the NOTICE file
13   * distributed with this work for additional information
14   * regarding copyright ownership.  The ASF licenses this file
15   * to you under the Apache License, Version 2.0 (the
16   * "License"); you may not use this file except in compliance
17   * with the License.  You may obtain a copy of the License at
18   *
19   *   http://www.apache.org/licenses/LICENSE-2.0
20   *
21   * Unless required by applicable law or agreed to in writing,
22   * software distributed under the License is distributed on an
23   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24   * KIND, either express or implied.  See the License for the
25   * specific language governing permissions and limitations
26   * under the License.
27   */
28  
29  import org.apache.rat.api.Document;
30  import org.apache.rat.api.MetaData;
31  import org.apache.rat.api.RatException;
32  import org.apache.rat.document.impl.DocumentImplUtils;
33  import org.apache.rat.report.IReportable;
34  import org.apache.rat.report.RatReport;
35  
36  /**
37   * Implementation of IReportable that traverses over a set of files.
38   */
39  class FilesReportable implements IReportable {
40      private final File basedir;
41  
42      private final String[] files;
43  
44      FilesReportable(File basedir, String[] files) throws IOException {
45          final File currentDir = new File(System.getProperty("user.dir")).getCanonicalFile();
46          final File f = basedir.getCanonicalFile();
47          if (currentDir.equals(f)) {
48              this.basedir = null;
49          } else {
50              this.basedir = basedir;
51          }
52          this.files = files;
53      }
54  
55      @Override
56      public void run(RatReport report) throws RatException {
57          FileDocument document = new FileDocument();
58          for (String file : files) {
59              document.setFile(new File(basedir, file));
60              document.getMetaData().clear();
61              report.report(document);
62          }
63      }
64  
65      private static class FileDocument implements Document {
66          private File file;
67          private final MetaData metaData = new MetaData();
68  
69          void setFile(File file) {
70              this.file = file;
71          }
72  
73          @Override
74          public boolean isComposite() {
75              return DocumentImplUtils.isZip(file);
76          }
77  
78          @Override
79          public Reader reader() throws IOException {
80              final InputStream in = Files.newInputStream(file.toPath());
81              return new InputStreamReader(in);
82          }
83  
84          @Override
85          public String getName() {
86              return DocumentImplUtils.toName(file);
87          }
88  
89          @Override
90          public MetaData getMetaData() {
91              return metaData;
92          }
93  
94          @Override
95          public InputStream inputStream() throws IOException {
96              return Files.newInputStream(file.toPath());
97          }
98  
99          @Override
100         public String toString() {
101             return "File:" + file.getAbsolutePath();
102         }
103     }
104 }