Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File
- import scala.xml.XML
- import java.nio.file.Path
- import java.nio.charset.Charset
- import scala.xml.Node
- import scala.xml.Attribute
- import scala.xml.Elem
- import scala.xml.Text
- import scala.xml.PrettyPrinter
- import scala.xml.TopScope
- def matchy[A] = new Matchy[A] //hack for partial type application
- class Matchy[A] { def apply[B](f: A => B) = f} //identity, but the parameter type is now specified
- val pom = scala.xml.XML.loadFile("pom.xml")
- var newExecution = pom\\"project"\\"build"\\"plugins"\\"plugin(1)"
- /*
- val original = {"V1_0"}
- val replacaement = {"V1_3"}
- def updateXML = matchy[Node] {
- case elem @ Elem(_, "plugin", _ , _, child @ _*) if (elem \ "groupId").text == "org.jvnet.jaxb2.maven2" => addExecution(elem)
- case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map updateXML)
- case other => other
- }
- def addExecution(node: Node): Node = node match {
- 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") ))
- case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map addExecution)
- case other => other
- }
- */
- def updateXML (node: Node): Node = node match {
- case elem @ Elem(_, "plugin", _ , _, child @ _*) if (elem \ "groupId").text == "org.jvnet.jaxb2.maven2" => addExecution(elem)
- case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map updateXML)
- case other => other
- }
- def addExecution(node: Node): Node = node match {
- case elem @ Elem(_, "executions", _ , _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child ++ child(0) ++ replaceVersions(child(1)) ++ child(child.length - 1) )
- case elem @ Elem(_, _, _, _, child @ _*) => elem.asInstanceOf[Elem].copy(child = child map addExecution)
- case other => other
- }
- def replaceVersions = matchy[Node] {
- case elem @ Elem => xml.XML.loadString( (node.toString).replaceAll( original, replacement).replaceAll(original.toLowerCase,replacement.toLowerCase) )
- case other => other
- }
- println ( updateXML(pom) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement