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.document;
20  
21  import java.io.File;
22  import java.io.FileFilter;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.nio.file.Files;
26  import java.util.Collections;
27  import java.util.SortedSet;
28  import java.util.TreeSet;
29  
30  import org.apache.rat.api.Document;
31  import org.apache.rat.config.exclusion.ExclusionUtils;
32  
33  /**
34   * Document wrapping a File object.
35   */
36  public class FileDocument extends Document {
37  
38      /** The wrapped file */
39      private final File file;
40  
41      /**
42       * Creates a File document.
43       * @param basedir the base directory for this document.
44       * @param file the file to wrap.
45       * @param nameMatcher the path matcher to filter files/directories with.
46       */
47      public FileDocument(final DocumentName basedir, final File file, final DocumentNameMatcher nameMatcher) {
48          super(DocumentName.builder(file).setBaseName(basedir.getBaseName()).build(), nameMatcher);
49          this.file = file;
50      }
51  
52      /**
53       * Creates a File document where the baseDir is the root directory.
54       * @param file the file to wrap.
55       * @param nameMatcher the path matcher to filter files/directories with.
56       */
57      public FileDocument(final File file, final DocumentNameMatcher nameMatcher) {
58          super(DocumentName.builder(file).setBaseName(File.separator).build(), nameMatcher);
59          this.file = file;
60      }
61  
62      @Override
63      public boolean isDirectory() {
64          return file.isDirectory();
65      }
66  
67      @Override
68      public SortedSet<Document> listChildren() {
69          if (isDirectory()) {
70              SortedSet<Document> result = new TreeSet<>();
71              File[] files = file.listFiles();
72              if (files != null) {
73                  FileFilter fileFilter = ExclusionUtils.asFileFilter(name, nameMatcher);
74                  for (File child : files) {
75                      if (fileFilter.accept(child)) {
76                          result.add(new FileDocument(name, child, nameMatcher));
77                      } else {
78                          result.add(new IgnoredDocument(name, child, nameMatcher));
79                      }
80                  }
81              }
82              return result;
83          }
84          return Collections.emptySortedSet();
85      }
86  
87      @Override
88      public InputStream inputStream() throws IOException {
89          return Files.newInputStream(file.toPath());
90      }
91  
92      /**
93       * Gets the file underlying this document.
94       * @return the file underlying this document.
95       */
96      public File getFile() {
97          return file;
98      }
99  }