1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.block; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashSet; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Set; |
27 |
|
|
28 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
29 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
30 |
|
import org.xwiki.rendering.parser.ParseException; |
31 |
|
import org.xwiki.rendering.parser.Parser; |
32 |
|
import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
@since |
39 |
|
|
|
|
| 89.3% |
Uncovered Elements: 3 (28) |
Complexity: 9 |
Complexity Density: 0.47 |
|
40 |
|
public class PlainTextBlockFilter implements BlockFilter |
41 |
|
{ |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
private static final Set<Class<? extends Block>> VALID_PLAINTEXT_BLOCKS = new HashSet<Class<? extends Block>>() |
46 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
47 |
5 |
{... |
48 |
5 |
add(WordBlock.class); |
49 |
5 |
add(SpaceBlock.class); |
50 |
5 |
add(SpecialSymbolBlock.class); |
51 |
5 |
add(NewLineBlock.class); |
52 |
|
} |
53 |
|
}; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private Parser plainTextParser; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private LinkLabelGenerator linkLabelGenerator; |
64 |
|
|
65 |
|
|
66 |
|
@param |
67 |
|
@param |
68 |
|
@since |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
70 |
19 |
public PlainTextBlockFilter(Parser plainTextParser, LinkLabelGenerator linkLabelGenerator)... |
71 |
|
{ |
72 |
19 |
this.plainTextParser = plainTextParser; |
73 |
19 |
this.linkLabelGenerator = linkLabelGenerator; |
74 |
|
} |
75 |
|
|
|
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 7 |
Complexity Density: 0.54 |
|
76 |
211 |
@Override... |
77 |
|
public List<Block> filter(Block block) |
78 |
|
{ |
79 |
211 |
if (VALID_PLAINTEXT_BLOCKS.contains(block.getClass())) { |
80 |
208 |
return Collections.singletonList(block); |
81 |
3 |
} else if (LinkBlock.class.isAssignableFrom(block.getClass()) && block.getChildren().size() == 0) { |
82 |
1 |
ResourceReference reference = ((LinkBlock) block).getReference(); |
83 |
|
|
84 |
1 |
try { |
85 |
1 |
String label; |
86 |
|
|
87 |
1 |
ResourceType resourceType = reference.getType(); |
88 |
1 |
if (ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)) { |
89 |
0 |
label = this.linkLabelGenerator.generate(reference); |
90 |
|
} else { |
91 |
1 |
label = reference.getReference(); |
92 |
|
} |
93 |
|
|
94 |
1 |
return this.plainTextParser.parse(new StringReader(label)).getChildren().get(0).getChildren(); |
95 |
|
} catch (ParseException e) { |
96 |
|
|
97 |
|
|
98 |
0 |
throw new RuntimeException("Failed to parse link label as plain text", e); |
99 |
|
} |
100 |
|
} else { |
101 |
2 |
return Collections.emptyList(); |
102 |
|
} |
103 |
|
} |
104 |
|
} |