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

File AbstractAnnotationRenderer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
12
2
1
124
49
2
0.17
6
2
1

Classes

Class Line # Actions
AbstractAnnotationRenderer 47 12 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 62 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.annotation.renderer;
21   
22    import java.util.Collection;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26   
27    import org.xwiki.annotation.Annotation;
28    import org.xwiki.annotation.content.ContentAlterer;
29    import org.xwiki.annotation.internal.renderer.AnnotationGeneratorChainingListener;
30    import org.xwiki.component.phase.Initializable;
31    import org.xwiki.component.phase.InitializationException;
32    import org.xwiki.rendering.listener.chaining.BlockStateChainingListener;
33    import org.xwiki.rendering.listener.chaining.EmptyBlockChainingListener;
34    import org.xwiki.rendering.listener.chaining.ListenerChain;
35    import org.xwiki.rendering.listener.chaining.MetaDataStateChainingListener;
36    import org.xwiki.rendering.parser.StreamParser;
37    import org.xwiki.rendering.renderer.AbstractChainingPrintRenderer;
38    import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator;
39   
40    /**
41    * Abstract class for annotation renderer, any specific syntax renderer should implement this class and provide the
42    * specific annotation listener.
43    *
44    * @version $Id: a72cd8c2bfb4d80f449217d8221273ec1f813023 $
45    * @since 2.3M1
46    */
 
47    public abstract class AbstractAnnotationRenderer extends AbstractChainingPrintRenderer implements Initializable,
48    AnnotationPrintRenderer
49    {
50    /**
51    * Selection cleaner so that the selection can be mapped on the content. <br>
52    * TODO: not really sure if this is the right place for this pull, but the annotations generator is not a component
53    * so it cannot 'require' it.
54    */
55    @Inject
56    @Named("whitespace")
57    protected ContentAlterer selectionAlterer;
58   
59    /**
60    * Plain text parser used to parse generated link labels.
61    */
62    @Inject
63    @Named("plain/1.0")
64    protected StreamParser plainTextParser;
65   
66    /**
67    * The annotations generator listener to use in this renderer.
68    */
69    protected AnnotationGeneratorChainingListener annotationsGenerator;
70   
 
71  132 toggle @Override
72    public void initialize() throws InitializationException
73    {
74  132 ListenerChain chain = new ListenerChain();
75  132 setListenerChain(chain);
76   
77    // create the annotations generator
78  132 annotationsGenerator = new AnnotationGeneratorChainingListener(selectionAlterer, chain);
79   
80    // chain'em all
81    // Construct the listener chain in the right order. Listeners early in the chain are called before listeners
82    // placed later in the chain.
83  132 chain.addListener(this);
84   
85    // empty block listener is needed by the label generator
86  132 chain.addListener(new GeneratorEmptyBlockChainingListener(chain));
87    // link label generator generates events for link labels automatically generated for empty links
88    // TODO: find a better way for this. Right now, the stream of events is modified by this link label generator,
89    // which means that a xwiki 2.0 syntax renderer would be in trouble when trying to render after this generator,
90    // since it will get word events for link labels. However, if the linkLabelGenerator is the generator which
91    // always returns empty labels, rendering will happen as it would without it. With the exception that, for links
92    // which are external uris, the label is not generated using the generator, so an empty labels generator would
93    // still not do much. crap!
94  132 chain.addListener(new LinkLabelGeneratorChainingListener(getLinkLabelGenerator(), plainTextParser, chain));
95    // annotations generator, chained to map the annotations and maintain the annotations state while rendering
96  132 chain.addListener((AnnotationGeneratorChainingListener) annotationsGenerator);
97    // Following listeners are needed by the XHTML renderer
98  132 chain.addListener(new BlockStateChainingListener(chain));
99  132 chain.addListener(new EmptyBlockChainingListener(chain));
100  132 chain.addListener(new MetaDataStateChainingListener(chain));
101    // the actual annotations renderer
102  132 chain.addListener(getAnnotationPrintRenderer(chain));
103    }
104   
105    /**
106    * @param chain the chain in which the renderer needs to be added.
107    * @return the print renderer which should render the result with annotations on it
108    */
109    public abstract ChainingPrintRenderer getAnnotationPrintRenderer(ListenerChain chain);
110   
111    /**
112    * Getter for the link label generator to be used for generating link labels in this mapping and rendering process
113    * for links that don't have labels.
114    *
115    * @return the {@link LinkLabelGenerator} used to generate labels for links without labels by this renderer
116    */
117    public abstract LinkLabelGenerator getLinkLabelGenerator();
118   
 
119  70 toggle @Override
120    public void setAnnotations(Collection<Annotation> annotations)
121    {
122  70 this.annotationsGenerator.setAnnotations(annotations);
123    }
124    }