1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.addam.ddlrun.utils;
23
24
25
26
27 /**
28 * Used to retrieve a ClassLoaderReader.
29 *
30 * @author TIM3
31 * @since Mar 5, 2005
32 */
33 public class ClassLoaderReaderFactory implements ResourceReaderFactory {
34
35 /**
36 * Constructs a ClassLoaderReaderFactory with no base path
37 */
38 public ClassLoaderReaderFactory() {
39 basePath = "";
40 }
41
42 /**
43 * Constructs a ClassLoaderReaderFactory the provided base path, which is
44 * prepended to all non-absolute requests (i.e. those not beginning with '/')
45 *
46 * @param basePath the path to prepend to relative paths passed to getReader
47 */
48 public ClassLoaderReaderFactory(String basePath) {
49 if (!basePath.endsWith(SEPARATOR) && basePath.length() > 0) {
50
51 basePath += SEPARATOR;
52 }
53 this.basePath = basePath;
54 }
55
56 public ResourceReader getReader(String resource) {
57 return this.getReader("", resource);
58 }
59
60 public ResourceReader getReader(String path, String resource) {
61 String pathToUse;
62 if (path.startsWith(SEPARATOR)) {
63
64 pathToUse = path;
65 } else {
66 pathToUse = basePath + path;
67 }
68 if (!pathToUse.endsWith(SEPARATOR)) {
69
70 pathToUse += SEPARATOR;
71 }
72 return new ClassLoaderReader(pathToUse, resource);
73 }
74
75 private static final String SEPARATOR = "/";
76 private final String basePath;
77 }