Since properties are immutable I don't think you need to check to see if a
property is set or not. Make sure you set the default after any other
possible setting of the property. If the property gets set then setting the
default will do nothing. 

HTH Bill

-----Original Message-----
From: Peter Reilly [mailto:[EMAIL PROTECTED] 
Sent: Thursday, June 02, 2005 9:40 AM
To: Ant Users List
Subject: Re: Ant Contrib <if> and <macrodef> question

Your macro is incorrect as the line

<property name="temp.checkIsSet" value="@{checkIsSet}" /> will cause the
property "temp.checkIsSet" to be set always.

I am not quite sure exactly what you want to do but the following macro
should be it:

<macrodef name="setFromPropertyOrDefault">
   <attribute name="varName"/>
   <attribute name="default"/>
   <attribute name="property"/>
   <sequential>
      <ac:if>
         <isset property="@{property}"/>
         <then>
            <ac:var name="@{varName}" value="[EMAIL PROTECTED]"/>
         </then>
         <else>
            <ac:var name="@{varName}" value="@{default}"/>
         </else>
      </ac:if>
    </sequential>
</macrodef>

<setFromPropertyOrDefault  varName="testVar" default="a default"
property="not_present"/> <echo>testVar is ${testVar}</echo> <property
name="present" value="a present property"/> <setFromPropertyOrDefault
varName="testVar" default="a default" property="present"/> <echo>testVar is
${testVar}</echo>


Brent Bain wrote:

>Hello:
>
>I'm trying to write a macro to check if a property has been set in a 
>xml property file.  If it has not been set then I want to give it a 
>default value.  This is running in a loop (using the xmltask's call) to 
>create db property files for several different environments.  The loop 
>works great after some minor tweaking (thanks Brian!) but I've hit 
>another stumbling block.
> 
>My macro looks like this:
> <macrodef name="replaceDefaults">
>  <attribute name="varName" />
>  <attribute name="currentDefault" />
>  <attribute name="checkIsSet" />
>  <sequential>
>   <echo message="checkIsSet: @{checkIsSet}" />
>   <property name="temp.checkIsSet" value="@{checkIsSet}" />
>   <if>
>    <isset property="temp.checkIsSet" />
>    <then>
>     <echo message="isset" />
>     <var name="@{varName}" value="@{checkIsSet}" />
>    </then>
>    <else>
>     <echo message="else" />
>     <var name="@{varName}" value="@{currentDefault}" />
>    </else>
>   </if>
>  </sequential>
> </macrodef>
> 
>I've tried this several different ways and keep hitting what I think is 
>an error between the macrodef @{property} and how the ant contrib's 
><if> handles the <isset> condition.
> 
>If I call it like this:
><property name="temp.dbServer" value="dbServer2" /> <replaceDefaults 
>varName="testVar" currentDefault="dbServer1"
>checkIsSet="${temp.dbServer}" />
>It correctly enters the <then> and sets the testVar = dbServer2
> 
>If I call it like this (NOT setting the property first):
><replaceDefaults varName="testVar" currentDefault="dbServer1"
>checkIsSet="${temp.dbServer}" />
>It still enters the <then>
> 
>If I remove the line <property name="temp.checkIsSet" value="@{checkIsSet}"
>/> and then also change <isset property="checkIsSet" /> then it always 
>enteres the <else>
> 
>Is there something obviously wrong that I'm doing or is this just a 
>"limitation" of the contrib task and macrodef?  Is there some more 
>obvious way to set properties (I'm using ant contrib's <var>'s so I can 
>do my
>looping) with defaults??
> 
>Any tips would be greatly appreciated!
>Thanks,
>Brent
> 
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional
commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to