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

File AbstractXHTMLImageTypeRenderer.java

 

Coverage histogram

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

Code metrics

4
26
4
1
141
69
8
0.31
6.5
4
2

Classes

Class Line # Actions
AbstractXHTMLImageTypeRenderer 40 26 0% 8 8
0.764705976.5%
 

Contributing tests

This file is covered by 54 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.image;
21   
22    import java.util.LinkedHashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26   
27    import org.apache.commons.lang3.exception.ExceptionUtils;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.rendering.listener.reference.ResourceReference;
31    import org.xwiki.rendering.renderer.printer.XHTMLWikiPrinter;
32    import org.xwiki.rendering.renderer.reference.link.URILabelGenerator;
33   
34    /**
35    * Common code for XHTML Image Type Renderer implementations.
36    *
37    * @version $Id: 9cd469b0bafefb175a539e23c194b43d28a7be7d $
38    * @since 5.4RC1
39    */
 
40    public abstract class AbstractXHTMLImageTypeRenderer implements XHTMLImageTypeRenderer
41    {
42    /**
43    * The XHTML element <code>class</code> attribute.
44    */
45    protected static final String CLASS = "class";
46   
47    /**
48    * The name of the XHTML format element.
49    */
50    protected static final String SPAN = "span";
51   
52    /**
53    * The name of the XHTML teletype element.
54    */
55    protected static final String TT = "tt";
56   
57    @Inject
58    protected ComponentManager componentManager;
59   
60    /**
61    * @see #setXHTMLWikiPrinter(XHTMLWikiPrinter)
62    */
63    private XHTMLWikiPrinter xhtmlPrinter;
64   
 
65  486 toggle @Override
66    public void setXHTMLWikiPrinter(XHTMLWikiPrinter printer)
67    {
68  486 this.xhtmlPrinter = printer;
69    }
70   
 
71  486 toggle @Override
72    public XHTMLWikiPrinter getXHTMLWikiPrinter()
73    {
74  486 return this.xhtmlPrinter;
75    }
76   
77    /**
78    * {@inheritDoc}
79    *
80    * @see XHTMLImageRenderer#onImage(org.xwiki.rendering.listener.reference.ResourceReference , boolean,
81    * java.util.Map)
82    * @since 2.5RC1
83    */
 
84  486 toggle @Override
85    public void onImage(ResourceReference reference, boolean freestanding, Map<String, String> parameters)
86    {
87  486 Map<String, String> attributes = new LinkedHashMap<String, String>();
88   
89  486 try {
90    // First we need to compute the image SRC attribute value.
91  486 String imageSrcAttributeValue = getImageSrcAttributeValue(reference, parameters);
92   
93    // Then add it as an attribute of the IMG element.
94  486 attributes.put(XHTMLImageRenderer.SRC, imageSrcAttributeValue);
95   
96    // Add the class if we're on a freestanding uri
97  486 if (freestanding) {
98  55 attributes.put(CLASS, "wikimodel-freestanding");
99    }
100   
101    // Add the other parameters as attributes
102  486 attributes.putAll(parameters);
103   
104    // If no ALT attribute has been specified, add it since the XHTML specifications makes it mandatory.
105  486 if (!parameters.containsKey(XHTMLImageRenderer.ALTERNATE)) {
106  418 attributes.put(XHTMLImageRenderer.ALTERNATE, computeAltAttributeValue(reference));
107    }
108   
109    // And generate the XHTML IMG element.
110  486 getXHTMLWikiPrinter().printXMLElement(XHTMLImageRenderer.IMG, attributes);
111    } catch (Throwable e) {
112    // Error title
113  0 getXHTMLWikiPrinter().printXMLStartElement(SPAN, new String[][] { { CLASS, "xwikirenderingerror" } });
114  0 getXHTMLWikiPrinter().printXML(e.getMessage());
115  0 getXHTMLWikiPrinter().printXMLEndElement(SPAN);
116   
117    // Error details
118  0 getXHTMLWikiPrinter().printXMLStartElement(SPAN,
119    new String[][] { { CLASS, "xwikirenderingerrordescription hidden" } });
120  0 getXHTMLWikiPrinter().printXMLStartElement(TT, new String[][] { { CLASS, "wikimodel-verbatim" } });
121  0 getXHTMLWikiPrinter().printXML(ExceptionUtils.getStackTrace(e));
122  0 getXHTMLWikiPrinter().printXMLEndElement(TT);
123  0 getXHTMLWikiPrinter().printXMLEndElement(SPAN);
124    }
125    }
126   
127    protected abstract String getImageSrcAttributeValue(ResourceReference reference, Map<String, String> parameters);
128   
 
129  418 toggle private String computeAltAttributeValue(ResourceReference reference)
130    {
131  418 String label;
132  418 try {
133  418 URILabelGenerator uriLabelGenerator =
134    this.componentManager.getInstance(URILabelGenerator.class, reference.getType().getScheme());
135  250 label = uriLabelGenerator.generateLabel(reference);
136    } catch (ComponentLookupException e) {
137  168 label = reference.getReference();
138    }
139  418 return label;
140    }
141    }