1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.macro.internal; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Locale; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.localization.LocalizationContext; |
33 |
|
import org.xwiki.localization.LocalizationManager; |
34 |
|
import org.xwiki.localization.Translation; |
35 |
|
import org.xwiki.localization.macro.TranslationMacroParameters; |
36 |
|
import org.xwiki.rendering.block.Block; |
37 |
|
import org.xwiki.rendering.block.CompositeBlock; |
38 |
|
import org.xwiki.rendering.block.GroupBlock; |
39 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
40 |
|
import org.xwiki.rendering.macro.MacroContentParser; |
41 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
42 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
43 |
|
import org.xwiki.rendering.parser.ParseException; |
44 |
|
import org.xwiki.rendering.parser.Parser; |
45 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
46 |
|
import org.xwiki.rendering.util.ParserUtils; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Named("translation") |
56 |
|
@Singleton |
|
|
| 92.1% |
Uncovered Elements: 3 (38) |
Complexity: 11 |
Complexity Density: 0.52 |
|
57 |
|
public class TranslationMacro extends AbstractMacro<TranslationMacroParameters> |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final String DESCRIPTION = "Display a translation message."; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private static final String CONTENT_DESCRIPTION = "the default translation message"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private static final ParserUtils PARSERUTILS = new ParserUtils(); |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
private LocalizationManager localization; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
private LocalizationContext localizationContext; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@Inject |
90 |
|
private MacroContentParser macroContentParser; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@Inject |
96 |
|
@Named("plain/1.0") |
97 |
|
private Parser plainParser; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
102 |
21 |
public TranslationMacro()... |
103 |
|
{ |
104 |
21 |
super("Translation", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), |
105 |
|
TranslationMacroParameters.class); |
106 |
|
|
107 |
21 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
108 |
|
} |
109 |
|
|
|
|
| 90.6% |
Uncovered Elements: 3 (32) |
Complexity: 9 |
Complexity Density: 0.5 |
|
110 |
217 |
@Override... |
111 |
|
public List<Block> execute(TranslationMacroParameters parameters, String content, |
112 |
|
MacroTransformationContext context) throws MacroExecutionException |
113 |
|
{ |
114 |
217 |
Locale locale = |
115 |
217 |
parameters.getLocale() != null ? parameters.getLocale() : this.localizationContext.getCurrentLocale(); |
116 |
|
|
117 |
217 |
Translation translation = this.localization.getTranslation(parameters.getKey(), locale); |
118 |
|
|
119 |
217 |
List<Block> blocks; |
120 |
217 |
if (translation != null) { |
121 |
215 |
Block block = |
122 |
215 |
parameters.getParameters() != null ? translation.render(locale, (Object[]) parameters.getParameters()) |
123 |
|
: translation.render(locale); |
124 |
|
|
125 |
215 |
if (block instanceof CompositeBlock) { |
126 |
203 |
blocks = block.getChildren(); |
127 |
|
} else { |
128 |
12 |
blocks = Arrays.asList(block); |
129 |
|
} |
130 |
|
|
131 |
215 |
if (!context.getCurrentMacroBlock().isInline()) { |
132 |
|
|
133 |
6 |
blocks = Arrays.<Block> asList(new GroupBlock(blocks)); |
134 |
|
} |
135 |
2 |
} else if (content != null) { |
136 |
1 |
blocks = |
137 |
|
this.macroContentParser.parse(content, context, false, context.getCurrentMacroBlock().isInline()) |
138 |
|
.getChildren(); |
139 |
|
} else { |
140 |
1 |
try { |
141 |
1 |
blocks = this.plainParser.parse(new StringReader(parameters.getKey())).getChildren(); |
142 |
|
|
143 |
1 |
if (context.getCurrentMacroBlock().isInline()) { |
144 |
0 |
PARSERUTILS.removeTopLevelParagraph(blocks); |
145 |
|
} |
146 |
|
} catch (ParseException e) { |
147 |
0 |
throw new MacroExecutionException("Failed to parse key [" + parameters.getKey() + "]", e); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
217 |
return blocks; |
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
209 |
@Override... |
155 |
|
public boolean supportsInlineMode() |
156 |
|
{ |
157 |
209 |
return true; |
158 |
|
} |
159 |
|
} |