Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created September 25, 2009 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/193789 to your computer and use it in GitHub Desktop.
Save chrislewis/193789 to your computer and use it in GitHub Desktop.
/*
* 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