1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.icon.internal

File DefaultIconRenderer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
24
7
1
123
77
11
0.46
3.43
7
1.57

Classes

Class Line # Actions
DefaultIconRenderer 42 24 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 10 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.icon.internal;
21   
22    import java.io.StringWriter;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28   
29    import org.apache.commons.lang.StringUtils;
30    import org.xwiki.icon.Icon;
31    import org.xwiki.icon.IconException;
32    import org.xwiki.icon.IconRenderer;
33    import org.xwiki.icon.IconSet;
34    import org.xwiki.skinx.SkinExtension;
35   
36    /**
37    * Default implementation of {@link org.xwiki.icon.IconRenderer}.
38    *
39    * @since 6.2M1
40    * @version $Id: 06ac1c408cbecada9db764d90903bba8062a1542 $
41    */
 
42    public class DefaultIconRenderer implements IconRenderer
43    {
44    @Inject
45    @Named("ssx")
46    private SkinExtension skinExtension;
47   
48    @Inject
49    @Named("linkx")
50    private SkinExtension linkExtension;
51   
52    @Inject
53    @Named("jsx")
54    private SkinExtension jsExtension;
55   
56    @Inject
57    private VelocityRenderer velocityRenderer;
58   
 
59  147 toggle @Override
60    public String render(String iconName, IconSet iconSet) throws IconException
61    {
62  147 return render(iconSet, iconName, iconSet.getRenderWiki());
63    }
64   
 
65  2534 toggle @Override
66    public String renderHTML(String iconName, IconSet iconSet) throws IconException
67    {
68  2535 return render(iconSet, iconName, iconSet.getRenderHTML());
69    }
70   
 
71  2681 toggle private String render(IconSet iconSet, String iconName, String renderer) throws IconException
72    {
73  2681 if (!StringUtils.isBlank(iconSet.getCss())) {
74  2 activeCSS(iconSet);
75    }
76  2681 if (!StringUtils.isBlank(iconSet.getSsx())) {
77  11 activeSSX(iconSet);
78    }
79  2682 if (!StringUtils.isBlank(iconSet.getJsx())) {
80  11 activeJSX(iconSet);
81    }
82  2681 return renderIcon(iconSet, iconName, renderer);
83    }
84   
 
85  2 toggle private void activeCSS(IconSet iconSet) throws IconException
86    {
87  2 String url = velocityRenderer.render(iconSet.getCss());
88  2 Map<String, Object> parameters = new HashMap();
89  2 parameters.put("rel", "stylesheet");
90  2 linkExtension.use(url, parameters);
91    }
92   
 
93  11 toggle private void activeSSX(IconSet iconSet)
94    {
95  11 skinExtension.use(iconSet.getSsx());
96    }
97   
 
98  11 toggle private void activeJSX(IconSet iconSet)
99    {
100  11 jsExtension.use(iconSet.getJsx());
101    }
102   
 
103  2681 toggle private String renderIcon(IconSet iconSet, String iconName, String renderer) throws IconException
104    {
105    // Get the icon
106  2681 Icon icon = iconSet.getIcon(iconName);
107   
108    // The icon may not exist
109  2681 if (icon == null) {
110    // return an empty string. Idea: fallback on a different icon instead?
111  1 return "";
112    }
113   
114    // Interpret the velocity command
115  2680 StringWriter contentToParse = new StringWriter();
116  2680 contentToParse.write("#set($icon = \"");
117  2681 contentToParse.write(icon.getValue());
118  2681 contentToParse.write("\")\n");
119  2681 contentToParse.write(renderer);
120   
121  2680 return velocityRenderer.render(contentToParse.toString());
122    }
123    }