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.nio.file.Path;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
32
33 import org.apache.rat.api.Document;
34
35
36
37
38 public class FileDocument extends Document {
39
40
41 private final File file;
42
43
44
45
46
47 public FileDocument(final File file) {
48 super(normalizeFileName(file));
49 this.file = file;
50 }
51
52
53
54
55
56
57 public final static String normalizeFileName(final File file) {
58 String path = file.getPath();
59 return path.replace('\\', '/');
60 }
61
62 @Override
63 public Path getPath() {
64 return file.toPath();
65 }
66
67 @Override
68 public boolean isDirectory() {
69 return file.isDirectory();
70 }
71
72 @Override
73 public SortedSet<Document> listChildren() {
74 if (isDirectory()) {
75 SortedSet<Document> result = new TreeSet<>();
76 Arrays.stream(file.listFiles()).map(f -> new FileDocument(f)).forEach(result::add);
77 return result;
78 }
79 return Collections.emptySortedSet();
80 }
81
82 public Reader reader() throws IOException {
83 return new FileReader(file);
84 }
85
86 public InputStream inputStream() throws IOException {
87 return Files.newInputStream(file.toPath());
88 }
89 }