scala.xml.NodeSeq Scala Examples

The following examples show how to use scala.xml.NodeSeq. 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: Xmlrpc.scala    From xmlrpc   with MIT License 5 votes vote down vote up
package xmlrpc

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding._
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.{FromResponseUnmarshaller, Unmarshal}
import akka.stream.Materializer
import akka.util.Timeout
import xmlrpc.protocol._

import scala.concurrent.{ExecutionContext, Future}
import scala.xml.NodeSeq


object Xmlrpc {

  import XmlrpcProtocol._

  case class XmlrpcServer(fullAddress: String) {
    def uri: Uri = Uri(fullAddress)
  }

  def invokeMethod[P: Datatype, R: Datatype](name: String, parameter: P = Void)
                                            (implicit xmlrpcServer: XmlrpcServer,
                                             as: ActorSystem,
                                             ma: Materializer,
                                             ec: ExecutionContext,
                                             fc: Timeout): XmlrpcResponse[R] = {

    import XmlrpcResponse.AkkaHttpToXmlrpcResponse

    def unmarshall[A](f: Future[HttpResponse])(implicit um: FromResponseUnmarshaller[A]): Future[A] =
      f.flatMap(Unmarshal(_).to[A])


    val request: NodeSeq = writeXmlRequest(name, parameter)
    val requestWithHeader: String = """<?xml version="1.0"?>""" + request.toString


    try {
      (Http().singleRequest(Post(xmlrpcServer.uri, request)) ~> unmarshall[NodeSeq]).asXmlrpcResponse[R]
    } catch {
      case t: Throwable => XmlrpcResponse(ConnectionError("An exception has been thrown by Spray", Some(t)).failures)
    }
  }
} 
Example 2
Source File: Poets.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
package edu.neu.coe.csye._7200.poets

import scala.xml.{Node, NodeSeq, XML}

case class Name(name: String, language: String) { 
  def toXML = <name language={language}>{name}</name>
}

case class Poet(names: Seq[Name]) {
  def toXML = <poet>{names map (_.toXML)}</poet>  
}

object Poet {
  def fromXML(node: Node) = Poet(Name.fromXML(node \ "name")) 
}

object Name {
  def getLanguage(x: Option[Seq[Node]]) = x match {case Some(Seq(y)) => y.text; case _ => ""} 
  def fromXML(nodes: NodeSeq): Seq[Name] = for {
    node <- nodes
  } yield Name(node.text,getLanguage(node.attribute("language")))
}




object Poets extends App {
  import spray.json._
  type PoetSeq = Seq[Poet]
  def toXML(poets: PoetSeq) = poets map {_ toXML}
  val xml = XML.loadFile("poets.xml")
  val poets: PoetSeq = for ( poet <- xml \\ "poet" ) yield Poet.fromXML(poet)

  case class Poets(poets: PoetSeq)

  object PoetsJsonProtocol extends DefaultJsonProtocol {
      implicit val nameFormat = jsonFormat2(Name.apply)
      implicit val poetFormat = ??? // TODO 5 points
      implicit val poetsFormat = jsonFormat1(Poets)
  }

  ??? // TODO 25 points. Write poets out as Json. Show the Json in the console...
      // ...Read the Json file back as poets1 and write that out as XML. Show it on console.
      // Show the comparison of the XML file you produced with the poets.xml file (as part of your submission).
} 
Example 3
Source File: EtlGraphParser.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package mass.rdp.etl.graph

import com.typesafe.scalalogging.StrictLogging
import helloscala.common.util.StringUtils
import mass.core.XmlUtils

import scala.util.Try
import scala.xml.NodeSeq

trait EtlGraphParser {
  def parse(): Try[EtlGraphSetting]

  def validation(setting: EtlGraphSetting): Try[EtlGraphSetting] = Try {
    val sourceOut = setting.source.out
    val sinkName = setting.sink.name
    if (!(setting.flows.exists(_.name == sourceOut) || sinkName == sourceOut)) {
      throw new EtlGraphException("source.out未找到指定的flow或sink")
    }

    if (!(setting.flows.exists(_.outs.exists(_ == sinkName)) || sourceOut == sinkName)) {
      throw new EtlGraphException("graph不是闭合的")
    }

    // TODO 其它 graph 校验

    setting
  }
}

trait EtlGraphParserFactory {
  def `type`: String
}

class EtlGraphXmlParserFactory extends EtlGraphParserFactory {
  override def `type`: String = "xml"

  def build(elem: NodeSeq): EtlGraphParser = new EtlGraphXmlParser(elem)

  class EtlGraphXmlParser(elem: NodeSeq) extends EtlGraphParser with StrictLogging {
    import mass.core.XmlUtils.XmlRich

    logger.trace(s"parse elem:\n$elem")

    def parse(): Try[EtlGraphSetting] = {
      val name = elem.attr("name")
      require(StringUtils.isNoneBlank(name), s"graph需要设置id属性:$elem")

      val source = parseSource(elem \ "source")
      val flows = (elem \ "flows" \ "flow").map(parseFlow).toVector
      val sink = parseSink(elem \ "sink")

      validation(EtlGraphSetting(name, source, flows, sink))
    }

    private def parseSource(node: NodeSeq): EtlSource = {
      val name = node.attr("name")
      val connector = parseConnector(node \ "connector")
      val script = parseScript(node \ "script")
      val out = XmlUtils.text(node \ "out")
      EtlSource(name, connector, script, out)
    }

    private def parseFlow(node: NodeSeq): EtlFlow = {
      val name = node.attr("name")
      val script = parseScript(node \ "script")
      val outs = (node \ "out").map(XmlUtils.text).toVector
      EtlFlow(name, script, outs)
    }

    private def parseSink(node: NodeSeq): EtlSink = {
      val name = node.attr("name")
      val connector = parseConnector(node \ "connector")
      val script = parseScript(node \ "script")
      EtlSink(name, connector, script)
    }

    @inline private def parseScript(node: NodeSeq): EtlScript = {
      logger.trace(s"parse script:\n$node")
      EtlScript(EtlScriptType.withName(node.attr("type")), node.getAttr("src"), node.getText)
    }

    @inline private def parseConnector(node: NodeSeq): EtlConnector =
      EtlConnector(node.attr("ref"))
  }
} 
Example 4
Source File: package.scala    From play-json-xml   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.json

import scala.xml.NodeSeq

package object implicits {

  implicit class RichPlayJson(val xml: NodeSeq) {
    def toJson: JsValue = {
      Xml.toJson(xml)
    }
  }

  implicit class RichXml(val json: JsValue) {
    def toXml: NodeSeq = {
      Xml.toXml(json)
    }
  }

} 
Example 5
Source File: ScalaWebTestBuild.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
import sbt._

import scala.xml.{Elem, NodeSeq}

object ScalaWebTestBuild {
  def bomDependencies(versions: Map[String, String])(scalaVersion: String): Elem = {
    val scalaMajorVersion = scalaVersion.substring(0, "2.XX".length)
    val dependencies = <dependencyManagement>
      <dependencyManagementDependencies>
        <dependency>
          <groupId>org.scalawebtest</groupId>
          <artifactId>scalawebtest-core_{scalaMajorVersion}</artifactId>
          <version>{versions("scalaWebTest")}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.scalawebtest</groupId>
          <artifactId>scalawebtest-json_{scalaMajorVersion}</artifactId>
          <version>{versions("scalaWebTest")}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.scalawebtest</groupId>
          <artifactId>scalawebtest-aem_{scalaMajorVersion}</artifactId>
          <version>{versions("scalaWebTest")}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.scalatest</groupId>
          <artifactId>scalatest_{scalaMajorVersion}</artifactId>
          <version>{versions("scalaTest")}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>{versions("selenium")}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>htmlunit-driver</artifactId>
          <version>{versions("htmlUnit")}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.typesafe.play</groupId>
          <artifactId>play-json</artifactId>
          <version>{versions("playJson")}</version>
          <scope>test</scope>
        </dependency>
      </dependencyManagementDependencies>
    </dependencyManagement>
    dependencies
  }

  def scalaWebTestPomExtra: NodeSeq = {
      <url>http://www.scalawebtest.org</url>
      <licenses>
        <license>
          <name>The Apache Software License, Version 2.0</name>
          <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
      </licenses>
      <scm>
        <url>[email protected]/unic/ScalaWebTest.git</url>
        <connection>scm:git:[email protected]/unic/ScalaWebTest.git</connection>
      </scm>
      <developers>
        <developer>
          <id>DaniRey</id>
          <name>Daniel Rey</name>
          <email>[email protected]</email>
          <organization>Unic AG</organization>
          <organizationUrl>http://www.unic.com</organizationUrl>
        </developer>
        <developer>
          <id>thedodobird2</id>
          <name>Hudson Muff</name>
          <email>[email protected]</email>
          <organization>Unic AG</organization>
          <organizationUrl>http://www.unic.com</organizationUrl>
        </developer>
      </developers>
  }
} 
Example 6
Source File: Config.scala    From opencv-darts   with GNU General Public License v3.0 5 votes vote down vote up
package darts

import org.joda.time.format.DateTimeFormat

import scala.xml.{NodeSeq, XML}
import java.io.File

import org.bytedeco.javacpp.opencv_core.{Mat, Point, Scalar}


class Config(val id: String) {
  def int(path: String):Int = Config.int(id, path)
  def bool(path: String):Boolean = Config.bool(id, path)
  def str(path: String):String = Config.str(id, path)

}

object Config {
  val Cyan    = new Scalar(255, 255, 0, 0)
  val Blue    = new Scalar(255, 100, 0, 0)
  val Purple    = new Scalar(255, 0, 255, 0)
  val Yellow  = new Scalar(0, 255, 255, 0)
  val Red = new Scalar(0, 0, 255, 0)
  val Green = new Scalar(0, 255, 0, 0)
  val Black = new Scalar(0, 0, 0, 0)
  val BlackMat = new Mat(Black)
  val WhiteMat = new Mat(new Scalar(255, 255, 255, 0))

  val conversion = 1f
  val nums = List(6, 13, 4, 18, 1, 20, 5, 12, 9, 14, 11, 8, 16, 7, 19, 3, 17, 2, 15, 10).map(_/conversion)
  val distancesFromBull = Array(14, 28, 174, 192, 284, 300).map(_/conversion)
  val bull = new Point((400/conversion).toInt, (400/conversion).toInt)


  val timeFormatter = DateTimeFormat.forPattern("Y-MMM-d_H-mm_ss-SS");

  val file = new File("config.xml")
  if (!file.isFile) {
    println("config.xml not found. To start with, copy config-sample.xml from the project.")
    System.exit(2)
  }
  var lastModified = file.lastModified()
  var xml = XML.loadFile("config.xml")
  var x: NodeSeq = xml \\ "DartsConfig"
  def camRoot(id: String): NodeSeq = {
    if (file.lastModified() != lastModified) {
      try {
        xml = XML.loadFile("config.xml")
      }catch {
        case e: Exception => {
          Thread.sleep(20)
          xml = XML.loadFile("config.xml")
        }
      }
      x = xml \\ "DartsConfig"
      lastModified = file.lastModified
      println("config reloaded")
    }
    (x \\ "camera").filter(n => (n \ "@camId").text == id)
  }

  def int(path: String):Int = (path.split("/").foldLeft(x)((root, key) => root \\ key)).text.toInt
  def bool(path: String):Boolean = (path.split("/").foldLeft(x)((root, key) => root \\ key)).text.toInt == 1
  def str(path: String):String = (path.split("/").foldLeft(x)((root, key) => root \\ key)).text
  def float(path: String):Float = (path.split("/").foldLeft(x)((root, key) => root \\ key)).text.toFloat

  def int(id: String, path: String):Int = {
    val cr = camRoot(id)
    val x = (path.split("/").foldLeft(cr)((root, key) => root \\ key))
    x.text.toInt
  }
  def bool(id: String, path: String):Boolean = (path.split("/").foldLeft(camRoot(id))((root, key) => root \\ key)).text.toInt == 1
  def str(id: String, path: String):String = (path.split("/").foldLeft(camRoot(id))((root, key) => root \\ key)).text

  def getConfig(id: String):Config = new Config(id)
} 
Example 7
Source File: OrderCalculatorXMLServer.scala    From Akka-Cookbook   with MIT License 5 votes vote down vote up
package com.packt.chapter9

import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.HttpApp
import akka.http.scaladsl.settings.ServerSettings
import com.typesafe.config.ConfigFactory

import scala.util.Random._
import scala.xml.NodeSeq

object OrderCalculatorXMLServer extends HttpApp with OrderXmlSupport {

  val route =
    path("calculateGrandTotal" ~ Slash.?) {
      post {
        entity(as[NodeSeq]) { xmlOrder =>
          complete {
            calculateGrandTotal(xmlOrder)
          }
        }
      }
    } ~
      path("randomOrder") {
        get {
          complete {
            generateRandomOrder()
          }
        }
      }

  private def calculateGrandTotal(o: Order) : NodeSeq = {
    val amount = o.items.map(i => i.percentageDiscount.getOrElse(1.0d) * i.unitPrice * i.quantity).sum + o.deliveryPrice
    GrandTotal(o.id, amount)
  }

  private def generateRandomOrder(): NodeSeq = {
    val items = (0 to nextInt(5)).map(i => {
      Item(i, nextInt(100), 100 * nextDouble(), if (nextBoolean()) Some(nextDouble()) else None)
    }).toList
    Order(nextString(4), System.currentTimeMillis(), items, 100 * nextDouble(), Map("notes" -> "random"))
  }
}

object OrderCalculatorXMLServerApplication extends App {
  OrderCalculatorXMLServer.startServer("0.0.0.0", 8088, ServerSettings(ConfigFactory.load))
} 
Example 8
Source File: OrderServiceTest.scala    From 006877   with MIT License 5 votes vote down vote up
package aia.integration

import scala.concurrent.duration._
import scala.xml.NodeSeq
import akka.actor.Props

import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server._
import akka.http.scaladsl.testkit.ScalatestRouteTest

import org.scalatest.{ Matchers, WordSpec }
 
class OrderServiceTest extends WordSpec 
    with Matchers 
    with OrderService
    with ScalatestRouteTest {

  implicit val executionContext = system.dispatcher
  implicit val requestTimeout = akka.util.Timeout(1 second)
  val processOrders = 
    system.actorOf(Props(new ProcessOrders), "orders")

  "The order service" should {
    "return NotFound if the order cannot be found" in {
      Get("/orders/1") ~> routes ~> check {
        status shouldEqual StatusCodes.NotFound
      }
    }

    "return the tracking order for an order that was posted" in {
      val xmlOrder = 
      <order><customerId>customer1</customerId>
        <productId>Akka in action</productId>
        <number>10</number>
      </order>
      
      Post("/orders", xmlOrder) ~> routes ~> check {
        status shouldEqual StatusCodes.OK
        val xml = responseAs[NodeSeq]
        val id = (xml \\ "id").text.toInt
        val orderStatus = (xml \\ "status").text
        id shouldEqual 1
        orderStatus shouldEqual "received"
      }
      Get("/orders/1") ~> routes ~> check {
        status shouldEqual StatusCodes.OK
        val xml = responseAs[NodeSeq]
        val id = (xml \\ "id").text.toInt
        val orderStatus = (xml \\ "status").text
        id shouldEqual 1
        orderStatus shouldEqual "processing"
      }
    }
  }
} 
Example 9
Source File: OrderServiceApi.scala    From 006877   with MIT License 5 votes vote down vote up
package aia.integration

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

import akka.actor._
import akka.pattern.ask
import akka.util.Timeout

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.model.MediaTypes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._

import scala.xml.{ Elem, XML, NodeSeq }
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._ 

class OrderServiceApi(
  system: ActorSystem, 
  timeout: Timeout, 
  val processOrders: ActorRef
) extends OrderService {
  implicit val requestTimeout = timeout
  implicit def executionContext = system.dispatcher
}

trait OrderService {
  val processOrders: ActorRef

  implicit def executionContext: ExecutionContext

  implicit def requestTimeout: Timeout


  val routes = getOrder ~ postOrders



  def getOrder = get {
    pathPrefix("orders" / IntNumber) { id =>
      onSuccess(processOrders.ask(OrderId(id))) {
        case result: TrackingOrder =>
          complete(<statusResponse>
            <id>{ result.id }</id>
            <status>{ result.status }</status>
          </statusResponse>)
        
        case result: NoSuchOrder => 
          complete(StatusCodes.NotFound)
      }
    }
  }

  

  def postOrders = post {
    path("orders") {
      entity(as[NodeSeq]) { xml =>
        val order = toOrder(xml)
        onSuccess(processOrders.ask(order)) {
          case result: TrackingOrder =>
            complete(
              <confirm>
                <id>{ result.id }</id>
                <status>{ result.status }</status>
              </confirm>
            )
        
          case result =>
            complete(StatusCodes.BadRequest)
        }
      }
    }
  }  



  def toOrder(xml: NodeSeq): Order = {
    val order = xml \\ "order"
    val customer = (order \\ "customerId").text
    val productId = (order \\ "productId").text
    val number = (order \\ "number").text.toInt
    new Order(customer, productId, number)
  }

} 
Example 10
Source File: BunsetsuKerasAnnotatorTest.scala    From jigg   with Apache License 2.0 5 votes vote down vote up
package jigg.pipeline



import java.util.Properties

import org.scalatest.FunSuite
import org.scalatest.Matchers._

import scala.xml.{NodeSeq, Node}

class BunsetsuKerasAnnotatorTest extends FunSuite {

  def findPath(localPath: String): String = getClass.getClassLoader.getResource(localPath).getPath

  def segment(node: Node, properties: Properties): NodeSeq = {
    val bunsetsuSplitter = new IPABunsetsuKerasAnnotator("bunsetsuKeras", properties)
    bunsetsuSplitter.mkLocalAnnotator.newSentenceAnnotation(node)
  }

  val properties = new Properties
  properties.setProperty("bunsetsuKeras.model", findPath("./data/keras/bunsetsu_model.h5"))
  properties.setProperty("bunsetsuKeras.table", findPath("data/keras/jpnLookupWords.json"))

  test("do chunking") {

    val chunks = segment(Sentences.xml("oneSentence"),properties) \\ "chunk"

    chunks.length should be (2)
  }

  object Sentences {
    val xml = Map("oneSentence" ->
      <sentence id="s1" characterOffsetBegin="0" characterOffsetEnd="6">
        梅が咲いた。
        <tokens annotators="mecab">
          <token id="s1_tok0" form="梅" offsetBegin="0" offsetEnd="1" pos="名詞" pos1="一般" pos2="*" pos3="*" cType="*" cForm="*" lemma="梅" yomi="ウメ" pron="ウメ"/>
          <token id="s1_tok1" form="が" offsetBegin="1" offsetEnd="2" pos="助詞" pos1="格助詞" pos2="一般" pos3="*" cType="*" cForm="*" lemma="が" yomi="ガ" pron="ガ"/>
          <token id="s1_tok2" form="咲い" offsetBegin="2" offsetEnd="4" pos="動詞" pos1="自立" pos2="*" pos3="*" cType="五段・カ行イ音便" cForm="連用タ接続" lemma="咲く" yomi="サイ" pron="サイ"/>
          <token id="s1_tok3" form="た" offsetBegin="4" offsetEnd="5" pos="助動詞" pos1="*" pos2="*" pos3="*" cType="特殊・タ" cForm="基本形" lemma="た" yomi="タ" pron="タ"/>
          <token id="s1_tok4" form="。" offsetBegin="5" offsetEnd="6" pos="記号" pos1="句点" pos2="*" pos3="*" cType="*" cForm="*" lemma="。" yomi="。" pron="。"/>
        </tokens>
      </sentence>
    )
  }
} 
Example 11
Source File: XmlParserGenerateNewField.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.nsfc

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.SparkSession

import scala.xml.{Elem, NodeSeq, XML}

class XmlParserGenerateNewField extends ConfigurableStop {

  val authorEmail: String = "[email protected]"
  val description: String = "parse xml string from a hive field, and generate a new field from a xml tag"
  val inportList: List[String] = List(Port.AnyPort.toString)
  val outportList: List[String] = List(Port.DefaultPort.toString)

  var decoderField: String = _
  var tagXPath: String = _
  var newField: String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {

    val spark = pec.get[SparkSession]()
    val sqlContext=spark.sqlContext
    val dfOld = in.read()
    dfOld.createOrReplaceTempView("thesis")
    val s1: Array[String] = tagXPath.split("/");
    sqlContext.udf.register("regexPro",(str:String)=>{
      val xml: Elem = XML.loadString(str)
      var type_id: NodeSeq = xml
      for (i <- 2 until s1.length) {
        type_id = type_id \ s1(i)
      }
      type_id.text
    })
    val sqlText:String="select *, regexPro(" + decoderField + ") as " + newField + " from thesis"

    val dfNew=sqlContext.sql(sqlText)
    dfNew.show()
    out.write(dfNew)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  def setProperties(map : Map[String, Any]) = {
    decoderField = MapUtil.get(map,"decoderField").asInstanceOf[String]
    tagXPath = MapUtil.get(map,"tagXPath").asInstanceOf[String]
    newField = MapUtil.get(map,"newField").asInstanceOf[String]

  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val decoderField = new PropertyDescriptor().name("decoderField").displayName("decoder field").description("decoder field").defaultValue("").required(true)
    val tagXPath = new PropertyDescriptor().name("tagXPath").displayName("xml tag XPath").description("the tag you want to parse in xml file, XPath").defaultValue("").required(true)
    val newField = new PropertyDescriptor().name("newField").displayName("new Field name").description("generate a new field from tag").defaultValue("").required(true)
    descriptor = decoderField :: descriptor
    descriptor = tagXPath :: descriptor
    descriptor = newField :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/xml/XmlParser.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.XmlGroup.toString)
  }

} 
Example 12
Source File: XmlrpcResponse.scala    From xmlrpc   with MIT License 5 votes vote down vote up
package xmlrpc

import xmlrpc.protocol.Datatype
import xmlrpc.protocol.Deserializer.{AnyErrors, Deserialized}
import xmlrpc.protocol.XmlrpcProtocol.readXmlResponse

import scala.concurrent.{ExecutionContext, Future}
import scala.language.implicitConversions
import scala.xml.NodeSeq
import scalaz.Scalaz._

case class XmlrpcResponse[R](underlying: Future[Deserialized[R]])(implicit ec: ExecutionContext) {
  import XmlrpcResponse.ToFutureDeserialized

  def map[S](f: R => S): XmlrpcResponse[S] = flatMap(r => XmlrpcResponse.apply(f(r))) 
  
  def flatMap[S](f: R => XmlrpcResponse[S]): XmlrpcResponse[S] = XmlrpcResponse[S] {
    handleErrors flatMap (_ fold (e => e.asFutureFailure, f(_).handleErrors))
  }
  
  def fold[S](failure: AnyErrors => XmlrpcResponse[S], success: R => S): XmlrpcResponse[S] =
    XmlrpcResponse[S] {
      handleErrors flatMap (_ fold (failure(_).handleErrors, r => success(r).asFutureSuccess))
    }

  private lazy val handleErrors: Future[Deserialized[R]] = underlying recover {
    case error: Throwable => ConnectionError("Error when processing the future response", Some(error)).failures
  }
}

object XmlrpcResponse {
  def apply[R](value: R)(implicit ec: ExecutionContext): XmlrpcResponse[R] = XmlrpcResponse(value.asFutureSuccess)

  def apply[R](value: Deserialized[R])(implicit ec: ExecutionContext): XmlrpcResponse[R] = XmlrpcResponse[R] {
    Future.successful(value)
  }

  implicit class AkkaHttpToXmlrpcResponse(underlying: Future[NodeSeq])(implicit ec: ExecutionContext) {
    def asXmlrpcResponse[R: Datatype]: XmlrpcResponse[R] = XmlrpcResponse[R](underlying map readXmlResponse[R])
  }

  implicit class WithRetry[R](f: () => XmlrpcResponse[R])(implicit ec: ExecutionContext) {
    
    def retry[S](runFailure: AnyErrors => S, runSuccess: R => S, times: Int): XmlrpcResponse[S] = {
      def failureLogic(errors: AnyErrors, remaining: Int): XmlrpcResponse[S] =
        if(remaining == 0) XmlrpcResponse(runFailure(errors))
        else retry(runFailure, runSuccess, remaining - 1)

      def run(remaining: Int): XmlrpcResponse[S] = f() fold (failureLogic(_, remaining), runSuccess)

      if(times <= 0) throw new IllegalArgumentException("Retry must be executed at least one time.")
      else run(times)
    }

    def retry(times: Int): XmlrpcResponse[R] = {
      def failureLogic(errors: AnyErrors, remaining: Int): XmlrpcResponse[R] =
        if(remaining == 0) XmlrpcResponse(errors.asFutureFailure)
        else retry(remaining - 1)

      def run(remaining: Int): XmlrpcResponse[R] = f() fold (failureLogic(_, remaining), r => r)

      if(times <= 0) throw new IllegalArgumentException("Retry must be executed at least one time.")
      else run(times)
    }
  }

  implicit class ToFutureDeserialized[T](t: T) {
    def asFutureSuccess = Future.successful(t.success)
    def asFutureFailure = Future.successful(t.failure)
  }
} 
Example 13
Source File: ScalaTypes.scala    From xmlrpc   with MIT License 5 votes vote down vote up
package xmlrpc.protocol

import xmlrpc.protocol.Deserializer.Deserialized

import scala.language.{postfixOps, implicitConversions}
import scala.xml.NodeSeq

import scalaz.Scalaz._

trait ScalaTypes extends Protocol {
  implicit def optionXmlrpc[T: Datatype]: Datatype[Option[T]] = new Datatype[Option[T]] {
    override def serialize(value: Option[T]): NodeSeq = value match {
      case Some(a) => toXmlrpc[T](a)
      case None => NodeSeq.Empty
    }

    override def deserialize(from: NodeSeq): Deserialized[Option[T]] =
      from \\ "value" headOption match {
        case Some(a) => fromXmlrpc[T](a) map (Some(_))
        case None => None.success
      }
  }
} 
Example 14
Source File: ShapelessTypes.scala    From xmlrpc   with MIT License 5 votes vote down vote up
package xmlrpc.protocol

import xmlrpc.protocol.Deserializer._
import shapeless._

import scala.xml.NodeSeq
import scalaz.Scalaz._

trait ShapelessTypes extends Protocol {
  

  implicit def hconsXmlrpc[T, H <: HList](implicit hd: Lazy[Datatype[H]], td: Lazy[Datatype[T]]): Datatype[T :: H] = new Datatype[T :: H] {
    override def serialize(value: T :: H): NodeSeq = value match {
      case t :: h => td.value.serialize(t) ++ hd.value.serialize(h)
    }

    override def deserialize(from: NodeSeq): Deserialized[T :: H] =
      (td.value.deserialize(from.head) |@| hd.value.deserialize(from.tail)) { (t, h) => t :: h }
  }

  implicit object hnilXmlrpc extends Datatype[HNil] {
    override def serialize(value: HNil): NodeSeq = NodeSeq.Empty
    override def deserialize(from: NodeSeq): Deserialized[HNil] = HNil.success
  }
} 
Example 15
Source File: CollectionTypes.scala    From xmlrpc   with MIT License 5 votes vote down vote up
package xmlrpc.protocol

import xmlrpc.protocol.Deserializer.Deserialized

import scala.xml.{NodeSeq, Node}
import scala.language.postfixOps
import scalaz.Scalaz._

trait CollectionTypes extends Protocol {
  import Deserializer.StringToError

  // We only support array of the same type, if an array contains elements with different
  // types, we deserialize it with case classes
  implicit def ArrayXmlrpc[T: Datatype]: Datatype[Seq[T]] = new Datatype[Seq[T]] {
    override def serialize(value: Seq[T]): Node =
      <array><data>{for {elem <- value} yield toXmlrpc(elem)}</data></array>.inValue

    override def deserialize(from: NodeSeq): Deserialized[Seq[T]] =
      from \\ "array" headOption match {
        case Some(<array><data>{array @ _*}</data></array>) =>
          (for { value <- array}
            yield fromXmlrpc[T](value)).toList.sequence[Deserialized, T]

        case _ => "Expected array structure in $from".toError.failures
      }
  }

  implicit def StructXmlrpc[T: Datatype]: Datatype[Map[String, T]] = new Datatype[Map[String, T]] {
    override def serialize(map: Map[String, T]): Node = {
      def inName(name: String): Node = <name>{name}</name>
      def inMember(elems: NodeSeq): NodeSeq = <member>{elems}</member>

      lazy val struct: NodeSeq = (for {
        (key, value) <- map
      } yield inMember(inName(key) ++ toXmlrpc(value))).reduce(_ ++ _)

      <struct>{struct}</struct>.inValue
    }

    override def deserialize(from: NodeSeq): Deserialized[Map[String, T]] =
      from \\ "struct" headOption match {
        case Some(<struct>{members @ _*}</struct>) =>
          (for { member <- members }
            yield fromXmlrpc[T](member \ "value" head) map ((member \ "name" text) -> _))
            .toList
            .sequence[Deserialized, (String, T)]
            .map(_.toMap[String, T])

        case _ => s"Expected struct in:\n$from".toError.failures
      }
  }
} 
Example 16
Source File: BrokerResources.scala    From reactive-activemq   with Apache License 2.0 5 votes vote down vote up
package akka.stream.integration

import java.io.InputStream
import java.net.URL

import akka.stream.integration.BrokerResources.{ QueueStat, TopicStat }
import org.scalatest.BeforeAndAfterEach

import scala.xml.NodeSeq

trait BrokerResources extends BeforeAndAfterEach { _: TestSpec =>

  def enableClearQueus: Boolean

  private def callBroker(path: String): InputStream = {
    val amqHost = system.settings.config.getString("amq.host")
    val url = new URL(s"http://$amqHost:8161" + path)
    val urlConnection = url.openConnection()
    val basicAuth = "Basic " + new String(java.util.Base64.getUrlEncoder.encode("admin:admin".getBytes()))
    urlConnection.addRequestProperty("Authorization", basicAuth)
    urlConnection.getInputStream
  }

  // communicate with the broker //
  private def getQueueXmlFromBroker: NodeSeq = {
    import scala.xml.XML
    XML.load(callBroker("/admin/xml/queues.jsp"))
  }

  def getTopicXmlFromBroker: NodeSeq = {
    import scala.xml.XML
    XML.load(callBroker("/admin/xml/topics.jsp"))
  }

  def getQueueStats: List[QueueStat] = (for {
    e ← getQueueXmlFromBroker \\ "queue"
    stat ← e \ "stats"
  } yield QueueStat(
    (e \ "@name").text,
    (stat \ "@size").text.toInt,
    (stat \ "@consumerCount").text.toInt,
    (stat \ "@enqueueCount").text.toInt,
    (stat \ "@dequeueCount").text.toInt
  )).toList

  def getTopicStats: List[TopicStat] = (for {
    e ← getTopicXmlFromBroker \\ "topic"
    stat ← e \ "stats"
  } yield TopicStat(
    (e \ "@name").text,
    (stat \ "@size").text.toInt,
    (stat \ "@consumerCount").text.toInt,
    (stat \ "@enqueueCount").text.toInt,
    (stat \ "@dequeueCount").text.toInt
  )).toList

  def purgeQueues(): Unit = {
    def purgeQueue(destinationName: String): InputStream = {
      val path = s"/api/jolokia/exec/org.apache.activemq:brokerName=localhost,destinationName=$destinationName,destinationType=Queue,type=Broker/purge"
      callBroker(path)
    }
    getQueueList.foreach(purgeQueue)
  }

  def getQueueList: List[String] = (for {
    e ← getQueueXmlFromBroker \\ "queue"
  } yield (e \ "@name").text).toList

  def getQueueStatFor(topic: String): Option[QueueStat] =
    getQueueStats.find(_.name contains topic)

  def getQueueMessageCount(topic: String): Option[Int] = for {
    stat ← getQueueStatFor(topic)
  } yield stat.enqueueCount - stat.dequeueCount

  override protected def beforeEach(): Unit = {
    if (enableClearQueus)
      purgeQueues()
    super.beforeEach()
  }
}

object BrokerResources {
  case class QueueStat(name: String, size: Int, consumerCount: Int, enqueueCount: Int, dequeueCount: Int)
  case class TopicStat(name: String, size: Int, consumerCount: Int, enqueueCount: Int, dequeueCount: Int)
} 
Example 17
Source File: AwsErrorCodes.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.provider.aws

import akka.http.scaladsl.model.{ StatusCode, StatusCodes }
import com.ing.wbaa.rokku.proxy.data.RequestId

import scala.xml.NodeSeq


object AwsErrorCodes {

  val errors: Map[StatusCode, (String, String)] =
    Map(
      StatusCodes.Forbidden -> (("AccessDenied", "Access Denied")),
      StatusCodes.InternalServerError -> (("InternalServerError", "Internal Server Error")),
      StatusCodes.Unauthorized -> (("Unauthorized", "Unauthorized")),
      StatusCodes.TooManyRequests -> (("TooManyRequests", "Too Many Requests")),
      StatusCodes.ServiceUnavailable -> (("Throttling", "SLOW DOWN")))

  def response(code: StatusCode, resource: String = "")(implicit requestId: RequestId = RequestId("")): NodeSeq = {
    val responseError = errors.getOrElse(code, ("Unexpected Error", "Unexpected Error"))
    <Error>
      <Code>{ responseError._1 }</Code>
      <Message>{ responseError._2 }</Message>
      <Resource>{ resource }</Resource>
      <RequestId>{ requestId.value }</RequestId>
    </Error>
  }
} 
Example 18
Source File: ProxyServiceWithListAllBuckets.scala    From rokku   with Apache License 2.0 5 votes vote down vote up
package com.ing.wbaa.rokku.proxy.api

import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import com.ing.wbaa.rokku.proxy.data.{ Read, RequestId, S3Request, User }

import scala.xml.NodeSeq


trait ProxyServiceWithListAllBuckets extends ProxyService with ScalaXmlSupport {

  protected[this] def listAllBuckets: Seq[String]

  override protected[this] def processAuthorizedRequest(httpRequest: HttpRequest, s3Request: S3Request, userSTS: User)(implicit id: RequestId): Route = {
    s3Request match {
      //only when list buckets is requested we show all buckets
      case S3Request(_, None, None, accessType, _, _, _) if accessType.isInstanceOf[Read] =>
        complete(getListAllMyBucketsXml())
      case _ => super.processAuthorizedRequest(httpRequest, s3Request, userSTS)
    }
  }

  private def getListAllMyBucketsXml(user: String = "npa", createDate: String = "2018-01-01T00:00:00.000Z"): NodeSeq = {
    <ListAllMyBucketsResult>
      <Owner>
        <ID>{ user }</ID>
        <DisplayName>{ user }</DisplayName>
      </Owner>
      <Buckets>
        { for (bucketName <- listAllBuckets) yield <Bucket><Name>{ bucketName }</Name><CreationDate>{ createDate }</CreationDate></Bucket> }
      </Buckets>
    </ListAllMyBucketsResult>
  }
} 
Example 19
Source File: ScalastyleInspectionsGenerator.scala    From sonar-scala   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.mwz.sonar.scala.metadata.scalastyle

import java.io.InputStream
import java.nio.file.Paths

import com.mwz.sonar.scala.metadata.scalastyle._
import com.typesafe.config.{Config, ConfigFactory}
import org.scalastyle.{Level, _}
import sbt.Keys._
import sbt._

import scala.meta._
import scala.xml.{Node, NodeSeq, XML}


  def transform(source: Tree, inspections: Seq[ScalastyleInspection]): Tree = {
    val stringified: Seq[String] = inspections.collect {
      case inspection =>
        // Is there a better way of embedding multi-line text?
        val extraDescription = inspection.extraDescription.map(s => "\"\"\"" + s + "\"\"\"")
        val justification = inspection.justification.map(s => "\"\"\"" + s + "\"\"\"")
        val params = inspection.params.map { p =>
          s"""
             |ScalastyleParam(
             |  name = "${p.name}",
             |  typ = ${p.typ},
             |  label = "${p.label}",
             |  description = \"\"\"${p.description}\"\"\",
             |  default = \"\"\"${p.default}\"\"\"
             |)
           """.stripMargin
        }

        // It doesn't seem to be straightforward to automatically convert a collection
        // into a tree using scalameta, so I'm turning it into a String so it can be parsed,
        // which is easier than constructing the tree manually.
        // Totally doable with shapeless though, but it would be a bit of an overkill in this case.
        s"""
           |ScalastyleInspection(
           |  clazz = "${inspection.clazz}",
           |  id = "${inspection.id}",
           |  label = "${inspection.label}",
           |  description = "${inspection.description}",
           |  extraDescription = $extraDescription,
           |  justification = $justification,
           |  defaultLevel = ${inspection.defaultLevel},
           |  params = ${params.toString.parse[Term].get.syntax}
           |)
         """.stripMargin
    }

    // Transform the template file.
    val term: Term = stringified.toString.parse[Term].get
    source.transform {
      case q"val AllInspections: $tpe = $expr" =>
        q"val AllInspections: $tpe = $term"
    }
  }
} 
Example 20
Source File: XPath.scala    From xtract   with Apache License 2.0 5 votes vote down vote up
package com.lucidchart.open.xtract

import scala.util.matching.Regex
import scala.xml.{Node, NodeSeq}

sealed trait XPathNode extends Function[NodeSeq, NodeSeq]

case class IdxXPathNode(idx: Int) extends XPathNode {
  def apply(xml: NodeSeq): NodeSeq = xml(idx)
  override def toString = s"[$idx]"
}

case class KeyXPathNode(key: String) extends XPathNode {
  def apply(xml: NodeSeq): NodeSeq = xml \ key
  override def toString = s"/$key"
}

case class RecursiveXPathNode(key: String) extends XPathNode {
  def apply(xml: NodeSeq): NodeSeq = xml \\ key
  override def toString = s"//$key"
}

case class RecursiveWildCardXPathNode(regex: Regex) extends XPathNode {
  def apply(xml: NodeSeq): NodeSeq = (xml \\ "_").filter(node => node.label.matches(regex.regex))
  override def toString = s"//?$regex"
}

case class WildCardXPathNode(regex: Regex) extends XPathNode {
  def apply(xml: NodeSeq): NodeSeq = (xml \ "_").filter(node => node.label.matches(regex.regex))
  override def toString = s"/?$regex"
}

case class AttributedXPathNode(attr: String, value: Option[String]) extends XPathNode {
  def apply(xml: NodeSeq): NodeSeq = xml.filter{ node =>
    node.attribute(attr) match {
      case Some(attrValues) => {
        value.fold(true)(_ == attrValues.toString)
      }
      case None => false
    }
  }
  override def toString = {
    value match {
      case Some(v) => s"[@$attr=$v]"
      case None => s"[@$attr]"
    }
  }
}


object XPath extends XPath(Nil) {
} 
Example 21
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 22
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 23
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")
    }
  }
}