if (annotations.isEmpty()) {
return true;
}
try {
Filer filer = processingEnv.getFiler();
Map<String, MessageBundleInfo> messageBundles = new HashMap<String, MessageBundleInfo>();
for (Element e : roundEnv
.getElementsAnnotatedWith(MessageBundle.class)) {
if (e instanceof TypeElement) {
TypeElement messageBundleElement = (TypeElement) e;
String messageBundle = messageBundleElement
.getQualifiedName().toString();
String messageBundleSimpleName = messageBundleElement.getSimpleName().toString();
String messageBundleEnumName = messageBundle + "Enum";
Set<String> messages = new HashSet<String>();
for (Element messageBundleChild : messageBundleElement
.getEnclosedElements()) {
if (messageBundleChild instanceof ExecutableElement) {
messages.add(getKey((ExecutableElement) messageBundleChild));
}
}
if (!messages.isEmpty()) {
String baseName = messageBundle.replaceAll("\\.", "/");
FileObject javaFileObject;
try {
javaFileObject = filer.getResource(
StandardLocation.SOURCE_PATH, "", baseName
+ ".java");
} catch(FileNotFoundException ex) {
throw new IllegalArgumentException("Could not find the source file '" + baseName + ".java' that actually triggered the enum generation process", ex);
}
long lastModified = javaFileObject.getLastModified();
Set<Locale> locales = new TreeSet<Locale>(LOCALE_COMPARATOR);
URI baseUri = null;
// Here we assume that the resources are present in the
// source output location
try {
// Normally there is an english properties file
try {
baseUri = filer.getResource(
StandardLocation.CLASS_PATH, "",
baseName + "_en.properties").toUri();
} catch (FileNotFoundException ex1) {
try {
baseUri = filer.getResource(
StandardLocation.CLASS_OUTPUT, "",
baseName + "_en.properties").toUri();
} catch (FileNotFoundException ex2) {
baseUri = filer.getResource(
StandardLocation.SOURCE_PATH, "",
baseName + "_en.properties").toUri();
}
}
} catch (FileNotFoundException ex1) {
// Otherwise we have to go through all available
// locales
for (Locale l : Locale.getAvailableLocales()) {
String path;
try {
path = baseName + "_" + l.getLanguage()
+ ".properties";
baseUri = filer.getResource(
StandardLocation.CLASS_PATH, "",
path).toUri();
break;
} catch (FileNotFoundException ex2) {
try {
path = baseName + "_" + l.getLanguage()
+ ".properties";
baseUri = filer.getResource(
StandardLocation.CLASS_OUTPUT, "",
path).toUri();
break;
} catch (FileNotFoundException ex3) {
try {
path = baseName + "_" + l.getLanguage()
+ ".properties";
baseUri = filer.getResource(
StandardLocation.SOURCE_PATH, "",
path).toUri();
break;
} catch (FileNotFoundException ex4) {
// Nothing we can do about it
}
}
}
if (l.getCountry() != null
&& !l.getCountry().isEmpty()) {
try {
path = baseName + "_" + l.getLanguage()
+ "_" + l.getCountry()
+ ".properties";
baseUri = filer.getResource(
StandardLocation.CLASS_PATH,
"", path).toUri();
break;
} catch (FileNotFoundException ex2) {
try {
path = baseName + "_" + l.getLanguage()
+ "_" + l.getCountry()
+ ".properties";
baseUri = filer.getResource(
StandardLocation.CLASS_OUTPUT,
"", path).toUri();
break;
} catch (FileNotFoundException ex3) {
try {
path = baseName + "_" + l.getLanguage()
+ "_" + l.getCountry()
+ ".properties";
baseUri = filer.getResource(
StandardLocation.SOURCE_PATH,
"", path).toUri();
break;
} catch (FileNotFoundException ex4) {
// Nothing we can do about it
}
}
}
}
}
}
if (baseUri == null) {
throw new IllegalArgumentException(
"No properties files could be found for '"
+ messageBundle + "'");
}
File baseDir = new File(baseUri).getParentFile();
if(!baseDir.exists() || !baseDir.isDirectory()) {
throw new IllegalArgumentException("The base directory for the properties files could not be found!");
}
for (File propertiesFile : baseDir.listFiles()) {
// Only check properties files that belong to the message bundle
if(propertiesFile.getName().startsWith(messageBundleSimpleName + "_") || propertiesFile.getName().equals(messageBundleSimpleName + ".properties")) {
checkPropertiesFile(propertiesFile, messages);
String name = propertiesFile.getName();
int index = name.indexOf('_');
int dotIndex = name.lastIndexOf('.');
if (index > -1 && dotIndex > -1) {
locales.add(LocaleUtils.getLocale(name
.substring(index + 1, dotIndex)));
}
}
}
MessageBundleInfo info = new MessageBundleInfo(
baseName, lastModified, locales, messages);
messageBundles.put(messageBundleEnumName, info);
}
}
}
for (Map.Entry<String, MessageBundleInfo> entry : messageBundles
.entrySet()) {
String className = entry.getKey();
JavaFileObject jfo = filer.createSourceFile(className);
if (jfo.getLastModified() == entry.getValue().getLastModified()) {
// Skip unchanged files
continue;
}