simple way to expand $value in Java String?

View: New views
1 Messages — Rating Filter:   Alert me  

simple way to expand $value in Java String?

by Ian Cairns-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm working with ConfigSlurper and I want to handle input from a
configuration file like:

mysql {
    mycnf = '${root}/my.cnf'
    root = '/tmp'
}

and end up with a map like ["root":"/tmp", "mycnf":"/tmp/my.cnf"].

My current solution is:

def cs = new ConfigSlurper().parse(new File('x.cfg').toURL())
def resolved = [:]
def vars = [:]
cs.mysql.each { key, value ->
    if ( value.contains('$') ) {
        vars[key] = value
    } else {
        resolved[key] = value
    }
}
def engine = new groovy.text.GStringTemplateEngine()
vars.each { key, value ->
    def template = engine.createTemplate(value).make(resolved)
    resolved[key] = template.toString()
}
println 'resolved = ' + resolved


Which seems a bit complicated; I'm sure there's a simpler way that, as a
newbie, I'm missing.

Any suggestions for improvement would be welcome.

Thanks,

    Ian.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email