Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
33   162   13   5.5
12   94   0.39   6
6     2.17  
1    
 
  DocumentXHTMLLinkTypeRenderer       Line # 49 33 0% 13 5 90.2% 0.9019608
 
  (24)
 
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    import javax.inject.Named;
27   
28    import org.apache.commons.lang3.StringUtils;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.annotation.InstantiationStrategy;
31    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.phase.Initializable;
34    import org.xwiki.component.phase.InitializationException;
35    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
36    import org.xwiki.rendering.listener.reference.ResourceReference;
37    import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator;
38    import org.xwiki.rendering.wiki.WikiModel;
39   
40    /**
41    * Handle XHTML rendering for links to documents.
42    *
43    * @version $Id: 552d4f90979d244d33dc04272f268daa1a87ed56 $
44    * @since 2.5M2
45    */
46    @Component
47    @Named("doc")
48    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
49    public class DocumentXHTMLLinkTypeRenderer extends AbstractXHTMLLinkTypeRenderer implements Initializable
50    {
51    /**
52    * The class attribute 'wikilink'.
53    */
54    private static final String WIKILINK = "wikilink";
55   
56    /**
57    * Used to generate the link targeting a local document.
58    */
59    private WikiModel wikiModel;
60   
61    /**
62    * Used to generate a link label.
63    */
64    @Inject
65    private LinkLabelGenerator linkLabelGenerator;
66   
 
67  92 toggle @Override
68    public void initialize() throws InitializationException
69    {
70    // Try to find a WikiModel implementation and set it if it can be found. If not it means we're in
71    // non wiki mode (i.e. no attachment in wiki documents and no links to documents for example).
72  92 try {
73  92 this.wikiModel = this.componentManager.lookup(WikiModel.class);
74    } catch (ComponentLookupException e) {
75    // There's no WikiModel implementation available. this.wikiModel stays null.
76    }
77    }
78   
 
79  46 toggle @Override
80    public void beginLink(ResourceReference reference, boolean isFreeStandingURI, Map<String, String> parameters)
81    {
82  46 if (this.wikiModel == null) {
83  9 super.beginLink(reference, isFreeStandingURI, parameters);
84    } else {
85  37 beginInternalLink(reference, isFreeStandingURI, parameters);
86    }
87    }
88   
 
89  18 toggle @Override
90    protected String computeLabel(ResourceReference reference)
91    {
92  18 return this.linkLabelGenerator.generate(reference);
93    }
94   
 
95  9 toggle @Override
96    protected void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes,
97    Map<String, String> anchorAttributes)
98    {
99  9 if (StringUtils.isEmpty(reference.getReference())) {
100  9 renderAutoLink(reference, spanAttributes, anchorAttributes);
101    } else {
102  0 anchorAttributes.put(XHTMLLinkRenderer.HREF, reference.getReference());
103    }
104    }
105   
106    /**
107    * Start of an internal link.
108    *
109    * @param reference the reference to the link
110    * @param isFreeStandingURI if true then the link is a free standing URI directly in the text
111    * @param parameters a generic list of parameters. Example: style="background-color: blue"
112    */
 
113  37 toggle private void beginInternalLink(ResourceReference reference, boolean isFreeStandingURI,
114    Map<String, String> parameters)
115    {
116  37 Map<String, String> spanAttributes = new LinkedHashMap<String, String>();
117  37 Map<String, String> anchorAttributes = new LinkedHashMap<String, String>();
118   
119    // Add all parameters to the A attributes
120  37 anchorAttributes.putAll(parameters);
121   
122  37 if (StringUtils.isEmpty(reference.getReference())) {
123  4 spanAttributes.put(CLASS, WIKILINK);
124  4 renderAutoLink(reference, spanAttributes, anchorAttributes);
125  33 } else if (this.wikiModel.isDocumentAvailable(reference)) {
126  10 spanAttributes.put(CLASS, WIKILINK);
127  10 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentViewURL(reference));
128    } else {
129    // The wiki document doesn't exist
130  23 spanAttributes.put(CLASS, "wikicreatelink");
131  23 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentEditURL(reference));
132    }
133   
134  37 getXHTMLWikiPrinter().printXMLStartElement(SPAN, spanAttributes);
135  37 getXHTMLWikiPrinter().printXMLStartElement(XHTMLLinkRenderer.ANCHOR, anchorAttributes);
136    }
137   
138    /**
139    * @param reference the reference to the link
140    * @param spanAttributes the span element where to put the class
141    * @param aAttributes the anchor element where to put the reference
142    */
 
143  13 toggle private void renderAutoLink(ResourceReference reference, Map<String, String> spanAttributes,
144    Map<String, String> aAttributes)
145    {
146  13 spanAttributes.put(CLASS, WIKILINK);
147   
148  13 StringBuilder buffer = new StringBuilder();
149  13 String queryString = reference.getParameter(DocumentResourceReference.QUERY_STRING);
150  13 if (queryString != null) {
151  0 buffer.append('?');
152  0 buffer.append(queryString);
153    }
154  13 buffer.append('#');
155  13 String anchor = reference.getParameter(DocumentResourceReference.ANCHOR);
156  13 if (anchor != null) {
157  11 buffer.append(anchor);
158    }
159   
160  13 aAttributes.put(XHTMLLinkRenderer.HREF, buffer.toString());
161    }
162    }