1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.model.internal.reference; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.UnsupportedEncodingException; |
24 |
|
import java.net.URLDecoder; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Collections; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.apache.commons.lang3.CharEncoding; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
36 |
|
|
37 |
|
|
38 |
|
@link |
39 |
|
|
40 |
|
@link |
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
@Component |
46 |
|
@Named("path") |
47 |
|
@Singleton |
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 4 |
Complexity Density: 0.27 |
|
48 |
|
public class PathStringDocumentReferenceResolver implements DocumentReferenceResolver<String> |
49 |
|
{ |
|
|
| 94.7% |
Uncovered Elements: 1 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
50 |
2 |
@Override... |
51 |
|
public DocumentReference resolve(String path, Object... args) |
52 |
|
{ |
53 |
2 |
try { |
54 |
2 |
File file = new File(path); |
55 |
|
|
56 |
2 |
String page = URLDecoder.decode(file.getName(), CharEncoding.UTF_8); |
57 |
|
|
58 |
2 |
File current = file; |
59 |
2 |
String segmentName = null; |
60 |
2 |
List<String> spaceNames = new ArrayList<>(); |
61 |
7 |
while (current.getParentFile() != null) { |
62 |
5 |
current = current.getParentFile(); |
63 |
5 |
segmentName = URLDecoder.decode(current.getName(), CharEncoding.UTF_8); |
64 |
5 |
if (current.getParentFile() != null) { |
65 |
3 |
spaceNames.add(segmentName); |
66 |
|
} |
67 |
|
} |
68 |
2 |
Collections.reverse(spaceNames); |
69 |
2 |
String wiki = segmentName; |
70 |
2 |
return new DocumentReference(wiki, spaceNames, page); |
71 |
|
} catch (UnsupportedEncodingException e) { |
72 |
|
|
73 |
0 |
throw new RuntimeException("UTF-8 encoding is not present on the system!", e); |
74 |
|
} |
75 |
|
} |
76 |
|
} |