package org.apache.commons.vfs2.util;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
public class CombinedResources extends ResourceBundle {
private final String resourceName;
private boolean inited;
private final Properties properties = new Properties();
public CombinedResources(final String resourceName) {
this.resourceName = resourceName;
}
protected void init() {
if (inited) {
return;
}
loadResources(getResourceName());
loadResources(Locale.getDefault());
loadResources(getLocale());
inited = true;
}
protected void loadResources(final Locale locale) {
if (locale == null) {
return;
}
final String[] parts = new String[] { locale.getLanguage(), locale.getCountry(), locale.getVariant() };
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
sb.append(getResourceName());
for (int j = 0; j < i; j++) {
sb.append('_').append(parts[j]);
}
if (parts[i].length() != 0) {
sb.append('_').append(parts[i]);
loadResources(sb.toString());
}
sb.setLength(0);
}
}
protected void loadResources(String resourceName) {
ClassLoader loader = getClass().getClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
if (loader != null) {
resourceName = resourceName.replace('.', '/') + ".properties";
try {
final Enumeration<URL> resources = loader.getResources(resourceName);
while (resources.hasMoreElements()) {
final URL resource = resources.nextElement();
try {
properties.load(resource.openConnection().getInputStream());
} catch (final IOException ignored) {
}
}
} catch (final IOException ignored) {
}
}
}
public String getResourceName() {
return resourceName;
}
@Override
public Enumeration<String> getKeys() {
if (!inited) {
init();
}
return new Enumeration<String>() {
@Override
public boolean hasMoreElements() {
return properties.keys().hasMoreElements();
}
@Override
public String nextElement() {
return (String) properties.keys().nextElement();
}
};
}
@Override
protected Object handleGetObject(final String key) {
if (!inited) {
init();
}
return properties.get(key);
}
}