问题
This is obviously a SSCCE.
I have the following template file:
xml(value)::=<<
<a>
<b>
^value^
</b>
</a>
>>
... with the following code:
private static final String VALUE="alpha\nbeta";
public static void main(String args[]) throws Exception {
STGroup stGroup = new STGroupFile("templates/a.xml.stg", '^', '^');
ST st = stGroup.getInstanceOf("xml");
st.add("value", VALUE);
System.out.printf("--------\n%s\n--------", st.render());
}
The code produces:
[java] --------
[java] <a>
[java] <b>
[java] alpha
[java] beta
[java] </b>
[java] </a>
[java] --------
In other words, the String value is modified as whitespace is added. When a client parses the XML file, even with whitespace trimming, the String value will be perceived as changed as whitespace has been added inside the String.
I understand that a solution is to declare the template as:
xml(value)::=<<
<a>
<b>^value^</b>
</a>
>>
... but even this solution necessitates that all other nodes which may contain the xml
rendering are also right-justified.
E.g., assuming that the above rendered value is embedded in another template, then the following template:
enclosing-document(xml)::=<<
<h1>
<h2>
^xml^
</h2>
</h1>
>>
... will also have to be re-written as:
enclosing-document(xml)::=<<
<h1>
<h2>
^xml^
</h2>
</h1>
>>
... in effect necessitating the left-justification of all nodes to be sure.
Is there a way to instruct StringTemplate to just emit a particular String parameter as it is (without indenting it in case of new lines present within) while at the same time preserve whitespace / indentation for all other cases (so that my rendered output will not be ugly)?
Update
I tried using the NoIndentWriter but was met with other problems. So it appears that the NoIndentWriter
does indeed emit the multiline String as it is but does not honor whitespace present in the template file. So I am back at square one.
回答1:
So in the end what I did was to use the NoIndentWriter with the code given in this post. Then, to solve the lack of indentation that results in ugly XML (complained about in the aforementioned post), I used the XML pretty-printer described here.
来源:https://stackoverflow.com/questions/27889951/stringtemplate-indentation-adds-whitespace-inside-string