1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.renderer.xhtml.link

File DocumentXHTMLLinkTypeRenderer.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
34
7
1
168
99
14
0.41
4.86
7
2

Classes

Class Line # Actions
DocumentXHTMLLinkTypeRenderer 49 34 0% 14 5
0.905660490.6%
 

Contributing tests

This file is covered by 43 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.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: 554bae43c65de2a6e9bfe168d29a49a7eda27239 $
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  686 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  686 try {
73  686 this.wikiModel = this.componentManager.getInstance(WikiModel.class);
74    } catch (ComponentLookupException e) {
75    // There's no WikiModel implementation available. this.wikiModel stays null.
76    }
77    }
78   
 
79  343 toggle @Override
80    public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
81    {
82  343 if (this.wikiModel == null) {
83  10 super.beginLink(reference, freestanding, parameters);
84    } else {
85  333 beginInternalLink(reference, freestanding, parameters);
86    }
87    }
88   
 
89  32 toggle @Override
90    protected String computeLabel(ResourceReference reference)
91    {
92  32 return this.linkLabelGenerator.generate(reference);
93    }
94   
 
95  10 toggle @Override
96    protected void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes,
97    Map<String, String> anchorAttributes)
98    {
99  10 if (StringUtils.isEmpty(reference.getReference())) {
100  10 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  333 toggle private void beginInternalLink(ResourceReference reference, boolean freestanding,
114    Map<String, String> parameters)
115    {
116  333 Map<String, String> spanAttributes = new LinkedHashMap<String, String>();
117  333 Map<String, String> anchorAttributes = new LinkedHashMap<String, String>();
118   
119    // Add all parameters to the A attributes
120  333 anchorAttributes.putAll(parameters);
121   
122  333 if (StringUtils.isEmpty(reference.getReference())) {
123  28 spanAttributes.put(CLASS, WIKILINK);
124  28 renderAutoLink(reference, spanAttributes, anchorAttributes);
125  305 } else if (this.wikiModel.isDocumentAvailable(reference)) {
126  267 spanAttributes.put(CLASS, WIKILINK);
127  267 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentViewURL(reference));
128    } else {
129    // The wiki document doesn't exist
130  38 spanAttributes.put(CLASS, "wikicreatelink");
131  38 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentEditURL(reference));
132    }
133   
134  333 getXHTMLWikiPrinter().printXMLStartElement(SPAN, spanAttributes);
135  333 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  38 toggle private void renderAutoLink(ResourceReference reference, Map<String, String> spanAttributes,
144    Map<String, String> aAttributes)
145    {
146  38 spanAttributes.put(CLASS, WIKILINK);
147   
148  38 StringBuilder buffer = new StringBuilder();
149  38 String queryString = reference.getParameter(DocumentResourceReference.QUERY_STRING);
150  38 if (queryString != null) {
151  0 buffer.append('?');
152  0 buffer.append(queryString);
153    }
154  38 buffer.append('#');
155  38 String anchor = reference.getParameter(DocumentResourceReference.ANCHOR);
156  38 if (anchor != null) {
157  36 buffer.append(anchor);
158    }
159   
160  38 aAttributes.put(XHTMLLinkRenderer.HREF, buffer.toString());
161    }
162   
 
163  10 toggle @Override
164    protected boolean isExternalLink(ResourceReference reference)
165    {
166  10 return false;
167    }
168    }