ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Patrick Martin" <antu...@gmail.com>
Subject Re: Counting lines in a file
Date Fri, 12 May 2006 12:06:39 GMT
Indeed:

<typedef resource="/net/sf/antcount/defs.properties" />
<concat>
	<fileset file="${my.file}" />
	<filterchain>
		<countfilter property="nb.lines" />
		<stopfilter />
	</filterchain>
</concat>
<echo message="number of lines: ${nb.lines}" />

Patrick

On 5/12/06, Jan.Materne@rzf.fin-nrw.de <Jan.Materne@rzf.fin-nrw.de> wrote:
> And maybe there are some existing tasks you could use, like http://antcount.sourceforge.net/
 ;-)
>
> Jan
>
> >-----Urspr√ľngliche Nachricht-----
> >Von: Jan.Materne@rzf.fin-nrw.de [mailto:Jan.Materne@rzf.fin-nrw.de]
> >Gesendet: Freitag, 12. Mai 2006 07:28
> >An: user@ant.apache.org
> >Betreff: AW: Counting lines in a file
> >
> >>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
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

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


Mime
View raw message