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

File DefaultIconSetManager.java

 

Coverage histogram

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

Code metrics

12
42
4
1
185
121
14
0.33
10.5
4
3.5

Classes

Class Line # Actions
DefaultIconSetManager 58 42 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 13 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.InputStreamReader;
23    import java.net.MalformedURLException;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.lang.StringUtils;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.configuration.ConfigurationSource;
35    import org.xwiki.icon.IconException;
36    import org.xwiki.icon.IconSet;
37    import org.xwiki.icon.IconSetCache;
38    import org.xwiki.icon.IconSetLoader;
39    import org.xwiki.icon.IconSetManager;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.model.reference.DocumentReferenceResolver;
42    import org.xwiki.query.Query;
43    import org.xwiki.query.QueryException;
44    import org.xwiki.query.QueryManager;
45    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
46   
47    import com.xpn.xwiki.XWiki;
48    import com.xpn.xwiki.XWikiContext;
49   
50    /**
51    * Default implementation of {@link org.xwiki.icon.IconSetManager}.
52    *
53    * @since 6.2M1
54    * @version $Id: 71fe0481d308004f9d1081f481c4d5d595a7c0ea $
55    */
56    @Component
57    @Singleton
 
58    public class DefaultIconSetManager implements IconSetManager
59    {
60    private static final String DEFAULT_ICONSET_NAME = "default";
61   
62    @Inject
63    private Provider<XWikiContext> xcontextProvider;
64   
65    @Inject
66    @Named("current")
67    private DocumentReferenceResolver<String> documentReferenceResolver;
68   
69    @Inject
70    private DocumentAccessBridge documentAccessBridge;
71   
72    @Inject
73    private IconSetCache iconSetCache;
74   
75    @Inject
76    private IconSetLoader iconSetLoader;
77   
78    @Inject
79    private QueryManager queryManager;
80   
81    @Inject
82    private WikiDescriptorManager wikiDescriptorManager;
83   
84    @Inject
85    @Named("all")
86    private ConfigurationSource configurationSource;
87   
 
88  2656 toggle @Override
89    public IconSet getCurrentIconSet() throws IconException
90    {
91    // Get the current icon theme
92  2657 String iconTheme = configurationSource.getProperty("iconTheme");
93   
94    // Get the icon set
95  2657 IconSet iconSet = null;
96  2657 DocumentReference iconThemeDocRef = documentReferenceResolver.resolve(iconTheme);
97  2657 if (!StringUtils.isBlank(iconTheme) && documentAccessBridge.exists(iconThemeDocRef)) {
98  2 iconSet = iconSetCache.get(iconThemeDocRef);
99  2 if (iconSet == null) {
100    // lazy loading
101  1 iconSet = iconSetLoader.loadIconSet(iconThemeDocRef);
102  1 iconSetCache.put(iconThemeDocRef, iconSet);
103  1 iconSetCache.put(iconSet.getName(), wikiDescriptorManager.getCurrentWikiId(), iconSet);
104    }
105    }
106   
107  2656 return iconSet;
108    }
109   
 
110  2658 toggle @Override
111    public IconSet getDefaultIconSet() throws IconException
112    {
113  2658 XWikiContext xcontext = xcontextProvider.get();
114  2658 XWiki xwiki = xcontext.getWiki();
115   
116  2658 IconSet iconSet = iconSetCache.get(DEFAULT_ICONSET_NAME);
117  2658 if (iconSet == null) {
118  31 try {
119    // lazy loading
120  31 iconSet = iconSetLoader.loadIconSet(new InputStreamReader(
121    xwiki.getResourceAsStream("/resources/icons/default.iconset")), DEFAULT_ICONSET_NAME);
122  30 iconSetCache.put(DEFAULT_ICONSET_NAME, iconSet);
123    } catch (IconException | MalformedURLException e) {
124  1 throw new IconException("Failed to get the current default icon set.", e);
125    }
126    }
127   
128  2657 return iconSet;
129    }
130   
 
131  23 toggle @Override
132    public IconSet getIconSet(String name) throws IconException
133    {
134    // Special case: the default icon theme
135  23 if (DEFAULT_ICONSET_NAME.equals(name)) {
136  1 return getDefaultIconSet();
137    }
138   
139    // Get the icon set from the cache
140  22 IconSet iconSet = iconSetCache.get(name, wikiDescriptorManager.getCurrentWikiId());
141   
142    // Load it if it is not loaded yet
143  22 if (iconSet == null) {
144  5 try {
145    // Search by name
146  5 String xwql = "FROM doc.object(IconThemesCode.IconThemeClass) obj WHERE obj.name = :name";
147  5 Query query = queryManager.createQuery(xwql, Query.XWQL);
148  4 query.bindValue("name", name);
149  4 List<String> results = query.execute();
150  4 if (results.isEmpty()) {
151  1 return null;
152    }
153   
154    // Get the first result
155  3 String docName = results.get(0);
156  3 DocumentReference docRef = documentReferenceResolver.resolve(docName);
157   
158    // Load the icon theme
159  3 iconSet = iconSetLoader.loadIconSet(docRef);
160   
161    // Put it in the cache
162  3 iconSetCache.put(docRef, iconSet);
163  3 iconSetCache.put(name, wikiDescriptorManager.getCurrentWikiId(), iconSet);
164    } catch (QueryException e) {
165  1 throw new IconException(String.format("Failed to load the icon set [%s].", name), e);
166    }
167    }
168   
169    // Return the icon set
170  20 return iconSet;
171    }
172   
 
173  11 toggle @Override
174    public List<String> getIconSetNames() throws IconException
175    {
176  11 try {
177  11 String xwql = "SELECT obj.name FROM Document doc, doc.object(IconThemesCode.IconThemeClass) obj "
178    + "ORDER BY obj.name";
179  11 Query query = queryManager.createQuery(xwql, Query.XWQL);
180  10 return query.execute();
181    } catch (QueryException e) {
182  1 throw new IconException("Failed to get the name of all icon sets.", e);
183    }
184    }
185    }