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

File LinkLabelGeneratorChainingListener.java

 

Coverage histogram

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

Code metrics

4
19
3
1
127
55
7
0.37
6.33
3
2.33

Classes

Class Line # Actions
LinkLabelGeneratorChainingListener 44 19 0% 7 1
0.9615384396.2%
 

Contributing tests

This file is covered by 173 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.io.StringReader;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.xwiki.rendering.listener.Format;
27    import org.xwiki.rendering.listener.InlineFilterListener;
28    import org.xwiki.rendering.listener.WrappingListener;
29    import org.xwiki.rendering.listener.chaining.AbstractChainingListener;
30    import org.xwiki.rendering.listener.chaining.EmptyBlockChainingListener;
31    import org.xwiki.rendering.listener.chaining.ListenerChain;
32    import org.xwiki.rendering.listener.reference.ResourceReference;
33    import org.xwiki.rendering.listener.reference.ResourceType;
34    import org.xwiki.rendering.parser.ParseException;
35    import org.xwiki.rendering.parser.StreamParser;
36    import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator;
37   
38    /**
39    * Chaining listener to generate labels for the links without labels and send these labels as events in the chain.
40    *
41    * @version $Id: 3db56620f8f4780d81972f8085b848d3926c7b65 $
42    * @since 2.3M1
43    */
 
44    public class LinkLabelGeneratorChainingListener extends AbstractChainingListener
45    {
46    /**
47    * The generator for the link labels.
48    */
49    protected LinkLabelGenerator linkLabelGenerator;
50   
51    /**
52    * The parser for the link labels, used to parse the generated labels and create events to the next listener in the
53    * chain.
54    */
55    protected StreamParser linkLabelParser;
56   
57    /**
58    * Creates a new link generator chaining listener.
59    *
60    * @param linkLabelGenerator the generator used to generate link labels
61    * @param linkLabelParser the parser for the link labels, normally a plain text parser
62    * @param listenerChain the chain this listener is part of
63    */
 
64  230 toggle public LinkLabelGeneratorChainingListener(LinkLabelGenerator linkLabelGenerator, StreamParser linkLabelParser,
65    ListenerChain listenerChain)
66    {
67  230 setListenerChain(listenerChain);
68  230 this.linkLabelGenerator = linkLabelGenerator;
69  230 this.linkLabelParser = linkLabelParser;
70    }
71   
72    /**
73    * @return the empty block chaining listener in this chain
74    */
 
75  16 toggle protected EmptyBlockChainingListener getEmptyBlockState()
76    {
77  16 return (GeneratorEmptyBlockChainingListener) getListenerChain().getListener(
78    GeneratorEmptyBlockChainingListener.class);
79    }
80   
81    /**
82    * {@inheritDoc}
83    *
84    * @see org.xwiki.rendering.listener.chaining.AbstractChainingListener#endLink(
85    * org.xwiki.rendering.listener.reference.ResourceReference , boolean, java.util.Map)
86    * @since 2.5RC1
87    */
 
88  16 toggle @Override
89    public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
90    {
91    // if the link has no label, generate one for it and send the generated link as events to this listener default
92    // behaviour
93  16 if (getEmptyBlockState().isCurrentContainerBlockEmpty()) {
94    // get the link label
95   
96    // FIXME: this should be generated by the link label generator, for all cases, so that the
97    // link label can be changed with whichever generator, that can handle all cases
98  10 String linkLabel = reference.getReference();
99  10 ResourceType resourceType = reference.getType();
100  10 if (ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)) {
101  6 linkLabel = linkLabelGenerator.generate(reference);
102    }
103   
104    // create the span around the label signaling that this is a generated label
105  10 Map<String, String> formatParams = new HashMap<String, String>();
106  10 formatParams.put("class", "wikigeneratedlinkcontent");
107    // the same as this.format. TODO: decide which one is more semantic.
108  10 super.beginFormat(Format.NONE, formatParams);
109   
110    // parse the linkLabel with a stream parser and an inline filter
111  10 WrappingListener inlineFilterListener = new InlineFilterListener();
112  10 inlineFilterListener.setWrappedListener(getListenerChain().getNextListener(getClass()));
113   
114  10 try {
115  10 linkLabelParser.parse(new StringReader(linkLabel), inlineFilterListener);
116    } catch (ParseException e) {
117    // couldn't parse it, send it raw (interesting)
118  0 super.onRawText(linkLabel, linkLabelParser.getSyntax());
119    }
120   
121  10 super.endFormat(Format.NONE, formatParams);
122    }
123   
124    // end the link
125  16 super.endLink(reference, freestanding, parameters);
126    }
127    }