Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.52 KB | None | 0 0
  1. package sdlscala
  2.  
  3. import java.io.File
  4. import xml._
  5. import transform.{RuleTransformer, RewriteRule}
  6.  
  7. object App {
  8.  
  9.  implicit def addGoodCopyToAttribute(attr: Attribute) = new {
  10.    def goodcopy(key: String = attr.key, value: Any = attr.value): Attribute =
  11.      Attribute(attr.pre, key, Text(value.toString), attr.next)
  12.  }
  13.  
  14.  implicit def iterableToMetaData(items: Iterable[MetaData]): MetaData = {
  15.    items match {
  16.      case Nil => Null
  17.      case head :: tail => head.copy(next = iterableToMetaData(tail))
  18.    }
  19.  }
  20.  
  21.  def main(args: Array[String]) {
  22.    val from = "Tridion"
  23.    val to = "SDL Tridion"
  24.  
  25.    def replace(s: String) = s.replace(to, from).replace(from, to)
  26.  
  27.    val transformation = new RewriteRule {
  28.      override def transform(n: Node): NodeSeq = n match {
  29.        case elem: Elem => elem.copy(attributes =
  30.          for (attr <- elem.attributes) yield attr match {
  31.            case attr@Attribute("title", _, _) =>
  32. attr.goodcopy(value=attr.value.text+"ddd")
  33.            case other => other
  34.          }
  35.        )
  36.        case Text(text) if text.contains(from) => new Text(replace(text))
  37.        case other => other
  38.      }
  39.    }
  40.  
  41.    val dir = new File(".")
  42.    val files = dir.listFiles.filter(f => f.isFile &&
  43. (f.getName.endsWith(".xml") || f.getName.endsWith(".xls")) &&
  44. !f.getName.endsWith("pom.xml"))
  45.  
  46.    files.foreach(f => {
  47.      try {
  48.        val xml = XML.loadFile(f)
  49.        println(new RuleTransformer(transformation).transform(xml))
  50.      } catch {
  51.        case e: Exception => {}
  52.      }
  53.    })
  54.  }
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement