1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.renderer.xhtml.link

File DocumentXHTMLLinkTypeRenderer.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart10.png
0% 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 2
0.962264296.2%
 

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: 3982e8793aa351dce46c48afac6fbcd13a71e909 $
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  27588 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  27590 try {
73  27589 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  13794 toggle @Override
80    public void beginLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
81    {
82  13795 if (this.wikiModel == null) {
83  3 super.beginLink(reference, freestanding, parameters);
84    } else {
85  13792 beginInternalLink(reference, freestanding, parameters);
86    }
87    }
88   
 
89  608 toggle @Override
90    protected String computeLabel(ResourceReference reference)
91    {
92  608 return this.linkLabelGenerator.generate(reference);
93    }
94   
 
95  3 toggle @Override
96    protected void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes,
97    Map<String, String> anchorAttributes)
98    {
99  3 if (StringUtils.isEmpty(reference.getReference())) {
100  3 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 freestanding 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  13790 toggle private void beginInternalLink(ResourceReference reference, boolean freestanding,
114    Map<String, String> parameters)
115    {
116  13792 Map<String, String> spanAttributes = new LinkedHashMap<String, String>();
117  13792 Map<String, String> anchorAttributes = new LinkedHashMap<String, String>();
118   
119    // Add all parameters to the A attributes
120  13793 anchorAttributes.putAll(parameters);
121   
122  13793 if (StringUtils.isEmpty(reference.getReference())) {
123  5454 spanAttributes.put(CLASS, WIKILINK);
124  5454 renderAutoLink(reference, spanAttributes, anchorAttributes);
125  8339 } else if (this.wikiModel.isDocumentAvailable(reference)) {
126  8259 spanAttributes.put(CLASS, WIKILINK);
127  8259 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentViewURL(reference));
128    } else {
129    // The wiki document doesn't exist
130  80 spanAttributes.put(CLASS, "wikicreatelink");
131  80 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getDocumentEditURL(reference));
132    }
133   
134  13790 getXHTMLWikiPrinter().printXMLStartElement(SPAN, spanAttributes);
135  13792 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  5457 toggle private void renderAutoLink(ResourceReference reference, Map<String, String> spanAttributes,
144    Map<String, String> aAttributes)
145    {
146  5457 spanAttributes.put(CLASS, WIKILINK);
147   
148  5457 StringBuilder buffer = new StringBuilder();
149  5457 String queryString = reference.getParameter(DocumentResourceReference.QUERY_STRING);
150  5457 if (queryString != null) {
151  289 buffer.append('?');
152  289 buffer.append(queryString);
153    }
154  5457 buffer.append('#');
155  5457 String anchor = reference.getParameter(DocumentResourceReference.ANCHOR);
156  5457 if (anchor != null) {
157  3787 buffer.append(anchor);
158    }
159   
160  5457 aAttributes.put(XHTMLLinkRenderer.HREF, buffer.toString());
161    }
162   
 
163  3 toggle @Override
164    protected boolean isExternalLink(ResourceReference reference)
165    {
166  3 return false;
167    }
168    }