1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.render; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Named; |
24 |
|
import javax.inject.Provider; |
25 |
|
import javax.inject.Singleton; |
26 |
|
import javax.script.ScriptContext; |
27 |
|
|
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
31 |
|
import org.xwiki.script.ScriptContextInitializer; |
32 |
|
|
33 |
|
import com.xpn.xwiki.XWikiContext; |
34 |
|
import com.xpn.xwiki.XWikiException; |
35 |
|
import com.xpn.xwiki.api.Context; |
36 |
|
import com.xpn.xwiki.api.Document; |
37 |
|
import com.xpn.xwiki.api.XWiki; |
38 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
39 |
|
|
40 |
|
|
41 |
|
@link@link |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@version |
46 |
|
|
47 |
|
@Component |
48 |
|
@Named("xwiki") |
49 |
|
@Singleton |
|
|
| 91.7% |
Uncovered Elements: 4 (48) |
Complexity: 11 |
Complexity Density: 0.34 |
|
50 |
|
public class XWikiScriptContextInitializer implements ScriptContextInitializer |
51 |
|
{ |
52 |
|
@Inject |
53 |
|
private Logger logger; |
54 |
|
|
55 |
|
@Inject |
56 |
|
private SyntaxFactory syntaxFactory; |
57 |
|
|
58 |
|
@Inject |
59 |
|
private Provider<XWikiContext> xcontextProvider; |
60 |
|
|
|
|
| 90% |
Uncovered Elements: 4 (40) |
Complexity: 8 |
Complexity Density: 0.29 |
|
61 |
109615 |
@Override... |
62 |
|
public void initialize(ScriptContext scriptContext) |
63 |
|
{ |
64 |
109600 |
XWikiContext xcontext = this.xcontextProvider.get(); |
65 |
|
|
66 |
109597 |
if (scriptContext.getAttribute("util") == null) { |
67 |
|
|
68 |
15674 |
scriptContext.setAttribute("util", new com.xpn.xwiki.api.Util(xcontext.getWiki(), xcontext), |
69 |
|
ScriptContext.ENGINE_SCOPE); |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
15648 |
scriptContext.setAttribute("xwiki", new XWiki(xcontext.getWiki(), xcontext), ScriptContext.ENGINE_SCOPE); |
74 |
|
|
75 |
15660 |
scriptContext.setAttribute("request", xcontext.getRequest(), ScriptContext.ENGINE_SCOPE); |
76 |
15663 |
scriptContext.setAttribute("response", xcontext.getResponse(), ScriptContext.ENGINE_SCOPE); |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
15662 |
scriptContext.setAttribute("xcontext", new Context(xcontext), ScriptContext.ENGINE_SCOPE); |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
15658 |
scriptContext.setAttribute("syntaxFactory", this.syntaxFactory, ScriptContext.ENGINE_SCOPE); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
109569 |
XWikiDocument doc = xcontext.getDoc(); |
92 |
109584 |
if (doc != null) { |
93 |
100081 |
setDocument(scriptContext, "doc", doc, xcontext); |
94 |
|
|
95 |
100080 |
XWikiDocument tdoc = (XWikiDocument) xcontext.get("tdoc"); |
96 |
100088 |
if (tdoc == null) { |
97 |
9436 |
try { |
98 |
9437 |
tdoc = doc.getTranslatedDocument(xcontext); |
99 |
|
} catch (XWikiException e) { |
100 |
0 |
this.logger.warn("Failed to retrieve the translated document for [{}]. " |
101 |
|
+ "Continue using the default translation.", doc.getDocumentReference(), e); |
102 |
0 |
tdoc = doc; |
103 |
|
} |
104 |
|
} |
105 |
100078 |
setDocument(scriptContext, "tdoc", tdoc, xcontext); |
106 |
|
|
107 |
100104 |
XWikiDocument cdoc = (XWikiDocument) xcontext.get("cdoc"); |
108 |
100098 |
if (cdoc == null) { |
109 |
9435 |
cdoc = tdoc; |
110 |
9438 |
if (cdoc == null) { |
111 |
0 |
cdoc = doc; |
112 |
|
} |
113 |
|
} |
114 |
100088 |
setDocument(scriptContext, "cdoc", cdoc, xcontext); |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
109599 |
XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc"); |
119 |
109586 |
if (sdoc != null) { |
120 |
94176 |
setDocument(scriptContext, "sdoc", sdoc, xcontext); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
109574 |
scriptContext.setAttribute("locale", xcontext.getLocale(), ScriptContext.ENGINE_SCOPE); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
127 |
394422 |
private void setDocument(ScriptContext scriptContext, String key, XWikiDocument document, XWikiContext xcontext)... |
128 |
|
{ |
129 |
|
|
130 |
|
|
131 |
394421 |
Document previousDoc = (Document) scriptContext.getAttribute(key); |
132 |
394464 |
if (previousDoc == null || !previousDoc.same(document)) { |
133 |
110358 |
Document apiDocument = document.newDocument(xcontext); |
134 |
110340 |
scriptContext.setAttribute(key, apiDocument, ScriptContext.ENGINE_SCOPE); |
135 |
|
} |
136 |
|
} |
137 |
|
} |