Advertisement
Guest User

Untitled

a guest
Oct 16th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.19 KB | None | 0 0
  1. import java.io.File
  2. import scala.xml.XML
  3. import java.nio.file.Path
  4. import java.nio.charset.Charset
  5. import scala.xml.Node
  6. import scala.xml.Attribute
  7. import scala.xml.Elem
  8. import scala.xml.Text
  9. import scala.xml.PrettyPrinter
  10. import scala.xml.TopScope
  11.  
  12. def matchy[A] = new Matchy[A] //hack for partial type application
  13. class Matchy[A] { def apply[B](f: A => B) = f} //identity, but the parameter type is now specified
  14.  
  15. val pom = scala.xml.XML.loadFile("pom.xml")
  16.  
  17. var newExecution = pom\\"project"\\"build"\\"plugins"\\"plugin(1)"
  18.  
  19. /*
  20.  
  21. val original = {"V1_0"}
  22. val replacaement = {"V1_3"}
  23.  
  24. def updateXML = matchy[Node] {
  25.     case elem @ Elem(_, "plugin", _ , _, child @ _*)  if (elem \ "groupId").text == "org.jvnet.jaxb2.maven2" => addExecution(elem)
  26.     case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map updateXML)
  27.     case other => other
  28. }
  29.  
  30. def addExecution(node: Node): Node = node match {
  31.     case elem @ Elem(_, "executions", _ , _, child @ _*) => elem.asInstanceOf[Elem].copy(child = (child ++ child(0)) ++ xml.XML.loadString( (child(1).toString).replaceAll("V1_0","V1_3").replaceAll("v1_0","v1_3") ))
  32.     case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map addExecution)
  33.     case other => other
  34. }
  35.  
  36. */
  37.  
  38. def updateXML (node: Node): Node = node match {
  39.     case elem @ Elem(_, "plugin", _ , _, child @ _*)  if (elem \ "groupId").text == "org.jvnet.jaxb2.maven2" => addExecution(elem)
  40.     case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map updateXML)
  41.     case other => other
  42. }
  43.  
  44. def addExecution(node: Node): Node = node match {
  45.     case elem @ Elem(_, "executions", _ , _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child ++ child(0) ++ replaceVersions(child(1)) ++ child(child.length - 1) )
  46.     case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map addExecution)
  47.     case other => other
  48. }
  49.  
  50. def replaceVersions = matchy[Node] {
  51.     case elem @ Elem => xml.XML.loadString( (node.toString).replaceAll( original, replacement).replaceAll(original.toLowerCase,replacement.toLowerCase) )
  52.     case other => other
  53. }
  54.  
  55.  
  56. println ( updateXML(pom) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement