On Sun, Jul 22, 2012 at 4:03 PM, Martin Grigorov <[email protected]> wrote: > On Thu, Jul 19, 2012 at 2:09 AM, <[email protected]> wrote: >> Updated Branches: >> refs/heads/master af1cf1b29 -> a99ab59b3 >> >> >> WICKET-3753 moved converter handling from PropertyVariableInterpolator into >> Localizer, thus allowing Component relative converted lookup and >> interpolation without converters (needed by StringResourceModel for >> resources keys) >> >> >> Project: http://git-wip-us.apache.org/repos/asf/wicket/repo >> Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/a99ab59b >> Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/a99ab59b >> Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/a99ab59b >> >> Branch: refs/heads/master >> Commit: a99ab59b3169e2d0b1f8410f23e2e4bfb4239334 >> Parents: af1cf1b >> Author: svenmeier <[email protected]> >> Authored: Thu Jul 19 01:08:58 2012 +0200 >> Committer: svenmeier <[email protected]> >> Committed: Thu Jul 19 01:08:58 2012 +0200 >> >> ---------------------------------------------------------------------- >> .../src/main/java/org/apache/wicket/Localizer.java | 24 +++++- >> .../interpolator/PropertyVariableInterpolator.java | 74 ++++----------- >> .../apache/wicket/model/StringResourceModel.java | 10 +- >> .../test/java/org/apache/wicket/LocalizerTest.java | 16 ++- >> .../StringResourceModelTest$TestPage.properties | 4 +- >> .../wicket/model/StringResourceModelTest.java | 14 +++ >> .../wicket/resource/DummyApplication.properties | 2 +- >> .../PropertyVariableInterpolatorTest.java | 6 +- >> .../string/interpolator/VariableInterpolator.java | 6 + >> 9 files changed, 82 insertions(+), 74 deletions(-) >> ---------------------------------------------------------------------- >> >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/main/java/org/apache/wicket/Localizer.java >> ---------------------------------------------------------------------- >> diff --git a/wicket-core/src/main/java/org/apache/wicket/Localizer.java >> b/wicket-core/src/main/java/org/apache/wicket/Localizer.java >> index b6d9bf2..0e33c36 100644 >> --- a/wicket-core/src/main/java/org/apache/wicket/Localizer.java >> +++ b/wicket-core/src/main/java/org/apache/wicket/Localizer.java >> @@ -25,13 +25,14 @@ import java.util.concurrent.ConcurrentHashMap; >> import java.util.concurrent.ConcurrentMap; >> import java.util.concurrent.atomic.AtomicLong; >> >> +import >> org.apache.wicket.core.util.string.interpolator.PropertyVariableInterpolator; >> import org.apache.wicket.markup.repeater.AbstractRepeater; >> import org.apache.wicket.model.IModel; >> import org.apache.wicket.resource.loader.IStringResourceLoader; >> import org.apache.wicket.settings.IResourceSettings; >> +import org.apache.wicket.util.convert.IConverter; >> import org.apache.wicket.util.lang.Generics; >> import org.apache.wicket.util.string.AppendingStringBuffer; >> -import >> org.apache.wicket.core.util.string.interpolator.PropertyVariableInterpolator; >> import org.slf4j.Logger; >> import org.slf4j.LoggerFactory; >> >> @@ -533,7 +534,26 @@ public class Localizer >> { >> if ((string != null) && (model != null)) >> { >> - return >> PropertyVariableInterpolator.interpolate(string, model.getObject()); >> + return new PropertyVariableInterpolator(string, >> model.getObject()) >> + { >> + @Override >> + protected String toString(Object value) >> + { >> + IConverter converter; >> + if (component == null) >> + { >> + converter = Application.get() >> + >> .getConverterLocator() >> + >> .getConverter(value.getClass()); >> + } >> + else >> + { >> + converter = >> component.getConverter(value.getClass()); >> + } >> + >> + return >> converter.convertToString(value, Session.get().getLocale()); > > Is it better if we use component.getLocale() if 'component' is not null ? > ^^^^
Sorry. Just saw that a later commit actually does this. > > >> + } >> + }.toString(); >> } >> return string; >> } >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/PropertyVariableInterpolator.java >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/PropertyVariableInterpolator.java >> >> b/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/PropertyVariableInterpolator.java >> index 0b74d77..40d8461 100644 >> --- >> a/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/PropertyVariableInterpolator.java >> +++ >> b/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/PropertyVariableInterpolator.java >> @@ -16,11 +16,8 @@ >> */ >> package org.apache.wicket.core.util.string.interpolator; >> >> -import org.apache.wicket.Application; >> -import org.apache.wicket.IConverterLocator; >> -import org.apache.wicket.Session; >> import org.apache.wicket.core.util.lang.PropertyResolver; >> -import org.apache.wicket.util.convert.IConverter; >> +import org.apache.wicket.util.string.Strings; >> import org.apache.wicket.util.string.interpolator.VariableInterpolator; >> >> /** >> @@ -38,13 +35,11 @@ import >> org.apache.wicket.util.string.interpolator.VariableInterpolator; >> * "$" is the escape char. Thus "$${text}" can be used to escape it (ignore >> interpretation). If >> * '$3.24' is needed then '$$${amount}' should be used. The first $ sign >> escapes the second, and the >> * third is used to interpolate the variable. >> - * >> + * >> * @author Jonathan Locke >> * @since 1.2.6 >> */ >> -public final class PropertyVariableInterpolator extends VariableInterpolator >> - implements >> - IConverterLocator >> +public class PropertyVariableInterpolator extends VariableInterpolator >> { >> private static final long serialVersionUID = 1L; >> >> @@ -52,48 +47,19 @@ public final class PropertyVariableInterpolator extends >> VariableInterpolator >> private final Object model; >> >> /** >> - * Private constructor to force use of static interpolate method. >> - * >> + * Constructor. >> + * >> * @param string >> * a <code>String</code> to interpolate into >> * @param model >> * the model to apply property expressions to >> */ >> - private PropertyVariableInterpolator(final String string, final >> Object model) >> + public PropertyVariableInterpolator(final String string, final >> Object model) >> { >> super(string); >> this.model = model; >> } >> >> - /** >> - * Interpolates the given <code>String</code>, substituting values >> for property expressions. >> - * >> - * @param string >> - * a <code>String</code> containing property expressions >> like <code>${xyz}</code> >> - * @param object >> - * the <code>Object</code> to reflect on >> - * @return the interpolated <code>String</code> >> - */ >> - public static String interpolate(final String string, final Object >> object) >> - { >> - // If there's any reason to go to the expense of property >> expressions >> - if (string.contains("${")) >> - { >> - // Do property expression interpolation >> - return new PropertyVariableInterpolator(string, >> object).toString(); >> - } >> - >> - // Return simple string >> - return string; >> - } >> - >> - /** >> - * Retrieves a value for a variable name during interpolation. >> - * >> - * @param variableName >> - * the variable name >> - * @return the value >> - */ >> @Override >> protected String getValue(final String variableName) >> { >> @@ -101,29 +67,23 @@ public final class PropertyVariableInterpolator extends >> VariableInterpolator >> >> if (value != null) >> { >> - final IConverter converter = >> getConverter(value.getClass()); >> - if (converter != null) >> - { >> - return converter.convertToString(value, >> Session.get().getLocale()); >> - } >> - else >> - { >> - return value.toString(); >> - } >> + return toString(value); >> } >> return null; >> } >> >> /** >> - * {@inheritDoc} >> + * Convert the given value to a string for interpolation. >> + * <p> >> + * This default implementation delegates to {@link >> Strings#toString(Object)}. >> + * >> + * @param value >> + * the value, never {@code null} >> + * >> + * @return string representation >> */ >> - @Override >> - public <C> IConverter<C> getConverter(Class<C> type) >> + protected String toString(Object value) >> { >> - if (Application.exists()) >> - { >> - return >> Application.get().getConverterLocator().getConverter(type); >> - } >> - return null; >> + return Strings.toString(value); >> } >> } >> \ No newline at end of file >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java >> b/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java >> index 5a36032..aa11126 100644 >> --- >> a/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java >> +++ >> b/wicket-core/src/main/java/org/apache/wicket/model/StringResourceModel.java >> @@ -24,9 +24,9 @@ import org.apache.wicket.Application; >> import org.apache.wicket.Component; >> import org.apache.wicket.Localizer; >> import org.apache.wicket.Session; >> +import >> org.apache.wicket.core.util.string.interpolator.PropertyVariableInterpolator; >> import org.apache.wicket.resource.loader.ComponentStringResourceLoader; >> import org.apache.wicket.util.string.Strings; >> -import >> org.apache.wicket.core.util.string.interpolator.PropertyVariableInterpolator; >> >> >> /** >> @@ -402,7 +402,7 @@ public class StringResourceModel extends >> LoadableDetachableModel<String> >> return getString(component); >> } >> >> - private String getString(Component component) >> + private String getString(final Component component) >> { >> >> final Localizer localizer = getLocalizer(); >> @@ -451,8 +451,8 @@ public class StringResourceModel extends >> LoadableDetachableModel<String> >> } >> else if (model != null && >> parameters[i] instanceof String) >> { >> - realParams[i] = >> PropertyVariableInterpolator.interpolate( >> - >> (String)parameters[i], model.getObject()); >> + realParams[i] = >> localizer.substitutePropertyExpressions(component, >> + >> (String)parameters[i], model); >> } >> else >> { >> @@ -568,7 +568,7 @@ public class StringResourceModel extends >> LoadableDetachableModel<String> >> { >> if (model != null) >> { >> - return >> PropertyVariableInterpolator.interpolate(resourceKey, model.getObject()); >> + return new PropertyVariableInterpolator(resourceKey, >> model.getObject()).toString(); >> } >> else >> { >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/test/java/org/apache/wicket/LocalizerTest.java >> ---------------------------------------------------------------------- >> diff --git a/wicket-core/src/test/java/org/apache/wicket/LocalizerTest.java >> b/wicket-core/src/test/java/org/apache/wicket/LocalizerTest.java >> index de8edce..16eec28 100644 >> --- a/wicket-core/src/test/java/org/apache/wicket/LocalizerTest.java >> +++ b/wicket-core/src/test/java/org/apache/wicket/LocalizerTest.java >> @@ -167,10 +167,13 @@ public class LocalizerTest extends Assert >> @Test >> public void testGetStringPropertySubstitution() >> { >> + Session.get().setLocale(Locale.GERMAN); >> + >> ValueMap vm = new ValueMap(); >> vm.put("user", "John Doe"); >> + vm.put("rating", 4.5); >> IModel<ValueMap> model = new Model<ValueMap>(vm); >> - Assert.assertEquals("Property substitution should occur", >> "Welcome, John Doe", >> + Assert.assertEquals("Property substitution should occur", >> "John Doe gives 4,5 stars", >> localizer.getString("test.substitute", null, model, >> null)); >> } >> >> @@ -212,16 +215,19 @@ public class LocalizerTest extends Assert >> @Test >> public void testGetStringUseModel() >> { >> - HashMap<String, String> model = new HashMap<String, >> String>(); >> + Session.get().setLocale(Locale.GERMAN); >> + >> + HashMap<String, Object> model = new HashMap<String, >> Object>(); >> model.put("user", "juergen"); >> + model.put("rating", 4.5); >> >> - Assert.assertEquals("Expected string should be returned", >> "Welcome, juergen", >> + Assert.assertEquals("Expected string should be returned", >> "juergen gives 4,5 stars", >> localizer.getString("test.substitute", null, new >> PropertyModel<String>(model, null), >> "DEFAULT {user}")); >> >> Assert.assertEquals("Expected string should be returned", >> "DEFAULT juergen", >> - localizer.getString("test.substituteDoesNotExist", >> null, new PropertyModel<String>( >> - model, null), "DEFAULT ${user}")); >> + localizer.getString("test.substituteDoesNotExist", >> null, >> + new PropertyModel<HashMap<String, >> Object>>(model, null), "DEFAULT ${user}")); >> } >> >> /** >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest$TestPage.properties >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest$TestPage.properties >> >> b/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest$TestPage.properties >> index 2f38e11..51d45ed 100644 >> --- >> a/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest$TestPage.properties >> +++ >> b/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest$TestPage.properties >> @@ -26,4 +26,6 @@ weather.mixed=Weather station "${name}" reports that the >> temperature is {0} {1} >> weather.detail=The report for {0,date,medium}, shows the temperature as >> {2,number,###.##} {3} and the weather to be {1} >> with.quote={0,choice,-1#n/a|-1<'{1}{0,number,#,##0.00}'} >> with.quote.substitution=Let's play in the {0} >> -with.quote.and.no.substitution=Let's play in the rain! >> \ No newline at end of file >> +with.quote.and.no.substitution=Let's play in the rain! >> + >> +weather.25.7=Twenty-five dot seven >> \ No newline at end of file >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java >> >> b/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java >> index 15e9dd7..b69fee2 100644 >> --- >> a/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java >> +++ >> b/wicket-core/src/test/java/org/apache/wicket/model/StringResourceModelTest.java >> @@ -24,6 +24,7 @@ import java.util.Locale; >> import junit.framework.Assert; >> >> import org.apache.wicket.Component; >> +import org.apache.wicket.Session; >> import org.apache.wicket.WicketTestCase; >> import org.apache.wicket.markup.html.WebPage; >> import org.apache.wicket.markup.html.basic.Label; >> @@ -103,6 +104,19 @@ public class StringResourceModelTest extends >> WicketTestCase >> >> /** */ >> @Test >> + public void getSimpleResourceWithKeySubstitutionForNonString() >> + { >> + // German uses comma (,) as decimal separator >> + Session.get().setLocale(Locale.GERMAN); >> + >> + StringResourceModel model = new >> StringResourceModel("weather.${currentTemperature}", page, >> + wsModel); >> + Assert.assertEquals("Text should be as expected", >> "Twenty-five dot seven", >> + model.getString()); >> + } >> + >> + /** */ >> + @Test >> public void getPropertySubstitutedResource() >> { >> tester.getSession().setLocale(Locale.ENGLISH); >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/test/java/org/apache/wicket/resource/DummyApplication.properties >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-core/src/test/java/org/apache/wicket/resource/DummyApplication.properties >> >> b/wicket-core/src/test/java/org/apache/wicket/resource/DummyApplication.properties >> index 132ca48..30dbe86 100644 >> --- >> a/wicket-core/src/test/java/org/apache/wicket/resource/DummyApplication.properties >> +++ >> b/wicket-core/src/test/java/org/apache/wicket/resource/DummyApplication.properties >> @@ -17,4 +17,4 @@ >> # limitations under the License. >> # >> test.string=This is a test >> -test.substitute=Welcome, ${user} >> +test.substitute=${user} gives ${rating} stars >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-core/src/test/java/org/apache/wicket/util/string/interpolator/PropertyVariableInterpolatorTest.java >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-core/src/test/java/org/apache/wicket/util/string/interpolator/PropertyVariableInterpolatorTest.java >> >> b/wicket-core/src/test/java/org/apache/wicket/util/string/interpolator/PropertyVariableInterpolatorTest.java >> index eeffe71..8438f81 100644 >> --- >> a/wicket-core/src/test/java/org/apache/wicket/util/string/interpolator/PropertyVariableInterpolatorTest.java >> +++ >> b/wicket-core/src/test/java/org/apache/wicket/util/string/interpolator/PropertyVariableInterpolatorTest.java >> @@ -34,7 +34,7 @@ public class PropertyVariableInterpolatorTest extends >> Assert >> public void withValue() >> { >> TestClass object = new TestClass("value"); >> - String result = >> PropertyVariableInterpolator.interpolate("${key}", object); >> + String result = new PropertyVariableInterpolator("${key}", >> object).toString(); >> assertEquals("value", result.toString()); >> } >> >> @@ -46,7 +46,7 @@ public class PropertyVariableInterpolatorTest extends >> Assert >> public void withValueAndEscape() >> { >> TestClass object = new TestClass("3.24"); >> - String result = >> PropertyVariableInterpolator.interpolate("$$${key}", object); >> + String result = new PropertyVariableInterpolator("$$${key}", >> object).toString(); >> assertEquals("$3.24", result.toString()); >> } >> >> @@ -56,7 +56,7 @@ public class PropertyVariableInterpolatorTest extends >> Assert >> @Test >> public void withoutValue() >> { >> - String result = >> PropertyVariableInterpolator.interpolate("${key}", null); >> + String result = new PropertyVariableInterpolator("${key}", >> null).toString(); >> assertEquals("${key}", result.toString()); >> } >> >> >> http://git-wip-us.apache.org/repos/asf/wicket/blob/a99ab59b/wicket-util/src/main/java/org/apache/wicket/util/string/interpolator/VariableInterpolator.java >> ---------------------------------------------------------------------- >> diff --git >> a/wicket-util/src/main/java/org/apache/wicket/util/string/interpolator/VariableInterpolator.java >> >> b/wicket-util/src/main/java/org/apache/wicket/util/string/interpolator/VariableInterpolator.java >> index 0223837..86add15 100644 >> --- >> a/wicket-util/src/main/java/org/apache/wicket/util/string/interpolator/VariableInterpolator.java >> +++ >> b/wicket-util/src/main/java/org/apache/wicket/util/string/interpolator/VariableInterpolator.java >> @@ -105,6 +105,12 @@ public abstract class VariableInterpolator implements >> IClusterable >> @Override >> public String toString() >> { >> + // If there's any reason to go to the expense of property >> expressions >> + if (!string.contains("${")) >> + { >> + return string; >> + } >> + >> // Result buffer >> final StringBuilder buffer = new StringBuilder(); >> >> > > > > -- > Martin Grigorov > jWeekend > Training, Consulting, Development > http://jWeekend.com -- Martin Grigorov jWeekend Training, Consulting, Development http://jWeekend.com
