问题
I am trying to use the NoIndentWriter class to get around a problem I am facing related to the inserting of whitespace in multiline Strings.
However, it appears that NoIndentWriter
does a lot more than just not auto-indenting. In particular it trims all leading (but not trailing whitespace) even if explicitly present in a template.
E.g. with the following template file:
$ cat src/templates/a.xml.stg
ladder(value)::=<<
0
1
2
3
^value^
>>
... and the following code:
public static void main(String args[]) throws Exception {
STGroup stGroup = new STGroupFile("templates/a.xml.stg", '^', '^');
ST st = stGroup.getInstanceOf("ladder");
st.add("value", "4");
StringWriter sw = new StringWriter();
NoIndentWriter w = new NoIndentWriter(sw);
st.write(w);
String result = sw.toString();
System.out.printf("result is:\n%s\n", result);
PrintWriter pw = new PrintWriter("out");
pw.println(result);
pw.close();
}
... the following is produced:
$ cat out
0
1
2
3
4
By examining the out
file it is seen that trailing whitespace present in the template file was preserved but leading whitespace trimmed.
Is there any way to instruct StringTemplate to preserve whitespace in template files without using the AutoIndentWriter (because it is suffering from this problem)?
来源:https://stackoverflow.com/questions/27931007/noindentwriter-not-respecting-whitespace-in-template