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