1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/** Gets available locales for given resource and set.
* @param resourceID The resource ID.
* @param set The resource set.
* @return An array of <code>Locale</code> instance, never <code>null</code>.
* @since 2.9
*/
public Locale[] getAvailableLocales(String resourceID, String set) {
if ( (resourceID == null) || (resourceID.trim().length() == 0)) {
resourceID = ResourceConstants.STRING_RESOURCE;
}
if ( (set == null) || (set.trim().length() == 0)) {
set = DEFAULT_SET;
}
String bundlePath = ResourceConstants.RESOURCES_PATH + set + ResourceConstants.FILE_SEPARATOR;
final String basePrefix = bundlePath + resourceID;
final String baseSuffix = ".properties";
String baseFile = basePrefix + baseSuffix;
URL url = getClass().getClassLoader().getResource(baseFile);
System.out.println(bundlePath + " => " + url);
Locale[] result = null;
// We have a hold on a base translation file.
if (url != null) {
java.util.List<Locale> localeList = null;
String filePath = url.toString();
//System.out.println("The file path is " + filePath);
java.util.List<String> filenameList = null;
boolean useJar = false;
////////////////////////////////
// We are currently accessing a JAR file.
if (filePath.startsWith("jar:")) {
useJar = true;
// Beware of different protocols and platforms.
// Works under Windows.
/** @todo Should be tested on Linux, Mac and Java Web Start ASAP. */
/** @todo test, test and test !*/
filePath = filePath.replaceFirst("jar:file:/", "");
int index = filePath.indexOf("!");
filePath = filePath.substring(0, index);
//System.out.println("Trying to open " + filePath + " as a JAR");
try {
JarFile jar = new JarFile(filePath);
// Unfortunatly it seems it's impossible to get the content of a directory in a JAR.
// As of NOW we scan through the entire list of entries.
// It seems to be reasonably fast though.
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String entryName = entry.getName();
if (entryName.contains(basePrefix) && entryName.endsWith(baseSuffix)) {
if (filenameList == null) {
filenameList = new LinkedList<String>();
}
filenameList.add(entryName);
//System.out.printf("%s, directory %b\n", entry.getName(), entry.isDirectory());
}
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
////////////////////////////////
// We are currently accessing a directory.
else {
File hostDirectory = new File(url.getFile()).getParentFile();
if ( (hostDirectory != null)) { //&& (hostDirectory.isDirectory())) {
//System.out.printf("File %s (%s), exist %b, directory %b, readable %b\n", hostDirectory, url.toString(), hostDirectory.exists(), hostDirectory.isDirectory(), hostDirectory.canRead());
final String resourceName = resourceID;
FileFilter filter = new FileFilter() {
/** {@inheritDoc}
*/
public boolean accept(File pathname) {
String path = pathname.getName();
return (path.startsWith(resourceName) && path.endsWith(baseSuffix));
}
};
File[] files = hostDirectory.listFiles(filter);
if (files != null) {
filenameList = new LinkedList<String>();
for (File file : files) {
filenameList.add(file.getName());
}
}
}
}
////////////////////////////////
// Create locales from the filenames.
if ( (filenameList != null) && (filenameList.size() > 0)) {
localeList = new LinkedList<Locale>();
for (String name : filenameList) {
if (useJar) {
name = name.replaceAll(basePrefix, "");
}
else {
name = name.replaceAll(resourceID, "");
}
name = name.replaceAll(baseSuffix, "");
if (name.startsWith("_")) {
name = name.substring(1);
}
Locale locale = ResourceConstants.DEFAULT_LOCALE;
if (name.length() > 0) {
String language = "";
String country = "";
String variant = "";
StringTokenizer tokenizer = new StringTokenizer(name, "_");
if (tokenizer.countTokens() >= 1) {
language = tokenizer.nextToken();
}
if (tokenizer.countTokens() >= 1) {
country = tokenizer.nextToken();
}
if (tokenizer.countTokens() >= 1) {
variant = tokenizer.nextToken();
}
locale = new Locale(language, country, variant);
}
if (!localeList.contains(locale)) {
//System.out.println("Found locale " + locale);
localeList.add(locale);
}
}
////////////////////////////////
// Transform into array.
result = new Locale[localeList.size()];
result = localeList.toArray(result);
localeList.clear();
}
}
////////////////////////////////
// Could not get locales from file inspection, return locales supported by the JVM.
// This operation can take a long time to execute.
if (result == null) {
result = Locale.getAvailableLocales();
}
return result;
} |
Partager