ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicola Ken Barozzi <nicola...@apache.org>
Subject Re: <comment> task
Date Thu, 08 Jan 2004 16:00:50 GMT
Dominique Devienne wrote:

>>From: Peter Reilly [mailto:peter.reilly@corvil.com]
>>
>>>Whoa, for a second there I though you could call methods off a reference,
>>>before the <property name="my-foo.toString()" refid="my-foo"/> registered
>>>in my brain ;-)  That would be way cool actually!!!
>>>
>>>Hey Peter, can't you code that? ;-) --DD
>>
>>I think that this is coded already using the jexl property support
>>in proposal/embed.
> 
> I thought you might say that... Like Dion, I still haven't grasped the
> new PropertyHelper stuff. Anybody knowledgeable who could write up
> something about it on the Wiki? We haven't heard from Costin in ages
> on this list. --DD

It's actually quite easy.
A PropertyHelper, is a something that is asked to resolve a property.

So if I register a Jexl PropertyHelper, each time a property is asked ot 
be resolved by ant, it asks first this PropertyHelper.

Inside the Jexl PropertyHelper, it checks if the property starts with 
'jexl:'. If yes, it resolves it using jexl, if not, it passes it on the 
the next helper. If no helper resolves it, usual resolving is done.

For example, there is one that uses velocity.
 From the code:

the task VelocityProperties [1], when called, does this:

     public void execute() {
         try {
             // Prepare the engine
             helper.engine=new VelocityEngine();
             //initialize it
             helper.engine.init();
             //create a context
             helper.context=new VelocityContext();
             //add the ant *Project* to it
             helper.context.put( "ant", project );

             // Register this class instance with Ant
             PropertyHelper phelper=PropertyHelper.getPropertyHelper( 
project );
             helper.setNext( phelper.getNext() );
             helper.setProject( project );
             phelper.setNext( helper );

         } catch( Exception ex ) {
             ex.printStackTrace();
         }

     }

In practice it adds itself as a property helper, IOW a class that gets a 
chance in evaluating a property.

Then, each time a property is asked to be resolved, this method is called:

         public static final String PREFIX="vm:";

         public Object getPropertyHook( String ns, String name,
                                        boolean user ) {

             if( ! name.startsWith(PREFIX) ) {
                 // pass on to next, as the property
                 //is not to be resolved here
                 return super.getPropertyHook(ns, name, user);
             }
             try {
                 //the actual content
                 name=name.substring( PREFIX.length() );
                 StringWriter sw=new StringWriter();

                 engine.evaluate( context, sw, "antVM", name );

                 System.out.println("VM: getProperty " + ns + " " +
                                     name + "=" + sw.toString());
                 //return the resolved property
                 return sw.toString();
             } catch( Exception ex ) {
                 ex.printStackTrace();
                 return null;
             }
         }


[1] 
http://cvs.apache.org/viewcvs.cgi/ant/proposal/embed/src/java/org/apache/tools/ant/taskdefs/optional/VelocityProperties.java?view=markup

-- 
Nicola Ken Barozzi                   nicolaken@apache.org
             - verba volant, scripta manent -
    (discussions get forgotten, just code remains)
---------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message