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