1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.display.internal; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.xwiki.bridge.DocumentModelBridge; |
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.rendering.block.Block; |
32 |
|
import org.xwiki.rendering.block.HeaderBlock; |
33 |
|
import org.xwiki.rendering.block.XDOM; |
34 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
35 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
36 |
|
import org.xwiki.rendering.transformation.TransformationException; |
37 |
|
import org.xwiki.rendering.transformation.TransformationManager; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
@link |
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
46 |
|
@Component |
47 |
|
@Named("title") |
48 |
|
@Singleton |
|
|
| 82.6% |
Uncovered Elements: 4 (23) |
Complexity: 6 |
Complexity Density: 0.43 |
|
49 |
|
public class DocumentTitleDisplayer extends AbstractDocumentTitleDisplayer |
50 |
|
{ |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@Inject |
55 |
|
private TransformationManager transformationManager; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@Inject |
61 |
|
private DisplayConfiguration displayConfiguration; |
62 |
|
|
|
|
| 81.8% |
Uncovered Elements: 4 (22) |
Complexity: 6 |
Complexity Density: 0.43 |
|
63 |
9 |
@Override... |
64 |
|
protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters) |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
9 |
List<HeaderBlock> blocks = |
71 |
|
document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT); |
72 |
9 |
if (!blocks.isEmpty()) { |
73 |
9 |
HeaderBlock heading = blocks.get(0); |
74 |
|
|
75 |
9 |
if (heading.getLevel().getAsInt() <= displayConfiguration.getTitleHeadingDepth()) { |
76 |
8 |
XDOM headingXDOM = new XDOM(Collections.<Block> singletonList(heading)); |
77 |
8 |
try { |
78 |
8 |
TransformationContext txContext = |
79 |
|
new TransformationContext(headingXDOM, document.getSyntax(), |
80 |
|
parameters.isTransformationContextRestricted()); |
81 |
8 |
txContext.setTargetSyntax(parameters.getTargetSyntax()); |
82 |
8 |
transformationManager.performTransformations(headingXDOM, txContext); |
83 |
|
|
84 |
8 |
Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null; |
85 |
8 |
if (headingBlock instanceof HeaderBlock) { |
86 |
8 |
return new XDOM(headingBlock.getChildren()); |
87 |
|
} |
88 |
|
} catch (TransformationException e) { |
89 |
0 |
getLogger().warn("Failed to extract title from document content."); |
90 |
|
} |
91 |
|
} |
92 |
|
} |
93 |
1 |
return null; |
94 |
|
} |
95 |
|
} |