| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 52 |
|
|
| 53 |
|
@since |
| 54 |
|
@version |
| 55 |
|
|
| 56 |
|
@Component |
| 57 |
|
@Singleton |
| |
|
| 100% |
Uncovered Elements: 0 (58) |
Complexity: 14 |
Complexity Density: 0.33 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 88 |
2656 |
@Override... |
| 89 |
|
public IconSet getCurrentIconSet() throws IconException |
| 90 |
|
{ |
| 91 |
|
|
| 92 |
2657 |
String iconTheme = configurationSource.getProperty("iconTheme"); |
| 93 |
|
|
| 94 |
|
|
| 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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 110 |
2658 |
@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 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 5 |
Complexity Density: 0.28 |
|
| 131 |
23 |
@Override... |
| 132 |
|
public IconSet getIconSet(String name) throws IconException |
| 133 |
|
{ |
| 134 |
|
|
| 135 |
23 |
if (DEFAULT_ICONSET_NAME.equals(name)) { |
| 136 |
1 |
return getDefaultIconSet(); |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
22 |
IconSet iconSet = iconSetCache.get(name, wikiDescriptorManager.getCurrentWikiId()); |
| 141 |
|
|
| 142 |
|
|
| 143 |
22 |
if (iconSet == null) { |
| 144 |
5 |
try { |
| 145 |
|
|
| 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 |
|
|
| 155 |
3 |
String docName = results.get(0); |
| 156 |
3 |
DocumentReference docRef = documentReferenceResolver.resolve(docName); |
| 157 |
|
|
| 158 |
|
|
| 159 |
3 |
iconSet = iconSetLoader.loadIconSet(docRef); |
| 160 |
|
|
| 161 |
|
|
| 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 |
|
|
| 170 |
20 |
return iconSet; |
| 171 |
|
} |
| 172 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 173 |
11 |
@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 |
|
} |