groovy

SimpleTemplateEngine drops Windows new-line character (\r)

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-6
  • Fix Version/s: 1.1-rc-1
  • Component/s: XML Processing
  • Labels:
    None
  • Environment:
    Windows XP, JSR-6
  • Number of attachments :
    0

Description

My template files have \r\n at the end of lines, but the generated file comes out with just \n.

This can be a real pain in the behind for Windows tools that show the whole file in one line.

Thanks in advance for fixing this!

Activity

Hide
Jonathan Carlson added a comment -

Come to think of it, the problem may be in the File.write(aString) method. I'm not sure where to find that, though.

Show
Jonathan Carlson added a comment - Come to think of it, the problem may be in the File.write(aString) method. I'm not sure where to find that, though.
Hide
Jonathan Carlson added a comment -

SimpleTemplateEngine was not the problem. I'll open a new issue for File.write(string)

Show
Jonathan Carlson added a comment - SimpleTemplateEngine was not the problem. I'll open a new issue for File.write(string)
Hide
Jonathan Carlson added a comment -

I'm sorry I didn't get this straight before, but File.write(string) is working just like Java println does so that is not the problem. The problem still remains that a Windows template file (with \r\n) generates output with just \n

Thanks in advance!

Show
Jonathan Carlson added a comment - I'm sorry I didn't get this straight before, but File.write(string) is working just like Java println does so that is not the problem. The problem still remains that a Windows template file (with \r\n) generates output with just \n Thanks in advance!
Hide
Jonathan Carlson added a comment -

The template script generates a lot of out.print "whatever \n" commands (notice the \n at the end of the line). If, instead, these types of lines could be converted to out.println "whatever" (use println with no \n at the end of the line) then it would always behave as it should.

  • Jonathan
Show
Jonathan Carlson added a comment - The template script generates a lot of out.print "whatever \n" commands (notice the \n at the end of the line). If, instead, these types of lines could be converted to out.println "whatever" (use println with no \n at the end of the line) then it would always behave as it should.
  • Jonathan
Hide
Paul King added a comment -

Do you have a test or a template file which illustrates this problem?

Show
Paul King added a comment - Do you have a test or a template file which illustrates this problem?
Hide
Jonathan Carlson added a comment -

I'm sorry, it's been a long time since I've worked on this on Windows. Now I'm using a Mac and I haven't used the Template Engine since then, although I hope to use it again!

Show
Jonathan Carlson added a comment - I'm sorry, it's been a long time since I've worked on this on Windows. Now I'm using a Mac and I haven't used the Template Engine since then, although I hope to use it again!
Hide
Jonathan Carlson added a comment -

Here's a very basic template test script I found in my scripting archive if it would help you! It seems to break the lines fine on Mac.

// This script is an example for generating code or e-mails or HTML or whatever
// from a template file using Groovy markup.

import groovy.text.Template
import groovy.text.SimpleTemplateEngine

// ===== Define the Template =====
//def text = 'Dear \"${firstname} ${lastname}\",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}'
def templateFile = new File("TemplateFile.txt")
def verbose
def engine = new SimpleTemplateEngine(verbose = true) // prints out the template text
// You could also replace the file below with the text string above
def template = engine.createTemplate(templateFile)

println "\n=========== Generated Text 1 ==========="
def binding = [firstname:"Jim", lastname:"Slim", city:"Minneapolis", month:"December", signed:"Prince"]
println template.make(binding).toString()

println "\n=========== Generated Text 2 ==========="
binding.firstname = "Jonathan"
binding.lastname = "Carlson"
binding.city = "Minneapolis"
binding.month = "January"
binding.signed = "Jesse Ventura"
println template.make(binding).toString()

Show
Jonathan Carlson added a comment - Here's a very basic template test script I found in my scripting archive if it would help you! It seems to break the lines fine on Mac. // This script is an example for generating code or e-mails or HTML or whatever // from a template file using Groovy markup. import groovy.text.Template import groovy.text.SimpleTemplateEngine // ===== Define the Template ===== //def text = 'Dear \"${firstname} ${lastname}\",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}' def templateFile = new File("TemplateFile.txt") def verbose def engine = new SimpleTemplateEngine(verbose = true) // prints out the template text // You could also replace the file below with the text string above def template = engine.createTemplate(templateFile) println "\n=========== Generated Text 1 ===========" def binding = [firstname:"Jim", lastname:"Slim", city:"Minneapolis", month:"December", signed:"Prince"] println template.make(binding).toString() println "\n=========== Generated Text 2 ===========" binding.firstname = "Jonathan" binding.lastname = "Carlson" binding.city = "Minneapolis" binding.month = "January" binding.signed = "Jesse Ventura" println template.make(binding).toString()
Hide
Jesper Vrelits added a comment -

Made the following little patch to SimpleTemplateEngine (partial code):
/* Handle raw new line characters.
*/
if (c == '\n' || c == '\r') {
if (c == '\r') { // on Windows, "\r\n" is a new line.
reader.mark(1);
c = reader.read();
if (c != '\n') { reader.reset(); }
}
sw.write("\\r\\n\");\nout.print(\"");
continue;
}
sw.write(c);
Works as we are windows only.
Better solution: Should use println's to get platform dependent lf.

Show
Jesper Vrelits added a comment - Made the following little patch to SimpleTemplateEngine (partial code): /* Handle raw new line characters. */ if (c == '\n' || c == '\r') { if (c == '\r') { // on Windows, "\r\n" is a new line. reader.mark(1); c = reader.read(); if (c != '\n') { reader.reset(); } } sw.write("\\r\\n\");\nout.print(\""); continue; } sw.write(c); Works as we are windows only. Better solution: Should use println's to get platform dependent lf.
Hide
Jonathan Carlson added a comment -

Oh, here's the TemplateFile.txt content that the script uses:

Dear ${firstname} ${lastname},
So nice to meet you in <% print city %>.
See you in ${month},
${signed}

Show
Jonathan Carlson added a comment - Oh, here's the TemplateFile.txt content that the script uses: Dear ${firstname} ${lastname}, So nice to meet you in <% print city %>. See you in ${month}, ${signed}
Hide
Paul King added a comment -

There is now a PlatformLineWriter which you can use to convert Groovy normalised strings/streams into platform-friendly strings/streams.

Show
Paul King added a comment - There is now a PlatformLineWriter which you can use to convert Groovy normalised strings/streams into platform-friendly strings/streams.

People

Vote (1)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: