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
15
16 public final class ResourceLoader {
17
18
19
20
21
22 private ResourceLoader() {
23 }
24
25
26
27
28
29
30
31
32
33 public static InputStream loadResourceAsStream(String name) throws RuleSetNotFoundException {
34 InputStream stream = loadResourceAsStream(name, ResourceLoader.class.getClassLoader());
35 if (stream == null) {
36 throw new RuleSetNotFoundException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH");
37 }
38 return stream;
39 }
40
41
42
43
44
45
46
47
48
49 public static InputStream loadResourceAsStream(String name, ClassLoader loader) throws RuleSetNotFoundException {
50 File file = new File(name);
51 if (file.exists()) {
52 try {
53 return new FileInputStream(file);
54 } catch (FileNotFoundException e) {
55
56 }
57 } else {
58 try {
59 return new URL(name).openConnection().getInputStream();
60 } catch (Exception e) {
61 return loader.getResourceAsStream(name);
62 }
63 }
64 throw new RuleSetNotFoundException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH");
65 }
66 }