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

File DefaultIconSetLoader.java

 

Coverage histogram

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

Code metrics

12
30
2
1
126
79
10
0.33
15
2
5

Classes

Class Line # Actions
DefaultIconSetLoader 49 30 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 4 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.IOException;
23    import java.io.Reader;
24    import java.io.StringReader;
25    import java.util.Properties;
26   
27    import javax.inject.Inject;
28    import javax.inject.Singleton;
29   
30    import org.xwiki.bridge.DocumentAccessBridge;
31    import org.xwiki.bridge.DocumentModelBridge;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.icon.Icon;
34    import org.xwiki.icon.IconException;
35    import org.xwiki.icon.IconSet;
36    import org.xwiki.icon.IconSetLoader;
37    import org.xwiki.icon.IconType;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
40   
41    /**
42    * Default implementation of {@link org.xwiki.icon.IconSetLoader}.
43    *
44    * @since 6.2M1
45    * @version $Id: a5ec9dcd847c86b6f46156d464d86d4e6dad7d59 $
46    */
47    @Component
48    @Singleton
 
49    public class DefaultIconSetLoader implements IconSetLoader
50    {
51    private static final String CSS_PROPERTY_NAME = "xwiki.iconset.css";
52   
53    private static final String SSX_PROPERTY_NAME = "xwiki.iconset.ssx";
54   
55    private static final String JSX_PROPERTY_NAME = "xwiki.iconset.jsx";
56   
57    private static final String RENDER_WIKI_PROPERTY_NAME = "xwiki.iconset.render.wiki";
58   
59    private static final String RENDER_HTML_PROPERTY_NAME = "xwiki.iconset.render.html";
60   
61    private static final String ICON_TYPE_PROPERTY_NAME = "xwiki.iconset.type";
62   
63    private static final String ERROR_MSG = "Failed to load the IconSet [%s].";
64   
65    @Inject
66    private DocumentAccessBridge documentAccessBridge;
67   
68    @Inject
69    private WikiDescriptorManager wikiDescriptorManager;
70   
 
71  4 toggle @Override
72    public IconSet loadIconSet(DocumentReference iconSetReference) throws IconException
73    {
74  4 try {
75    // Get the document
76  4 DocumentModelBridge doc = documentAccessBridge.getDocument(iconSetReference);
77  3 String content = doc.getContent();
78    // The name of the icon set is stored in the IconThemesCode.IconThemeClass XObject of the document
79  3 DocumentReference iconClassRef = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(),
80    "IconThemesCode", "IconThemeClass");
81  3 String name = (String) documentAccessBridge.getProperty(iconSetReference, iconClassRef, "name");
82    // Load the icon set
83  3 return loadIconSet(new StringReader(content), name);
84    } catch (Exception e) {
85  1 throw new IconException(String.format(ERROR_MSG, iconSetReference), e);
86    }
87    }
88   
 
89  33 toggle @Override
90    public IconSet loadIconSet(Reader input, String name) throws IconException
91    {
92  33 try {
93  33 IconSet iconSet = new IconSet(name);
94   
95  33 Properties properties = new Properties();
96  33 properties.load(input);
97   
98    // Load all the keys
99  32 for (String key : properties.stringPropertyNames()) {
100  32009 String value = properties.getProperty(key);
101  32009 if (key.equals(CSS_PROPERTY_NAME)) {
102  2 iconSet.setCss(value);
103  32007 } else if (key.equals(SSX_PROPERTY_NAME)) {
104  3 iconSet.setSsx(value);
105  32004 } else if (key.equals(JSX_PROPERTY_NAME)) {
106  3 iconSet.setJsx(value);
107  32001 } else if (key.equals(RENDER_WIKI_PROPERTY_NAME)) {
108  32 iconSet.setRenderWiki(value);
109  31969 } else if (key.equals(RENDER_HTML_PROPERTY_NAME)) {
110  32 iconSet.setRenderHTML(value);
111  31937 } else if (key.equals(ICON_TYPE_PROPERTY_NAME)) {
112  32 iconSet.setType(IconType.valueOf(value.toUpperCase()));
113    } else {
114  31905 Icon icon = new Icon();
115  31905 icon.setValue(properties.getProperty(key));
116  31905 iconSet.addIcon(key, icon);
117    }
118    }
119   
120    // return
121  32 return iconSet;
122    } catch (IOException e) {
123  1 throw new IconException(String.format(ERROR_MSG, name), e);
124    }
125    }
126    }