use of org.osgi.service.log.LogService in project eclipse.platform.runtime by eclipse.
the class ResourceBundleHelper method toLocale.
/**
* <p>
* Converts a String to a Locale.
* </p>
*
* <p>
* This method takes the string format of a locale and creates the locale object from it.
* </p>
*
* <pre>
* MessageFactoryServiceImpl.toLocale("en") = new Locale("en", "")
* MessageFactoryServiceImpl.toLocale("en_GB") = new Locale("en", "GB")
* MessageFactoryServiceImpl.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx")
* </pre>
*
* <p>
* This method validates the input strictly. The language code must be lowercase. The country
* code must be uppercase. The separator must be an underscore. The length must be correct.
* </p>
*
* <p>
* This method is inspired by <code>org.apache.commons.lang.LocaleUtils.toLocale(String)</code>
* by fixing the parsing error for uncommon Locales like having a language and a variant code
* but no country code, or a Locale that only consists of a country code.
* </p>
*
* <p>
* <b>Note:</b> This is the same logic as used in <code>EquinoxConfiguration.toLocale()</code>
* </p>
*
* @param localeString
* the locale String to convert
* @param defaultLocale
* the Locale that should be returned in case of an invalid Locale String
* @return a Locale that matches the specified locale String. If the given input String is
* <code>null</code> or can not be parsed because of an invalid format, the given
* default {@link Locale} will be returned.
*/
public static Locale toLocale(String localeString, Locale defaultLocale) {
LogService logService = logTracker.getService();
if (localeString == null) {
if (logService != null)
logService.log(LogService.LOG_ERROR, "Given locale String is null" + // $NON-NLS-1$
" - Default Locale will be used instead.");
return defaultLocale;
}
// $NON-NLS-1$
String language = "";
// $NON-NLS-1$
String country = "";
// $NON-NLS-1$
String variant = "";
// $NON-NLS-1$
String[] localeParts = localeString.split("_");
if (localeParts.length == 0 || localeParts.length > 3 || (localeParts.length == 1 && localeParts[0].length() == 0)) {
logInvalidFormat(localeString, logService);
return defaultLocale;
}
if (localeParts[0].length() > 0 && !localeParts[0].matches("[a-zA-Z]{2,8}")) {
logInvalidFormat(localeString, logService);
return defaultLocale;
}
language = localeParts[0];
if (localeParts.length > 1) {
if (localeParts[1].length() > 0 && !localeParts[1].matches("[a-zA-Z]{2}|[0-9]{3}")) {
if (language.length() > 0) {
if (logService != null)
logService.log(LogService.LOG_ERROR, "Invalid locale format: " + localeString + // $NON-NLS-1$
" - Only language part will be used to create the Locale.");
return new Locale(language);
}
logInvalidFormat(localeString, logService);
return defaultLocale;
}
country = localeParts[1];
}
if (localeParts.length == 3) {
if (localeParts[2].length() == 0) {
if (logService != null)
logService.log(LogService.LOG_ERROR, "Invalid locale format: " + localeString + // $NON-NLS-1$
" - Only language and country part will be used to create the Locale.");
return new Locale(language, country);
}
variant = localeParts[2];
}
return new Locale(language, country, variant);
}
use of org.osgi.service.log.LogService in project ecf by eclipse.
the class RssPlugin method getLogService.
protected LogService getLogService() {
if (logServiceTracker == null) {
logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
logServiceTracker.open();
}
return (LogService) logServiceTracker.getService();
}
use of org.osgi.service.log.LogService in project ecf by eclipse.
the class Activator method getLogService.
protected LogService getLogService() {
synchronized (this) {
if (this.context == null)
return null;
if (logServiceTracker == null) {
logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
logServiceTracker.open();
}
return (LogService) logServiceTracker.getService();
}
}
use of org.osgi.service.log.LogService in project ecf by eclipse.
the class Activator method getLogService.
protected LogService getLogService() {
if (logServiceTracker == null) {
logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
logServiceTracker.open();
}
return (LogService) logServiceTracker.getService();
}
use of org.osgi.service.log.LogService in project ecf by eclipse.
the class Activator method getLogService.
protected LogService getLogService() {
if (logServiceTracker == null) {
logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
logServiceTracker.open();
}
return (LogService) logServiceTracker.getService();
}
Aggregations