Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
30   181   13   3.75
6   86   0.43   8
8     1.62  
1    
 
  AbstractXHTMLLinkTypeRenderer       Line # 39 30 0% 13 2 95.5% 0.95454544
 
  (59)
 
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.rendering.internal.renderer.xhtml.link;
21   
22    import java.util.LinkedHashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26   
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.rendering.listener.reference.ResourceReference;
30    import org.xwiki.rendering.renderer.printer.XHTMLWikiPrinter;
31    import org.xwiki.rendering.renderer.reference.link.URILabelGenerator;
32   
33    /**
34    * Common code for XHTML Link Type Renderer implementations.
35    *
36    * @version $Id: 4f83572c684e9138544ddffab5e33dced68694d6 $
37    * @since 2.5M2
38    */
 
39    public abstract class AbstractXHTMLLinkTypeRenderer implements XHTMLLinkTypeRenderer
40    {
41    /**
42    * The XHTML element <code>class</code> parameter.
43    */
44    protected static final String CLASS = "class";
45   
46    /**
47    * The name of the XHTML format element.
48    */
49    protected static final String SPAN = "span";
50   
51    /**
52    * Used to look for {@link org.xwiki.rendering.renderer.reference.link.URILabelGenerator} component implementations
53    * when computing labels.
54    */
55    @Inject
56    protected ComponentManager componentManager;
57   
58    /**
59    * The XHTML printer to use to output links as XHTML.
60    */
61    private XHTMLWikiPrinter xhtmlPrinter;
62   
63    /**
64    * @see #setHasLabel(boolean)
65    */
66    private boolean hasLabel;
67   
68    /**
69    * @return See {@link #setHasLabel(boolean)}
70    */
 
71  95 toggle protected boolean hasLabel()
72    {
73  95 return this.hasLabel;
74    }
75   
 
76  190 toggle @Override
77    public void setHasLabel(boolean hasLabel)
78    {
79  190 this.hasLabel = hasLabel;
80    }
81   
 
82  190 toggle @Override
83    public void setXHTMLWikiPrinter(XHTMLWikiPrinter printer)
84    {
85  190 this.xhtmlPrinter = printer;
86    }
87   
 
88  527 toggle @Override
89    public XHTMLWikiPrinter getXHTMLWikiPrinter()
90    {
91  527 return this.xhtmlPrinter;
92    }
93   
94    /**
95    * Hook called when rendering the beginning of a link to allow implementation classes to augment the passed span and
96    * anchor attributes as they see fit.
97    *
98    * @param reference the reference of the link being rendered
99    * @param spanAttributes the HTML attributes for the SPAN HTML element added around the ANCHOR HTML element
100    * @param anchorAttributes the HTML attributes for the ANCHOR element
101    */
102    protected abstract void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes,
103    Map<String, String> anchorAttributes);
104   
105    /**
106    * Default implementation for computing a link label when no label has been specified. Can be overwritten by
107    * implementations to provide a different algorithm.
108    *
109    * @param reference the reference of the link for which to compute the label
110    * @return the computed label
111    */
 
112  31 toggle protected String computeLabel(ResourceReference reference)
113    {
114    // Look for a component implementing URILabelGenerator with a role hint matching the link scheme.
115    // If not found then use the full reference as the label.
116    // If there's no scheme separator then use the full reference as the label. Note that this can happen
117    // when we're not in wiki mode (since all links are considered URIs when not in wiki mode).
118  31 String label;
119  31 try {
120  31 URILabelGenerator uriLabelGenerator =
121    this.componentManager.lookup(URILabelGenerator.class, reference.getType().getScheme());
122  13 label = uriLabelGenerator.generateLabel(reference);
123    } catch (ComponentLookupException e) {
124  18 label = reference.getReference();
125    }
126  31 return label;
127    }
128   
 
129  58 toggle @Override
130    public void beginLink(ResourceReference reference, boolean isFreeStandingURI, Map<String, String> parameters)
131    {
132  58 Map<String, String> spanAttributes = new LinkedHashMap<String, String>();
133  58 Map<String, String> anchorAttributes = new LinkedHashMap<String, String>();
134   
135    // Add all parameters to the A attributes
136  58 anchorAttributes.putAll(parameters);
137   
138  58 spanAttributes.put(CLASS, "wikiexternallink");
139  58 if (isFreeStandingURI) {
140  14 anchorAttributes.put(CLASS, addAttributeValue(anchorAttributes.get(CLASS), "wikimodel-freestanding"));
141    }
142   
143  58 beginLinkExtraAttributes(reference, spanAttributes, anchorAttributes);
144   
145  58 getXHTMLWikiPrinter().printXMLStartElement(SPAN, spanAttributes);
146  58 getXHTMLWikiPrinter().printXMLStartElement(XHTMLLinkRenderer.ANCHOR, anchorAttributes);
147    }
148   
 
149  95 toggle @Override
150    public void endLink(ResourceReference reference, boolean isFreeStandingURI, Map<String, String> parameters)
151    {
152    // If there was no link content then generate it based on the passed reference
153  95 if (!hasLabel()) {
154  49 getXHTMLWikiPrinter().printXMLStartElement(SPAN, new String[][] {{CLASS, "wikigeneratedlinkcontent"}});
155  49 getXHTMLWikiPrinter().printXML(computeLabel(reference));
156  49 getXHTMLWikiPrinter().printXMLEndElement(SPAN);
157    }
158   
159  95 getXHTMLWikiPrinter().printXMLEndElement(XHTMLLinkRenderer.ANCHOR);
160  95 getXHTMLWikiPrinter().printXMLEndElement(SPAN);
161    }
162   
163    /**
164    * Add an attribute value to an existing attribute. This is useful for example for adding a value to an HTML CLASS
165    * attribute.
166    *
167    * @param currentValue the current value of the attribute (can be null)
168    * @param valueToAdd the value to add
169    * @return the current value augmented by the value to add
170    */
 
171  14 toggle private String addAttributeValue(String currentValue, String valueToAdd)
172    {
173  14 String newValue;
174  14 if (currentValue == null || currentValue.length() == 0) {
175  14 newValue = "";
176    } else {
177  0 newValue = currentValue + " ";
178    }
179  14 return newValue + valueToAdd;
180    }
181    }