Updated Branches:
  refs/heads/master 53e7e235c -> 9249b3e6d

extracted conversion into new class; Locale and IConverterLocator now 
determined once only


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9249b3e6
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9249b3e6
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9249b3e6

Branch: refs/heads/master
Commit: 9249b3e6d1dedf1835505b0bb79bd9c92ea43136
Parents: 53e7e23
Author: svenmeier <svenme...@apache.org>
Authored: Wed Aug 22 20:03:12 2012 +0200
Committer: svenmeier <svenme...@apache.org>
Committed: Wed Aug 22 20:03:12 2012 +0200

----------------------------------------------------------------------
 .../src/main/java/org/apache/wicket/Localizer.java |   56 +++++-------
 .../ConvertingPropertyVariableInterpolator.java    |   74 +++++++++++++++
 .../interpolator/PropertyVariableInterpolator.java |   27 +++---
 3 files changed, 110 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/9249b3e6/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 313702d..0594233 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Localizer.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Localizer.java
@@ -25,12 +25,11 @@ 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.core.util.string.interpolator.ConvertingPropertyVariableInterpolator;
 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.slf4j.Logger;
@@ -516,11 +515,11 @@ public class Localizer
                }
        }
 
-       /**
+/**
         * Helper method to handle property variable substitution in strings.
         * 
         * @param component
-        *            The component requesting a model value
+        *            The component requesting a model value or {@code null]
         * @param string
         *            The string to substitute into
         * @param model
@@ -532,38 +531,29 @@ public class Localizer
        {
                if ((string != null) && (model != null))
                {
-                       return new PropertyVariableInterpolator(string, 
model.getObject())
+                       final IConverterLocator locator;
+                       final Locale locale;
+                       if (component == null)
                        {
-                               @SuppressWarnings({ "rawtypes", "unchecked" })
-                               @Override
-                               protected String toString(Object value)
+                               locator = 
Application.get().getConverterLocator();
+
+                               if (Session.exists())
+                               {
+                                       locale = Session.get().getLocale();
+                               }
+                               else
                                {
-                                       IConverter converter;
-                                       Locale locale;
-                                       if (component == null)
-                                       {
-                                               converter = Application.get()
-                                                       .getConverterLocator()
-                                                       
.getConverter(value.getClass());
-
-                                               if (Session.exists())
-                                               {
-                                                       locale = 
Session.get().getLocale();
-                                               }
-                                               else
-                                               {
-                                                       locale = 
Locale.getDefault();
-                                               }
-                                       }
-                                       else
-                                       {
-                                               converter = 
component.getConverter(value.getClass());
-                                               locale = component.getLocale();
-                                       }
-
-                                       return converter.convertToString(value, 
locale);
+                                       locale = Locale.getDefault();
                                }
-                       }.toString();
+                       }
+                       else
+                       {
+                               locator = component;
+                               locale = component.getLocale();
+                       }
+
+                       return new 
ConvertingPropertyVariableInterpolator(string, model.getObject(), locator,
+                               locale).toString();
                }
                return string;
        }

http://git-wip-us.apache.org/repos/asf/wicket/blob/9249b3e6/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/ConvertingPropertyVariableInterpolator.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/ConvertingPropertyVariableInterpolator.java
 
b/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/ConvertingPropertyVariableInterpolator.java
new file mode 100644
index 0000000..5e8cc37
--- /dev/null
+++ 
b/wicket-core/src/main/java/org/apache/wicket/core/util/string/interpolator/ConvertingPropertyVariableInterpolator.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.core.util.string.interpolator;
+
+import java.util.Locale;
+
+import org.apache.wicket.IConverterLocator;
+import org.apache.wicket.util.convert.IConverter;
+
+/**
+ * A {@link PropertyVariableInterpolator} converting values with {@link 
IConverter}s.
+ * 
+ * @author svenmeier
+ */
+public class ConvertingPropertyVariableInterpolator extends 
PropertyVariableInterpolator
+{
+       private static final long serialVersionUID = 1L;
+
+       private IConverterLocator locator;
+
+       private Locale locale;
+
+       /**
+        * Constructor.
+        * 
+        * @param string
+        *            a <code>String</code> to interpolate into
+        * @param object
+        *            the object to apply property expressions to
+        * @param locator
+        *            the locator of converters
+        * @param locale
+        *            the locale for conversion
+        */
+       public ConvertingPropertyVariableInterpolator(final String string, 
final Object object,
+               IConverterLocator locator, Locale locale)
+       {
+               super(string, object);
+
+               this.locator = locator;
+               this.locale = locale;
+       }
+
+       /**
+        * Use an {@link IConverter} to convert the given value to a String.
+        * 
+        * @param value
+        *            the value, never {@code null}
+        * 
+        * @return converted value
+        */
+       @Override
+       @SuppressWarnings({ "rawtypes", "unchecked" })
+       protected String toString(Object value)
+       {
+               IConverter converter = locator.getConverter(value.getClass());
+
+               return converter.convertToString(value, locale);
+       }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/9249b3e6/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 40d8461..19889fc 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
@@ -22,15 +22,14 @@ import 
org.apache.wicket.util.string.interpolator.VariableInterpolator;
 
 /**
  * Interpolates values into <code>String</code>s that are produced by 
interpreting property
- * expressions against a beans model.
+ * expressions against an object.
  * <p>
- * The <code>interpolate(String string, Object model)</code> method takes a 
string such as "
- * <code>My name is ${name}</code>" and a beans model such as a 
<code>Person</code>, and reflects on
- * the object using any property expressions found inside <code>${}</code> 
markers in the
- * <code>String</code>. In this case, if the <code>Person</code> model has a 
<code>getName()</code>
- * method. The results of calling that method would be substituted for 
<code>${name}</code>. If
- * <code>getName()</code> returned <code>"Jonathan"</code>, then 
<code>interpolate()</code> would
- * return <code>"My name is Jonathan"</code>.
+ * Takes a string such as "<code>My name is ${name}</code>" and a bean such as 
a <code>Person</code>
+ * , and reflects on the object using any property expressions found inside 
<code>${}</code> markers
+ * in the <code>String</code>. In this case, if the <code>Person</code> model 
has a
+ * <code>getName()</code> method. The results of calling that method would be 
substituted for
+ * <code>${name}</code>. If <code>getName()</code> returned 
<code>"Jonathan"</code>, then the result
+ * would return <code>"My name is Jonathan"</code>.
  * <p>
  * "$" 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
@@ -43,27 +42,27 @@ public class PropertyVariableInterpolator extends 
VariableInterpolator
 {
        private static final long serialVersionUID = 1L;
 
-       /** The model to introspect on */
-       private final Object model;
+       /** The object to introspect on */
+       private final Object oject;
 
        /**
         * Constructor.
         * 
         * @param string
         *            a <code>String</code> to interpolate into
-        * @param model
+        * @param object
         *            the model to apply property expressions to
         */
-       public PropertyVariableInterpolator(final String string, final Object 
model)
+       public PropertyVariableInterpolator(final String string, final Object 
object)
        {
                super(string);
-               this.model = model;
+               oject = object;
        }
 
        @Override
        protected String getValue(final String variableName)
        {
-               Object value = PropertyResolver.getValue(variableName, model);
+               Object value = PropertyResolver.getValue(variableName, oject);
 
                if (value != null)
                {

Reply via email to