2010-08-31 52 views
1

我在Groovy中实现了一些现有文件格式的DSL。 在这种格式我们有像Groovy DSL:处理标签


group basic_test { 
    test vplan_testing { 
     dir: global_storage; 
    }; 
}; 

构建在这里,我有这个dir: global_storage问题 - 常规认为“导演:”作为一个标签,所以我不能处理它。

你有一个想法,我怎么可以接收一些回调(getProperty,invokeMissingMethod)这个构造?

谢谢!

回答

2

我不相信你可以这样做,你需要改变你的dsl以便能够捕获这些信息。这里是你如何能做到这一点:

class Foo { 
    static plan = { 
     vplan_testing { 
      dir 'global_storage' 
     } 
    } 
} 

def closure = Foo.plan 
closure.delegate = this 
closure() 

def methodMissing(String name, Object args) { 
    println "$name $args"  
    if(args[0] instanceof Closure) 
     args[0].call() 
} 

输出将是

DIR [global_storage]

,或者你可以定义你的DSL这种方式:

class Foo { 
    static plan = { 
     vplan_testing { 
      test dir:'global_storage' 
     } 
    } 
} 

替换“测试“的东西有意义的域名。在这种情况下,输出将

测试[导演:global_storage]

希望这有助于

-Ken

+0

谢谢。我希望我可以使用Groovy DSL作为现有格式的解析器,而无需进行任何更改 – 2010-09-01 06:10:55