【问题标题】:How to code Writable Interface in groovy如何在 groovy 中编写可写接口
【发布时间】:2014-02-07 22:29:42
【问题描述】:

希望使用 groovy 为 Asciidoctor 标记编写 groovy.text.Template。到目前为止:

//-----------------------------------

public class AsciidocTemplate implements groovy.text.Template
{
boolean includeHeaderFooter = true;

Asciidoctor asciidoctor = create();
org.asciidoctor.Options asciidoctorJOptions=["backend": "html","header_footer":includeHeaderFooter]

def payload="";
Map binding = [:]

public AsciidocTemplate()
{
} // end of constructor

public AsciidocTemplate(def payload)
{
    this.payload = payload;
} // end of constructor

// use doctor to transform the template
public Writable process()
{
    def output = asciidoctor.render(this.payload, asciidoctorJOptions);
    Writer ou = output;
    return ou;        
} // end of make

Writable make()
{
    return process();  //output as Writable;
} // end of make

// -----------------------------------

render() 返回一个字符串,我们如何将该字符串转换为实现 Writable 接口的对象,我只是不明白。也看不到如何使用/读取/存储返回的“可写”对象:-P

【问题讨论】:

    标签: java groovy interface writable


    【解决方案1】:

    你可以使用the asWritable method on a Closure来获得一个Writable返回,即:

    Writable process()
    {
        def output = asciidoctor.render(this.payload, asciidoctorJOptions);
        { w -> w.println output }.asWritable()
    }
    

    【讨论】:

    • 杰出的蒂姆。确实尝试过“可写”但没有乐趣,所以你的想法看起来很享受。
    • @jnorthr Cool :-) 如果可行,您可以accept it as an answer 吗?手指交叉:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多