From: Chris L. <chr...@jb...> - 2006-03-03 20:43:07
|
User: claprun Date: 06/03/03 15:42:53 Modified: common/src/main/org/jboss/portal/common/util LocaleInfo.java LocalizedString.java Log: - Updated javadoc: we don't do full RFC 3066 support. - Modified method name. - Adapted PortalContainerBridge to code changes. Revision Changes Path 1.7 +50 -29 jboss-portal/common/src/main/org/jboss/portal/common/util/LocaleInfo.java (In the diff below, changes in quantity of whitespace are not shown.) Index: LocaleInfo.java =================================================================== RCS file: /cvsroot/jboss/jboss-portal/common/src/main/org/jboss/portal/common/util/LocaleInfo.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -b -r1.6 -r1.7 --- LocaleInfo.java 3 Mar 2006 17:46:28 -0000 1.6 +++ LocaleInfo.java 3 Mar 2006 20:42:53 -0000 1.7 @@ -21,25 +21,25 @@ */ package org.jboss.portal.common.util; +import org.apache.log4j.Logger; + +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Arrays; -import java.util.List; -import java.util.ArrayList; -import java.util.Collections; -import java.util.regex.Pattern; import java.util.regex.Matcher; - -import org.apache.log4j.Logger; +import java.util.regex.Pattern; /** * Additional infos about a locale. * * @author <a href="mailto:ju...@jb...">Julien Viet</a> - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * @noinspection ALL */ public final class LocaleInfo @@ -47,13 +47,19 @@ // Static fields **************************************************************************************************** - /** The logger. */ + /** + * The logger. + */ private static Logger log = Logger.getLogger(LocaleInfo.class); - /** The locale info map. */ + /** + * The locale info map. + */ private static volatile Infos sharedInfos = createFromAvailableLocales(); - /** Separator. */ + /** + * Separator. + */ private static final String RFC3066_SEPARATOR = "-"; /** @@ -91,16 +97,24 @@ // Fields *********************************************************************************************************** - /** The parent locale info or null if no parent. */ + /** + * The parent locale info or null if no parent. + */ private LocaleInfo parent; - /** The relaled locale. */ + /** + * The relaled locale. + */ private Locale locale; - /** The trailing name used to compute resource bundle name. */ + /** + * The trailing name used to compute resource bundle name. + */ private String trailingName; - /**The RFC3066 language tag. */ + /** + * The RFC3066 language tag. + */ private String rfc3066LangageTag; // Constructor ****************************************************************************************************** @@ -134,7 +148,11 @@ } /** - * Retrieves a <a href="http://www.ietf.org/rfc/rfc3066.txt">IETF RFC 3066</a>-compatible language tag. + * Retrieves the language identification tag associated to this LocaleInfo as defined by + * <a href="http://www.ietf.org/rfc/rfc3066.txt">IETF RFC 3066</a> limited to 2-letter + * language code per ISO standard 639, a "-" (dash) and a 2-letter country code per + * ISO 3166 alpha-2 country codes. E.g. "en-US" for American English, "en-GB" for + * British English, etc. * * @return a <a href="http://www.ietf.org/rfc/rfc3066.txt">IETF RFC 3066</a>-compatible language tag. * @throws IllegalArgumentException if the given locale is not valid @@ -187,15 +205,15 @@ /** * <p>Get an info object.</p> - * + * <p/> * <p>Whenever the info object cannot be found then it creates a new one and possibly several * since each info object may have a non null parent.</p> - * + * <p/> * <p>If the infos argument is null, then the static shared infos is used to perform a lookup. * If the lookup returns null then the shared infos is cloned and updated with the newly created * info objects. The operation performed is a copy on write and is thread safe. Note that concurrent * updates may cause the lost of info object in the shared infos.</p> - * + * <p/> * <p>If the infos argument is not null, then it is used to perform a lookup first. If the lookup returns null * then the infos object udpated with the newly created info objects. Note that no cloning of the infos object * is performed.</p> @@ -276,7 +294,7 @@ // Initialize all objects Infos workInfos = new Infos(); Locale[] temp = Locale.getAvailableLocales(); - for (int i = 0;i < temp.length;i++) + for (int i = 0; i < temp.length; i++) { Locale locale = temp[i]; getInfo(locale, workInfos); @@ -309,24 +327,27 @@ } /** - * Returns an info object based on the given compound language String (as defined by - * <a href="http://www.ietf.org/rfc/rfc3066.txt">IETF RFC 3066</a>). + * Returns an info object based on the given language identification tag as defined by + * <a href="http://www.ietf.org/rfc/rfc3066.txt">IETF RFC 3066</a> limited to 2-letter + * language code per ISO standard 639, a "-" (dash) and a 2-letter country code per + * ISO 3166 alpha-2 country codes. E.g. "en-US" for American English, "en-GB" for + * British English, etc. * - * @param compoundLanguage the language tag to build the Locale frome + * @param languageIdentificationTag the language tag to build the Locale from * @return a LocaleInfo associated to the given compound language tag * @throws IllegalArgumentException if the given compound language is not a valid one or null * @since 2.4 */ - public static LocaleInfo decodeLocaleInfoFromRFC3066LanguageTag(String compoundLanguage) + public static LocaleInfo decodeLocaleInfoFromRFC3066LanguageTag(String languageIdentificationTag) { - if (compoundLanguage == null) + if (languageIdentificationTag == null) { throw new IllegalArgumentException(""); } - LocaleInfo info = (LocaleInfo)sharedInfos.byRFC3066LanguageTag.get(compoundLanguage); + LocaleInfo info = (LocaleInfo)sharedInfos.byRFC3066LanguageTag.get(languageIdentificationTag); if (info == null) { - Matcher matcher = RFC3066_COMPOUND_LANG_PATTERN.matcher(compoundLanguage); + Matcher matcher = RFC3066_COMPOUND_LANG_PATTERN.matcher(languageIdentificationTag); if (matcher.matches()) { String language = matcher.group(1); @@ -340,7 +361,7 @@ throw new IllegalArgumentException("Invalid ISO country code: " + country); } } - throw new IllegalArgumentException("Invalid compound language: accepted format is xx-YY or xx_YY where xx is a " + + throw new IllegalArgumentException("Invalid compound language: accepted format is xx-YY where xx is a " + "valid ISO language code and YY is a valid country code. See java.util.Locale javadoc for more info."); } return info; 1.8 +9 -8 jboss-portal/common/src/main/org/jboss/portal/common/util/LocalizedString.java (In the diff below, changes in quantity of whitespace are not shown.) Index: LocalizedString.java =================================================================== RCS file: /cvsroot/jboss/jboss-portal/common/src/main/org/jboss/portal/common/util/LocalizedString.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -b -r1.7 -r1.8 --- LocalizedString.java 3 Mar 2006 17:46:28 -0000 1.7 +++ LocalizedString.java 3 Mar 2006 20:42:53 -0000 1.8 @@ -31,7 +31,7 @@ * * @author <a href="mailto:ju...@jb...">Julien Viet</a> * @author <a href="mailto:chr...@jb...">Chris Laprun</a> - * @version $Revision: 1.7 $ + * @version $Revision: 1.8 $ */ public class LocalizedString { @@ -155,11 +155,12 @@ */ public String getMostAppropriateValueFor(String[] desiredLocales) throws IllegalArgumentException { - return getMostAppropriateLocalizedMappingFor(desiredLocales).getString(); + return getPreferredOrBestLocalizedMappingFor(desiredLocales).getString(); } /** - * Retrieves the Locale-String mapping most appropriate based on the given desired locales. + * Retrieves the Locale-String mapping most appropriate based on the given desired locales, which are ordered + * according to locale preferences. * * @param desiredLocales an array of compound language tags (as defined by * <a href="http://www.ietf.org/rfc/rfc3066.txt">IETF RFC 3066</a>) ordered according to @@ -167,10 +168,10 @@ * @return a Map.Entry reprenting the most appropriate mapping between Locale and * localized value, based on locale preferences. * @throws IllegalArgumentException if the array is null or one of the array string - * is null or invalid (see #getLocaleFromRFC3066LanguageTag(String)) + * is null or invalid (see {@link LocaleInfo#decodeLocaleInfoFromRFC3066LanguageTag(String)} * @since 2.4 */ - public Value getMostAppropriateLocalizedMappingFor(String[] desiredLocales) throws IllegalArgumentException + public Value getPreferredOrBestLocalizedMappingFor(String[] desiredLocales) throws IllegalArgumentException { if (desiredLocales == null) { @@ -189,8 +190,8 @@ } // todo julien - // maybe we should have here an algorithm that finds out among the set of possible results - // the one which is the most appropriate (i.e more precise), the first result may not be the most precise one + // We could have a smarter version of this method but this version requires that desiredLocales + // are ordered by locale preference. Hence the first found is by definition the best. return value; } |