Skip to content

Instantly share code, notes, and snippets.

@soc
Created May 27, 2012 20:06
Show Gist options
  • Select an option

  • Save soc/2815694 to your computer and use it in GitHub Desktop.

Select an option

Save soc/2815694 to your computer and use it in GitHub Desktop.
package example.dynamicxml
import language.dynamics
class DynamicXML(x: scala.xml.NodeSeq, attributeMode: Boolean = false) extends Dynamic {
def selectDynamic(name: String) = {
if (attributeMode) {
println(s"... selecting attribute $name")
new DynamicXML(x \ (s"@$name"))
} else {
println(s"... selecting element $name")
new DynamicXML(x \ name)
}
}
def applyDynamic(name: String)(value: String) = {
assert(!attributeMode)
println(s"... applying $name with value $value")
(x \ name \ (s"@$value")).text
}
def getXml = {
println("... calling getXml")
x
}
def text = {
println("... calling text")
x.text
}
def @@ = {
assert(!attributeMode)
println("... calling @@")
new DynamicXML(x, true)
}
def attribute = {
assert(!attributeMode)
println("... calling attribute")
new DynamicXML(x, true)
}
}
object DynamicXML {
implicit def apply(x: scala.xml.NodeSeq) = new DynamicXML(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment