package com.sun.tools.javac.util;
import com.sun.tools.javac.api.Messages;
import java.lang.ref.SoftReference;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import com.sun.tools.javac.api.DiagnosticFormatter;
import com.sun.tools.javac.util.JCDiagnostic.Factory;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
public class JavacMessages implements Messages {
public static final Context.Key<JavacMessages> messagesKey = new Context.Key<>();
public static JavacMessages instance(Context context) {
JavacMessages instance = context.get(messagesKey);
if (instance == null)
instance = new JavacMessages(context);
return instance;
}
private Map<Locale, SoftReference<List<ResourceBundle>>> bundleCache;
private List<ResourceBundleHelper> bundleHelpers;
private Locale currentLocale;
private List<ResourceBundle> currentBundles;
private DiagnosticFormatter<JCDiagnostic> diagFormatter;
private JCDiagnostic.Factory diagFactory;
public Locale getCurrentLocale() {
return currentLocale;
}
public void setCurrentLocale(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
this.currentBundles = getBundles(locale);
this.currentLocale = locale;
}
Context context;
public JavacMessages(Context context) {
this(defaultBundleName, context.get(Locale.class));
this.context = context;
context.put(messagesKey, this);
Options options = Options.instance(context);
boolean rawDiagnostics = options.isSet("rawDiagnostics");
this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
new BasicDiagnosticFormatter(options, this);
}
public JavacMessages(String bundleName) throws MissingResourceException {
this(bundleName, null);
}
public JavacMessages(String bundleName, Locale locale) throws MissingResourceException {
bundleHelpers = List.nil();
bundleCache = new HashMap<>();
add(bundleName);
setCurrentLocale(locale);
}
public JavacMessages() throws MissingResourceException {
this(defaultBundleName);
}
@Override
public void add(String bundleName) throws MissingResourceException {
add(locale -> ResourceBundle.getBundle(bundleName, locale));
}
public void add(ResourceBundleHelper ma) {
bundleHelpers = bundleHelpers.prepend(ma);
if (!bundleCache.isEmpty())
bundleCache.clear();
currentBundles = null;
}
public List<ResourceBundle> getBundles(Locale locale) {
if (locale == currentLocale && currentBundles != null)
return currentBundles;
SoftReference<List<ResourceBundle>> bundles = bundleCache.get(locale);
List<ResourceBundle> bundleList = bundles == null ? null : bundles.get();
if (bundleList == null) {
bundleList = List.nil();
for (ResourceBundleHelper helper : bundleHelpers) {
try {
ResourceBundle rb = helper.getResourceBundle(locale);
bundleList = bundleList.prepend(rb);
} catch (MissingResourceException e) {
throw new InternalError("Cannot find requested resource bundle for locale " +
locale, e);
}
}
bundleCache.put(locale, new SoftReference<>(bundleList));
}
return bundleList;
}
public String getLocalizedString(String key, Object... args) {
return getLocalizedString(currentLocale, key, args);
}
public String getLocalizedString(JCDiagnostic.DiagnosticInfo diagInfo) {
return getLocalizedString(currentLocale, diagInfo);
}
@Override
public String getLocalizedString(Locale l, String key, Object... args) {
if (l == null)
l = getCurrentLocale();
return getLocalizedString(getBundles(l), key, args);
}
public String getLocalizedString(Locale l, JCDiagnostic.DiagnosticInfo diagInfo) {
if (l == null)
l = getCurrentLocale();
return getLocalizedString(getBundles(l), diagInfo);
}
private static final String defaultBundleName = "com.sun.tools.javac.resources.compiler";
private static ResourceBundle defaultBundle;
private static JavacMessages defaultMessages;
static String getDefaultLocalizedString(String key, Object... args) {
return getLocalizedString(List.of(getDefaultBundle()), key, args);
}
@Deprecated
static JavacMessages getDefaultMessages() {
if (defaultMessages == null)
defaultMessages = new JavacMessages(defaultBundleName);
return defaultMessages;
}
public static ResourceBundle getDefaultBundle() {
try {
if (defaultBundle == null)
defaultBundle = ResourceBundle.getBundle(defaultBundleName);
return defaultBundle;
}
catch (MissingResourceException e) {
throw new Error("Fatal: Resource for compiler is missing", e);
}
}
static private String getLocalizedString(List<ResourceBundle> bundles,
String key,
Object... args) {
String msg = null;
for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
ResourceBundle rb = l.head;
try {
msg = rb.getString(key);
}
catch (MissingResourceException e) {
}
}
if (msg == null) {
msg = "compiler message file broken: key=" + key +
" arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
}
return MessageFormat.format(msg, args);
}
private String getLocalizedString(List<ResourceBundle> bundles, JCDiagnostic.DiagnosticInfo diagInfo) {
String msg = null;
for (List<ResourceBundle> l = bundles; l.nonEmpty() && msg == null; l = l.tail) {
ResourceBundle rb = l.head;
try {
msg = rb.getString(diagInfo.key());
}
catch (MissingResourceException e) {
}
}
if (msg == null) {
msg = "compiler message file broken: key=" + diagInfo.key() +
" arguments={0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}";
}
if (diagInfo == Errors.Error) {
return MessageFormat.format(msg, new Object[0]);
} else {
return diagFormatter.format(getDiagFactory().create(DiagnosticSource.NO_SOURCE, null, diagInfo),
getCurrentLocale());
}
}
JCDiagnostic.Factory getDiagFactory() {
if (diagFactory == null) {
this.diagFactory = JCDiagnostic.Factory.instance(context);
}
return diagFactory;
}
public interface ResourceBundleHelper {
ResourceBundle getResourceBundle(Locale locale);
}
}