1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
package org.asyrinx.brownie.core.io.sf;
|
9
|
|
|
10
|
|
import java.io.File;
|
11
|
|
import java.io.IOException;
|
12
|
|
import java.io.InputStream;
|
13
|
|
import java.io.OutputStream;
|
14
|
|
import java.util.ArrayList;
|
15
|
|
import java.util.Collection;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.List;
|
18
|
|
|
19
|
|
import org.apache.commons.collections.IteratorUtils;
|
20
|
|
import org.apache.commons.logging.Log;
|
21
|
|
import org.apache.commons.logging.LogFactory;
|
22
|
|
import org.asyrinx.brownie.core.io.FileNameUtils;
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
public class StreamFactoryFacade implements StreamFactory {
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
3
|
public StreamFactoryFacade() {
|
33
|
3
|
super();
|
34
|
|
}
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
0
|
public StreamFactoryFacade copy() {
|
41
|
0
|
final StreamFactoryFacade result = new StreamFactoryFacade();
|
42
|
0
|
result.factories.addAll(this.factories);
|
43
|
0
|
return result;
|
44
|
|
}
|
45
|
|
|
46
|
|
private final List factories = new ArrayList();
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
3
|
public InputStream newInput(Object key) throws IOException {
|
52
|
3
|
final Iterator iterator = this.iterator();
|
53
|
3
|
while (iterator.hasNext()) {
|
54
|
3
|
final StreamFactory factory = (StreamFactory) iterator.next();
|
55
|
3
|
try {
|
56
|
3
|
final InputStream result = factory.newInput(key);
|
57
|
3
|
if (result != null)
|
58
|
3
|
return result;
|
59
|
|
} catch (IOException e) {
|
60
|
|
|
61
|
|
}
|
62
|
|
}
|
63
|
0
|
throw new IOException("no StreamFactory for \"" + key + "\"");
|
64
|
|
}
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
0
|
public OutputStream newOutput(Object key) throws IOException {
|
70
|
0
|
final Iterator iterator = this.iterator();
|
71
|
0
|
while (iterator.hasNext()) {
|
72
|
0
|
final StreamFactory factory = (StreamFactory) iterator.next();
|
73
|
0
|
try {
|
74
|
0
|
final OutputStream result = factory.newOutput(key);
|
75
|
0
|
if (result != null)
|
76
|
0
|
return result;
|
77
|
|
} catch (IOException e) {
|
78
|
|
|
79
|
|
}
|
80
|
|
}
|
81
|
0
|
throw new IOException("no StreamFactory for \"" + key + "\"");
|
82
|
|
}
|
83
|
|
|
84
|
3
|
public static StreamFactoryFacade newFacade() {
|
85
|
3
|
return newFacade(IteratorUtils.EMPTY_ITERATOR);
|
86
|
|
}
|
87
|
|
|
88
|
0
|
public static StreamFactoryFacade newFacade(String filenames) {
|
89
|
0
|
return newFacade(FileNameUtils.parseFileNames(filenames));
|
90
|
|
}
|
91
|
|
|
92
|
0
|
public static StreamFactoryFacade newFacade(Collection filenames) {
|
93
|
0
|
return newFacade(filenames.iterator());
|
94
|
|
}
|
95
|
|
|
96
|
3
|
public static StreamFactoryFacade newFacade(Iterator filenameIterator) {
|
97
|
3
|
final StreamFactoryFacade result = new StreamFactoryFacade();
|
98
|
3
|
result.add(new ClassResourceStreamFactory());
|
99
|
3
|
result.add(new SimpleFileStreamFactory());
|
100
|
3
|
while (filenameIterator.hasNext()) {
|
101
|
0
|
final File path = FileNameUtils.toFile(filenameIterator.next());
|
102
|
0
|
if (path.isDirectory()) {
|
103
|
0
|
result.add(new DirectoryBaseFileStreamFactory(path
|
104
|
|
.getAbsolutePath()));
|
105
|
0
|
} else if (FileNameUtils.hasZipExtension(path.getPath())) {
|
106
|
0
|
result.add(new ZipEntryFileStreamFactory(path.getPath()));
|
107
|
|
} else {
|
108
|
0
|
final Log log = LogFactory.getLog(StreamFactoryFacade.class);
|
109
|
0
|
log.warn("path\"" + path.getPath() + "\" was not found.");
|
110
|
|
}
|
111
|
|
}
|
112
|
3
|
return result;
|
113
|
|
}
|
114
|
|
|
115
|
0
|
public StreamFactoryFacade addRelativeClassResourceSF() {
|
116
|
0
|
this.add(new RelativeClassResourceStreamFactory(
|
117
|
|
StreamFactoryFacade.class.getName()));
|
118
|
0
|
return this;
|
119
|
|
}
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
0
|
public void add(int index, StreamFactory element) {
|
126
|
0
|
factories.add(index, element);
|
127
|
|
}
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
6
|
public boolean add(StreamFactory o) {
|
134
|
6
|
return factories.add(o);
|
135
|
|
}
|
136
|
|
|
137
|
|
|
138
|
|
|
139
|
|
|
140
|
0
|
public void clear() {
|
141
|
0
|
factories.clear();
|
142
|
|
}
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
|
|
148
|
0
|
public boolean contains(StreamFactory o) {
|
149
|
0
|
return factories.contains(o);
|
150
|
|
}
|
151
|
|
|
152
|
|
|
153
|
|
|
154
|
|
|
155
|
|
|
156
|
0
|
public StreamFactory get(int index) {
|
157
|
0
|
return (StreamFactory) factories.get(index);
|
158
|
|
}
|
159
|
|
|
160
|
|
|
161
|
|
|
162
|
|
|
163
|
|
|
164
|
0
|
public int indexOf(StreamFactory o) {
|
165
|
0
|
return factories.indexOf(o);
|
166
|
|
}
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
|
|
171
|
0
|
public boolean isEmpty() {
|
172
|
0
|
return factories.isEmpty();
|
173
|
|
}
|
174
|
|
|
175
|
|
|
176
|
|
|
177
|
|
|
178
|
3
|
public Iterator iterator() {
|
179
|
3
|
return factories.iterator();
|
180
|
|
}
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
0
|
public StreamFactory remove(int index) {
|
187
|
0
|
return (StreamFactory) factories.remove(index);
|
188
|
|
}
|
189
|
|
|
190
|
|
|
191
|
|
|
192
|
|
|
193
|
|
|
194
|
0
|
public boolean remove(StreamFactory o) {
|
195
|
0
|
return factories.remove(o);
|
196
|
|
}
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
|
|
201
|
0
|
public int size() {
|
202
|
0
|
return factories.size();
|
203
|
|
}
|
204
|
|
|
205
|
|
}
|