NoIndentWriter not respecting whitespace in template

守給你的承諾、 提交于 2019-12-10 08:48:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!