Hi,

> Example:- If I was to provide the value "Test2", I would like the Ant 
> to search for "Test2" in the file, and return the value "blue". If I 
> provide the value - "Test3", it should return the value "green"

with less coding, you need =

bsf.jar (Bean Scripting Framwork)
http://jakarta.apache.org/bsf/
jruby.jar
http://jruby.codehaus.org/

available for ant  =


<project name="bla" default="main" basedir=".">

    <property name="scanfor" value="Test2"/>

        <target name="depends">
   
        <script language="ruby">
        <![CDATA[
            File.read('Y:/test/sample.txt').scan(/(#{$scanfor})\s*\n\w+=(\w+)/) 
{|a,b| 
            $project.setProperty "scanresult",b
            }
         ]]>
         </script>
        
        </target>
 
        <target name="main" depends="depends">     
     
        <echo>$${scanresult} ==
              ${scanfor} => ${scanresult}       
        </echo>      
        
        </target>

</project>

depends:
main:
     [echo] ${scanresult} ==
     [echo] Test2 => blue



Regards, Gilbert

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 23, 2007 9:02 AM
To: user@ant.apache.org
Subject: AW: Using ant to parse text file

I played a little bit ...

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;


public class LoadGroupTask extends Task {

        String prefix;
        File   file;
        String section;
        
        @Override
        public void execute() throws BuildException {
                validate();
                BufferedReader rdr;
                try {
                        rdr = new BufferedReader(new FileReader(file));
                        boolean needThisSection = false;
                        String line = rdr.readLine();
                        while (line!=null) {
                                line = line.trim();
                                if (line.length()>0) {
                                        if (line.indexOf("=") > 0) {
                                                if (needThisSection) {
                                                        String[] parts = 
line.split("=");
                                                        String propName  = 
(prefix==null)   ? parts[0] : prefix + parts[0];
                                                        String propValue = 
(parts[1]!=null) ? parts[1] : "";
                                                        log("Setting property " 
+ propName + " -> " + propValue);
                                                        
getProject().setNewProperty(propName, propValue);
                                                }
                                        } else {
                                                needThisSection = 
section.equals(line);
                                        }
                                }
                                line = rdr.readLine();
                        }
                        rdr.close();
                } catch (Exception e) {
                        throw new BuildException(e, getLocation());
                }
        }


        protected void validate() {
                if (file==null)      throw new BuildException("You must specify 
a file to read.");
                if (!file.canRead()) throw new BuildException("Can not read 
file " + file.getAbsolutePath());
                if (section==null)   throw new BuildException("You must specify 
a section to read.");
        }
        
        
        public void setFile(File file) {
                this.file = file;
        }
        public void setPrefix(String prefix) {
                this.prefix = prefix;
        }
        public void setSection(String section) {
                this.section = section;
        }
        
        
}


<project>
        <taskdef name="loadgroup" classname="LoadGroupTask" 
classpath="build/classes"/>
        <loadgroup file="data.txt" prefix="data." section="Test2"/>
        <echoproperties prefix="data"/>
</project>


Jan 

>-----Ursprüngliche Nachricht-----
>Von: Kevin Jackson [mailto:[EMAIL PROTECTED] 
>Gesendet: Freitag, 23. Februar 2007 03:31
>An: Ant Users List
>Betreff: Re: Using ant to parse text file
>
>Hi,
>
>If you don't want to write a custom task in java, you could also try
><scriptdef> and use a scripting language of your choice
>
>http://ant.apache.org/manual/OptionalTasks/scriptdef.html
>
>Kev
>
>---------------------------------------------------------------------
>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]


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

Reply via email to