Treebuilder1

This is the example code for my blog entry JCR with Scala: Adding Nodes in ASCII Art.

package com.day.scalademo;

import org.apache.jackrabbit.core.TransientRepository
import javax.jcr.{Node, Property, SimpleCredentials}

object Treebuilder1 {
    import Extensions._

    def main(args: Array[String]) {
        val repository = new TransientRepository
        val session = repository.login(new SimpleCredentials("username", "password".toCharArray));

        try {
            val node = session.getRootNode ¦- "test"
            addSubtree(node)
            dump(node)
        }
        finally {
            session.logout
        }
    }

    def addSubtree(n: Node) {
        n ¦- "1" -+ { n: Node =>
                n ¦- "i"
                n ¦- "ii"
                n ¦- "iii"
                n ¦- "iv"
                n ¦- "v"
        }
        n ¦- "2"
        n ¦- "3" -+ { n: Node =>
                n ¦- "i"
                n ¦- "ii" -+ { n: Node =>
                         n ¦- "a"
                         n ¦- "b"
                         n ¦- "c"
                }
                n ¦- "iii"
                n ¦- "iv"
                n ¦- "v"
        }
        n ¦- "4"
        n ¦- "5"
        n ¦- ("6", "nt:file")
    }

    def dump(node: Node) {
        println(node.getPath)

        for (property <- node.properties) {
            if (property.getDefinition.isMultiple)
                for(value <- property.getValues)
                    println(property.getPath + " = " + value)
            else
                println(property.getPath + " = " + property.getString)
        }

        for (child <- node.childNodes)
            dump(child)
    }

}

object Extensions {
    implicit def extendNode(node: Node) = new NodeExtender(node)

    protected class NodeExtender(node: Node) {

        def childNodes = new Iterator&#91;Node&#93; {
            val it = node.getNodes
            def hasNext = it.hasNext
            def next = it.nextNode
        }

        def properties = new Iterator&#91;Property&#93; {
            val it = node.getProperties
            def hasNext = it.hasNext
            def next = it.nextProperty
        }

        def -+&#91;T&#93;(f: Node => T) = f(node)

        def ¦-(relPath: String) = node.addNode(relPath)
        def ¦-(relPath: String, tyqe: String) = node.addNode(relPath, tyqe)
    }

}

One response

23 01 2008
Adding nodes in ASCII art (update) « Michid’s Weblog

[…] nodes in ASCII art (update) 23 01 2008 I just fixed the Treebuilder code from my previous article. It seems that this stupid editor keeps eating random lines from […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: