1. Project Clover database Sat Feb 2 2019 06:45:20 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
128
56
8
0.42
6.33
3
2.67

Classes

Class Line # Actions
LinkLabelGeneratorChainingListener 44 19 0% 8 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: d9fc7300e9ab0b366e1111eeeeba70b6b04562fa $
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    || ResourceType.PAGE.equals(resourceType)) {
102  6 linkLabel = linkLabelGenerator.generate(reference);
103    }
104   
105    // create the span around the label signaling that this is a generated label
106  10 Map<String, String> formatParams = new HashMap<String, String>();
107  10 formatParams.put("class", "wikigeneratedlinkcontent");
108    // the same as this.format. TODO: decide which one is more semantic.
109  10 super.beginFormat(Format.NONE, formatParams);
110   
111    // parse the linkLabel with a stream parser and an inline filter
112  10 WrappingListener inlineFilterListener = new InlineFilterListener();
113  10 inlineFilterListener.setWrappedListener(getListenerChain().getNextListener(getClass()));
114   
115  10 try {
116  10 linkLabelParser.parse(new StringReader(linkLabel), inlineFilterListener);
117    } catch (ParseException e) {
118    // couldn't parse it, send it raw (interesting)
119  0 super.onRawText(linkLabel, linkLabelParser.getSyntax());
120    }
121   
122  10 super.endFormat(Format.NONE, formatParams);
123    }
124   
125    // end the link
126  16 super.endLink(reference, freestanding, parameters);
127    }
128    }