1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import java.io.FileNotFoundException;
17 import java.net.MalformedURLException;
18 import java.net.URL;
19
20
21
22
23
24
25
26 public class LocationUtil {
27
28
29 public static final String SCHEME_PATTERN = "^\\p{Alpha}[\\p{Alnum}+.-]*:.*$";
30
31
32 public static final String CLASSPATH_SCHEME = "classpath:";
33
34
35
36
37
38
39
40
41
42
43
44
45 public static URL urlForResource(String location) throws MalformedURLException, FileNotFoundException {
46 if (location == null) {
47 throw new NullPointerException("location is required");
48 }
49 URL url = null;
50 if (!location.matches(SCHEME_PATTERN)) {
51 url = Loader.getResourceBySelfClassLoader(location);
52 } else if (location.startsWith(CLASSPATH_SCHEME)) {
53 String path = location.substring(CLASSPATH_SCHEME.length());
54 if (path.startsWith("/")) {
55 path = path.substring(1);
56 }
57 if (path.length() == 0) {
58 throw new MalformedURLException("path is required");
59 }
60 url = Loader.getResourceBySelfClassLoader(path);
61 } else {
62 url = new URL(location);
63 }
64 if (url == null) {
65 throw new FileNotFoundException(location);
66 }
67 return url;
68 }
69
70 }