1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.script; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Provider; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.component.manager.ComponentManager; |
35 |
|
import org.xwiki.rendering.block.Block; |
36 |
|
import org.xwiki.rendering.block.XDOM; |
37 |
|
import org.xwiki.rendering.configuration.ExtendedRenderingConfiguration; |
38 |
|
import org.xwiki.rendering.configuration.RenderingConfiguration; |
39 |
|
import org.xwiki.rendering.parser.ParseException; |
40 |
|
import org.xwiki.rendering.parser.Parser; |
41 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
42 |
|
import org.xwiki.rendering.renderer.PrintRendererFactory; |
43 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
44 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
45 |
|
import org.xwiki.rendering.syntax.Syntax; |
46 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
47 |
|
import org.xwiki.script.service.ScriptService; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Named("rendering") |
57 |
|
@Singleton |
|
|
| 92.9% |
Uncovered Elements: 5 (70) |
Complexity: 22 |
Complexity Density: 0.42 |
|
58 |
|
public class RenderingScriptService implements ScriptService |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@Inject |
64 |
|
@Named("context") |
65 |
|
private Provider<ComponentManager> componentManagerProvider; |
66 |
|
|
67 |
|
|
68 |
|
@see |
69 |
|
|
70 |
|
@Inject |
71 |
|
private SyntaxFactory syntaxFactory; |
72 |
|
|
73 |
|
@Inject |
74 |
|
private Logger logger; |
75 |
|
|
76 |
|
@Inject |
77 |
|
private RenderingConfiguration baseConfiguration; |
78 |
|
|
79 |
|
@Inject |
80 |
|
private ExtendedRenderingConfiguration extendedConfiguration; |
81 |
|
|
82 |
|
|
83 |
|
@return |
84 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
85 |
1 |
public List<Syntax> getAvailableParserSyntaxes()... |
86 |
|
{ |
87 |
1 |
List<Syntax> syntaxes = new ArrayList<Syntax>(); |
88 |
1 |
try { |
89 |
1 |
for (Parser parser : this.componentManagerProvider.get().<Parser>getInstanceList(Parser.class)) { |
90 |
5 |
syntaxes.add(parser.getSyntax()); |
91 |
|
} |
92 |
|
} catch (ComponentLookupException e) { |
93 |
|
|
94 |
0 |
this.logger.error("Failed to lookup parsers", e); |
95 |
|
} |
96 |
|
|
97 |
1 |
return syntaxes; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
@return |
102 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
103 |
1064 |
public List<Syntax> getAvailableRendererSyntaxes()... |
104 |
|
{ |
105 |
1064 |
List<Syntax> syntaxes = new ArrayList<Syntax>(); |
106 |
1063 |
try { |
107 |
1064 |
List<PrintRendererFactory> factories = |
108 |
|
this.componentManagerProvider.get().getInstanceList(PrintRendererFactory.class); |
109 |
1064 |
for (PrintRendererFactory factory : factories) { |
110 |
8272 |
syntaxes.add(factory.getSyntax()); |
111 |
|
} |
112 |
|
} catch (ComponentLookupException e) { |
113 |
|
|
114 |
0 |
this.logger.error("Failed to lookup renderers", e); |
115 |
|
} |
116 |
|
|
117 |
1064 |
return syntaxes; |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
@return |
122 |
|
|
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
124 |
17 |
public List<String> getDefaultTransformationNames()... |
125 |
|
{ |
126 |
17 |
return this.baseConfiguration.getTransformationNames(); |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
@param |
133 |
|
@param |
134 |
|
@return |
135 |
|
@since |
136 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
137 |
4 |
public XDOM parse(String text, String syntaxId)... |
138 |
|
{ |
139 |
4 |
XDOM result; |
140 |
4 |
try { |
141 |
4 |
Parser parser = this.componentManagerProvider.get().getInstance(Parser.class, syntaxId); |
142 |
4 |
result = parser.parse(new StringReader(text)); |
143 |
|
} catch (Exception e) { |
144 |
0 |
result = null; |
145 |
|
} |
146 |
4 |
return result; |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@param |
153 |
|
@param |
154 |
|
@return |
155 |
|
@since |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
157 |
1319 |
public String render(Block block, String outputSyntaxId)... |
158 |
|
{ |
159 |
1319 |
String result; |
160 |
1319 |
WikiPrinter printer = new DefaultWikiPrinter(); |
161 |
1319 |
try { |
162 |
1319 |
BlockRenderer renderer = |
163 |
|
this.componentManagerProvider.get().getInstance(BlockRenderer.class, outputSyntaxId); |
164 |
1318 |
renderer.render(block, printer); |
165 |
1318 |
result = printer.toString(); |
166 |
|
} catch (Exception e) { |
167 |
1 |
result = null; |
168 |
|
} |
169 |
1319 |
return result; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@return |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
178 |
2 |
public Syntax resolveSyntax(String syntaxId)... |
179 |
|
{ |
180 |
2 |
Syntax syntax; |
181 |
2 |
try { |
182 |
2 |
syntax = this.syntaxFactory.createSyntaxFromIdString(syntaxId); |
183 |
|
} catch (ParseException exception) { |
184 |
1 |
syntax = null; |
185 |
|
} |
186 |
2 |
return syntax; |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
@link |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@param@link@link |
199 |
|
@link |
200 |
|
|
201 |
|
@return |
202 |
|
|
203 |
|
@since |
204 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
205 |
182 |
public String escape(String content, Syntax syntax)... |
206 |
|
{ |
207 |
182 |
if (content == null || syntax == null) { |
208 |
3 |
return null; |
209 |
|
} |
210 |
179 |
String input = String.valueOf(content); |
211 |
|
|
212 |
|
|
213 |
179 |
char escapeChar; |
214 |
179 |
try { |
215 |
179 |
escapeChar = getEscapeCharacter(syntax); |
216 |
|
} catch (Exception e) { |
217 |
|
|
218 |
1 |
return null; |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
178 |
char[] result = new char[input.length() * 2]; |
223 |
|
|
224 |
|
|
225 |
9464 |
for (int i = 0; i < input.length(); i++) { |
226 |
9286 |
result[2 * i] = escapeChar; |
227 |
9286 |
result[2 * i + 1] = input.charAt(i); |
228 |
|
} |
229 |
|
|
230 |
178 |
return String.valueOf(result); |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
@return |
235 |
|
|
236 |
|
@since |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
1 |
public List<Syntax> getConfiguredSyntaxes()... |
239 |
|
{ |
240 |
1 |
return this.extendedConfiguration.getConfiguredSyntaxes(); |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
|
@return |
245 |
|
|
246 |
|
@since |
247 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
0 |
public List<Syntax> getDisabledSyntaxes()... |
249 |
|
{ |
250 |
0 |
return this.extendedConfiguration.getDisabledSyntaxes(); |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
253 |
179 |
private char getEscapeCharacter(Syntax syntax) throws IllegalArgumentException... |
254 |
|
{ |
255 |
179 |
if (Syntax.XWIKI_1_0.equals(syntax)) { |
256 |
7 |
return '\\'; |
257 |
172 |
} else if (Syntax.XWIKI_2_0.equals(syntax) || Syntax.XWIKI_2_1.equals(syntax)) { |
258 |
171 |
return '~'; |
259 |
|
} |
260 |
|
|
261 |
1 |
throw new IllegalArgumentException(String.format("Escaping is not supported for Syntax [%s]", syntax)); |
262 |
|
} |
263 |
|
} |