ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dominique Devienne" <DDevie...@lgc.com>
Subject RE: AW: Looping through a file contents
Date Thu, 19 Aug 2004 14:59:03 GMT
> From: Peter Reilly [mailto:peterreilly@apache.org]
> an example with ant 1.6.2 and ant-contrib:
>     <loadfile property="names" srcfile="x.input"/>
>     <ac:for delimiter="${line.separator}" param="name"
>             list="${names}">
>       <sequential>
>         <echo>name is @{name}</echo>
>       </sequential>
>     </ac:for>

And you can of course use Peter's very own <macrodef>
to make it even more explicit (see further down).

1) Note the use of the lines4@{file} to emulate local
   properties. Are we adding local properties soon???

2) Note that empty lines are lost.

3) Note that ${line.separator} is replaced by it's value.

4) Peter, did you put in more stringent rules on what
   can be used for the <ac:for>

Maybe we need an attribute on <echo> to not perform
property expansion? --DD

C:\oss\org_apache\antx>type foreach-line.xml
<?xml version="1.0"?>

<project name="foreach-line"
         xmlns:ac="antlib:net.sf.antcontrib">

  <macrodef name="foreach-line">
    <attribute name="file" />
    <attribute name="var" default="line" />
    <element name="body" implicit="true" />
    <sequential>
      <loadfile property="lines4@{file}" srcfile="@{file}" />
      <ac:for delimiter="${line.separator}"
              param="@{var}" list="${lines4@{file}}">
        <sequential>
          <body />
        </sequential>
      </ac:for>
    </sequential>
  </macrodef>

  <echo>${line.separator}Printing ${ant.file} (var=line)</echo>
  <foreach-line file="${ant.file}">
    <echo>@{line}</echo>
  </foreach-line>

  <echo>${line.separator}Printing ${ant.file} (var=l)</echo>
  <foreach-line file="${ant.file}" var="l">
    <echo>@{l}</echo>
  </foreach-line>

</project>

C:\oss\org_apache\antx>...\ant\bin\ant -f foreach-line.xml
Buildfile: foreach-line.xml
     [echo]
     [echo] Printing C:\oss\org_apache\antx\foreach-line.xml (var=line)
     [echo] <?xml version="1.0"?>
     [echo] <project name="foreach-line"
     [echo]          xmlns:ac="antlib:net.sf.antcontrib">
     [echo]   <macrodef name="foreach-line">
     [echo]     <attribute name="file" />
     [echo]     <attribute name="var" default="line" />
     [echo]     <element name="body" implicit="true" />
     [echo]     <sequential>
     [echo]       <loadfile property="lines4@{file}" srcfile="@{file}"
/>
     [echo]       <ac:for delimiter="
     [echo] "
     [echo]               param="@{var}" list="${lines4@{file}}">
     [echo]         <sequential>
     [echo]           <body />
     [echo]         </sequential>
     [echo]       </ac:for>
     [echo]     </sequential>
     [echo]   </macrodef>
     [echo]   <echo>
     [echo] Printing C:\oss\org_apache\antx\foreach-line.xml
(var=line)</echo>
     [echo]   <foreach-line
file="C:\oss\org_apache\antx\foreach-line.xml">
     [echo]     <echo>@{line}</echo>
     [echo]   </foreach-line>
     [echo]   <echo>
     [echo] Printing C:\oss\org_apache\antx\foreach-line.xml
(var=l)</echo>
     [echo]   <foreach-line
file="C:\oss\org_apache\antx\foreach-line.xml" var="l">
     [echo]     <echo>@{l}</echo>
     [echo]   </foreach-line>
     [echo] </project>
     [echo]
     [echo] Printing C:\oss\org_apache\antx\foreach-line.xml (var=l)
     [echo] <?xml version="1.0"?>
     [echo] <project name="foreach-line"
     [echo]          xmlns:ac="antlib:net.sf.antcontrib">
     [echo]   <macrodef name="foreach-line">
     [echo]     <attribute name="file" />
     [echo]     <attribute name="var" default="line" />
     [echo]     <element name="body" implicit="true" />
     [echo]     <sequential>
     [echo]       <loadfile property="lines4@{file}" srcfile="@{file}"
/>
     [echo]       <ac:for delimiter="
     [echo] "
     [echo]               param="@{var}" list="${lines4@{file}}">
     [echo]         <sequential>
     [echo]           <body />
     [echo]         </sequential>
     [echo]       </ac:for>
     [echo]     </sequential>
     [echo]   </macrodef>
     [echo]   <echo>
     [echo] Printing C:\oss\org_apache\antx\foreach-line.xml
(var=line)</echo>
     [echo]   <foreach-line
file="C:\oss\org_apache\antx\foreach-line.xml">
     [echo]     <echo>@{line}</echo>
     [echo]   </foreach-line>
     [echo]   <echo>
     [echo] Printing C:\oss\org_apache\antx\foreach-line.xml
(var=l)</echo>
     [echo]   <foreach-line
file="C:\oss\org_apache\antx\foreach-line.xml" var="l">
     [echo]     <echo>@{l}</echo>
     [echo]   </foreach-line>
     [echo] </project>

BUILD SUCCESSFUL
Total time: 1 second

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


Mime
View raw message