1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.renderer; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Named; |
24 |
|
import javax.inject.Singleton; |
25 |
|
|
26 |
|
import org.apache.commons.lang3.StringUtils; |
27 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
28 |
|
import org.xwiki.bridge.DocumentModelBridge; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.model.EntityType; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.EntityReference; |
33 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
34 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
35 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
36 |
|
import org.xwiki.rendering.configuration.RenderingConfiguration; |
37 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
38 |
|
import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@link |
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@Singleton |
|
|
| 88.3% |
Uncovered Elements: 14 (120) |
Complexity: 31 |
Complexity Density: 0.35 |
|
49 |
|
public class XWikiLinkLabelGenerator implements LinkLabelGenerator |
50 |
|
{ |
51 |
|
@Inject |
52 |
|
private RenderingConfiguration renderingConfiguration; |
53 |
|
|
54 |
|
@Inject |
55 |
|
private DocumentAccessBridge documentAccessBridge; |
56 |
|
|
57 |
|
@Inject |
58 |
|
private EntityReferenceResolver<ResourceReference> resourceReferenceResolver; |
59 |
|
|
60 |
|
@Inject |
61 |
|
private EntityReferenceProvider defaultEntityReferenceProvider; |
62 |
|
|
63 |
|
@Inject |
64 |
|
@Named("local") |
65 |
|
private EntityReferenceSerializer<String> localReferenceSerializer; |
66 |
|
|
67 |
|
|
68 |
|
@inheritDoc |
69 |
|
|
70 |
|
@since |
71 |
|
|
|
|
| 83.7% |
Uncovered Elements: 14 (86) |
Complexity: 19 |
Complexity Density: 0.28 |
|
72 |
27 |
@Override... |
73 |
|
public String generate(ResourceReference reference) |
74 |
|
{ |
75 |
27 |
StringBuilder result = new StringBuilder(); |
76 |
|
|
77 |
27 |
String format = this.renderingConfiguration.getLinkLabelFormat(); |
78 |
|
|
79 |
27 |
EntityReference resolvedReference = resourceReferenceResolver.resolve(reference, EntityType.DOCUMENT); |
80 |
27 |
if (resolvedReference == null) { |
81 |
0 |
throw new IllegalArgumentException(String.valueOf(reference)); |
82 |
|
} |
83 |
27 |
DocumentReference documentReference = new DocumentReference(resolvedReference); |
84 |
|
|
85 |
342 |
for (int i = 0; i < format.length(); i++) { |
86 |
315 |
char c = format.charAt(i); |
87 |
315 |
if (c == '%' && i + 1 < format.length()) { |
88 |
|
|
89 |
135 |
i++; |
90 |
135 |
char cc = format.charAt(i); |
91 |
135 |
switch (cc) { |
92 |
12 |
case 's': |
93 |
|
|
94 |
12 |
result.append(getSpacesLabel(documentReference)); |
95 |
12 |
break; |
96 |
14 |
case 'p': |
97 |
|
|
98 |
14 |
result.append(getPageLabel(documentReference)); |
99 |
14 |
break; |
100 |
12 |
case 't': |
101 |
|
|
102 |
12 |
result.append(getTitleLabel(documentReference)); |
103 |
12 |
break; |
104 |
12 |
case 'P': |
105 |
|
|
106 |
12 |
result.append(getCamelPageLabel(documentReference)); |
107 |
12 |
break; |
108 |
12 |
case 'w': |
109 |
|
|
110 |
12 |
result.append(getWikiLabel(documentReference)); |
111 |
12 |
break; |
112 |
18 |
case 'l': |
113 |
18 |
if (i + 1 < format.length()) { |
114 |
18 |
i++; |
115 |
18 |
char ccc = format.charAt(i); |
116 |
18 |
if (ccc == 's') { |
117 |
|
|
118 |
12 |
result.append(getLastSpaceLabel(documentReference)); |
119 |
|
} else { |
120 |
6 |
result.append(c); |
121 |
6 |
result.append(cc); |
122 |
6 |
result.append(ccc); |
123 |
|
} |
124 |
|
} else { |
125 |
0 |
result.append(c); |
126 |
0 |
result.append(cc); |
127 |
|
} |
128 |
18 |
break; |
129 |
37 |
case 'n': |
130 |
37 |
if (i + 1 < format.length()) { |
131 |
37 |
i++; |
132 |
37 |
char ccc = format.charAt(i); |
133 |
37 |
if (ccc == 'p') { |
134 |
|
|
135 |
|
|
136 |
31 |
result.append(getNestedPageLabel(documentReference)); |
137 |
|
} else { |
138 |
6 |
result.append(c); |
139 |
6 |
result.append(cc); |
140 |
6 |
result.append(ccc); |
141 |
|
} |
142 |
|
} else { |
143 |
0 |
result.append(c); |
144 |
0 |
result.append(cc); |
145 |
|
} |
146 |
37 |
break; |
147 |
18 |
case 'N': |
148 |
18 |
if (i + 1 < format.length()) { |
149 |
18 |
i++; |
150 |
18 |
char ccc = format.charAt(i); |
151 |
18 |
if (ccc == 'P') { |
152 |
|
|
153 |
12 |
result.append(getCamelNestedPageLabel(documentReference)); |
154 |
|
} else { |
155 |
6 |
result.append(c); |
156 |
6 |
result.append(cc); |
157 |
6 |
result.append(ccc); |
158 |
|
} |
159 |
|
} else { |
160 |
0 |
result.append(c); |
161 |
0 |
result.append(cc); |
162 |
|
} |
163 |
18 |
break; |
164 |
0 |
default: |
165 |
0 |
result.append(c); |
166 |
0 |
result.append(cc); |
167 |
|
} |
168 |
|
} else { |
169 |
180 |
result.append(c); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
27 |
return result.toString(); |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
12 |
private String getCamelNestedPageLabel(DocumentReference documentReference)... |
177 |
|
{ |
178 |
12 |
return convertCamelString(getNestedPageLabel(documentReference)); |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
12 |
private String getLastSpaceLabel(DocumentReference documentReference)... |
182 |
|
{ |
183 |
12 |
return documentReference.getLastSpaceReference().getName(); |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
12 |
private String getWikiLabel(DocumentReference documentReference)... |
187 |
|
{ |
188 |
12 |
return documentReference.getWikiReference().getName(); |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
191 |
12 |
private String getCamelPageLabel(DocumentReference documentReference)... |
192 |
|
{ |
193 |
12 |
return convertCamelString(documentReference.getName()); |
194 |
|
} |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
196 |
24 |
private String convertCamelString(String value)... |
197 |
|
{ |
198 |
24 |
return value.replaceAll("([a-z])([A-Z])", "$1 $2"); |
199 |
|
} |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
201 |
14 |
private String getPageLabel(DocumentReference documentReference)... |
202 |
|
{ |
203 |
14 |
return documentReference.getName(); |
204 |
|
} |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
12 |
private String getSpacesLabel(DocumentReference documentReference)... |
207 |
|
{ |
208 |
12 |
return this.localReferenceSerializer.serialize(documentReference.getParent()); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
211 |
47 |
private String getNestedPageLabel(DocumentReference documentReference)... |
212 |
|
{ |
213 |
47 |
String result; |
214 |
47 |
if (this.defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName().equals( |
215 |
|
documentReference.getName())) |
216 |
|
{ |
217 |
22 |
result = documentReference.getLastSpaceReference().getName(); |
218 |
|
} else { |
219 |
25 |
result = documentReference.getName(); |
220 |
|
} |
221 |
47 |
return result; |
222 |
|
} |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
224 |
12 |
private String getTitleLabel(DocumentReference documentReference)... |
225 |
|
{ |
226 |
12 |
String result; |
227 |
12 |
try { |
228 |
12 |
DocumentModelBridge document = this.documentAccessBridge.getDocument(documentReference); |
229 |
10 |
if (StringUtils.isNotBlank(document.getTitle())) { |
230 |
8 |
result = document.getTitle(); |
231 |
|
} else { |
232 |
|
|
233 |
2 |
result = getNestedPageLabel(documentReference); |
234 |
|
} |
235 |
|
} catch (Exception e) { |
236 |
|
|
237 |
|
|
238 |
2 |
result = getNestedPageLabel(documentReference); |
239 |
|
} |
240 |
12 |
return result; |
241 |
|
} |
242 |
|
} |