scala.xml.transform.RuleTransformer Scala Examples

The following examples show how to use scala.xml.transform.RuleTransformer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: Helpers.scala    From xmlrpc   with MIT License 4 votes vote down vote up
package xmlrpc.protocol

import xmlrpc.protocol.Deserializer.{DeserializationError, Deserialized}

import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
import scala.xml.transform.{RewriteRule, RuleTransformer}
import scala.xml.{Elem, Node, NodeSeq}
import scalaz.Scalaz._

trait Helpers {
  implicit class PimpedNode(node: NodeSeq) {
    def inValue = <value>{node}</value>
    def inParam = <param>{node}</param>
  }

  object FromRequestToResponse extends RewriteRule {
    override def transform(n: Node): Seq[Node] = n match {
      case e: Elem if e.label == "methodCall" =>
        e.copy(label="methodResponse",child=e.child.tail.tail)

      case _ => n
    }
  }

  object ToResponse extends RuleTransformer(FromRequestToResponse)

  implicit class RequestTransformer(request: Node) {
    val asResponse: Node = ToResponse(request)
  }

  def makeNumericConversion[T : Datatype : ClassTag](f: String => T, input: String): Deserialized[T] =
    Try(f(input)) match {
      case Success(convertedValue) => convertedValue.success
      case Failure(e) =>
        DeserializationError(s"The value $input couldn't be converted to a ${implicitly[ClassTag[T]].runtimeClass.getSimpleName}", Some(e)).failures
    }
} 
Example 2
Source File: XmlTransformer.scala    From nexus   with Apache License 2.0 4 votes vote down vote up
import sbt._
import sbt.librarymanagement.ModuleFilter

import scala.xml.transform.{RewriteRule, RuleTransformer}
import scala.xml.{Elem, Node, NodeSeq}

object XmlTransformer {

  
  def transformer(blacklist: ModuleFilter): RuleTransformer =
    new RuleTransformer(new RewriteRule {
      override def transform(node: Node): NodeSeq =
        node match {
          case e: Elem if e.label == "dependency" =>
            val organization = e.child.filter(_.label == "groupId").flatMap(_.text).mkString
            val artifact     = e.child.filter(_.label == "artifactId").flatMap(_.text).mkString
            val version      = e.child.filter(_.label == "version").flatMap(_.text).mkString
            if (blacklist(organization % artifact % version)) NodeSeq.Empty else node
          case _                                  => node
        }
    })
} 
Example 3
Source File: Scapegoat.scala    From ReactiveMongo-Streaming   with Apache License 2.0 4 votes vote down vote up
import sbt.Keys._
import sbt._

import com.sksamuel.scapegoat.sbt.ScapegoatSbtPlugin

object Scapegoat {
  import ScapegoatSbtPlugin.autoImport._

  val settings = Seq(
    scapegoatVersion in ThisBuild := "1.3.9",
    scapegoatReports in ThisBuild := Seq("xml"),
    pomPostProcess := transformPomDependencies { dep =>
      if ((dep \ "groupId").text == "com.sksamuel.scapegoat") {
        None
      } else Some(dep)
    },
    libraryDependencies := {
      if (scalaVersion.value startsWith "2.11") {
        libraryDependencies.value.filter {
          _.organization != "com.sksamuel.scapegoat"
        }
      } else {
        libraryDependencies.value
      }
    }
  )

  import scala.xml.{ Elem => XmlElem, Node => XmlNode }
  private def transformPomDependencies(tx: XmlElem => Option[XmlNode]): XmlNode => XmlNode = { node: XmlNode =>
    import scala.xml.{ NodeSeq, XML }
    import scala.xml.transform.{ RewriteRule, RuleTransformer }

    val tr = new RuleTransformer(new RewriteRule {
      override def transform(node: XmlNode): NodeSeq = node match {
        case e: XmlElem if e.label == "dependency" => tx(e) match {
          case Some(n) => n
          case _       => NodeSeq.Empty
        }

        case _ => node
      }
    })

    tr.transform(node).headOption match {
      case Some(transformed) => transformed
      case _                 => sys.error("Fails to transform the POM")
    }
  }
} 
Example 4
Source File: XMLUtils.scala    From infinispan-spark   with Apache License 2.0 4 votes vote down vote up
package org.infinispan.spark.test

import java.io.File

import scala.xml.transform.{RewriteRule, RuleTransformer}
import scala.xml.{Elem, Node, XML}
import scala.language.postfixOps

object XMLUtils {

   private def addChildToNode(element: Node, elementName: String, attributeName: String, attributeValue: String, elementToAdd: Node) = {
      object Rule extends RewriteRule {
         override def transform(n: Node): Seq[Node] = n match {
            case Elem(prefix, en, att, scope, child@_*)
               if en == elementName && att.asAttrMap.exists(t => t._1 == attributeName && t._2 == attributeValue) =>
               Elem(prefix, en, att, scope, child.isEmpty, elementToAdd ++ child: _*)
            case other => other
         }
      }
      object Transform extends RuleTransformer(Rule)
      Transform(element)
   }

   
   def addCacheTemplate(cacheContainer: String, configFile: File): Unit = {
      val xmlFile = XML.loadFile(configFile)
      val exists = ((xmlFile \\ "cache-container").filter(n => n.attributes.asAttrMap.exists {
         case (k, v) => k.equals("name") && v.equals(cacheContainer)
      }) \ "replicated-cache-configuration" \ "@name" text) == "replicated"

      val cacheConfig = <replicated-cache-configuration name="replicated"/>

      if (!exists) {
         val newXML = XMLUtils.addChildToNode(xmlFile, "cache-container", "name", cacheContainer, cacheConfig)
         XML.save(configFile.getAbsolutePath, newXML, "UTF-8")
      }
   }
}