Issue Details (XML | Word | Printable)

Key: GROOVY-1503
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Paul King
Reporter: Jonathan Carlson
Votes: 1
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
groovy

SimpleTemplateEngine drops Windows new-line character (\r)

Created: 19/Sep/06 03:29 PM   Updated: 22/Nov/07 05:29 AM
Component/s: XML Processing
Affects Version/s: 1.0-JSR-6
Fix Version/s: 1.1-rc-1

Time Tracking:
Not Specified

Environment: Windows XP, JSR-6


 Description  « Hide
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!



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Jonathan Carlson added a comment - 19/Sep/06 03:50 PM
Come to think of it, the problem may be in the File.write(aString) method. I'm not sure where to find that, though.

Jonathan Carlson added a comment - 19/Sep/06 04:16 PM
SimpleTemplateEngine was not the problem. I'll open a new issue for File.write(string)

Jonathan Carlson added a comment - 19/Sep/06 04:36 PM
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!


Jonathan Carlson added a comment - 20/Sep/06 11:29 AM
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

Paul King added a comment - 04/Jul/07 08:19 AM
Do you have a test or a template file which illustrates this problem?

Jonathan Carlson added a comment - 05/Jul/07 08:56 AM
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!

Jonathan Carlson added a comment - 05/Jul/07 09:04 AM
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()


Jesper Vrelits added a comment - 05/Jul/07 09:04 AM
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.

Jonathan Carlson added a comment - 05/Jul/07 09:51 AM
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}


Paul King added a comment - 10/Oct/07 05:49 AM
There is now a PlatformLineWriter which you can use to convert Groovy normalised strings/streams into platform-friendly strings/streams.