1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.display.internal

File DocumentTitleDisplayer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
14
1
1
95
51
6
0.43
14
1
6

Classes

Class Line # Actions
DocumentTitleDisplayer 49 14 0% 6 4
0.8260869482.6%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Displays the title of a document. If the title is not specified, extracts the document title from the first heading
41    * in the document content that has the level less than or equal to {@link DisplayConfiguration#getTitleHeadingDepth()}.
42    *
43    * @version $Id: dc11f1da4a9a8e7d85f4db891b6b5fc7661138cd $
44    * @since 3.2M3
45    */
46    @Component
47    @Named("title")
48    @Singleton
 
49    public class DocumentTitleDisplayer extends AbstractDocumentTitleDisplayer
50    {
51    /**
52    * The component used to perform the rendering transformations on the title extracted from the document content.
53    */
54    @Inject
55    private TransformationManager transformationManager;
56   
57    /**
58    * The display configuration.
59    */
60    @Inject
61    private DisplayConfiguration displayConfiguration;
62   
 
63  9 toggle @Override
64    protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters)
65    {
66    // Note: Ideally we should apply transformations on the document's returned XDOM here since macros could
67    // generate headings for example or some other transformations could modify headings. However we don't do this
68    // at the moment since it would be too costly to do so. In the future we will even probably remove the feature
69    // of generating the title from the content.
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    // Check the heading depth after which we should return null if no heading was found.
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    }