1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38 public class FileDocument extends Document {
39
40
41 private final File file;
42
43
44
45
46
47
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 }