You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2007/05/12 16:58:47 UTC
svn commit: r537451 - in
/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket:
Localizer.java model/StringResourceModel.java
Author: jdonnerstag
Date: Sat May 12 07:58:46 2007
New Revision: 537451
URL: http://svn.apache.org/viewvc?view=rev&rev=537451
Log:
wicket-528: StringResourceModel does not use defaultValue properly
fixed
Modified:
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/StringResourceModel.java
Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java?view=diff&rev=537451&r1=537450&r2=537451
==============================================================================
--- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java (original)
+++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/Localizer.java Sat May 12 07:58:46 2007
@@ -114,10 +114,10 @@
{
return getString(key, component, null, defaultValue);
}
-
+
/**
* This method is now deprecated.
- *
+ *
* @param key
* @param component
* @param model
@@ -127,7 +127,8 @@
* @return String
* @throws MissingResourceException
*
- * @Deprecated please use {@link #getString(String, Component, IModel, String)}
+ * @Deprecated please use
+ * {@link #getString(String, Component, IModel, String)}
*/
public String getString(final String key, final Component component, final IModel model,
final Locale locale, final String style, final String defaultValue)
@@ -135,7 +136,7 @@
{
return null;
}
-
+
/**
* Get the localized string using all of the supplied parameters. This
* method is left public to allow developers full control over string
@@ -162,8 +163,9 @@
// Iterate over all registered string resource loaders until the
// property has been found
String string = null;
-
- Iterator iter = Application.get().getResourceSettings().getStringResourceLoaders().iterator();
+
+ final IResourceSettings resourceSettings = Application.get().getResourceSettings();
+ Iterator iter = resourceSettings.getStringResourceLoaders().iterator();
while (iter.hasNext())
{
IStringResourceLoader loader = (IStringResourceLoader)iter.next();
@@ -174,25 +176,27 @@
}
}
- // If a property value has been found, than replace the placeholder
- // and we are done
- if (string != null)
+ if ((string == null) && (defaultValue != null))
{
- return substitutePropertyExpressions(component, string, model);
+ // Resource not found, so handle missing resources based on
+ // application configuration and try the default value
+ if (resourceSettings.getUseDefaultOnMissingResource())
+ {
+ string = defaultValue;
+ }
}
- // Resource not found, so handle missing resources based on application
- // configuration
- final IResourceSettings resourceSettings = Application.get().getResourceSettings();
- if (resourceSettings.getUseDefaultOnMissingResource() && (defaultValue != null))
+ // If a property value has been found, or a default value was given,
+ // than replace the placeholder and we are done
+ if (string != null)
{
- return defaultValue;
+ return substitutePropertyExpressions(component, string, model);
}
if (resourceSettings.getThrowExceptionOnMissingResource())
{
- AppendingStringBuffer message = new AppendingStringBuffer("Unable to find resource: "
- + key);
+ AppendingStringBuffer message = new AppendingStringBuffer("Unable to find resource: " +
+ key);
if (component != null)
{
message.append(" for component: ");
Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/StringResourceModel.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/StringResourceModel.java?view=diff&rev=537451&r1=537450&r2=537451
==============================================================================
--- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/StringResourceModel.java (original)
+++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/model/StringResourceModel.java Sat May 12 07:58:46 2007
@@ -19,6 +19,7 @@
import java.text.MessageFormat;
import java.util.Locale;
+import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.Session;
@@ -52,8 +53,8 @@
* Page on which the component resides. For reusable components/containers that
* are packaged with their own string resource bundles it should be the actual
* component/container rather than the page. For more information on this please
- * see {@link org.apache.wicket.resource.loader.ComponentStringResourceLoader}. The
- * relative component may actually be <code>null</code> when all resource
+ * see {@link org.apache.wicket.resource.loader.ComponentStringResourceLoader}.
+ * The relative component may actually be <code>null</code> when all resource
* loading is to be done from a global resource loader. However, we recommend
* that a relative component is still supplied even in these cases in order to
* 'future proof' your application with regards to changing resource loading
@@ -88,15 +89,13 @@
* In its simplest form, the model can be used as follows:
*
* <pre>
- *
- * public MyPage extends WebPage
- * {
- * public MyPage(final PageParameters parameters)
- * {
- * add(new Label("username", new StringResourceModel("label.username", this, null)));
- * }
- * }
- *
+ * public MyPage extends WebPage
+ * {
+ * public MyPage(final PageParameters parameters)
+ * {
+ * add(new Label("username", new StringResourceModel("label.username", this, null)));
+ * }
+ * }
* </pre>
*
* Where the resource bundle for the page contains the entry
@@ -108,17 +107,15 @@
* property expression:
*
* <pre>
- *
- * public MyPage extends WebPage
- * {
- * public MyPage(final PageParameters parameters)
- * {
- * WeatherStation ws = new WeatherStation();
- * add(new Label("weatherMessage",
- * new StringResourceModel("weather.${currentStatus}", this, new Model(ws)));
- * }
- * }
- *
+ * public MyPage extends WebPage
+ * {
+ * public MyPage(final PageParameters parameters)
+ * {
+ * WeatherStation ws = new WeatherStation();
+ * add(new Label("weatherMessage",
+ * new StringResourceModel("weather.${currentStatus}", this, new Model(ws)));
+ * }
+ * }
* </pre>
*
* Which will call the WeatherStation.getCurrentStatus() method each time the
@@ -126,12 +123,10 @@
* contains the entries:
*
* <pre>
- *
- * weather.sunny=Don't forget sunscreen!
- * weather.raining=You might need an umberella
- * weather.snowing=Got your skis?
- * weather.overcast=Best take a coat to be safe
- *
+ * weather.sunny=Don't forget sunscreen!
+ * weather.raining=You might need an umberella
+ * weather.snowing=Got your skis?
+ * weather.overcast=Best take a coat to be safe
* </pre>
*
* <p>
@@ -141,25 +136,15 @@
* is substituted via the model:
*
* <pre>
- *
- *
- *
- *
- *
- * public MyPage extends WebPage
- * {
- * public MyPage(final PageParameters parameters)
- * {
- * WeatherStation ws = new WeatherStation();
- * add(new Label("weatherMessage",
- * new StringResourceModel("weather.message", this, new Model(ws)));
- * }
- * }
- *
- *
- *
- *
- *
+ * public MyPage extends WebPage
+ * {
+ * public MyPage(final PageParameters parameters)
+ * {
+ * WeatherStation ws = new WeatherStation();
+ * add(new Label("weatherMessage",
+ * new StringResourceModel("weather.message", this, new Model(ws)));
+ * }
+ * }
* </pre>
*
* Where the resource bundle contains the entry
@@ -173,51 +158,31 @@
* powerful use of the string resource model:
*
* <pre>
- *
- *
- *
- *
- *
- * public MyPage extends WebPage
- * {
- * public MyPage(final PageParameters parameters)
- * {
- * WeatherStation ws = new WeatherStation();
- * Model model = new Model(ws);
- * add(new Label("weatherMessage",
- * new StringResourceModel(
- * "weather.detail", this, model,
- * new Object[]
- * {
- * new Date(),
- * new PropertyModel(model, "currentStatus"),
- * new PropertyModel(model, "currentTemperature"),
- * new PropertyModel(model, "units")
- * }));
- * }
- * }
- *
- *
- *
- *
- *
+ * public MyPage extends WebPage
+ * {
+ * public MyPage(final PageParameters parameters)
+ * {
+ * WeatherStation ws = new WeatherStation();
+ * Model model = new Model(ws);
+ * add(new Label("weatherMessage",
+ * new StringResourceModel(
+ * "weather.detail", this, model,
+ * new Object[]
+ * {
+ * new Date(),
+ * new PropertyModel(model, "currentStatus"),
+ * new PropertyModel(model, "currentTemperature"),
+ * new PropertyModel(model, "units")
+ * }));
+ * }
+ * }
* </pre>
*
* And where the resource bundle entry is:
*
* <pre>
- *
- *
- *
- *
- *
- * weather.detail=The report for {0,date}, shows the temparature as {2,number,###.##} {3} \
- * and the weather to be {1}
- *
- *
- *
- *
- *
+ * weather.detail=The report for {0,date}, shows the temparature as {2,number,###.##} {3} \
+ * and the weather to be {1}
* </pre>
*
* @author Chris Turner
@@ -264,7 +229,7 @@
public StringResourceModel(final String resourceKey, final Component component,
final IModel model)
{
- this(resourceKey, component, model, null,null);
+ this(resourceKey, component, model, null, null);
}
/**
@@ -276,17 +241,17 @@
* The component that the resource is relative to
* @param model
* The model to use for property substitutions
- * @param defaultValue
+ * @param defaultValue
* The default value if the resource key is not found.
- *
+ *
* @see #StringResourceModel(String, Component, IModel, Object[])
*/
public StringResourceModel(final String resourceKey, final Component component,
final IModel model, final String defaultValue)
{
- this(resourceKey, component, model, null,defaultValue);
+ this(resourceKey, component, model, null, defaultValue);
}
-
+
/**
* Creates a new string resource model using the supplied parameters.
* <p>
@@ -315,7 +280,7 @@
{
this(resourceKey, component, model, parameters, null);
}
-
+
/**
* Creates a new string resource model using the supplied parameters.
* <p>
@@ -338,7 +303,7 @@
* The model to use for property substitutions
* @param parameters
* The parameters to substitute using a Java MessageFormat object
- * @param defaultValue
+ * @param defaultValue
* The default value if the resource key is not found.
*/
public StringResourceModel(final String resourceKey, final Component component,
@@ -390,10 +355,13 @@
// Get the string resource, doing any property substitutions as part
// of the get operation
- String s = localizer.getString(getResourceKey(), component, model);
- if(s == null) s = defaultValue;
+ String value = localizer.getString(getResourceKey(), component, model, defaultValue);
+ if (value == null)
+ {
+ value = defaultValue;
+ }
- if(s != null)
+ if (value != null)
{
// Substitute any parameters if necessary
Object[] parameters = getParameters();
@@ -409,24 +377,24 @@
}
else if (model != null && parameters[i] instanceof String)
{
- realParams[i] = PropertyVariableInterpolator.interpolate((String)parameters[i],
- model.getObject());
+ realParams[i] = PropertyVariableInterpolator.interpolate(
+ (String)parameters[i], model.getObject());
}
else
{
realParams[i] = parameters[i];
}
}
-
+
// Apply the parameters
- final MessageFormat format = new MessageFormat(s, component != null ? component
+ final MessageFormat format = new MessageFormat(value, component != null ? component
.getLocale() : locale);
- s = format.format(realParams);
+ value = format.format(realParams);
}
}
// Return the string resource
- return s;
+ return value;
}
/**
@@ -475,15 +443,14 @@
{
if (model != null)
{
- return PropertyVariableInterpolator
- .interpolate(resourceKey, model.getObject());
+ return PropertyVariableInterpolator.interpolate(resourceKey, model.getObject());
}
else
{
return resourceKey;
}
}
-
+
/**
* Gets the string that this string resource model currently represents. The
* string is returned as an object to allow it to be used generically within
@@ -496,7 +463,7 @@
final Session session = Session.get();
if (session != null)
{
- this.localizer = session.getApplication().getResourceSettings().getLocalizer();
+ this.localizer = Application.get().getResourceSettings().getLocalizer();
this.locale = session.getLocale();
}
else