Living without here-documents

Since Java won't have here-documents any time soon, what's the best alternative for embedding blocks of formatted text in your code? Obviously you can use regular concatenated strings, but that places a dual burden of needing to maintain the entwined syntaxes of Java and whatever's being wrapped. It may be expedient but it's always going to be ugly.


I think a better mechanism is to use the class loader's getResourceAsStream("filename") call to load a file containing the pre formatted text. My only concern with this approach is there's no standard way of dividing multiple multi-line strings in a single text document. I'd rather not have one file per block of text.


So there's no standard solution, but there are examples in the SDK of Java solving the same problem. The policy documents under the jre/lib/security directory use a simple curly brace delimited syntax for encompassing multiple lines.


grant codeBase "file:${java.home}/lib/ext/*" {
permission java.security.AllPermission;
....
};


Simply using curly braces isn't a good choice for a generic solution though because the text being wrapped may well contain curlies and you would need to start escaping the content; and that's pretty much what this exercise is trying to avoid.



For my proposed solution I turn back to the syntax of the fore mentioned here-documents as the best generic solution to this problem.


NAME=END-BLOCK-TOKEN
random content
END-BLOCK-TOKEN

The benefit of this approach include the following :

1.) The contained blocks of text would be accessible in a manner consistent with regular property files.

   String s = BlockProperties.get("NAME");

2.) The syntax is close to that of a standard property file.

3.) Content would not need escaping.

4.) Super simple to parse.










Comments

Popular posts from this blog

Shark Crackers

Running roughshod or ripshod

Axis, Axes, Axii?