1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.resourceloader;
35
36 import info.magnolia.resourceloader.util.ResourceTreeWalker;
37
38 import java.io.BufferedReader;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
42 import java.io.Reader;
43 import java.nio.charset.Charset;
44 import java.util.List;
45
46
47
48
49
50
51
52
53 public abstract class AbstractResourceOrigin<P extends AbstractResource> implements ResourceOrigin<P> {
54 private final String name;
55
56 protected AbstractResourceOrigin(String name) {
57 this.name = name;
58 }
59
60 @Override
61 public String getName() {
62 return name;
63 }
64
65 @Override
66 public void traverseWith(ResourceVisitor visitor) {
67 new ResourceTreeWalker(visitor).traverse(getRoot());
68 }
69
70
71
72
73 protected abstract boolean isFile(P resource);
74
75
76
77
78 protected abstract boolean isDirectory(P resource);
79
80
81
82
83 protected boolean isEditable(P resource) {
84 return false;
85 }
86
87
88
89
90 protected abstract String getPath(P resource);
91
92
93
94
95 protected abstract String getName(P resource);
96
97
98
99
100 protected abstract long getLastModified(P resource);
101
102
103
104
105 protected abstract List<P> listChildren(P resource);
106
107
108
109
110 protected abstract P getParent(P resource);
111
112
113
114
115 protected InputStream openStream(P resource) throws IOException {
116 if (resource.isDirectory()) {
117 throw new IllegalStateException("Can not open stream on directory " + resource);
118 }
119 final InputStream inputStream = doOpenStream(resource);
120 if (inputStream == null) {
121 throw new IllegalStateException("InputStream is null for " + resource);
122 }
123 return inputStream;
124 }
125
126 protected abstract InputStream doOpenStream(P resource) throws IOException;
127
128
129
130
131 protected Reader openReader(P resource) throws IOException {
132 final InputStream in = openStream(resource);
133 final Charset charset = getCharsetFor(resource);
134 final Reader reader = new InputStreamReader(in, charset);
135 return new BufferedReader(reader);
136 }
137
138
139
140
141 protected abstract Charset getCharsetFor(P resource);
142
143 }