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.impl;
20  
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.nio.file.Files;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.SortedSet;
30  import java.util.TreeSet;
31  
32  import org.apache.rat.api.Document;
33  import org.apache.rat.config.exclusion.ExclusionUtils;
34  
35  /**
36   * Document wrapping a File object
37   */
38  public class FileDocument extends Document {
39  
40      /** the wrapped file */
41      private final File file;
42  
43      /**
44       * Creates a File document.
45       * @param basedir the base directory for this document.
46       * @param file the file to wrap.
47       * @param nameMatcher the path matcher to filter files/directories with.
48       */
49      public FileDocument(final DocumentName basedir, final File file, final DocumentNameMatcher nameMatcher) {
50          super(new DocumentName(file, basedir), nameMatcher);
51          this.file = file;
52      }
53  
54      @Override
55      public boolean isDirectory() {
56          return file.isDirectory();
57      }
58  
59      @Override
60      public SortedSet<Document> listChildren() {
61          if (isDirectory()) {
62              SortedSet<Document> result = new TreeSet<>();
63              File[] lst = file.listFiles(ExclusionUtils.asFileFilter(name, nameMatcher));
64              if (lst != null && lst.length > 0) {
65                  Arrays.stream(lst).map(f -> new FileDocument(name, f, nameMatcher)).forEach(result::add);
66              }
67              return result;
68          }
69          return Collections.emptySortedSet();
70      }
71  
72      public Reader reader() throws IOException {
73          return new FileReader(file);
74      }
75  
76      public InputStream inputStream() throws IOException {
77          return Files.newInputStream(file.toPath());
78      }
79  }