1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.display.internal; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.io.StringWriter; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.Map; |
26 |
|
import java.util.Stack; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
|
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
34 |
|
import org.xwiki.bridge.DocumentModelBridge; |
35 |
|
import org.xwiki.configuration.ConfigurationSource; |
36 |
|
import org.xwiki.context.Execution; |
37 |
|
import org.xwiki.model.EntityType; |
38 |
|
import org.xwiki.model.ModelContext; |
39 |
|
import org.xwiki.model.reference.DocumentReference; |
40 |
|
import org.xwiki.model.reference.EntityReference; |
41 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
42 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
43 |
|
import org.xwiki.rendering.block.XDOM; |
44 |
|
import org.xwiki.rendering.parser.ParseException; |
45 |
|
import org.xwiki.rendering.parser.Parser; |
46 |
|
import org.xwiki.rendering.util.ParserUtils; |
47 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
48 |
|
import org.xwiki.security.authorization.Right; |
49 |
|
import org.xwiki.velocity.VelocityEngine; |
50 |
|
import org.xwiki.velocity.VelocityManager; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
|
|
|
| 89.5% |
Uncovered Elements: 9 (86) |
Complexity: 22 |
Complexity Density: 0.37 |
|
58 |
|
public abstract class AbstractDocumentTitleDisplayer implements DocumentDisplayer |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private static final String DOCUMENT_REFERENCE_STACK_KEY = "internal.displayer.title.documentReferenceStack"; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@Inject |
72 |
|
private Logger logger; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
@Named("plain/1.0") |
79 |
|
private Parser plainTextParser; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
private VelocityManager velocityManager; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@Inject |
92 |
|
private DocumentAccessBridge documentAccessBridge; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@Inject |
98 |
|
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@Inject |
104 |
|
private Execution execution; |
105 |
|
|
106 |
|
@Inject |
107 |
|
@Named("xwikicfg") |
108 |
|
private ConfigurationSource xwikicfg; |
109 |
|
|
110 |
|
@Inject |
111 |
|
private AuthorizationManager authorizationManager; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
@see |
117 |
|
|
118 |
|
@Inject |
119 |
|
private EntityReferenceProvider defaultEntityReferenceProvider; |
120 |
|
|
121 |
|
@Inject |
122 |
|
private ModelContext modelContext; |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
private ParserUtils parserUtils = new ParserUtils(); |
128 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
129 |
19891 |
@Override... |
130 |
|
public XDOM display(DocumentModelBridge document, DocumentDisplayerParameters parameters) |
131 |
|
{ |
132 |
|
|
133 |
|
|
134 |
19889 |
Map<Object, Object> xwikiContext = getXWikiContextMap(); |
135 |
19890 |
@SuppressWarnings("unchecked") |
136 |
|
Stack<DocumentReference> documentReferenceStack = |
137 |
|
(Stack<DocumentReference>) xwikiContext.get(DOCUMENT_REFERENCE_STACK_KEY); |
138 |
19891 |
if (documentReferenceStack == null) { |
139 |
8232 |
documentReferenceStack = new Stack<DocumentReference>(); |
140 |
8232 |
xwikiContext.put(DOCUMENT_REFERENCE_STACK_KEY, documentReferenceStack); |
141 |
11658 |
} else if (documentReferenceStack.contains(document.getDocumentReference())) { |
142 |
0 |
logger.warn("Infinite recursion detected while displaying the title of [{}]. " |
143 |
|
+ "Using the document name as title.", document.getDocumentReference()); |
144 |
0 |
return getStaticTitle(document); |
145 |
|
} |
146 |
|
|
147 |
19889 |
documentReferenceStack.push(document.getDocumentReference()); |
148 |
19890 |
try { |
149 |
19890 |
return displayTitle(document, parameters); |
150 |
|
} finally { |
151 |
19886 |
documentReferenceStack.pop(); |
152 |
|
} |
153 |
|
} |
154 |
|
|
|
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 7 |
Complexity Density: 0.47 |
|
155 |
19889 |
private XDOM displayTitle(DocumentModelBridge document, DocumentDisplayerParameters parameters)... |
156 |
|
{ |
157 |
|
|
158 |
19886 |
String rawTitle = document.getTitle(); |
159 |
19891 |
if (!StringUtils.isEmpty(rawTitle)) { |
160 |
5087 |
try { |
161 |
5087 |
String title = rawTitle; |
162 |
|
|
163 |
5087 |
if (authorizationManager.hasAccess(Right.SCRIPT, document.getContentAuthorReference(), |
164 |
|
document.getDocumentReference())) { |
165 |
5031 |
title = evaluateTitle(rawTitle, document.getDocumentReference(), parameters); |
166 |
|
} |
167 |
5086 |
return parseTitle(title); |
168 |
|
} catch (Exception e) { |
169 |
1 |
logger.warn("Failed to interpret title of document [{}].", document.getDocumentReference(), e); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
14805 |
if ("1".equals(this.xwikicfg.getProperty("xwiki.title.compatibility", "0"))) { |
175 |
9 |
try { |
176 |
9 |
XDOM title = extractTitleFromContent(document, parameters); |
177 |
9 |
if (title != null) { |
178 |
8 |
return title; |
179 |
|
} |
180 |
|
} catch (Exception e) { |
181 |
0 |
logger.warn("Failed to extract title from content of document [{}].", document.getDocumentReference(), |
182 |
|
e); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
14796 |
return getStaticTitle(document); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
@param |
194 |
|
@return |
195 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
196 |
19878 |
protected XDOM parseTitle(String title)... |
197 |
|
{ |
198 |
19878 |
try { |
199 |
19880 |
XDOM xdom = plainTextParser.parse(new StringReader(title)); |
200 |
19881 |
parserUtils.removeTopLevelParagraph(xdom.getChildren()); |
201 |
19882 |
return xdom; |
202 |
|
} catch (ParseException e) { |
203 |
0 |
throw new RuntimeException(e); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
@param |
211 |
|
@param |
212 |
|
@param |
213 |
|
@return |
214 |
|
|
|
|
| 92.6% |
Uncovered Elements: 2 (27) |
Complexity: 6 |
Complexity Density: 0.29 |
|
215 |
5031 |
protected String evaluateTitle(String title, DocumentReference documentReference,... |
216 |
|
DocumentDisplayerParameters parameters) |
217 |
|
{ |
218 |
5030 |
StringWriter writer = new StringWriter(); |
219 |
5029 |
String namespace = |
220 |
5030 |
defaultEntityReferenceSerializer.serialize(parameters.isTransformationContextIsolated() ? documentReference |
221 |
|
: documentAccessBridge.getCurrentDocumentReference()); |
222 |
|
|
223 |
|
|
224 |
5033 |
VelocityEngine velocityEngine; |
225 |
5033 |
try { |
226 |
5032 |
velocityEngine = this.velocityManager.getVelocityEngine(); |
227 |
|
} catch (Exception e) { |
228 |
0 |
throw new RuntimeException(e); |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
5033 |
Map<String, Object> backupObjects = null; |
233 |
5032 |
boolean canPop = false; |
234 |
5033 |
EntityReference currentWikiReference = this.modelContext.getCurrentEntityReference(); |
235 |
5033 |
try { |
236 |
5033 |
if (parameters.isExecutionContextIsolated()) { |
237 |
4163 |
backupObjects = new HashMap<String, Object>(); |
238 |
|
|
239 |
4163 |
documentAccessBridge.pushDocumentInContext(backupObjects, documentReference); |
240 |
|
|
241 |
4163 |
canPop = true; |
242 |
|
|
243 |
4163 |
modelContext.setCurrentEntityReference(documentReference.getWikiReference()); |
244 |
|
} |
245 |
5033 |
velocityEngine |
246 |
|
.evaluate(velocityManager.getVelocityContext(), writer, namespace, title); |
247 |
|
} catch (Exception e) { |
248 |
1 |
throw new RuntimeException(e); |
249 |
|
} finally { |
250 |
5033 |
if (canPop) { |
251 |
4163 |
documentAccessBridge.popDocumentFromContext(backupObjects); |
252 |
|
|
253 |
4163 |
this.modelContext.setCurrentEntityReference(currentWikiReference); |
254 |
|
} |
255 |
|
} |
256 |
5031 |
return writer.toString(); |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
@return |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
19889 |
@SuppressWarnings("unchecked")... |
263 |
|
private Map<Object, Object> getXWikiContextMap() |
264 |
|
{ |
265 |
19888 |
return (Map<Object, Object>) execution.getContext().getProperty("xwikicontext"); |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
@param |
272 |
|
@param |
273 |
|
@return |
274 |
|
@deprecated |
275 |
|
|
276 |
|
@Deprecated |
277 |
|
protected abstract XDOM extractTitleFromContent(DocumentModelBridge document, |
278 |
|
DocumentDisplayerParameters parameters); |
279 |
|
|
280 |
|
|
281 |
|
@param |
282 |
|
@return |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
284 |
14794 |
private XDOM getStaticTitle(DocumentModelBridge document)... |
285 |
|
{ |
286 |
14795 |
String documentName = document.getDocumentReference().getName(); |
287 |
14797 |
if (defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName().equals(documentName)) { |
288 |
|
|
289 |
7459 |
documentName = document.getDocumentReference().getParent().getName(); |
290 |
|
} |
291 |
14797 |
return parseTitle(documentName); |
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
|
@return |
296 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
297 |
0 |
protected Logger getLogger()... |
298 |
|
{ |
299 |
0 |
return logger; |
300 |
|
} |
301 |
|
} |