[groovy-user] Build map & write to file for slurping

344 views
Skip to first unread message

boardtc

unread,
Jun 26, 2010, 8:34:26 PM6/26/10
to us...@groovy.codehaus.org
I need to build a map up at runtime and append it to an existing  .groooy file for future slurping.

I could not find a groovy way to do this so I ended up constructing the map like:

def stringList = [ "1,8,2,6,5", "6,7,4,9,0", "6,3,8,5,2", "5,7,3,2,6"]
def builder = new StringBuilder()
def index = 0
builder.append(/example{/)
stringList.each() { obj ->
  index++     
  builder.append(/e${index}=[example:"${obj}"]/)
}
builder.append(/}/)
     
//println builder.toString()

new File("tester.groovy").append(builder.toString())

Each new map value needs to be on a new line for slurping it seems so when I slurp it in fails when it hits e2. I played with adding \r\n in the strings and also new File(slotsFileName).withWriterAppend but I was not able to get the maps split out over different lines.

Any advice appreciated,

Cheers,

Tom.

Ian Cairns

unread,
Jun 27, 2010, 5:07:00 AM6/27/10
to us...@groovy.codehaus.org

Have you looked at ConfigObject and its withWriter method?

ian.


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

http://xircles.codehaus.org/manage_email


boardtc

unread,
Jun 27, 2010, 5:17:08 PM6/27/10
to us...@groovy.codehaus.org
Thanks for the idea. I think you are suggesting to add my maps to my configObject, that sounds ideal. Looking at the api, t descends from a LinkedHashMap. 

Would one use addEntry(int hash, K key, V value, int bucketIndex) .to add the map? Some example code would be much appreciated. 

Cheers,

Tom.

Ian Cairns

unread,
Jun 27, 2010, 10:36:57 PM6/27/10
to us...@groovy.codehaus.org

Bottom posted, hope it helps,

Ian

boardtc wrote:
> Thanks for the idea. I think you are suggesting to add my maps to my
> configObject, that sounds ideal. Looking at the api, t descends from a
> LinkedHashMap.
>
> Would one use addEntry(int hash, K key, V value, int bucketIndex) .to
> add the map? Some example code would be much appreciated.
>
> Cheers,
>
> Tom.
>
>
> On 27 June 2010 10:07, Ian Cairns <i...@iancairns.org

I'm not sure the Groovy gurus would consider this groovy, it's probably
too Java-ish.

The basis of what I do, ignoring error checking code, is:
----------------------------------------8<-----------------------------------------

class Configuration {
ConfigObject cfg
File file

Configuration(filePath) {
this.file = new File(filePath)
if ( file.exists() ) {
cfg = new ConfigSlurper().parse(file.toURL())
} else {
cfg = new ConfigObject()
}
}

def persist() {
file.withWriter { writer ->
cfg.writeTo(writer)
}
}

def put(String key, value) {
def pathElements = []
key.split('/').each { if (it) {pathElements << it} }
def depth = pathElements.size() - 1
def node = cfg
for (int i = 0; i < depth; i++) {
node = node.getProperty(pathElements[i])
}
node.put(pathElements[depth], value)
}

def get(String key) {
def pathElements = []
key.split('/').each { if (it) {pathElements << it} }
def depth = pathElements.size() - 1
def node = cfg
for (int i = 0; i < depth; i++) {
node = node.getProperty(pathElements[i])
}
return node.get(pathElements[depth])
}
}

def config = new Configuration('/tmp/testConfig')

config.put('/a/b/f1', 'first field')
config.put('/a/b/f2', 'second field')

config.persist()

----------------------------------------8<-----------------------------------------

and then just "cat /tmp/testConfig" and you should see something like:

a {
b {
f1="first field"
f2="second field"
}
}

Note the use of "getProperty(key)" which creates subsidiary ConfigObject
objects when necessary.

How get() and put() are implemented is really dependent on your
application. I've assumed the key is a String to keep things simple.

I actually have a tree of DefaultMutableTreeNode objects based on the
ConfigObject tree, and the latter gets updated when changes are made to
the former, so there's no direct get() / put() as such.

boardtc

unread,
Jun 29, 2010, 9:09:37 AM6/29/10
to us...@groovy.codehaus.org
Ian,

Can't thank you enough! I have that sorted now. Initially I was trying to draw from your example and was trying a top level node = node.getProperty("toplevelnode"). But that will not work. Once I realised you must have a lower level as well, it all worked. I am now fully using your code. I am adding a map calling your code::
node.put("tester/t1", myMap)

Cheers,

Tom.

Ian Cairns

unread,
Jun 29, 2010, 9:23:05 AM6/29/10
to us...@groovy.codehaus.org
boardtc wrote:
> Ian,
>
> Can't thank you enough! I have that sorted now. Initially I was trying
> to draw from your example and was trying a top level�node =
> node.getProperty("toplevelnode"). But that will not work. Once I
> realised you must have a lower level as well, it all worked. I am now
> fully using your code. I am adding a map calling your code::
> node.put("tester/t1", myMap)

Glad to be able to help.

What flavour of Map are you storing? HashMap? LinkedHashMap?

I'm not sure that persisting the ConfigObject to file will work if
there's a POJO Map in there.

Ian

boardtc

unread,
Jun 29, 2010, 11:21:09 AM6/29/10
to us...@groovy.codehaus.org
Ian,

It works fine with ["stuff!":"5,3,6,7,2"].

Cheers,

Tom.
Reply all
Reply to author
Forward
0 new messages