ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Jan.Mate...@rzf.fin-nrw.de>
Subject AW: Counting lines in a file
Date Fri, 12 May 2006 05:28:27 GMT
>import java.io.BufferedReader;
>import java.io.File;
>import java.io.FileReader;
>import java.util.ArrayList;
>
>import org.apache.tools.ant.BuildException;
>import org.apache.tools.ant.Task;
>
>public class LineCounter extends Task
>{
>	public String filename;
>	public void Execute() throws BuildException

as mentioned earlier it must be   execute()


>	{
>		LineCounter lc = new LineCounter();

You dont need that. You are inside LineCounter - why instantiating
another object?



>		log("about to call countlines");
>		log(filename+ " contains " +
lc.countLines(this.filename) +" lines.");

therefore just   .... + countLines(filename) + ....


>	}
>	public void setFilename(String filename)

You could use   setFilename(java.io.File file)  directly.
Ant will convert it.


>	{
>		System.out.println("we are about to parse ->"+filename);

Better use log()


>		this.filename= filename;
>	}
>	public int countLines(String file)
>	{
>		ArrayList integlist = new ArrayList();

Why holding the content if you just want to count the lines?
Think of huge files ....


>		try 
>		{
>			BufferedReader br = new 
>BufferedReader(new FileReader(new File(file)));
>			String line;
>			while((line = br.readLine())!=null)
>			{
>				integlist.add(line);
>			}
>			System.out.println(integlist.size());

Better use log()


>			br.close();
>		}
>		catch (Exception e) 
>		{
>			e.printStackTrace();
>			System.exit(-1);


just do a 
    throw new BuildException(e)
calling a System.exit is not a very scalable approach. Think of a
buildfile integrated into
a server. I dont know if CruiseControl would start a new VM for each
build ... If not you would 
stop the build loop of the continous integration server.




>		}
>		return integlist.size();
>	}
>}

Just returing? No access from the buildfile?
* add another attribute 'property'
    private String propertyName;
    public void setProperty(String p) { propertyName = p; }
* enhance the execute() method for setting the the value
    getProject().setNewProperty(propertyName,
Integer.valueOf(countLines(filename)));
* refactor using local fields for not calling countLines(filename)
multiple times for each file.


Jan

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


Mime
View raw message