1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.messagetool.internal; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.text.MessageFormat; |
24 |
|
import java.util.Collection; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Locale; |
27 |
|
|
28 |
|
import org.xwiki.localization.TranslationBundle; |
29 |
|
import org.xwiki.localization.message.TranslationMessage; |
30 |
|
import org.xwiki.rendering.block.Block; |
31 |
|
import org.xwiki.rendering.block.CompositeBlock; |
32 |
|
import org.xwiki.rendering.parser.ParseException; |
33 |
|
import org.xwiki.rendering.parser.Parser; |
34 |
|
import org.xwiki.rendering.util.ParserUtils; |
35 |
|
|
36 |
|
|
37 |
|
@link@link |
38 |
|
|
39 |
|
@version |
40 |
|
@since |
41 |
|
|
|
|
| 89.7% |
Uncovered Elements: 4 (39) |
Complexity: 12 |
Complexity Density: 0.48 |
|
42 |
|
public class MessageFormatTranslationMessage implements TranslationMessage |
43 |
|
{ |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
private static final ParserUtils PARSERUTILS = new ParserUtils(); |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private String message; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private Parser plainParser; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private Block noParamCache; |
63 |
|
|
64 |
|
|
65 |
|
@param |
66 |
|
@param |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
68 |
297085 |
public MessageFormatTranslationMessage(String message, Parser plainParser)... |
69 |
|
{ |
70 |
297085 |
this.message = message; |
71 |
297085 |
this.plainParser = plainParser; |
72 |
|
} |
73 |
|
|
|
|
| 87.1% |
Uncovered Elements: 4 (31) |
Complexity: 9 |
Complexity Density: 0.43 |
|
74 |
76258 |
@Override... |
75 |
|
public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters) |
76 |
|
{ |
77 |
|
|
78 |
76256 |
if (parameters.length == 0 && this.noParamCache != null) { |
79 |
66101 |
return this.noParamCache.clone(); |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
10149 |
String result; |
84 |
10149 |
if (parameters.length > 0) { |
85 |
3826 |
try { |
86 |
3826 |
result = MessageFormat.format(this.message, parameters); |
87 |
|
} catch (IllegalArgumentException e) { |
88 |
|
|
89 |
0 |
result = this.message; |
90 |
|
} |
91 |
|
} else { |
92 |
6323 |
result = this.message; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
10148 |
Block block; |
97 |
10148 |
try { |
98 |
10148 |
List<Block> blocks = this.plainParser.parse(new StringReader(result)).getChildren(); |
99 |
|
|
100 |
10149 |
PARSERUTILS.removeTopLevelParagraph(blocks); |
101 |
|
|
102 |
10148 |
if (blocks.size() == 0) { |
103 |
0 |
block = new CompositeBlock(); |
104 |
10148 |
} else if (blocks.size() == 1) { |
105 |
2505 |
block = blocks.get(0); |
106 |
|
} else { |
107 |
7643 |
block = new CompositeBlock(blocks); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
10149 |
if (parameters.length == 0) { |
112 |
6323 |
this.noParamCache = block.clone(); |
113 |
|
} |
114 |
|
} catch (ParseException e) { |
115 |
|
|
116 |
0 |
block = null; |
117 |
|
} |
118 |
|
|
119 |
10149 |
return block; |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
3483 |
@Override... |
123 |
|
public String getRawSource() |
124 |
|
{ |
125 |
3483 |
return this.message; |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
3459 |
@Override... |
129 |
|
public String toString() |
130 |
|
{ |
131 |
3459 |
return getRawSource(); |
132 |
|
} |
133 |
|
} |