Skip to content

Commit

Permalink
sanitized null locales in Dict for #10
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Mar 4, 2016
1 parent 4df1681 commit a3a8797
Showing 1 changed file with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

Expand All @@ -39,6 +41,8 @@
@Immutable
public class Dict implements IDict {

private static final Logger LOG = Logger.getLogger(Dict.class.getName());

private Map<Locale, List<String>> translations;

public Dict() {
Expand All @@ -53,7 +57,7 @@ protected Dict(@Nullable IDict dict) {
for (String s : dict.getStrings(loc)) {
names.add(s);
}
this.translations.put(loc, Collections.unmodifiableList(names));
this.translations.put(sanitizeLocale(loc), Collections.unmodifiableList(names));
}
}
}
Expand Down Expand Up @@ -94,13 +98,27 @@ public Dict(final String text, Locale locale) {
} */


translations.put(locale, new ArrayList() {
translations.put(sanitizeLocale(locale), new ArrayList() {
{
add(text);
}
});
}

/**
* Concerts null locale to {@link Locale#ROOT}
* @since 0.26.3
* @param locale
*/
private Locale sanitizeLocale(@Nullable Locale locale){
if (locale == null){
LOG.warning("Found null locale, converting it to Locale.ROOT (the corresponding language tag is the empty string)");
return Locale.ROOT;
} else {
return locale;
}
}

/**
* Sets translations to provided locale
*/
Expand All @@ -113,9 +131,8 @@ public Dict(final List<String> texts, Locale locale) {
throw new IllegalArgumentException("null texts are not allowed in a " + Dict.class.getSimpleName());
}
ts.add(s);
}
// todo put null check on locale, see https://github.com/opendatatrentino/openentity-api/issues/7
this.translations.put(locale, texts);
}
this.translations.put(sanitizeLocale(locale), texts);
}

public Set<Locale> getLocales() {
Expand Down Expand Up @@ -154,8 +171,8 @@ public Dict putTranslation(Locale locale, List<String> strings) {
newTranslations.put(loc, translations.get(loc));
}

// todo check null locale https://github.com/opendatatrentino/openentity-api/issues/7
newTranslations.put(locale, Collections.unmodifiableList(lst));
// todo check null locale https://github.com/opendatatrentino/openentity-api/issues/7
newTranslations.put(sanitizeLocale(locale), Collections.unmodifiableList(lst));

return new Dict(newTranslations);
}
Expand All @@ -177,7 +194,7 @@ public Dict putTranslation(Locale locale, String string) {
for (Locale loc : translations.keySet()) {
newTranslations.put(loc, translations.get(loc));
}
newTranslations.put(locale, Collections.unmodifiableList(lst));
newTranslations.put(sanitizeLocale(locale), Collections.unmodifiableList(lst));

return new Dict(newTranslations);
}
Expand Down Expand Up @@ -279,14 +296,15 @@ private static String padLeft(String msg, int maxLength) {
public Dict merge(IDict dict) {
Dict ret = new Dict(this);
for (Locale locale : dict.getLocales()) {
if (ret.translations.containsKey(locale)) {
Locale sanitizedlocale = sanitizeLocale(locale);
if (ret.translations.containsKey(sanitizedlocale)) {
for (String t : dict.getStrings(locale)) {
if (!ret.translations.get(locale).contains(t)) {
ret.translations.get(locale).add(t);
if (!ret.translations.get(sanitizedlocale).contains(t)) {
ret.translations.get(sanitizedlocale).add(t);
}
}
} else {
ret.translations.put(locale, dict.getStrings(locale));
ret.translations.put(sanitizedlocale, dict.getStrings(locale));
}
}
return ret;
Expand Down

0 comments on commit a3a8797

Please sign in to comment.