1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.transformation.icon; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.phase.Initializable; |
34 |
|
import org.xwiki.component.phase.InitializationException; |
35 |
|
import org.xwiki.rendering.block.Block; |
36 |
|
import org.xwiki.rendering.block.ImageBlock; |
37 |
|
import org.xwiki.rendering.block.SpecialSymbolBlock; |
38 |
|
import org.xwiki.rendering.block.WordBlock; |
39 |
|
import org.xwiki.rendering.block.XDOM; |
40 |
|
import org.xwiki.rendering.internal.block.ProtectedBlockFilter; |
41 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
42 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
43 |
|
import org.xwiki.rendering.parser.ParseException; |
44 |
|
import org.xwiki.rendering.parser.Parser; |
45 |
|
import org.xwiki.rendering.transformation.AbstractTransformation; |
46 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
47 |
|
import org.xwiki.rendering.transformation.TransformationException; |
48 |
|
import org.xwiki.rendering.transformation.icon.IconTransformationConfiguration; |
49 |
|
import org.xwiki.rendering.util.ParserUtils; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@version |
56 |
|
@since |
57 |
|
|
58 |
|
@Component |
59 |
|
@Named("icon") |
60 |
|
@Singleton |
|
|
| 97.2% |
Uncovered Elements: 3 (107) |
Complexity: 27 |
Complexity Density: 0.4 |
|
61 |
|
public class IconTransformation extends AbstractTransformation implements Initializable |
62 |
|
{ |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@Inject |
67 |
|
private IconTransformationConfiguration configuration; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
@Named("plain/1.0") |
74 |
|
private Parser plainTextParser; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@Inject |
80 |
|
private Logger logger; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private ParserUtils parserUtils = new ParserUtils(); |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private XDOM mappingTree; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
private ProtectedBlockFilter filter = new ProtectedBlockFilter(); |
96 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
97 |
34 |
@Override... |
98 |
|
public void initialize() throws InitializationException |
99 |
|
{ |
100 |
34 |
this.mappingTree = new XDOM(Collections.<Block>emptyList()); |
101 |
|
|
102 |
|
|
103 |
34 |
for (Map.Entry<Object, Object> entry : this.configuration.getMappings().entrySet()) { |
104 |
578 |
try { |
105 |
578 |
XDOM xdom = this.plainTextParser.parse(new StringReader((String) entry.getKey())); |
106 |
|
|
107 |
578 |
this.parserUtils.removeTopLevelParagraph(xdom.getChildren()); |
108 |
578 |
mergeTree(this.mappingTree, convertToDeepTree(xdom, (String) entry.getValue())); |
109 |
|
} catch (ParseException e) { |
110 |
0 |
this.logger.warn("Failed to parse icon symbols [" + entry.getKey() + "]. Reason = [" |
111 |
|
+ e.getMessage() + "]"); |
112 |
|
} |
113 |
|
} |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
116 |
7172 |
@Override... |
117 |
|
public void transform(Block source, TransformationContext context) throws TransformationException |
118 |
|
{ |
119 |
7172 |
List<Block> filteredBlocks = this.filter.filter(source.getChildren()); |
120 |
7171 |
if (!this.mappingTree.getChildren().isEmpty() && !filteredBlocks.isEmpty()) { |
121 |
6881 |
parseTree(filteredBlocks); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
@param |
130 |
|
@param |
131 |
|
@return |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
133 |
578 |
private Block convertToDeepTree(Block sourceTree, String iconName)... |
134 |
|
{ |
135 |
578 |
XDOM targetTree = new XDOM(Collections.<Block>emptyList()); |
136 |
578 |
Block pointer = targetTree; |
137 |
578 |
for (Block block : sourceTree.getChildren()) { |
138 |
1564 |
pointer.addChild(block); |
139 |
1564 |
pointer = block; |
140 |
|
} |
141 |
|
|
142 |
578 |
pointer.addChild(new ImageBlock(new ResourceReference(iconName, ResourceType.ICON), true)); |
143 |
578 |
return targetTree; |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
@param |
150 |
|
@param |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
152 |
1054 |
private void mergeTree(Block targetTree, Block sourceTree)... |
153 |
|
{ |
154 |
1054 |
for (Block block : sourceTree.getChildren()) { |
155 |
|
|
156 |
1054 |
int pos = indexOf(targetTree.getChildren(), block); |
157 |
1054 |
if (pos > -1) { |
158 |
476 |
Block foundBlock = targetTree.getChildren().get(pos); |
159 |
476 |
mergeTree(foundBlock, block); |
160 |
|
} else { |
161 |
578 |
targetTree.addChild(block); |
162 |
|
} |
163 |
|
} |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
@param |
170 |
|
@param |
171 |
|
@return |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
173 |
1054 |
private int indexOf(List<Block> targetBlocks, Block block)... |
174 |
|
{ |
175 |
1054 |
int pos = 0; |
176 |
1054 |
for (Block targetBlock : targetBlocks) { |
177 |
|
|
178 |
3400 |
if (blockEquals(targetBlock, block)) { |
179 |
476 |
return pos; |
180 |
|
} |
181 |
2924 |
pos++; |
182 |
|
} |
183 |
578 |
return -1; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
@param |
190 |
|
@param |
191 |
|
@return |
192 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 8 |
Complexity Density: 0.8 |
|
193 |
2370146 |
private boolean blockEquals(Block target, Block source)... |
194 |
|
{ |
195 |
2370146 |
boolean found = false; |
196 |
2370146 |
if (source instanceof SpecialSymbolBlock && target instanceof SpecialSymbolBlock) { |
197 |
820722 |
if (((SpecialSymbolBlock) target).getSymbol() == ((SpecialSymbolBlock) source).getSymbol()) { |
198 |
13595 |
found = true; |
199 |
|
} |
200 |
1549424 |
} else if (source instanceof WordBlock && target instanceof WordBlock) { |
201 |
6106 |
if (((WordBlock) target).getWord().equals(((WordBlock) source).getWord())) { |
202 |
17 |
found = true; |
203 |
|
} |
204 |
1543318 |
} else if (source.equals(target)) { |
205 |
0 |
found = true; |
206 |
|
} |
207 |
2370147 |
return found; |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@param |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (45) |
Complexity: 9 |
Complexity Density: 0.31 |
|
215 |
40384 |
private void parseTree(List<Block> sourceBlocks)... |
216 |
|
{ |
217 |
40384 |
Block matchStartBlock = null; |
218 |
40384 |
int count = 0; |
219 |
40384 |
Block mappingCursor = this.mappingTree.getChildren().get(0); |
220 |
40384 |
Block sourceBlock = sourceBlocks.get(0); |
221 |
842980 |
while (sourceBlock != null) { |
222 |
3156228 |
while (mappingCursor != null) { |
223 |
2366746 |
if (blockEquals(sourceBlock, mappingCursor)) { |
224 |
13136 |
if (matchStartBlock == null) { |
225 |
13089 |
matchStartBlock = sourceBlock; |
226 |
|
} |
227 |
13136 |
count++; |
228 |
13136 |
mappingCursor = mappingCursor.getChildren().get(0); |
229 |
|
|
230 |
13136 |
if (mappingCursor instanceof ImageBlock) { |
231 |
|
|
232 |
67 |
for (int i = 0; i < count - 1; i++) { |
233 |
43 |
matchStartBlock.getParent().removeBlock(matchStartBlock.getNextSibling()); |
234 |
|
} |
235 |
24 |
sourceBlock = mappingCursor.clone(); |
236 |
24 |
matchStartBlock.getParent().replaceChild(sourceBlock, matchStartBlock); |
237 |
24 |
mappingCursor = null; |
238 |
24 |
matchStartBlock = null; |
239 |
24 |
count = 0; |
240 |
|
} else { |
241 |
|
|
242 |
13112 |
break; |
243 |
|
} |
244 |
|
} else { |
245 |
2353611 |
mappingCursor = mappingCursor.getNextSibling(); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
802595 |
List<Block> filteredSourceBlocks = this.filter.filter(sourceBlock.getChildren()); |
250 |
802595 |
if (filteredSourceBlocks.size() > 0) { |
251 |
33503 |
parseTree(filteredSourceBlocks); |
252 |
769092 |
} else if (mappingCursor == null) { |
253 |
|
|
254 |
755980 |
mappingCursor = this.mappingTree.getChildren().get(0); |
255 |
755980 |
count = 0; |
256 |
755980 |
matchStartBlock = null; |
257 |
|
} |
258 |
802596 |
sourceBlock = this.filter.getNextSibling(sourceBlock); |
259 |
|
} |
260 |
|
} |
261 |
|
} |