使用Java中的长字符串(heredocs) - 可读方法? [英] Working with long strings (heredocs) in Java - the readable approach?

查看:102
本文介绍了使用Java中的长字符串(heredocs) - 可读方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理包含Java中换行符的长字符串。这些都是用于HTML生成的,但它并不是最重要的。



我知道Java在某种程度上没有 heredocs 即可。但是我可以使用其他机制:



1)String concatenation (或StringBuilders),不是非常易读和可复制粘贴。



2)在 .properties 文件中存储字符串,要么不易读,要么具有更高的复制性。



3)将每个heredoc存储在单独的 .txt 文件中,非常易读且易于复制,但会产生大量的txt文件。



4)模板引擎,如 Velocity Freemarker - 将设计移出Java,需要大量的地图操作,这将是相当不错的,但Velocity语法和循环/如果能力不如Smarty那样可读



每个都有优点和反对意见,我想选择3但是管理层更喜欢1因为纯粹的意识形态原因。我想在Java中使用heredocs有一些标准,可能是库让事情变得更容易。我很感激任何建议(有很好的论据)如何使用heredocs。



Thanx

解决方案

尝试使用Rythm模板引擎,它是一个高性能的Java模板引擎,使用Razor,语法干净。



链接:





更新



< Rythm现在有一个网站: http://rythmengine.org 和一个小提琴e site: http://fiddle.rythmengine.org


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

解决方案

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

这篇关于使用Java中的长字符串(heredocs) - 可读方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆