Created
September 25, 2009 20:14
-
-
Save chrislewis/193789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Recursive xml transformation util. Provides a simplistic tool for | |
* registering transforms as functions, which will be called recursively | |
* on each node of the tree. | |
*/ | |
import scala.xml._ | |
trait XmlRenderer { | |
val handlers: Map[(Node) => Boolean, (Node) => Node] | |
def render(seq: NodeSeq): NodeSeq = seq.map(n => | |
n match { | |
case e: Elem => processNode(e) match { | |
case Some(handled) => handled | |
case None => Elem(e.prefix, e.label, e.attributes, e.scope, render(e.child):_*) | |
} | |
case _ => n | |
} | |
) | |
def processNode(n: Node): Option[Node] = | |
handlers.filterKeys(_(n)).values.find((v_) => true).map(_(n)) | |
} | |
object Loader extends XmlRenderer { | |
val handlers = Map( | |
((n: Node) => n.label == "span") -> ((n: Node) => | |
Text("TRANSFORMED")) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment