10

I need to work with long strings containing line breaks in Java. Those are for HTML generation, but it is not the most important here.

I'm aware Java is cripple in a way it doesn't have heredocs. But there are other mechanisms I could use:

1) String concatenation (or StringBuilders), not very readable and copy-pasteable.

2) Storing strings in .properties files, either not very readable, but with higher copypasteability.

3) Storing each "heredoc" in seperate .txt file, quite readable and copypasteable, but resulting in a horde of txt files.

4) Template engines, like Velocity or Freemarker - moves design out of Java, requires a lot of map operations, it would be quite good, but the Velocity syntax and loop/if abilities aren't as readable as for example those from Smarty

Each have pros and contras, I'd like to choose 3 but the management prefers 1 because of pure ideological reasons. I'd like to have some standard for working with heredocs in Java, possibly library that makes things easier. I'd be gratefull for any suggestions (with good arguments) how to work with heredocs.

Thanx

FolksLord
  • 992
  • 2
  • 9
  • 17

2 Answers2

10

I hate to be "that guy", who suggests that you take a completely different approach than what you asked about, but have you looked at Groovy? It's JVM language, can be mixed freely with Java, and in addition to a bunch of other really nice language features, it has heredocs.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • 2
    2021 update. JDK 15 has heredocs. They're referred to as "Text Blocks". See https://docs.oracle.com/en/java/javase/15/text-blocks/index.html – GreyBeardedGeek Nov 21 '21 at 11:58
  • Good Stack Overflow answer with examples of what you can do from Java 13, 14 an 15 with heredoc which they've denoted as "Text Blocks": https://stackoverflow.com/a/60560576/1852005 – stwr667 Dec 28 '22 at 01:26
2

Try Rythm template engine, which is built as a high performance Java template engine with Razor like clean syntax.

Links:

Updates

Rythm now has a web site: http://rythmengine.org, and a fiddle site: http://fiddle.rythmengine.org

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139