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

Udo Borkowski ub at abego-software.de
Sun Jan 30 11:06:13 PST 2011


I would also add a charset/encoding parameter to the most general write method:

> 	public int write(File outputFile,
> 					 STErrorListener listener,
> 					 Locale locale,
> 					 int lineWidth,
> 					 String charsetName)…

and pass this to the OutputStreamWriter constructor

> 			OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName);


The other write methods could then pass an "UTF-8", e.g.

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


Udo

On 30.01.2011, at 19:49, Terence Parr wrote:

> 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
> 
> _______________________________________________
> 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/5dfb6bbf/attachment.html 


More information about the stringtemplate-interest mailing list