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
24   149   12   4.8
8   79   0.5   5
5     2.4  
1    
 
  DefaultXHTMLImageRenderer       Line # 52 24 0% 12 2 94.6% 0.9459459
 
  (239)
 
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.image;
21   
22    import java.util.LinkedHashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.component.annotation.InstantiationStrategy;
29    import org.xwiki.component.manager.ComponentLookupException;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.component.phase.Initializable;
32    import org.xwiki.component.phase.InitializationException;
33    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
34    import org.xwiki.rendering.listener.reference.ResourceReference;
35    import org.xwiki.rendering.listener.reference.ResourceType;
36    import org.xwiki.rendering.renderer.reference.link.URILabelGenerator;
37    import org.xwiki.rendering.renderer.printer.XHTMLWikiPrinter;
38    import org.xwiki.rendering.wiki.WikiModel;
39   
40    /**
41    * Default implementation for rendering images as XHTML. We handle both cases:
42    * <ul>
43    * <li>when inside a wiki (ie when an implementation of {@link WikiModel} is provided.</li>
44    * <li>when outside of a wiki. In this case we only handle external images and document images don't display anything.</li>
45    * </ul>
46    *
47    * @version $Id: 058e8ebd0638ef4cf207d1fc8c7317355b298d37 $
48    * @since 2.0M3
49    */
50    @Component
51    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
52    public class DefaultXHTMLImageRenderer implements XHTMLImageRenderer, Initializable
53    {
54    /**
55    * @see #setXHTMLWikiPrinter(XHTMLWikiPrinter)
56    */
57    private XHTMLWikiPrinter xhtmlPrinter;
58   
59    /**
60    * Use to resolve local image URL when the image is attached to a document.
61    */
62    private WikiModel wikiModel;
63   
64    @Inject
65    private ComponentManager componentManager;
66   
 
67  247 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  247 try {
73  247 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  34 toggle @Override
80    public void setXHTMLWikiPrinter(XHTMLWikiPrinter printer)
81    {
82  34 this.xhtmlPrinter = printer;
83    }
84   
 
85  66 toggle @Override
86    public XHTMLWikiPrinter getXHTMLWikiPrinter()
87    {
88  66 return this.xhtmlPrinter;
89    }
90   
91    /**
92    * {@inheritDoc}
93    *
94    * @see XHTMLImageRenderer#onImage(org.xwiki.rendering.listener.reference.ResourceReference , boolean, java.util.Map)
95    * @since 2.5RC1
96    */
 
97  34 toggle @Override
98    public void onImage(ResourceReference reference, boolean isFreeStandingURI, Map<String, String> parameters)
99    {
100  34 Map<String, String> attributes = new LinkedHashMap<String, String>();
101   
102    // First we need to compute the image URL.
103  34 String imageURL;
104  34 if (reference.getType().equals(ResourceType.ATTACHMENT) || reference.getType().equals(ResourceType.ICON)) {
105    // Note if wikiModel is null then all Image reference objects will be of type URL. This must be ensured by
106    // the Image Reference parser used beforehand. However we're adding a protection here against Image
107    // Reference parsers that would not honor this contract...
108  23 if (this.wikiModel != null) {
109  23 imageURL = this.wikiModel.getImageURL(reference, parameters);
110    } else {
111  0 throw new RuntimeException("Invalid Image type. In non wiki mode, all image types must be URL images.");
112    }
113    } else {
114  11 imageURL = reference.getReference();
115    }
116   
117    // Then add it as an attribute of the IMG element.
118  34 attributes.put(SRC, imageURL);
119   
120    // Add the class if we're on a freestanding uri
121  34 if (isFreeStandingURI) {
122  20 attributes.put("class", "wikimodel-freestanding");
123    }
124   
125    // Add the other parameters as attributes
126  34 attributes.putAll(parameters);
127   
128    // If no ALT attribute has been specified, add it since the XHTML specifications makes it mandatory.
129  34 if (!parameters.containsKey(ALTERNATE)) {
130  32 attributes.put(ALTERNATE, computeAltAttributeValue(reference));
131    }
132   
133    // And generate the XHTML IMG element.
134  34 getXHTMLWikiPrinter().printXMLElement(IMG, attributes);
135    }
136   
 
137  32 toggle private String computeAltAttributeValue(ResourceReference reference)
138    {
139  32 String label;
140  32 try {
141  32 URILabelGenerator uriLabelGenerator = this.componentManager.lookup(URILabelGenerator.class,
142    reference.getType().getScheme());
143  21 label = uriLabelGenerator.generateLabel(reference);
144    } catch (ComponentLookupException e) {
145  11 label = reference.getReference();
146    }
147  32 return label;
148    }
149    }