| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.localization.internal; |
| 21 |
|
|
| 22 |
|
import java.util.Collection; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.List; |
| 25 |
|
import java.util.Map; |
| 26 |
|
import java.util.SortedSet; |
| 27 |
|
import java.util.TreeSet; |
| 28 |
|
|
| 29 |
|
import javax.inject.Inject; |
| 30 |
|
import javax.inject.Named; |
| 31 |
|
import javax.inject.Provider; |
| 32 |
|
import javax.inject.Singleton; |
| 33 |
|
|
| 34 |
|
import org.slf4j.Logger; |
| 35 |
|
import org.xwiki.component.annotation.Component; |
| 36 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 37 |
|
import org.xwiki.component.manager.ComponentManager; |
| 38 |
|
import org.xwiki.context.Execution; |
| 39 |
|
import org.xwiki.context.ExecutionContext; |
| 40 |
|
import org.xwiki.localization.TranslationBundle; |
| 41 |
|
import org.xwiki.localization.TranslationBundleContext; |
| 42 |
|
import org.xwiki.model.EntityType; |
| 43 |
|
import org.xwiki.model.ModelContext; |
| 44 |
|
import org.xwiki.model.reference.EntityReference; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@link |
| 48 |
|
|
| 49 |
|
@version |
| 50 |
|
@since |
| 51 |
|
|
| 52 |
|
@Component |
| 53 |
|
@Singleton |
| |
|
| 95.8% |
Uncovered Elements: 2 (48) |
Complexity: 12 |
Complexity Density: 0.38 |
|
| 54 |
|
public class DefaultTranslationBundleContext implements TranslationBundleContext |
| 55 |
|
{ |
| 56 |
|
|
| 57 |
|
@link |
| 58 |
|
|
| 59 |
|
public static final String CKEY_BUNDLES = "localization.bundles"; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
@Inject |
| 65 |
|
private Execution execution; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
@Inject |
| 71 |
|
@Named("context") |
| 72 |
|
private Provider<ComponentManager> componentManagerProvider; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
@Inject |
| 78 |
|
private Logger logger; |
| 79 |
|
|
| 80 |
|
@Inject |
| 81 |
|
private ModelContext modelContext; |
| 82 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 83 |
2911 |
private SortedSet<TranslationBundle> initializeCurrentBundles()... |
| 84 |
|
{ |
| 85 |
2909 |
SortedSet<TranslationBundle> currentBundles = new TreeSet<>(); |
| 86 |
|
|
| 87 |
2911 |
try { |
| 88 |
2909 |
ComponentManager componentManager = this.componentManagerProvider.get(); |
| 89 |
2910 |
List<TranslationBundle> availableBundles = |
| 90 |
|
componentManager.<TranslationBundle>getInstanceList(TranslationBundle.class); |
| 91 |
2911 |
currentBundles.addAll(availableBundles); |
| 92 |
|
} catch (ComponentLookupException e) { |
| 93 |
0 |
this.logger.error("Failed to lookup Bundle components", e); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
2910 |
return currentBundles; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
@return |
| 101 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 102 |
242319 |
private Map<String, SortedSet<TranslationBundle>> getBundlesInternal()... |
| 103 |
|
{ |
| 104 |
242319 |
Map<String, SortedSet<TranslationBundle>> bundles; |
| 105 |
|
|
| 106 |
242320 |
ExecutionContext context = this.execution.getContext(); |
| 107 |
242320 |
if (context != null) { |
| 108 |
242300 |
bundles = (Map<String, SortedSet<TranslationBundle>>) context.getProperty(CKEY_BUNDLES); |
| 109 |
|
|
| 110 |
242298 |
if (bundles == null) { |
| 111 |
|
|
| 112 |
2888 |
bundles = new HashMap<>(); |
| 113 |
2885 |
context.newProperty(CKEY_BUNDLES).inherited().cloneValue().initial(bundles).declare(); |
| 114 |
|
} |
| 115 |
|
} else { |
| 116 |
20 |
bundles = new HashMap<>(); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
242320 |
return bundles; |
| 120 |
|
} |
| 121 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 122 |
242308 |
private SortedSet<TranslationBundle> getCurrentBundlesInternal()... |
| 123 |
|
{ |
| 124 |
242308 |
String currentWiki = getCurrentWiki(); |
| 125 |
242318 |
Map<String, SortedSet<TranslationBundle>> bundlesMap = getBundlesInternal(); |
| 126 |
242320 |
SortedSet<TranslationBundle> currentBundles = bundlesMap.get(currentWiki); |
| 127 |
|
|
| 128 |
242321 |
if (currentBundles == null) { |
| 129 |
|
|
| 130 |
2910 |
currentBundles = initializeCurrentBundles(); |
| 131 |
2910 |
bundlesMap.put(currentWiki, currentBundles); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
242320 |
return currentBundles; |
| 135 |
|
} |
| 136 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 137 |
242306 |
@Override... |
| 138 |
|
public Collection<TranslationBundle> getBundles() |
| 139 |
|
{ |
| 140 |
242307 |
return getCurrentBundlesInternal(); |
| 141 |
|
} |
| 142 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
2 |
@Override... |
| 144 |
|
public void addBundle(TranslationBundle bundle) |
| 145 |
|
{ |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
2 |
getCurrentBundlesInternal().add(bundle); |
| 151 |
|
} |
| 152 |
|
|
| |
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 153 |
242307 |
private String getCurrentWiki()... |
| 154 |
|
{ |
| 155 |
|
|
| 156 |
|
|
| 157 |
242308 |
String currentWiki = ""; |
| 158 |
|
|
| 159 |
242307 |
EntityReference currentReference = modelContext.getCurrentEntityReference(); |
| 160 |
242320 |
if (currentReference != null) { |
| 161 |
242300 |
EntityReference wikiReference = currentReference.extractReference(EntityType.WIKI); |
| 162 |
242299 |
if (wikiReference != null) { |
| 163 |
242300 |
currentWiki = wikiReference.getName(); |
| 164 |
|
} |
| 165 |
|
} |
| 166 |
|
|
| 167 |
242317 |
return currentWiki; |
| 168 |
|
} |
| 169 |
|
} |