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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.io.Reader;
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  /**
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 ( int i = 0; i < files.length; i++ )
66          {
67              document.setFile( new File( basedir, files[i] ) );
68              document.getMetaData().clear();
69              report.report( document );
70          }
71      }
72  
73      private class FileDocument implements Document
74      {
75          private File file;
76          private final MetaData metaData = new MetaData();
77          
78          void setFile( File file )
79          {
80              this.file = file;
81          }
82  
83          public boolean isComposite() {
84              return DocumentImplUtils.isZip(file);
85          }
86  
87          public Reader reader() throws IOException
88          {
89              final InputStream in = new FileInputStream( file );
90              return new InputStreamReader( in );
91          }
92  
93          public String getName()
94          {
95              return DocumentImplUtils.toName( file );
96          }
97  
98          public MetaData getMetaData() {
99              return metaData;
100         }
101         
102         public InputStream inputStream() throws IOException {
103             return new FileInputStream(file);
104         }
105         
106         public String toString() {
107             return "File:" + file.getAbsolutePath();
108         }
109     }
110 }