Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change attribute on Scala XML Element

Tags:

I have an XML file that I would like to map some attributes of in with a script. For example:

<a>   <b attr1 = "100" attr2 = "50"/> </a> 

might have attributes scaled by a factor of two:

<a>   <b attr1 = "200" attr2 = "100"/> </a> 

This page has a suggestion for adding attributes but doesn't detail a way to map a current attribute with a function (this way would make that very hard): http://www.scalaclass.com/book/export/html/1

What I've come up with is to manually create the XML (non-scala) linked-list... something like:

// a typical match case for running thru XML elements: case  Elem(prefix, e, attributes, scope, children @ _*) => {  var newAttribs = attributes  for(attr <- newAttribs)  attr.key match {   case "attr1" => newAttribs = attribs.append(new UnprefixedAttribute("attr1", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))   case "attr2" => newAttribs = attribs.append(new UnprefixedAttribute("attr2", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))   case _ =>  }  Elem(prefix, e, newAttribs, scope, updateSubNode(children) : _*)  // set new attribs and process the child elements } 

Its hideous, wordy, and needlessly re-orders the attributes in the output, which is bad for my current project due to some bad client code. Is there a scala-esque way to do this?

like image 971
Dave Avatar asked Apr 02 '10 22:04

Dave


1 Answers

Ok, best effort, Scala 2.8. We need to reconstruct attributes, which means we have to decompose them correctly. Let's create a function for that:

import scala.xml._  case class GenAttr(pre: Option[String],                     key: String,                     value: Seq[Node],                     next: MetaData) {   def toMetaData = Attribute(pre, key, value, next) }  def decomposeMetaData(m: MetaData): Option[GenAttr] = m match {   case Null => None   case PrefixedAttribute(pre, key, value, next) =>      Some(GenAttr(Some(pre), key, value, next))   case UnprefixedAttribute(key, value, next) =>      Some(GenAttr(None, key, value, next)) } 

Next, let's decompose the chained attributes into a sequence:

def unchainMetaData(m: MetaData): Iterable[GenAttr] =    m flatMap (decomposeMetaData) 

At this point, we can easily manipulate this list:

def doubleValues(l: Iterable[GenAttr]) = l map {   case g @ GenAttr(_, _, Text(v), _) if v matches "\\d+" =>      g.copy(value = Text(v.toInt * 2 toString))   case other => other } 

Now, chain it back again:

def chainMetaData(l: Iterable[GenAttr]): MetaData = l match {   case Nil => Null   case head :: tail => head.copy(next = chainMetaData(tail)).toMetaData } 

Now, we only have to create a function to take care of these things:

def mapMetaData(m: MetaData)(f: GenAttr => GenAttr): MetaData =    chainMetaData(unchainMetaData(m).map(f)) 

So we can use it like this:

import scala.xml.transform._  val attribs = Set("attr1", "attr2") val rr = new RewriteRule {   override def transform(n: Node): Seq[Node] = (n match {     case e: Elem =>       e.copy(attributes = mapMetaData(e.attributes) {         case g @ GenAttr(_, key, Text(v), _) if attribs contains key =>           g.copy(value = Text(v.toInt * 2 toString))         case other => other       })     case other => other   }).toSeq } val rt = new RuleTransformer(rr) 

Which finally let you do the translation you wanted:

rt.transform(<a><b attr1="100" attr2="50"></b></a>) 

All of this could be simplified if:

  • Attribute actually defined prefix, key and value, with an optional prefix
  • Attribute was a sequence, not a chain
  • Attribute had a map, mapKeys, mapValues
  • Elem had a mapAttribute
like image 52
Daniel C. Sobral Avatar answered Oct 12 '22 18:10

Daniel C. Sobral