You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Leonardo Quijano Vincenzi <le...@dtqsoftware.com> on 2006/03/05 22:57:04 UTC

Dynamic translators

Is there any way to specify dynamic translators, that depend on some 
OGNL expression??

Something like this:

<input jwcid="someField@TextField"
          translator="translator:number,pattern=SomeOGNLExpression"
          value="ognl:someNumber" />

???

I've been looking around but I didn't found a way to inject the 
translator in the page class or something similar.
Ideas?

-- 
Ing. Leonardo Quijano Vincenzi
DTQ Software



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Bryan Lewis <br...@maine.rr.com>.
I believe you're correct, that the built-in translators do not take an
expression there.  There was a posting a couple of weeks ago about a
related thing, using an expression in a date validator.  One solution
was to use an expression in a bean specification and use that as the
validator.

    <bean name="maxDate1"
class="org.apache.tapestry.form.validator.MaxDate">
      <set name="maxDate" value="new java.util.Date()"/>
    </bean>

    <component id="inputLastPasswordDate" type="TextField">
        <binding name="value" value="employee.hireDate"/>
        <binding name="translator" value="translator:date"/>
        <binding name="validators" value="validators:$maxDate1"/>
    </component>

I tried the same idea on a translator.  This doesn't work:

    <component id="startDatePicker" type="DatePicker">
        <binding name="translator"
value="translator:date,pattern=ognl:datePattern"/>

but this does:

    <bean name="dateTranslator1"
class="org.apache.tapestry.form.translator.DateTranslator">
        <set name="pattern" value="ognl:datePattern"/>
    </bean>

    <component id="startDatePicker" type="DatePicker">
        <binding name="translator" value="bean:dateTranslator1"/>


I tested it with a toy method getDatePattern() that returned a
randomized choice from two patterns.

I arrived at that after some googling and trial and error.  I expected
the last line to have a syntax more like the validator's, like: 
value="translator:$dateTranslator1".  Someone might point out a better way.



Leonardo Quijano Vincenzi wrote:

> Is there any way to specify dynamic translators, that depend on some
> OGNL expression??
>
> Something like this:
>
> <input jwcid="someField@TextField"
>          translator="translator:number,pattern=SomeOGNLExpression"
>          value="ognl:someNumber" />
>
> ???
>
> I've been looking around but I didn't found a way to inject the
> translator in the page class or something similar.
> Ideas?
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Ryan Holmes <ry...@hyperstep.com>.
Whoops, the reference would be:

<component id="integerField" type="TextField">
     <binding name="translator" value="translator:integer" />
</component>

-Ryan

Leonardo Quijano Vincenzi wrote:
> If that's true, then we can actually contribute to the standard 
> Translators, right?
>
>  <contribution configuration-id="Translators">
>    <bean name="date" class="SpecialTranslator"/>
>  </contribution>
>
>   <bean name="specialTranslator" class="xxx.xxx.SpecialTranslator">
>       <set name="something" value="someval" />
>   </bean>
>
> And use it like this:
>
>   <component id="dateField" type="DatePicker">
>       <binding name="translator" value="translator:specialTranslator" />
>   </component>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Ryan Holmes <ry...@hyperstep.com>.
We can certainly contribute to the standard Translators, although I 
don't believe HiveMind will pick up contributions from a page 
specification (I could be wrong). The following will work:

Your hivemodule.xml (you can put hivemodule.xml in WEB-INF if you don't 
already have one):

<?xml version="1.0"?>

<module id="lis" version="1.8.0"
    package="com.example.services">

    <contribution
        configuration-id="tapestry.form.translator.Translators">

        Add an integer-only translator with strict enforcement. Any
        non-integer input will throw a validation exception.
        <bean name="integer" class="IntegerTranslator" />
    </contribution>

</module>


The IntegerTranslator class:

package com.example.services;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.form.translator.NumberTranslator;
import org.apache.tapestry.valid.ValidatorException;

/**
 * A strict version of Tapestry's NumberTranslator that throws a validation
 * error on any non-numeric input.
 *
 */
public class IntegerTranslator extends NumberTranslator {

  // Tapestry already contains an appropriate message
  // for invalid integers, so we use its key here.
  private static final String INVALID_FORMAT_KEY = "invalid-int-format";
  private static final Pattern NUMERIC_PATTERN = Pattern.compile( 
"-?[0-9]*" );

   public IntegerTranslator(String initializer) {
    PropertyUtils.configureProperties( this, initializer );
  }

 @Override
  protected Object parseText(IFormComponent field, ValidationMessages 
messages,
      String text) throws ValidatorException {

    // Validate against pattern
    Matcher matcher = NUMERIC_PATTERN.matcher( text );
    if ( !matcher.matches() ) {
      throw new ValidatorException( buildMessage( messages, field,
          getMessageKey() ), getConstraint() );
    }

    return super.parseText( field, messages, text );
  }

 @Override
   protected String getMessageKey() {
    return INVALID_FORMAT_KEY;
  }

}


And reference it as normal:

<component id="integerField" type="TextField">
      <binding name="translator" value="translator:specialTranslator" />
</component>


-Ryan

Leonardo Quijano Vincenzi wrote:
> If that's true, then we can actually contribute to the standard 
> Translators, right?
>
>  <contribution configuration-id="Translators">
>    <bean name="date" class="SpecialTranslator"/>
>  </contribution>
>
>   <bean name="specialTranslator" class="xxx.xxx.SpecialTranslator">
>       <set name="something" value="someval" />
>   </bean>
>
> And use it like this:
>
>   <component id="dateField" type="DatePicker">
>       <binding name="translator" value="translator:specialTranslator" />
>   </component>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Leonardo Quijano Vincenzi <le...@dtqsoftware.com>.
If that's true, then we can actually contribute to the standard 
Translators, right?

  <contribution configuration-id="Translators">
    <bean name="date" class="SpecialTranslator"/>
  </contribution>

   <bean name="specialTranslator" class="xxx.xxx.SpecialTranslator">
       <set name="something" value="someval" />
   </bean>

And use it like this:

   <component id="dateField" type="DatePicker">
       <binding name="translator" value="translator:specialTranslator" />
   </component>

-- 
Ing. Leonardo Quijano Vincenzi
DTQ Software


Ryan Holmes wrote:
> In addition to the other recommendations, you can define translators 
> and validators as beans:
>
>    <bean name="dateTranslator"
>        class="org.apache.tapestry.form.translator.DateTranslator">
>        <set name="pattern" value="myPattern" />
>    </bean>
>
>    <bean name="minDate" 
> class="org.apache.tapestry.form.validator.MinDate">
>        <set name="minDate" value="minValidDate" />
>    </bean>
>
>    <bean name="maxDate" 
> class="org.apache.tapestry.form.validator.MaxDate">
>        <set name="maxDate" value="maxValidDate" />
>    </bean>
>
>
>
> And then refer to them in component specs like so:
>
>    <component id="dateField" type="DatePicker">
>        <binding name="value" value="myDate" />
>        <binding name="translator" value="bean:dateTranslator" />
>        <binding name="validators"
>            value="validators:required,$minDate,$maxDate" />
>    </component>
>
> -Ryan
>
> Leonardo Quijano Vincenzi wrote:
>> Is there any way to specify dynamic translators, that depend on some 
>> OGNL expression??
>>
>> Something like this:
>>
>> <input jwcid="someField@TextField"
>>          translator="translator:number,pattern=SomeOGNLExpression"
>>          value="ognl:someNumber" />
>>
>> ???
>>
>> I've been looking around but I didn't found a way to inject the 
>> translator in the page class or something similar.
>> Ideas?
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>




---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Massimo Lusetti <ml...@gmail.com>.
On 3/6/06, Ryan Holmes <ry...@hyperstep.com> wrote:

> In addition to the other recommendations, you can define translators and
> validators as beans:

Thanks Ryan and others! This gives me idea on how to (i think
elegantly) solve one of my problems  which i posted not a long ago.

It's really fun to work with this stuff!
--
Massimo
http://meridio.blogspot.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Ryan Holmes <ry...@hyperstep.com>.
In addition to the other recommendations, you can define translators and 
validators as beans:

    <bean name="dateTranslator"
        class="org.apache.tapestry.form.translator.DateTranslator">
        <set name="pattern" value="myPattern" />
    </bean>

    <bean name="minDate" class="org.apache.tapestry.form.validator.MinDate">
        <set name="minDate" value="minValidDate" />
    </bean>

    <bean name="maxDate" class="org.apache.tapestry.form.validator.MaxDate">
        <set name="maxDate" value="maxValidDate" />
    </bean>



And then refer to them in component specs like so:

    <component id="dateField" type="DatePicker">
        <binding name="value" value="myDate" />
        <binding name="translator" value="bean:dateTranslator" />
        <binding name="validators"
            value="validators:required,$minDate,$maxDate" />
    </component>

-Ryan

Leonardo Quijano Vincenzi wrote:
> Is there any way to specify dynamic translators, that depend on some 
> OGNL expression??
>
> Something like this:
>
> <input jwcid="someField@TextField"
>          translator="translator:number,pattern=SomeOGNLExpression"
>          value="ognl:someNumber" />
>
> ???
>
> I've been looking around but I didn't found a way to inject the 
> translator in the page class or something similar.
> Ideas?
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Dynamic translators

Posted by Jesse Kuhnert <jk...@gmail.com>.
Translators are defined via hivemind contributions like most other things. I
don't know if any currently support random ognl expressions, but you're free
to create your own that can by contributing it to the correct hivemind
contrib point and using the ExpressionEvaluator service.


On 3/5/06, Leonardo Quijano Vincenzi <le...@dtqsoftware.com> wrote:
>
> Is there any way to specify dynamic translators, that depend on some
> OGNL expression??
>
> Something like this:
>
> <input jwcid="someField@TextField"
>           translator="translator:number,pattern=SomeOGNLExpression"
>           value="ognl:someNumber" />
>
> ???
>
> I've been looking around but I didn't found a way to inject the
> translator in the page class or something similar.
> Ideas?
>
> --
> Ing. Leonardo Quijano Vincenzi
> DTQ Software
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

Re: Dynamic translators

Posted by Ron Piterman <rp...@gmx.net>.
the translator is a normal parameter.
implement a method which returns the translator you want - probably 
generates it on the fly, and use the ognl (or prop) prefix instead of 
the translator: one...
cheers,
Ron


Leonardo Quijano Vincenzi wrote:
> Is there any way to specify dynamic translators, that depend on some 
> OGNL expression??
> 
> Something like this:
> 
> <input jwcid="someField@TextField"
>          translator="translator:number,pattern=SomeOGNLExpression"
>          value="ognl:someNumber" />
> 
> ???
> 
> I've been looking around but I didn't found a way to inject the 
> translator in the page class or something similar.
> Ideas?
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org