You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jesper Linvald <je...@kabelnettet.dk> on 2003/07/31 18:56:21 UTC
#foreach --> new File
Hello Gurus,
I would like to be able to do like the following :
#foreach($entity in $model.get_entities())
#set( $entityName = $entity.get_name() )
$writer.changeOutput("${entityName}.java")
public class $entityName{
...
}
#end
That is:
During iteration in a template I would like to be able to direct the
output to a specific file!
Do you have any tricks or suggestions as how do that ? I realize that I
could have my iteration in a Java file and supply a new writer whenever
I want – but this is not what I want.
The writer on which I call changeOutput(fileName) is homebrew but is
only able to create the new files in the loop – not to actually write to
them!
I suspect that Velocity might wrap the writer that you supply but what
can be done to control the output ?
Any suggestion is more than welcome!
Thank you :)esper
I append my Writer class aswell:
package dk.itu.next.rea.transform.velocity;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class VelocityContextWriter extends Writer {
private Writer writer;
private String fileName="dummy.java";
//----------- NEW METHODS --------------------------
public void changeOutput(String fileName){
this.fileName = fileName;
try {
flush();
close();
writer = new FileWriter(fileName);
}
catch (IOException e) {
e.printStackTrace();
}
}
public VelocityContextWriter() {
super();
try {
writer = new FileWriter(fileName);
}
catch (IOException e) {
e.printStackTrace();
}
}
// ---------- WRITER METHODS ----------------------
public VelocityContextWriter(Object lock) {
super(lock);
}
public void write(char[] cbuf, int off, int len) throws
IOException {
if(writer != null)
writer.write(cbuf, off, len);
}
public void flush() throws IOException {
writer.flush();
}
public void close() throws IOException {
writer.close();
}
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.498 / Virus Database: 297 - Release Date: 08-07-2003
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: #foreach --> new File
Posted by Claude Brisson <cl...@savoirweb.com>.
>From what I saw, the principle looks dirty but ok...
> I suspect that Velocity might wrap the writer that you supply but what
> can be done to control the output ?
Velocity doesn't do such a wrapping, it should be ok (since dirty)... provided you call directly the Template.merge(Context,Writer)
method.
Try to add some debugging to your writer to understand what happens, like write a dummy line in the file from the changeOutput
method...
Do you have free space on your hard drive ? ;-)
CloD
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org