You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by lu...@apache.org on 2002/04/16 17:49:50 UTC
cvs commit: jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/fmt MessageTag.java
luehe 02/04/16 08:49:50
Modified: standard/examples/web/format ItalianLocale.jsp
MissingResourceBundle.jsp ParametricReplacement.jsp
UndefinedKey.jsp
standard/src/javax/servlet/jsp/jstl/fmt LocaleSupport.java
LocalizationContext.java
standard/src/org/apache/taglibs/standard/tag/common/fmt
BundleSupport.java MessageSupport.java
SetBundleSupport.java SetLocaleSupport.java
standard/src/org/apache/taglibs/standard/tag/el/fmt
MessageTag.java
standard/src/org/apache/taglibs/standard/tag/rt/fmt
MessageTag.java
Log:
New LocalizationContext class now is fully integrated
Revision Changes Path
1.5 +1 -1 jakarta-taglibs/standard/examples/web/format/ItalianLocale.jsp
Index: ItalianLocale.jsp
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/examples/web/format/ItalianLocale.jsp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ItalianLocale.jsp 12 Apr 2002 19:16:49 -0000 1.4
+++ ItalianLocale.jsp 16 Apr 2002 15:49:50 -0000 1.5
@@ -8,7 +8,7 @@
<h3>Italian Locale</h3>
<fmt:setLocale value="it_IT"/>
-<fmt:bundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="itBundle" scope="page"/>
+<fmt:setBundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="itBundle" scope="page"/>
<fmt:message key="greetingMorning" bundle="${itBundle}"/>
</body>
1.3 +1 -1 jakarta-taglibs/standard/examples/web/format/MissingResourceBundle.jsp
Index: MissingResourceBundle.jsp
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/examples/web/format/MissingResourceBundle.jsp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MissingResourceBundle.jsp 5 Mar 2002 21:17:44 -0000 1.2
+++ MissingResourceBundle.jsp 16 Apr 2002 15:49:50 -0000 1.3
@@ -14,7 +14,7 @@
</fmt:bundle>
<li> Explicit collaboration with <bundle> (via <tt>var</tt> attribute):<br>
- <fmt:bundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="enBundle"/>
+ <fmt:setBundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="enBundle"/>
<fmt:message key="greetingEvening" bundle="${enBundle}"/>
</ul>
1.7 +1 -1 jakarta-taglibs/standard/examples/web/format/ParametricReplacement.jsp
Index: ParametricReplacement.jsp
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/examples/web/format/ParametricReplacement.jsp,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ParametricReplacement.jsp 12 Apr 2002 19:16:49 -0000 1.6
+++ ParametricReplacement.jsp 16 Apr 2002 15:49:50 -0000 1.7
@@ -9,7 +9,7 @@
<h3>Parametric Replacement</h3>
<fmt:setLocale value="de"/>
-<fmt:bundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="deBundle"/>
+<fmt:setBundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="deBundle"/>
<fmt:formatDate type="both" var="currentDateString"/>
<fmt:parseDate value="${currentDateString}" type="both" var="currentDate"/>
<ul>
1.4 +1 -1 jakarta-taglibs/standard/examples/web/format/UndefinedKey.jsp
Index: UndefinedKey.jsp
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/examples/web/format/UndefinedKey.jsp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- UndefinedKey.jsp 12 Apr 2002 19:16:49 -0000 1.3
+++ UndefinedKey.jsp 16 Apr 2002 15:49:50 -0000 1.4
@@ -8,7 +8,7 @@
<h3>Undefined Key</h3>
<fmt:setLocale value="it"/>
-<fmt:bundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="itBundle"/>
+<fmt:setBundle basename="org.apache.taglibs.standard.examples.i18n.Resources" var="itBundle"/>
<fmt:message key="invalidKey" bundle="${itBundle}"/>
</body>
1.5 +6 -5 jakarta-taglibs/standard/src/javax/servlet/jsp/jstl/fmt/LocaleSupport.java
Index: LocaleSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/javax/servlet/jsp/jstl/fmt/LocaleSupport.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- LocaleSupport.java 15 Apr 2002 06:54:24 -0000 1.4
+++ LocaleSupport.java 16 Apr 2002 15:49:50 -0000 1.5
@@ -167,20 +167,21 @@
*
* @return the localized message corresponding to the given key
*/
- public static String getLocalizedMessage(PageContext pageContext,
+ public static String getLocalizedMessage(PageContext pc,
String key,
Object[] args,
String basename) {
- ResourceBundle bundle = null;
+ LocalizationContext locCtxt = null;
String message = MessageSupport.UNDEFINED_KEY + key
+ MessageSupport.UNDEFINED_KEY;
if (basename != null) {
- bundle = BundleSupport.getBundle(pageContext, basename);
+ locCtxt = BundleSupport.getLocalizationContext(pc, basename);
} else {
- bundle = (ResourceBundle)
- Config.find(pageContext, Config.FMT_LOCALIZATIONCONTEXT);
+ locCtxt = BundleSupport.getLocalizationContext(pc);
}
+
+ ResourceBundle bundle = locCtxt.getResourceBundle();
if (bundle != null) {
try {
message = bundle.getString(key);
1.2 +8 -0 jakarta-taglibs/standard/src/javax/servlet/jsp/jstl/fmt/LocalizationContext.java
Index: LocalizationContext.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/javax/servlet/jsp/jstl/fmt/LocalizationContext.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LocalizationContext.java 15 Apr 2002 06:54:24 -0000 1.1
+++ LocalizationContext.java 16 Apr 2002 15:49:50 -0000 1.2
@@ -80,6 +80,14 @@
/**
* Constructor.
*
+ * Constructs empty LocalizationContext.
+ */
+ public LocalizationContext() {
+ }
+
+ /**
+ * Constructor.
+ *
* @param bundle The resource bundle, or null if no resource bundle was
* found
* @param locale The locale that led to the resource bundle match, or null
1.21 +72 -35 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/BundleSupport.java
Index: BundleSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/BundleSupport.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- BundleSupport.java 14 Apr 2002 04:35:19 -0000 1.20
+++ BundleSupport.java 16 Apr 2002 15:49:50 -0000 1.21
@@ -61,6 +61,7 @@
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.jstl.core.Config;
+import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import org.apache.taglibs.standard.tag.common.core.Util;
import org.apache.taglibs.standard.resources.Resources;
@@ -85,7 +86,7 @@
// Private state
private Locale fallbackLocale;
- private ResourceBundle bundle;
+ private LocalizationContext locCtxt;
//*********************************************************************
@@ -98,15 +99,15 @@
private void init() {
basename = prefix = null;
- bundle = null;
+ locCtxt = null;
}
//*********************************************************************
// Collaboration with subtags
- public ResourceBundle getBundle() {
- return bundle;
+ public LocalizationContext getLocalizationContext() {
+ return locCtxt;
}
public String getPrefix() {
@@ -119,7 +120,7 @@
public int doStartTag() throws JspException {
if ((basename != null) && !basename.equals("")) {
- bundle = getBundle(pageContext, basename);
+ locCtxt = getLocalizationContext(pageContext, basename);
}
return EVAL_BODY_BUFFERED;
@@ -143,7 +144,30 @@
//*********************************************************************
// Public utility methods
-
+
+ /**
+ * Gets the default I18N localization context.
+ *
+ * @param pc Page in which to look up the default I18N localization context
+ */
+ public static LocalizationContext getLocalizationContext(PageContext pc) {
+ LocalizationContext locCtxt = null;
+
+ Object obj = Config.find(pc, Config.FMT_LOCALIZATIONCONTEXT);
+ if (obj == null) {
+ return null;
+ }
+
+ if (obj instanceof LocalizationContext) {
+ locCtxt = (LocalizationContext) obj;
+ } else {
+ // localization context is a bundle basename
+ locCtxt = BundleSupport.getLocalizationContext(pc, (String) obj);
+ }
+
+ return locCtxt;
+ }
+
/**
* Gets the resource bundle with the given base name, whose locale is
* determined as follows:
@@ -165,39 +189,50 @@
* given base name is requested
* @param basename Resource bundle base name
*
- * @return Resource bundle with the given base name for which a match
- * between the preferred (or fallback) and available locales exists, or
- * <tt>null</tt> if no match was found
+ * @return Localization context containing the resource bundle with the
+ * given base name for which a match between the preferred (or fallback)
+ * and available locales exists, or empty localization context containing
+ * the null bundle if no resource bundle match was found
*/
- public static ResourceBundle getBundle(PageContext pageContext,
- String basename) {
- ResourceBundle ret = null;
-
- Locale pref = SetLocaleSupport.getLocale(pageContext,
- Config.FMT_LOCALE);
+ public static LocalizationContext getLocalizationContext(PageContext pc,
+ String basename) {
+ LocalizationContext locCtxt = null;
+ ResourceBundle bundle = null;
+
+ Locale pref = SetLocaleSupport.getLocale(pc, Config.FMT_LOCALE);
if (pref != null) {
// Preferred locale is application-based
- ret = findMatch(basename, pref);
+ bundle = findMatch(basename, pref);
+ if (bundle != null) {
+ locCtxt = new LocalizationContext(bundle, pref);
+ }
} else {
// Preferred locales are browser-based
- ret = findMatch(pageContext, basename);
+ locCtxt = findMatch(pc, basename);
}
-
- if (ret == null) {
- // no match found, use fallback locale (if present)
- pref = SetLocaleSupport.getLocale(pageContext,
- Config.FMT_FALLBACKLOCALE);
+
+ if (locCtxt == null) {
+ // No match found using preferred locale(s), go try fallback locale
+ pref = SetLocaleSupport.getLocale(pc, Config.FMT_FALLBACKLOCALE);
if (pref != null) {
- ret = findMatch(basename, pref);
+ bundle = findMatch(basename, pref);
+ if (bundle != null) {
+ locCtxt = new LocalizationContext(bundle, pref);
+ }
}
}
-
- if (ret != null) {
+
+ if (locCtxt == null) {
+ locCtxt = new LocalizationContext();
+ }
+
+ bundle = locCtxt.getResourceBundle();
+ if (bundle != null) {
// set response locale
- SetLocaleSupport.setResponseLocale(pageContext, ret.getLocale());
+ SetLocaleSupport.setResponseLocale(pc, bundle.getLocale());
}
-
- return ret;
+
+ return locCtxt;
}
@@ -213,12 +248,13 @@
* given base name is requested
* @param basename the resource bundle's base name
*
- * @return the resource bundle with the given base name and best matching
- * locale, or <tt>null</tt> if no match was found
+ * @return the localization context containing the resource bundle with
+ * the given base name and best matching locale, or <tt>null</tt> if no
+ * resource bundle match was found
*/
- private static ResourceBundle findMatch(PageContext pageContext,
- String basename) {
- ResourceBundle match = null;
+ private static LocalizationContext findMatch(PageContext pageContext,
+ String basename) {
+ LocalizationContext locCtxt = null;
// Determine locale from client's browser settings.
for (Enumeration enum = pageContext.getRequest().getLocales();
@@ -229,13 +265,14 @@
* locale, so it always contains at least one element.
*/
Locale pref = (Locale) enum.nextElement();
- match = findMatch(basename, pref);
+ ResourceBundle match = findMatch(basename, pref);
if (match != null) {
+ locCtxt = new LocalizationContext(match, pref);
break;
}
}
- return match;
+ return locCtxt;
}
/*
1.16 +12 -8 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/MessageSupport.java
Index: MessageSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/MessageSupport.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- MessageSupport.java 15 Apr 2002 06:54:24 -0000 1.15
+++ MessageSupport.java 16 Apr 2002 15:49:50 -0000 1.16
@@ -62,6 +62,7 @@
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.jstl.core.Config;
+import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import org.apache.taglibs.standard.tag.common.core.Util;
import org.apache.taglibs.standard.resources.Resources;
@@ -84,7 +85,7 @@
// Protected state
protected String key; // 'key' attribute
- protected ResourceBundle bundle; // 'bundle' attribute
+ protected LocalizationContext locCtxt; // 'bundle' attribute
//*********************************************************************
@@ -106,7 +107,7 @@
private void init() {
key = var = null;
- bundle = null;
+ locCtxt = null;
scope = PageContext.PAGE_SCOPE;
}
@@ -163,23 +164,26 @@
}
String prefix = null;
- if (bundle == null) {
+ if (locCtxt == null) {
Tag t = findAncestorWithClass(this, BundleSupport.class);
if (t != null) {
// use resource bundle from parent <bundle> tag
BundleSupport parent = (BundleSupport) t;
- bundle = parent.getBundle();
+ locCtxt = parent.getLocalizationContext();
prefix = parent.getPrefix();
} else {
- bundle = (ResourceBundle)
- Config.find(pageContext, Config.FMT_LOCALIZATIONCONTEXT);
+ locCtxt = BundleSupport.getLocalizationContext(pageContext);
}
} else {
- SetLocaleSupport.setResponseLocale(pageContext,
- bundle.getLocale());
+ if (locCtxt.getResourceBundle() != null) {
+ SetLocaleSupport.setResponseLocale(
+ pageContext,
+ locCtxt.getResourceBundle().getLocale());
+ }
}
String message = null;
+ ResourceBundle bundle = locCtxt.getResourceBundle();
if (bundle == null) {
message = UNDEFINED_KEY + key + UNDEFINED_KEY;
} else {
1.4 +6 -20 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/SetBundleSupport.java
Index: SetBundleSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/SetBundleSupport.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SetBundleSupport.java 15 Apr 2002 06:54:24 -0000 1.3
+++ SetBundleSupport.java 16 Apr 2002 15:49:50 -0000 1.4
@@ -60,6 +60,7 @@
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.jstl.core.Config;
+import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import org.apache.taglibs.standard.tag.common.core.Util;
import org.apache.taglibs.standard.resources.Resources;
@@ -74,17 +75,6 @@
//*********************************************************************
- // Package-scoped constants
-
- private static final ResourceBundle EMPTY_BUNDLE =
- new ListResourceBundle() {
- public Object[][] getContents() {
- return new Object[][] { { "", "" } };
- }
- };
-
-
- //*********************************************************************
// Protected state
protected String basename; // 'basename' attribute
@@ -127,21 +117,17 @@
// Tag logic
public int doEndTag() throws JspException {
- ResourceBundle bundle = null;
+ LocalizationContext locCtxt = null;
if ((basename != null) && !basename.equals("")) {
- bundle = BundleSupport.getBundle(pageContext, basename);
- }
-
- if (bundle == null) {
- // storing "null" in a scoped variable would cause a NPE
- bundle = EMPTY_BUNDLE;
+ locCtxt = BundleSupport.getLocalizationContext(pageContext,
+ basename);
}
if (var != null) {
- pageContext.setAttribute(var, bundle, Util.getScope(scope));
+ pageContext.setAttribute(var, locCtxt, Util.getScope(scope));
} else {
- Config.set(pageContext, Config.FMT_LOCALIZATIONCONTEXT, bundle,
+ Config.set(pageContext, Config.FMT_LOCALIZATIONCONTEXT, locCtxt,
Util.getScope(scope));
}
1.4 +44 -29 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java
Index: SetLocaleSupport.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SetLocaleSupport.java 15 Apr 2002 06:54:24 -0000 1.3
+++ SetLocaleSupport.java 16 Apr 2002 15:49:50 -0000 1.4
@@ -60,6 +60,7 @@
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.jstl.core.Config;
+import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import org.apache.taglibs.standard.tag.common.core.Util;
import org.apache.taglibs.standard.resources.Resources;
@@ -259,43 +260,57 @@
boolean format,
Locale[] avail) {
Locale match = null;
- ResourceBundle bundle = null;
-
+ LocalizationContext locCtxt = null;
+
+ // Get formatting locale from enclosing <fmt:bundle>
Tag parent = findAncestorWithClass(fromTag, BundleSupport.class);
if (parent != null) {
// use locale from parent <fmt:bundle> tag
- match = ((BundleSupport) parent).getBundle().getLocale();
- } else if ((bundle = (ResourceBundle)
- Config.find(pc, Config.FMT_LOCALIZATIONCONTEXT)) != null) {
- // Use locale associated with default bundle base name.
- match = bundle.getLocale();
- } else {
- /*
- * Compare the preferred locales (in order of preference) against
- * the available formatting locales, and determine the best
- * matching locale.
- */
- Locale pref = getLocale(pc, Config.FMT_LOCALE);
- if (pref != null) {
- // Preferred locale is application-based
- match = findFormattingMatch(pref, avail);
- } else {
- // Preferred locales are browser-based
- match = findFormattingMatch(pc, avail);
+ locCtxt = ((BundleSupport) parent).getLocalizationContext();
+ ResourceBundle bundle = locCtxt.getResourceBundle();
+ if (bundle != null) {
+ if (format) {
+ setResponseLocale(pc, bundle.getLocale());
+ }
+ return bundle.getLocale();
}
- if (match == null) {
- //Use fallback locale.
- pref = getLocale(pc, Config.FMT_FALLBACKLOCALE);
- if ((pref == null)
- || ((match = findFormattingMatch(pref,
- avail)) == null)) {
- // Use runtime's default locale.
- match = Locale.getDefault();
+ }
+
+ // Get formatting locale from default I18N localization context
+ if ((locCtxt = BundleSupport.getLocalizationContext(pc)) != null) {
+ ResourceBundle bundle = locCtxt.getResourceBundle();
+ if (bundle != null) {
+ if (format) {
+ setResponseLocale(pc, bundle.getLocale());
}
+ return bundle.getLocale();
}
}
- if (format) {
+ /*
+ * Establish formatting locale by comparing the preferred locales
+ * (in order of preference) against the available formatting
+ * locales, and determining the best matching locale.
+ */
+ Locale pref = getLocale(pc, Config.FMT_LOCALE);
+ if (pref != null) {
+ // Preferred locale is application-based
+ match = findFormattingMatch(pref, avail);
+ } else {
+ // Preferred locales are browser-based
+ match = findFormattingMatch(pc, avail);
+ }
+ if (match == null) {
+ //Use fallback locale.
+ pref = getLocale(pc, Config.FMT_FALLBACKLOCALE);
+ if ((pref == null)
+ || ((match = findFormattingMatch(pref,
+ avail)) == null)) {
+ // Use runtime's default locale.
+ match = Locale.getDefault();
+ }
+ }
+ if (format) {
setResponseLocale(pc, match);
}
1.5 +5 -2 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/el/fmt/MessageTag.java
Index: MessageTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/el/fmt/MessageTag.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- MessageTag.java 9 Mar 2002 00:55:59 -0000 1.4
+++ MessageTag.java 16 Apr 2002 15:49:50 -0000 1.5
@@ -58,6 +58,7 @@
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
+import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import org.apache.taglibs.standard.lang.support.*;
import org.apache.taglibs.standard.tag.common.fmt.*;
@@ -156,8 +157,10 @@
}
if (bundle_ != null) {
- bundle = (ResourceBundle) ExpressionEvaluatorManager.evaluate(
- "bundle", bundle_, ResourceBundle.class, this, pageContext);
+ locCtxt = (LocalizationContext)
+ ExpressionEvaluatorManager.evaluate(
+ "bundle", bundle_, LocalizationContext.class, this,
+ pageContext);
}
}
}
1.4 +3 -3 jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/fmt/MessageTag.java
Index: MessageTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/rt/fmt/MessageTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MessageTag.java 28 Feb 2002 00:54:06 -0000 1.3
+++ MessageTag.java 16 Apr 2002 15:49:50 -0000 1.4
@@ -55,9 +55,9 @@
package org.apache.taglibs.standard.tag.rt.fmt;
-import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
+import javax.servlet.jsp.jstl.fmt.LocalizationContext;
import org.apache.taglibs.standard.tag.common.fmt.*;
/**
@@ -78,7 +78,7 @@
}
// for tag attribute
- public void setBundle(ResourceBundle bundle) throws JspTagException {
- this.bundle = bundle;
+ public void setBundle(LocalizationContext locCtxt) throws JspTagException {
+ this.locCtxt = locCtxt;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>