1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.uiextension.internal; |
21 |
|
|
22 |
|
import java.io.StringWriter; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import org.apache.velocity.VelocityContext; |
27 |
|
import org.slf4j.Logger; |
28 |
|
import org.slf4j.LoggerFactory; |
29 |
|
import org.xwiki.component.manager.ComponentLookupException; |
30 |
|
import org.xwiki.component.manager.ComponentManager; |
31 |
|
import org.xwiki.component.wiki.WikiComponentException; |
32 |
|
import org.xwiki.context.Execution; |
33 |
|
import org.xwiki.model.EntityType; |
34 |
|
import org.xwiki.model.ModelContext; |
35 |
|
import org.xwiki.velocity.VelocityEngine; |
36 |
|
import org.xwiki.velocity.VelocityManager; |
37 |
|
import org.xwiki.velocity.XWikiVelocityException; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 95.7% |
Uncovered Elements: 2 (46) |
Complexity: 14 |
Complexity Density: 0.4 |
|
45 |
|
public class WikiUIExtensionParameters |
46 |
|
{ |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(WikiUIExtensionParameters.class); |
51 |
|
|
52 |
|
private String id; |
53 |
|
|
54 |
|
|
55 |
|
@see |
56 |
|
|
57 |
|
private Map<String, String> parameters; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private Map<String, String> evaluatedParameters; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private int previousContextId; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private String previousWiki; |
75 |
|
|
76 |
|
|
77 |
|
@see |
78 |
|
|
79 |
|
private VelocityManager velocityManager; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
private ModelContext modelContext; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
private Execution execution; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
@param |
95 |
|
@param |
96 |
|
@param |
97 |
|
@throws |
98 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
99 |
134 |
public WikiUIExtensionParameters(String id, String rawParameters, ComponentManager cm)... |
100 |
|
throws WikiComponentException |
101 |
|
{ |
102 |
134 |
this.id = id; |
103 |
134 |
this.parameters = parseParameters(rawParameters); |
104 |
|
|
105 |
134 |
try { |
106 |
134 |
this.execution = cm.getInstance(Execution.class); |
107 |
134 |
this.velocityManager = cm.getInstance(VelocityManager.class); |
108 |
134 |
this.modelContext = cm.getInstance(ModelContext.class); |
109 |
|
} catch (ComponentLookupException e) { |
110 |
0 |
throw new WikiComponentException( |
111 |
|
"Failed to get an instance for a component role required by Wiki Components.", e); |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@param |
121 |
|
@return |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 4 |
Complexity Density: 0.67 |
|
123 |
134 |
private Map<String, String> parseParameters(String rawParameters)... |
124 |
|
{ |
125 |
134 |
Map<String, String> result = new HashMap<String, String>(); |
126 |
134 |
for (String line : rawParameters.split("[\\r\\n]+")) { |
127 |
315 |
String[] pair = line.split("=", 2); |
128 |
315 |
if (pair.length == 2 && !"".equals(pair[0]) && !"".equals(pair[1])) { |
129 |
290 |
result.put(pair[0], pair[1]); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
134 |
return result; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
@return |
138 |
|
|
|
|
| 96.4% |
Uncovered Elements: 1 (28) |
Complexity: 8 |
Complexity Density: 0.36 |
|
139 |
2973 |
public Map<String, String> get()... |
140 |
|
{ |
141 |
2973 |
boolean isCacheValid = false; |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
2971 |
int currentContextId = this.execution.getContext().hashCode(); |
147 |
2973 |
String currentWiki = modelContext.getCurrentEntityReference().extractReference(EntityType.WIKI).getName(); |
148 |
2973 |
if (currentContextId == this.previousContextId |
149 |
|
&& currentWiki.equals(previousWiki) && this.evaluatedParameters != null) { |
150 |
1798 |
isCacheValid = true; |
151 |
|
} |
152 |
|
|
153 |
2972 |
if (!isCacheValid) { |
154 |
1175 |
this.evaluatedParameters = new HashMap<String, String>(); |
155 |
|
|
156 |
1174 |
if (this.parameters.size() > 0) { |
157 |
1071 |
try { |
158 |
1072 |
VelocityEngine velocityEngine = this.velocityManager.getVelocityEngine(); |
159 |
1072 |
VelocityContext velocityContext = this.velocityManager.getVelocityContext(); |
160 |
|
|
161 |
1072 |
for (Map.Entry<String, String> entry : this.parameters.entrySet()) { |
162 |
2046 |
StringWriter writer = new StringWriter(); |
163 |
2047 |
try { |
164 |
2046 |
String namespace = this.id + ':' + entry.getKey(); |
165 |
2046 |
velocityEngine.evaluate(new VelocityContext(velocityContext), writer, namespace, |
166 |
|
entry.getValue()); |
167 |
2046 |
this.evaluatedParameters.put(entry.getKey(), writer.toString()); |
168 |
|
} catch (XWikiVelocityException e) { |
169 |
1 |
LOGGER.warn(String.format( |
170 |
|
"Failed to evaluate UI extension data value, key [%s], value [%s]. Reason: [%s]", |
171 |
|
entry.getKey(), entry.getValue(), e.getMessage())); |
172 |
|
} |
173 |
|
} |
174 |
|
} catch (XWikiVelocityException ex) { |
175 |
0 |
LOGGER.warn(String.format("Failed to get velocity engine. Reason: [%s]", ex.getMessage())); |
176 |
|
} |
177 |
1072 |
this.previousContextId = currentContextId; |
178 |
1072 |
this.previousWiki = currentWiki; |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
2973 |
return this.evaluatedParameters; |
183 |
|
} |
184 |
|
} |