01: ....<insert> 02: ........line 1 03: .... 04: ........line 2 05: ....</insert>
You could delete empty lines, but then you loose the 03-line in this example. Maybe it would be better to place the text in an external file. Mmh - concat filter was a nice idea. So you could use the tail- and headfilter to split your file and use concat for appending them. Jan > -----Urspr�ngliche Nachricht----- > Von: Gilbert Rebhan [mailto:[EMAIL PROTECTED] > Gesendet am: Sonntag, 30. Januar 2005 13:59 > An: Ant Users List > Betreff: Re: AW: regexp question > > Hi, Jan > > thanks for the task!! > > Works great, but one thing similar to the echo task = > > f.e. if i write : > > ..........<echo file="foo.txt"> > ..........bla > ..........</echo> > > all the blanks (the '.') are echoed to the file > if i write > <echo file="foo.txt">bla</echo> it's echoed without blanks. > > So when using your task i have to write the insert part > all on one line, cause if writing : > > <insert inputfile="orig.txt" outputfile="new.txt" after="2"> > The text to be inserted. > </insert> > > the file would contain two line breaks i do not want : > bla > bla > > The text to be inserted. > > bla > bla > bla > > Had to look more than two times until i discovered what > went on here. > > Is it possible to get rid of that 'echo' behaviour ? > A build script with all those oneliners isn't that readable ... > > Gilbert > > > Quick hack: > > > > <project> > > <taskdef name="insert" classname="InsertTask" classpath="."/> > > <insert inputfile="orig.txt" outputfile="new.txt" after="5"> > > The text to be inserted. > > Maybe multiple lines? > > </insert> > > </project> > > > > > > > > import java.io.BufferedWriter; > > import java.io.File; > > import java.io.FileNotFoundException; > > import java.io.FileReader; > > import java.io.FileWriter; > > import java.io.IOException; > > import java.io.LineNumberReader; > > > > public class InsertTask { > > > > int after; > > File inputfile; > > File outputfile; > > String text = ""; > > > > > > public void execute() { > > try { > > LineNumberReader inp = new LineNumberReader (new > > FileReader(inputfile)); > > BufferedWriter out = new BufferedWriter(new > > FileWriter(outputfile)); > > String line; > > while ((line = inp.readLine()) != null) { > > out.write(line); > > out.write(System.getProperty("line.separator")); > > if (inp.getLineNumber()==after) { > > out.write(text); > > out.write(System.getProperty("line.separator")); > > } > > } > > inp.close(); > > out.close(); > > } catch (FileNotFoundException e) { > > e.printStackTrace(); > > } catch (IOException e) { > > e.printStackTrace(); > > } > > } > > > > public void addText(String text) { > > this.text += text; > > } > > public void setAfter(int after) { > > this.after = after; > > } > > public void setInputfile(File inputfile) { > > this.inputfile = inputfile; > > } > > public void setOutputfile(File outputfile) { > > this.outputfile = outputfile; > > } > > > > public static void main(String[] args) { > > InsertTask ins = new InsertTask(); > > ins.setInputfile(new File(args[0])); > > ins.setOutputfile(new File(args[1])); > > ins.setAfter(Integer.parseInt(args[2])); > > for(int i=3; i<args.length; i++) ins.addText(args[i]+" "); > > ins.execute(); > > } > > } > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] >
