[stringtemplate-interest] ST4: simple write to file method.

Terence Parr parrt at cs.usfca.edu
Sun Jan 30 10:49:38 PST 2011


I'm adding a few options. will this work for you?

Ter
	public int write(File outputFile, STErrorListener listener)
		throws IOException
	{
		return write(outputFile, listener, Locale.getDefault(), STWriter.NO_WRAP);
	}

	public int write(File outputFile, STErrorListener listener, int lineWidth)
		throws IOException
	{
		return write(outputFile, listener, Locale.getDefault(), lineWidth);
	}

	public int write(File outputFile,
					 STErrorListener listener,
					 Locale locale,
					 int lineWidth)
		throws IOException
	{
		Writer bw = null;
		try {
			FileOutputStream fos = new FileOutputStream(outputFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			AutoIndentWriter w = new AutoIndentWriter(bw);
			w.setLineWidth(lineWidth);
			int n = write(w, locale, listener);
			bw.close();
			bw = null;
			return n;
		}
		finally {
			if (bw != null) bw.close();
		}
	}

On Jan 30, 2011, at 7:26 AM, Collin Fagan wrote:

> Hi,
> 
> I find myself constantly writing the output of templates to files. I was wondering if we could add a method to ST that writes directly to a file?
> 
> Something like this:
> 
> public int write(File outputFile) throws IOException {
>         Writer writer = null;
>         try {
>             writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile)));
>             int returnValue = write(new AutoIndentWriter(writer));
>             writer.flush();
>             return returnValue;
>         } finally {
>             try {
>                 if (writer != null) {
>                     writer.close();
>                 }
>             } catch (IOException ex) {
>                 // what can you do when a close fails???
>                 // nothing :(
>             }
>         }
>     }
> 
> One might even provide a variant that lets you specify the encoding. 
> 
> Does something like this sounds useful?
> 
> Thanks,
> 
> Collin
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110130/6222f8a5/attachment-0001.html 


More information about the stringtemplate-interest mailing list