You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2010/04/29 18:11:16 UTC
svn commit: r939359 - in /tapestry/tapestry5/trunk:
tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/
tapestry-core/src/main/java/org/apache/tapestry5/internal/services/
tapestry-core/src/test/java/org/apache/tapestry5/internal/services/
Author: hlship
Date: Thu Apr 29 16:11:16 2010
New Revision: 939359
URL: http://svn.apache.org/viewvc?rev=939359&view=rev
Log:
TAP5-152: Add @Translate annotation to define name of translator to be used with a bean property (rather than lookup by property type)
Added:
tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Translate.java
- copied, changed from r939358, tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java
Modified:
tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImplTest.java
Copied: tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Translate.java (from r939358, tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Translate.java?p2=tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Translate.java&p1=tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java&r1=939358&r2=939359&rev=939359&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java (original)
+++ tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Translate.java Thu Apr 29 16:11:16 2010
@@ -1,4 +1,4 @@
-// Copyright 2007, 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2010 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.
@@ -28,13 +28,13 @@ import java.lang.annotation.Target;
import org.apache.tapestry5.ioc.annotations.UseWith;
/**
- * Used to attach validation constraints directly to a property (either the getter or the setter method). The annotation
- * value is a comma separated list of <em>validation constraints</em>, each one identifying a validator type (such as
- * "required", "minlength") and optionally, a constraint value. Most validators need a constraint value, which is
- * separated from the type by an equals size (i.e., "maxlength=30"). In addition, the value may include
- * validator macros.
+ * Used to attach the name of a Translator used to convert the associated property between server-side and
+ * client-side representations.
* <p/>
* May be placed on any getter or setter method, or on the matching field.
+ *
+ * @see Validate
+ * @since 5.2.0
*/
@Target(
{ ElementType.FIELD, ElementType.METHOD })
@@ -42,7 +42,7 @@ import org.apache.tapestry5.ioc.annotati
@Documented
@UseWith(
{ BEAN, COMPONENT, MIXIN, PAGE })
-public @interface Validate
+public @interface Translate
{
String value();
}
Modified: tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java?rev=939359&r1=939358&r2=939359&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java (original)
+++ tapestry/tapestry5/trunk/tapestry-annotations/src/main/java/org/apache/tapestry5/beaneditor/Validate.java Thu Apr 29 16:11:16 2010
@@ -35,6 +35,8 @@ import org.apache.tapestry5.ioc.annotati
* validator macros.
* <p/>
* May be placed on any getter or setter method, or on the matching field.
+ *
+ * @see Translate
*/
@Target(
{ ElementType.FIELD, ElementType.METHOD })
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImpl.java?rev=939359&r1=939358&r2=939359&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImpl.java Thu Apr 29 16:11:16 2010
@@ -1,10 +1,10 @@
-// Copyright 2008 The Apache Software Foundation
+// Copyright 2008, 2010 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
+// 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,
@@ -15,9 +15,11 @@
package org.apache.tapestry5.internal.services;
import org.apache.tapestry5.ComponentResources;
+
import org.apache.tapestry5.Field;
import org.apache.tapestry5.FieldTranslator;
import org.apache.tapestry5.Translator;
+import org.apache.tapestry5.beaneditor.Translate;
import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.MessageFormatter;
import org.apache.tapestry5.ioc.Messages;
@@ -29,6 +31,7 @@ import org.apache.tapestry5.services.Val
import java.util.Locale;
+@SuppressWarnings("unchecked")
public class FieldTranslatorSourceImpl implements FieldTranslatorSource
{
private final TranslatorSource translatorSource;
@@ -38,7 +41,7 @@ public class FieldTranslatorSourceImpl i
private final FormSupport formSupport;
public FieldTranslatorSourceImpl(TranslatorSource translatorSource,
- ValidationMessagesSource validationMessagesSource, FormSupport formSupport)
+ ValidationMessagesSource validationMessagesSource, FormSupport formSupport)
{
this.translatorSource = translatorSource;
this.validationMessagesSource = validationMessagesSource;
@@ -53,32 +56,44 @@ public class FieldTranslatorSourceImpl i
Field field = (Field) resources.getComponent();
Class propertyType = resources.getBoundType(parameterName);
- return createDefaultTranslator(field, resources.getId(), resources.getContainerMessages(),
- resources.getLocale(), propertyType,
- resources.getAnnotationProvider(parameterName));
+ return createDefaultTranslator(field, resources.getId(), resources.getContainerMessages(), resources
+ .getLocale(), propertyType, resources.getAnnotationProvider(parameterName));
}
public FieldTranslator createDefaultTranslator(Field field, String overrideId, Messages overrideMessages,
- Locale locale, Class propertyType,
- AnnotationProvider propertyAnnotations)
+ Locale locale, Class propertyType, AnnotationProvider propertyAnnotations)
{
Defense.notNull(field, "field");
Defense.notBlank(overrideId, "overrideId");
Defense.notNull(overrideMessages, "overrideMessages");
Defense.notNull(locale, "locale");
+ if (propertyType == null)
+ return null;
- if (propertyType == null) return null;
-
- Translator translator = translatorSource.findByType(propertyType);
+ Translator translator = findTranslator(propertyType, propertyAnnotations);
- if (translator == null) return null;
+ if (translator == null)
+ return null;
return createTranslator(field, overrideId, overrideMessages, locale, translator);
}
+ Translator findTranslator(Class propertyType, AnnotationProvider propertyAnnotations)
+ {
+ Translate annotation = propertyAnnotations.getAnnotation(Translate.class);
+
+ if (annotation != null)
+ return translatorSource.get(annotation.value());
+
+ if (propertyType == null)
+ return null;
+
+ return translatorSource.findByType(propertyType);
+ }
+
public FieldTranslator createTranslator(Field field, String overrideId, Messages overrideMessages, Locale locale,
- Translator translator)
+ Translator translator)
{
MessageFormatter formatter = findFormatter(overrideId, overrideMessages, locale, translator);
@@ -95,12 +110,11 @@ public class FieldTranslatorSourceImpl i
Translator translator = translatorSource.get(translatorName);
return createTranslator(field, resources.getId(), resources.getContainerMessages(), resources.getLocale(),
- translator);
+ translator);
}
-
private MessageFormatter findFormatter(String overrideId, Messages overrideMessages, Locale locale,
- Translator translator)
+ Translator translator)
{
// TAP5-228: Try to distinguish message overrides by form id and overrideId (i.e., property name) first.
Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImplTest.java?rev=939359&r1=939358&r2=939359&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/FieldTranslatorSourceImplTest.java Thu Apr 29 16:11:16 2010
@@ -1,10 +1,10 @@
-// Copyright 2008 The Apache Software Foundation
+// Copyright 2008, 2010 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
+// 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,
@@ -15,7 +15,9 @@
package org.apache.tapestry5.internal.services;
import org.apache.tapestry5.*;
+import org.apache.tapestry5.beaneditor.Translate;
import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.MessageFormatter;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.root.FieldComponent;
@@ -31,6 +33,7 @@ import java.util.Map;
/**
* Fills in some gaps that are not currently tested by the integration tests.
*/
+@SuppressWarnings("unchecked")
public class FieldTranslatorSourceImplTest extends InternalBaseTestCase
{
@Test
@@ -57,6 +60,7 @@ public class FieldTranslatorSourceImplTe
Locale locale = Locale.ENGLISH;
Class propertyType = Map.class;
TranslatorSource ts = mockTranslatorSource();
+ AnnotationProvider ap = mockAnnotationProvider(null);
train_findByType(ts, propertyType, null);
@@ -64,12 +68,48 @@ public class FieldTranslatorSourceImplTe
FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, null, null);
- assertNull(source.createDefaultTranslator(field, "override", messages, locale, propertyType, null));
+ assertNull(source.createDefaultTranslator(field, "override", messages, locale, propertyType, ap));
verify();
}
@Test
+ public void create_default_translator_with_annotation()
+ {
+ TranslatorSource ts = mockTranslatorSource();
+ AnnotationProvider ap = mockAnnotationProvider("fred");
+ Translator t = mockTranslator();
+
+ expect(ts.get("fred")).andReturn(t);
+
+ replay();
+
+ FieldTranslatorSourceImpl source = new FieldTranslatorSourceImpl(ts, null, null);
+
+ assertSame(source.findTranslator(Map.class, ap), t);
+
+ verify();
+ }
+
+ private AnnotationProvider mockAnnotationProvider(String translatorName)
+ {
+ AnnotationProvider ap = mockAnnotationProvider();
+
+ if (translatorName == null)
+ {
+ train_getAnnotation(ap, Translate.class, null);
+ }
+ else
+ {
+ Translate t = newMock(Translate.class);
+ expect(t.value()).andReturn(translatorName).atLeastOnce();
+ train_getAnnotation(ap, Translate.class, t);
+ }
+
+ return ap;
+ }
+
+ @Test
public void create_default_translator_with_name()
{
Field field = mockField();
@@ -85,7 +125,7 @@ public class FieldTranslatorSourceImplTe
MarkupWriter writer = mockMarkupWriter();
String label = "Field Label";
String message = "Woops, did it again.";
-
+ AnnotationProvider ap = mockAnnotationProvider(null);
train_findByType(ts, propertyType, translator);
@@ -106,7 +146,7 @@ public class FieldTranslatorSourceImplTe
FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, vms, fs);
- FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, null);
+ FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap);
assertEquals(ft.getType(), Map.class);
@@ -130,6 +170,7 @@ public class FieldTranslatorSourceImplTe
MarkupWriter writer = mockMarkupWriter();
String label = "My Label";
String message = "Formatted Message";
+ AnnotationProvider ap = mockAnnotationProvider(null);
train_findByType(ts, propertyType, translator);
@@ -148,7 +189,7 @@ public class FieldTranslatorSourceImplTe
FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, vms, fs);
- FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, null);
+ FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap);
assertEquals(ft.getType(), Map.class);
@@ -172,6 +213,7 @@ public class FieldTranslatorSourceImplTe
MarkupWriter writer = mockMarkupWriter();
String label = "My Label";
String message = "Formatted Message";
+ AnnotationProvider ap = mockAnnotationProvider(null);
train_findByType(ts, propertyType, translator);
@@ -189,7 +231,7 @@ public class FieldTranslatorSourceImplTe
FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, vms, fs);
- FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, null);
+ FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap);
assertEquals(ft.getType(), Map.class);
@@ -231,7 +273,6 @@ public class FieldTranslatorSourceImplTe
train_getMessageKey(translator, "mykey");
train_getMessageFormatter(validationMessages, "mykey", formatter);
-
train_getLabel(field, label);
train_format(formatter, message, label);