1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.test.utils;
20
21 import org.apache.rat.document.impl.DocumentImplUtils;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32 import java.nio.charset.StandardCharsets;
33
34
35
36
37
38
39 public class Resources {
40
41
42
43 private Resources() {
44
45 }
46
47 public static final String SRC_TEST_RESOURCES = "src/test/resources";
48 public static final String SRC_MAIN_RESOURCES = "src/main/resources";
49 private static final File TEST_RESOURCE_BASE_PATH = new File(SRC_TEST_RESOURCES);
50 private static final File RESOURCE_BASE_PATH = new File(SRC_MAIN_RESOURCES);
51
52
53
54
55 public static File getResourceFile(String pResource) throws IOException {
56 return getResourceFromBase(TEST_RESOURCE_BASE_PATH, pResource);
57 }
58
59
60
61
62 public static File getMainResourceFile(String pResource) throws IOException {
63 return getResourceFromBase(RESOURCE_BASE_PATH, pResource);
64 }
65
66
67
68
69 private static File getResourceFromBase(File baseDir, String pResource) throws IOException {
70 File f = new File(baseDir, pResource);
71 if (!f.isFile()) {
72 throw new FileNotFoundException("Unable to locate resource file: " + pResource);
73 }
74 return f;
75 }
76
77
78
79
80
81 public static File[] getResourceFiles(String pResource) throws IOException {
82 File f = new File(TEST_RESOURCE_BASE_PATH, pResource);
83 if (!f.isDirectory()) {
84 throw new FileNotFoundException("Unable to locate resource directory: " + pResource);
85 }
86
87 return f.listFiles(new FileFilter() {
88 public boolean accept(File pathname) {
89 return pathname.isFile();
90 }
91 });
92 }
93
94
95
96
97 public static InputStream getResourceStream(String pResource) throws IOException {
98 return new FileInputStream(getResourceFile(pResource));
99 }
100
101
102
103
104 public static Reader getResourceReader(String pResource) throws IOException {
105 return new InputStreamReader(getResourceStream(pResource), StandardCharsets.UTF_8);
106 }
107
108
109
110
111 public static BufferedReader getBufferedResourceReader(String pResource) throws IOException {
112 return new BufferedReader(getResourceReader(pResource));
113 }
114
115
116
117
118 public static BufferedReader getBufferedReader(File file) throws IOException {
119 return new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
120 }
121
122
123
124
125
126 public static String getResourceDirectory(String pResource) throws IOException {
127 final File resource = getResourceFile(pResource);
128 final File dir = resource.getParentFile();
129 return DocumentImplUtils.toName(dir);
130 }
131 }