1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.qedeq.kernel.se.common;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.qedeq.base.io.Path;
26 import org.qedeq.base.io.UrlUtility;
27 import org.qedeq.base.trace.Trace;
28 import org.qedeq.base.utility.StringUtility;
29 import org.qedeq.kernel.se.base.module.LocationList;
30 import org.qedeq.kernel.se.base.module.Specification;
31
32
33
34
35
36
37
38 public class DefaultModuleAddress implements ModuleAddress {
39
40
41 public static final DefaultModuleAddress MEMORY = new DefaultModuleAddress();
42
43
44 private static final Class CLASS = DefaultModuleAddress.class;
45
46
47 private final String url;
48
49
50 private final String header;
51
52
53 private final String path;
54
55
56 private final String fileName;
57
58
59 private final boolean relativeAddress;
60
61
62 private final boolean fileAddress;
63
64
65 private final String name;
66
67
68
69
70
71
72
73
74
75 public DefaultModuleAddress(final String u) throws MalformedURLException {
76 this(u, (DefaultModuleAddress) null);
77 }
78
79
80
81
82
83
84
85
86
87 public DefaultModuleAddress(final URL u) throws MalformedURLException {
88 this(u.toExternalForm(), (ModuleAddress) null);
89 }
90
91
92
93
94
95
96
97
98 public DefaultModuleAddress(final File file) throws IOException {
99 this(UrlUtility.toUrl(file.getCanonicalFile()));
100 }
101
102
103
104
105 public DefaultModuleAddress() {
106 this(true, "default");
107 }
108
109
110
111
112
113
114
115
116
117 public DefaultModuleAddress(final boolean memory, final String identifier) {
118 if (!memory) {
119 throw new IllegalArgumentException("memory must be true");
120 }
121 if (identifier == null) {
122 throw new NullPointerException();
123 }
124 url = "memory://" + identifier;
125 name = identifier;
126 fileAddress = false;
127 fileName = identifier;
128 header = "memory:";
129 path = "";
130 relativeAddress = false;
131 }
132
133
134
135
136
137
138
139
140
141
142
143 public DefaultModuleAddress(final String address, final ModuleAddress parent)
144 throws MalformedURLException {
145 final String method = "ModuleAddress(String, ModuleAddress)";
146 if (address == null) {
147 throw new NullPointerException();
148 }
149 URL urmel;
150 try {
151 if (parent != null) {
152 urmel = new URL(new URL(StringUtility.replace(parent.getUrl(), "file://", "file:")), address);
153 } else {
154 urmel = new URL(address);
155 }
156 } catch (MalformedURLException e) {
157 Trace.trace(CLASS, this, method, "address=" + address);
158 Trace.trace(CLASS, this, method, "parent=" + parent);
159 Trace.trace(CLASS, this, method, e);
160 try {
161 final String newAddress = "file:" + address;
162 if (parent != null) {
163 urmel = new URL(new URL(parent.getUrl()), newAddress);
164 } else {
165 urmel = new URL(newAddress);
166 }
167 } catch (MalformedURLException ex) {
168 throw e;
169 }
170 }
171 final Path p = new Path(urmel.getPath());
172 this.path = p.getDirectory();
173 this.fileName = p.getFileName();
174 Trace.trace(CLASS, this, method, "path=" + this.path);
175 Trace.trace(CLASS, this, method, "fileName=" + this.fileName);
176 this.relativeAddress = p.isRelative();
177 if (!this.fileName.endsWith(".xml")) {
178 throw new MalformedURLException("file name doesn't end with \".xml\": "
179 + this.fileName);
180 }
181 Trace.trace(CLASS, this, method, "protocol=" + urmel.getProtocol());
182 fileAddress = urmel.getProtocol().equalsIgnoreCase("file");
183 String urm = urmel.toString();
184 Trace.trace(CLASS, this, method, "replacing " + urmel.getPath() + " by " + p.toString());
185 urm = StringUtility.replace(urm, urmel.getPath(), p.toString());
186 if (fileAddress) {
187 if (urm.startsWith("file:") && !urm.startsWith("file://")) {
188 urm = "file://" + urm.substring("file:".length());
189 }
190 }
191 url = urm;
192 final int positionBefore = this.fileName.lastIndexOf(".");
193 final String mname = this.fileName.substring(0, positionBefore);
194 this.name = mname;
195 final int positionPath = url.lastIndexOf(p.toString());
196 if (positionPath < 0) {
197 throw new IllegalArgumentException(
198 "couldn't determine begin of file path: "
199 + url + "\nsearching for: " + p);
200 }
201 this.header = url.substring(0, positionPath);
202 }
203
204
205
206
207
208
209 public ModuleContext createModuleContext() {
210 return new ModuleContext(this);
211 }
212
213
214
215
216
217
218
219 public String getHeader() {
220 return header;
221 }
222
223
224
225
226
227
228 public String getPath() {
229 return path;
230 }
231
232
233
234
235
236
237 public String getFileName() {
238 return fileName;
239 }
240
241
242
243
244
245
246 public String getName() {
247 return this.name;
248 }
249
250
251
252
253
254
255 public String getUrl() {
256 return this.url;
257 }
258
259
260
261
262
263
264 public boolean isRelativeAddress() {
265 return this.relativeAddress;
266 }
267
268
269
270
271
272
273 public boolean isFileAddress() {
274 return fileAddress;
275 }
276
277 public final String toString() {
278 return url;
279 }
280
281 public final int hashCode() {
282 return url.hashCode();
283 }
284
285 public final boolean equals(final Object object) {
286 if (!(object instanceof DefaultModuleAddress)) {
287 return false;
288 }
289 return url.equals(((DefaultModuleAddress) object).url);
290 }
291
292
293
294
295
296
297
298
299 private static final String getModuleFileName(final Specification spec) {
300
301 return spec.getName() + ".xml";
302 }
303
304 public final ModuleAddress[] getModulePaths(final Specification spec) throws IOException {
305
306 final String fileNameEnd = getModuleFileName(spec);
307 final LocationList locations = spec.getLocationList();
308 final List result = new ArrayList();
309 for (int i = 0; locations != null && i < locations.size(); i++) {
310 if (locations.get(i) == null) {
311 continue;
312 }
313 String file = locations.get(i).getLocation();
314 if (file.equals(".")) {
315 file = "";
316 } else if (!file.endsWith("/")) {
317 file += "/";
318 }
319 file += fileNameEnd;
320 result.add(new DefaultModuleAddress(file, this));
321 }
322 return (ModuleAddress[]) result.toArray(new ModuleAddress[] {});
323 }
324
325
326
327
328
329
330
331
332
333 public static final String createRelativeAddress(final String origin,
334 final String next) {
335 if (origin.equals(next)) {
336 return "";
337 }
338 final URL urlOrgin;
339 try {
340 urlOrgin = new URL(origin);
341 } catch (MalformedURLException e) {
342 return createRelative(origin, next);
343 }
344 try {
345 final URL urlNext = new URL(next);
346 if (urlOrgin.getProtocol().equals(urlNext.getProtocol())
347 && urlOrgin.getHost().equals(urlNext.getHost())
348 && urlOrgin.getPort() == urlNext.getPort()) {
349 final String org = urlOrgin.getFile();
350 final String nex = urlNext.getFile();
351 return createRelative(org, nex);
352 }
353
354 return urlNext.toString();
355 } catch (MalformedURLException e) {
356 return next;
357 }
358 }
359
360
361
362
363
364
365
366
367 public static String createRelative(final String org, final String nex) {
368 final Path from = new Path(org);
369 return from.createRelative(nex).toString();
370 }
371
372 }
373