1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.util; |
5 |
| |
6 |
| import net.sourceforge.pmd.RuleSetNotFoundException; |
7 |
| |
8 |
| import java.io.File; |
9 |
| import java.io.FileInputStream; |
10 |
| import java.io.FileNotFoundException; |
11 |
| import java.io.InputStream; |
12 |
| import java.net.URL; |
13 |
| |
14 |
| public class ResourceLoader { |
15 |
| |
16 |
| |
17 |
0
| private ResourceLoader() {
|
18 |
| } |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
17
| public static InputStream loadResourceAsStream(String name) throws RuleSetNotFoundException {
|
26 |
17
| InputStream stream = ResourceLoader.loadResourceAsStream(name, ResourceLoader.class.getClassLoader());
|
27 |
17
| if (stream == null) {
|
28 |
0
| throw new RuleSetNotFoundException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH");
|
29 |
| } |
30 |
17
| return stream;
|
31 |
| } |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
238
| public static InputStream loadResourceAsStream(String name, ClassLoader loader) throws RuleSetNotFoundException {
|
38 |
238
| File file = new File(name);
|
39 |
238
| if (file.exists()) {
|
40 |
237
| try {
|
41 |
237
| return new FileInputStream(file);
|
42 |
| } catch (FileNotFoundException e) { |
43 |
| |
44 |
| } |
45 |
| } else { |
46 |
1
| try {
|
47 |
1
| return new URL(name).openConnection().getInputStream();
|
48 |
| } catch (Exception e) { |
49 |
1
| return loader.getResourceAsStream(name);
|
50 |
| } |
51 |
| } |
52 |
0
| throw new RuleSetNotFoundException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH");
|
53 |
| } |
54 |
| } |