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

File AttachmentXHTMLLinkTypeRenderer.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
8
3
1
93
49
5
0.62
2.67
3
1.67

Classes

Class Line # Actions
AttachmentXHTMLLinkTypeRenderer 46 8 0% 5 3
0.769230876.9%
 

Contributing tests

This file is covered by 7 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.Map;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.component.annotation.InstantiationStrategy;
29    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.phase.Initializable;
32    import org.xwiki.component.phase.InitializationException;
33    import org.xwiki.rendering.listener.reference.ResourceReference;
34    import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer;
35    import org.xwiki.rendering.wiki.WikiModel;
36   
37    /**
38    * Handle XHTML rendering for links to attachments.
39    *
40    * @version $Id: 4c902bd58eb3c23e32b238053c9690215479e068 $
41    * @since 2.5M2
42    */
43    @Component
44    @Named("attach")
45    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
46    public class AttachmentXHTMLLinkTypeRenderer extends AbstractXHTMLLinkTypeRenderer implements Initializable
47    {
48    /**
49    * Used to serialize the attachment link to XWiki Syntax 2.0 when we're not inside a wiki.
50    * We choose the XWiki Syntax 2.0 arbitrarily. Normally the user should never use a link to an attachment when
51    * not inside a wiki.
52    */
53    @Inject
54    @Named("xwiki/2.0")
55    private ResourceReferenceTypeSerializer defaultResourceReferenceTypeSerializer;
56   
57    /**
58    * Used to generate the link targeting a local document.
59    */
60    private WikiModel wikiModel;
61   
 
62  26 toggle @Override
63    public void initialize() throws InitializationException
64    {
65    // Try to find a WikiModel implementation and set it if it can be found. If not it means we're in
66    // non wiki mode (i.e. no attachment in wiki documents and no links to documents for example).
67  26 try {
68  26 this.wikiModel = this.componentManager.getInstance(WikiModel.class);
69    } catch (ComponentLookupException e) {
70    // There's no WikiModel implementation available. this.wikiModel stays null.
71    }
72    }
73   
 
74  13 toggle @Override
75    protected void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes,
76    Map<String, String> anchorAttributes)
77    {
78  13 if (this.wikiModel != null) {
79  13 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.wikiModel.getLinkURL(reference));
80  13 spanAttributes.put(CLASS, "wikiattachmentlink");
81    } else {
82  0 anchorAttributes.put(XHTMLLinkRenderer.HREF, this.defaultResourceReferenceTypeSerializer.serialize(
83    reference));
84  0 spanAttributes.put(CLASS, "wikiattachmentlink wikiexternallink");
85    }
86    }
87   
 
88  13 toggle @Override
89    protected boolean isExternalLink(ResourceReference reference)
90    {
91  13 return false;
92    }
93    }