You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/01/17 19:13:42 UTC

svn commit: r612905 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/ main/java/org/apache/tapestry/internal/services/ main/java/org/apache/tapestry/services/ test/app1/ test/java/org/apache/tapestry/integration/ test/jav...

Author: hlship
Date: Thu Jan 17 10:13:39 2008
New Revision: 612905

URL: http://svn.apache.org/viewvc?rev=612905&view=rev
Log:
TAPETRY-1598: Tapestry should not require explicit value encoders (via the encoder parameter) where it can automatically coerce the value between string and the appropriate server-side type

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TypeCoercedValueEncoderFactory.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/MagicValueEncoder.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/MagicValueEncoder.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/ValueEncoder.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ValueEncoderFactory.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/Start.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/ValueEncoder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/ValueEncoder.java?rev=612905&r1=612904&r2=612905&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/ValueEncoder.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/ValueEncoder.java Thu Jan 17 10:13:39 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -17,21 +17,20 @@
 import org.apache.tapestry.corelib.components.Select;
 
 /**
- * Used by {@link Select} (and similar components) to encode server side values into client-side
- * strings, and back.
+ * Used by {@link Select} (and similar components) to encode server side values into client-side strings, and back.
  * <p/>
- * Most often a custom implementation is needed for entity type objects, where the
- * {@link #toClient(Object)} method extracts a primary key, and the {@link #toValue(String)}
- * re-acquires the corresponding entity object.
+ * Most often a custom implementation is needed for entity type objects, where the {@link #toClient(Object)} method
+ * extracts a primary key, and the {@link #toValue(String)} re-acquires the corresponding entity object.
  *
  * @see SelectModel
+ * @see org.apache.tapestry.services.ValueEncoderSource
  */
 public interface ValueEncoder<V>
 {
     /**
-     * Converts a value into a client-side representation. The value should be parseable by
-     * {@link #toValue(String)}. In some cases, what is returned is an identifier used to locate
-     * the true object, rather than a string representation of the value itself.
+     * Converts a value into a client-side representation. The value should be parseable by {@link #toValue(String)}. In
+     * some cases, what is returned is an identifier used to locate the true object, rather than a string representation
+     * of the value itself.
      *
      * @param value to be encoded
      * @return a string representation of the value, or the value's identity
@@ -39,8 +38,7 @@
     String toClient(V value);
 
     /**
-     * Converts a client-side representation, provided by {@link #toClient(Object)}, back into a
-     * server-side value.
+     * Converts a client-side representation, provided by {@link #toClient(Object)}, back into a server-side value.
      *
      * @param clientValue string representation of the value's identity
      * @return the corresponding entity, or null if not found

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TypeCoercedValueEncoderFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TypeCoercedValueEncoderFactory.java?rev=612905&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TypeCoercedValueEncoderFactory.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TypeCoercedValueEncoderFactory.java Thu Jan 17 10:13:39 2008
@@ -0,0 +1,49 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.internal.services;
+
+import org.apache.tapestry.ValueEncoder;
+import org.apache.tapestry.ioc.services.TypeCoercer;
+import org.apache.tapestry.services.ValueEncoderFactory;
+
+/**
+ * Provides {@link org.apache.tapestry.ValueEncoder} instances that are backed by the {@link
+ * org.apache.tapestry.ioc.services.TypeCoercer} service.
+ */
+public class TypeCoercedValueEncoderFactory implements ValueEncoderFactory<Object>
+{
+    private final TypeCoercer _typeCoercer;
+
+    public TypeCoercedValueEncoderFactory(TypeCoercer typeCoercer)
+    {
+        _typeCoercer = typeCoercer;
+    }
+
+    public ValueEncoder<Object> create(final Class<Object> type)
+    {
+        return new ValueEncoder<Object>()
+        {
+            public String toClient(Object value)
+            {
+                return _typeCoercer.coerce(value, String.class);
+            }
+
+            public Object toValue(String clientValue)
+            {
+                return _typeCoercer.coerce(clientValue, type);
+            }
+        };
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java?rev=612905&r1=612904&r2=612905&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java Thu Jan 17 10:13:39 2008
@@ -1521,13 +1521,13 @@
     }
 
     /**
-     * Contributes {@link ValueEncoderFactory}s for types: <ul> <li>String <li>Enum </ul>
-     *
-     * @param configuration
+     * Contributes {@link ValueEncoderFactory}s for types: <ul> <li>Object <li>String <li>Enum </ul>
      */
     @SuppressWarnings("unchecked")
-    public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration)
+    public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration,
+                                                    ObjectLocator locator)
     {
+        configuration.add(Object.class, locator.autobuild(TypeCoercedValueEncoderFactory.class));
         configuration.add(String.class, new GenericValueEncoderFactory(new StringValueEncoder()));
         configuration.add(Enum.class, new EnumValueEncoderFactory());
     }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ValueEncoderFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ValueEncoderFactory.java?rev=612905&r1=612904&r2=612905&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ValueEncoderFactory.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/ValueEncoderFactory.java Thu Jan 17 10:13:39 2008
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,7 +16,16 @@
 
 import org.apache.tapestry.ValueEncoder;
 
+/**
+ * A source for {@link ValueEncoder} instances of a given type.
+ */
 public interface ValueEncoderFactory<V>
 {
+    /**
+     * For a given type, create an encoder.
+     *
+     * @param type type of object for which an encoder is needed
+     * @return the encoder for the object
+     */
     ValueEncoder<V> create(Class<V> type);
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/MagicValueEncoder.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/MagicValueEncoder.tml?rev=612905&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/MagicValueEncoder.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/MagicValueEncoder.tml Thu Jan 17 10:13:39 2008
@@ -0,0 +1,23 @@
+<html t:type="Border"
+      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+
+    <h1>Magic Value Encoder</h1>
+
+    <p>Ok, Magic is too strong a word. This is really based on the TypeCoercer, which is not quite magic (but close).
+    </p>
+
+
+    <t:form>
+        <t:select t:id="number" model="options"/>
+        <br/>
+        <input type="submit" value="Update"/>
+    </t:form>
+
+    <hr/>
+
+    <p>
+        Selected number:
+        <span id="selectednumber">${number}</span>
+    </p>
+
+</html>    

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java?rev=612905&r1=612904&r2=612905&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java Thu Jan 17 10:13:39 2008
@@ -1392,4 +1392,26 @@
 
         assertText(contextSpan, "1");
     }
+
+    /**
+     * TAPESTRY-1598
+     */
+    @Test
+    public void value_encoder_via_type_coercer()
+    {
+        start("Magic ValueEncoder Demo");
+
+        select("number", "25");
+
+        clickAndWait(SUBMIT);
+
+        String locator = "//span[@id='selectednumber']";
+
+        assertText(locator, "25");
+
+        select("number", "100");
+        clickAndWait(SUBMIT);
+
+        assertText(locator, "100");
+    }
 }

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/MagicValueEncoder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/MagicValueEncoder.java?rev=612905&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/MagicValueEncoder.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/MagicValueEncoder.java Thu Jan 17 10:13:39 2008
@@ -0,0 +1,40 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry.integration.app1.pages;
+
+import org.apache.tapestry.annotations.Persist;
+
+public class MagicValueEncoder
+{
+    @Persist("flash")
+    private int _number;
+
+    private static final int[] OPTIONS = {5, 10, 25, 100};
+
+    public int getNumber()
+    {
+        return _number;
+    }
+
+    public void setNumber(int number)
+    {
+        _number = number;
+    }
+
+    public int[] getOptions()
+    {
+        return OPTIONS;
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/Start.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/Start.java?rev=612905&r1=612904&r2=612905&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/Start.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/Start.java Thu Jan 17 10:13:39 2008
@@ -196,7 +196,10 @@
             new Item("blockcaller", "Action Links off of Active Page",
                      "Actions can exist on pages other than the active page, via Blocks."),
 
-            new Item("unlessdemo", "Unless Demo", "use of the Unless component"));
+            new Item("unlessdemo", "Unless Demo", "use of the Unless component"),
+
+            new Item("MagicValueEncoder", "Magic ValueEncoder Demo",
+                     "Automatic creation of ValueEncoder using the TypeCoercer"));
 
     static
     {