Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with large text snippets in Java source

Tags:

Are there any good ways to work with blocks of text (Strings) within Java source code? Many other languages have heredoc syntax available to them, but Java does not. This makes it pretty inconvenient to work with things like tag libraries which output a lot of static markup, and unit tests where you need to assert comparisons against blocks of XML.

How do other people work around this? Is it even possible? Or do I just have to put up with it?

like image 604
Rob Hruska Avatar asked Apr 23 '09 17:04

Rob Hruska


2 Answers

If the text is static, or can be parameterized, a possible solution would be to store it in an external file and then import it. However, this creates file I/O which may be unnecessary or have a performance impact. Using this solution would need to involve caching the file contents to reduce the number of file reads.

like image 167
Rob Hruska Avatar answered Sep 25 '22 03:09

Rob Hruska


The closes option in Java to HereDoc is java.text.MessageFormat. You can not embed logic. It a simple value escape utility. There are no variables used. You have to use zero based indexing. Just follow the javadoc.

http://download.oracle.com/javase/1,5.0/docs/api/java/text/MessageFormat.html

like image 36
Aris Bartee Avatar answered Sep 25 '22 03:09

Aris Bartee