scala.xml.Elem Scala Examples

The following examples show how to use scala.xml.Elem. 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: ResponseSpec.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect

import com.twitter.finagle.http.Status
import com.twitter.io.Buf.ByteArray.Shared.extract
import io.fintrospect.formats.{Argo, JsonLibrary}
import io.fintrospect.parameters.BodySpec

import scala.util.Try
import scala.xml.Elem


class ResponseSpec private[fintrospect](statusAndDescription: (Status, String), val example: Option[String] = None) {
  val status = statusAndDescription._1
  val description = statusAndDescription._2
}

object ResponseSpec {
  def json[T](statusAndDescription: (Status, String), example: T, jsonLib: JsonLibrary[T, _] = Argo): ResponseSpec =
    ResponseSpec(statusAndDescription, example, BodySpec.json(jsonLib))

  def xml(statusAndDescription: (Status, String), example: Elem): ResponseSpec =
    ResponseSpec(statusAndDescription, example, BodySpec.xml())

  def apply(statusAndDescription: (Status, String)): ResponseSpec = new ResponseSpec(statusAndDescription)

  def apply[T](statusAndDescription: (Status, String), example: T, bodySpec: BodySpec[T]): ResponseSpec =
    new ResponseSpec(statusAndDescription, Try(new String(extract(bodySpec.serialize(example)))).toOption)
} 
Example 2
Source File: ResponseParser.scala    From scalando   with MIT License 5 votes vote down vote up
package com.jcranky.flickr

import com.jcranky.flickr.model.Foto
import com.typesafe.config.Config

import scala.xml.{Elem, XML}

sealed trait ResponseParser {
  def parse(xmlStr: String): Either[FlickrError, Seq[Foto]]
}

final class XmlFlickrParser extends ResponseParser {
  import ResponseParser._

  override def parse(xmlStr: String): Either[FlickrError, Seq[Foto]] = {
    val xml = XML.loadString(xmlStr)

    (xml \\ "rsp" \ "@stat").text match {
      case "ok" => Right(processSuccess(xml))
      case `failStat` => Left(processFailure(xml))
      case _ => Left(FlickrUnknownError(unknownFlickrResp))
    }
  }

  def processSuccess(xml: Elem): Seq[Foto] =
    (xml \\ "photo").map { photoXml =>
      Foto(
        (photoXml \ "@id").text,
        (photoXml \ "@owner").text,
        (photoXml \ "@secret").text,
        (photoXml \ "@server").text,
        (photoXml \ "@farm").text.toInt,
        (photoXml \ "@title").text,
        flickrBoolean((photoXml \ "@ispublic").text),
        flickrBoolean((photoXml \ "@isfriend").text),
        flickrBoolean((photoXml \ "@isfamily").text)
      )
    }

  def processFailure(xml: Elem): FlickrError =
    (xml \\ "err").map { errXml =>
      FlickrKnownError(
        (errXml \ "@code").text.toInt,
        (errXml \ "@msg").text
      )
    }.headOption.getOrElse(
      FlickrUnknownError(errNotFound)
    )
}

final class JsonFlickrParser extends ResponseParser {
  
  def parse(xmlStr: String): Either[FlickrError, Seq[Foto]] = ???
}

object ResponseParser {
  val okStat = "ok"
  val failStat = "fail"

  val unknownFlickrResp = "Could not parse Flickr response"
  val errNotFound = "Could not parser Flickr error response"

  def flickrBoolean(rawAttribute: String): Boolean =
    rawAttribute.toInt match {
      case 1 => true
      case _ => false
    }

  def fromConfig(config: Config): ResponseParser = {
    val parser = config.getString("flickr.api.parser")
    parser match {
      case "xml" => new XmlFlickrParser()
      case "json" => new JsonFlickrParser()
      // the config could be wrongly set by the user, so we default here to use the xml parser
      case _ => new XmlFlickrParser()
    }
  }
}

sealed trait FlickrError
final case class FlickrKnownError(code: Int, msg: String) extends FlickrError
final case class FlickrUnknownError(msg: String) extends FlickrError 
Example 3
Source File: EtlWorkflow.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package mass.rdp.etl

import java.nio.file.Path

import mass.connector.Connector
import mass.rdp.RdpSystem
import mass.rdp.etl.graph.{ EtlGraph, EtlGraphException, EtlGraphImpl, EtlGraphXmlParserFactory }
import mass.core.workflow.Workflow

import scala.collection.immutable
import scala.util.{ Failure, Try }
import scala.xml.{ Elem, XML }


case class EtlWorkflow(connectors: immutable.Seq[Connector], graph: EtlGraph, rdpSystem: RdpSystem)
    extends Workflow[EtlResult]
    with AutoCloseable {
  override def close(): Unit =
    connectors.foreach(_.close())

  override def run(): EtlWorkflowExecution =
    graph.run(connectors, rdpSystem)
}

object EtlWorkflow {
  def fromFile(path: Path, rdpSystem: RdpSystem): Try[EtlWorkflow] =
    fromXML(XML.loadFile(path.toFile), rdpSystem)

  def fromString(workflow: String, rdpSystem: RdpSystem): Try[EtlWorkflow] =
    fromXML(XML.loadString(workflow), rdpSystem)

  def fromXML(workflow: Elem, rdpSystem: RdpSystem): Try[EtlWorkflow] = {
    require(workflow.head.label == "workflow", s"workflow必需为根元素。elem: $workflow")

    val connectors = (workflow \ "connectors" \ "connector").flatMap(node => rdpSystem.connectorSystem.fromXML(node))
    rdpSystem.graphParserFactories.get("xml") match {
      case Some(factory) =>
        factory
          .asInstanceOf[EtlGraphXmlParserFactory]
          .build((workflow \ "graph").head)
          .parse()
          .map(setting => new EtlWorkflow(connectors, EtlGraphImpl(setting), rdpSystem))
      case _ =>
        Failure(new EtlGraphException("EtlGraphParserFactory type: xml 不存在"))
    }
  }
} 
Example 4
Source File: StackBootstraping.scala    From Mastering-Spark-for-Data-Science   with MIT License 5 votes vote down vote up
package io.gzet.tagging.stackoverflow


import io.gzet.tagging.classifier.Classifier
import io.gzet.tagging.html.HtmlHandler
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{SparkSession, DataFrame, SQLContext}

import scala.collection.mutable
import scala.xml.{Elem, XML}

object StackBootstraping {

  def parse(spark: SparkSession, posts: RDD[String]): DataFrame = {

    import spark.sqlContext.implicits._
    posts filter { line =>
      line.contains("row Id")
    } map { line =>
      val xml = XML.loadString(line)
      (getBody(xml), getTags(xml))
    } filter { case (body, tags) =>
      body.isDefined && tags.isDefined
    } flatMap  { case (body, tags) =>
      tags.get.map(tag => (body.get, tag))
    } toDF("body", "tag")
  }

  private def getBody(xml: Elem): Option[String] = {
    val bodyAttr = xml.attribute("Body")
    if (bodyAttr.isDefined) {
      val html = bodyAttr.get.head.text
      val htmlHandler = new HtmlHandler()
      val content = htmlHandler.parseHtml(html)
      if (content.isDefined) {
        return content.get.body
      }
    }
    None: Option[String]
  }

  private def getTags(xml: Elem): Option[Array[String]] = {
    val tagsAttr = xml.attribute("Tags")
    if (tagsAttr.isDefined) {
      val tagsText = tagsAttr.get.head.text
      val tags = tagsText
        .replaceAll("<", "")
        .replaceAll(">", ",")
        .split(",")
      return Some(tags)
    }
    None: Option[Array[String]]
  }

  def bootstrapNaiveBayes(df: DataFrame, vectorSize: Option[Int]) = {
    val labeledText = df.rdd map { row =>
      val body = row.getString(0)
      val labels = row.getAs[mutable.WrappedArray[String]](1)
      (body, labels.toArray)
    }
    Classifier.train(labeledText)
  }

} 
Example 5
Source File: PageFormatting.scala    From Neutrino   with Apache License 2.0 5 votes vote down vote up
package com.ebay.neutrino.www.ui

import java.util.Date

import com.typesafe.scalalogging.slf4j.StrictLogging
import nl.grons.metrics.scala.{Counter, Meter}
import spray.http.ContentType
import spray.http.MediaTypes._
import spray.httpx.marshalling.Marshaller

import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import scala.xml.Elem


trait PageFormatting extends StrictLogging {

  import com.twitter.conversions.storage._
  import scala.language.implicitConversions


  val pretty    = true
  val prettyXml = false   // Use XML parsing for prettyprinting
  val prettier  = new scala.xml.PrettyPrinter(160, 4)
  val starttime = new Date()


  // Run the HTML format content through the pretty-printer
  def prettify(content: String): String = {
    if (pretty && prettyXml)
      Try(prettier.format(scala.xml.XML.loadString(content))) match {
        case Success(content) => content
        case Failure(ex) => logger.warn("Unable to pretty-print html", ex); content
      }
    else if (pretty)
      PrettyPrinter.format(content)
    else
      content
  }

  def prettyxml(content: String) = {
    Try(prettier.format(scala.xml.XML.loadString(content))) match {
      case Success(content) => content
      case Failure(ex) => logger.warn("Unable to pretty-print html", ex); content
    }
  }

  // Convert current time to uptime
  def uptime() = pretty((System.currentTimeMillis()-starttime.getTime).millis)

  // Convenience method: pretty print storage size
  def bytes(data: Long): String = data.bytes.toHuman

  // Convenience method: pretty print count size
  def count(data: Long): String =
    data match {
      case count if count < (2<<10) => s"$count"
      case count if count < (2<<18) => "%.1f K".format(count/1000f)
      case count if count < (2<<28) => "%.1f M".format(count/1000000f)
      case count                    => "%.1f G".format(count/1000000000f)
    }

  // Convenience method; pretty print time
  def pretty(duration: FiniteDuration): String = {
    if      (duration.toDays  > 0) duration.toDays+" days"
    else if (duration.toHours > 0) duration.toHours+" hours"
    else if (duration.toMinutes > 0) duration.toMinutes+" minutes"
    else     duration.toSeconds +" seconds"
  }

  // Convenience method; ensure non-null string
  @inline def str(value: String) = if (value == null) "" else value
}


object PageFormatting extends PageFormatting {
  import scalatags.Text.all._

  val SupportedOutput: Seq[ContentType] =
    Seq(`text/xml`, `application/xml`, `text/html`, `application/xhtml+xml`)


  implicit val ScalaTagsMarshaller =
    Marshaller.delegate[Frag, String](SupportedOutput:_*) { frag =>
      "<!DOCTYPE html>\n" + frag.toString
    }

  implicit val ScalaTagsPrettyMarshaller =
    Marshaller.delegate[Frag, String](SupportedOutput:_*) { frag =>
      "<!DOCTYPE html>\n" + prettyxml(frag.toString)
    }

  implicit val XmlMarshaller =
    Marshaller.delegate[Elem, String](SupportedOutput:_*) { elem =>
      prettify(elem.toString)
    }
} 
Example 6
Source File: UIUtilsSuite.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.ui

import scala.xml.Elem

import org.apache.spark.SparkFunSuite

class UIUtilsSuite extends SparkFunSuite {
  import UIUtils._

  test("makeDescription") {
    verify(
      """test <a href="/link"> text </a>""",
      <span class="description-input">test <a href="/link"> text </a></span>,
      "Correctly formatted text with only anchors and relative links should generate HTML"
    )

    verify(
      """test <a href="/link" text </a>""",
      <span class="description-input">{"""test <a href="/link" text </a>"""}</span>,
      "Badly formatted text should make the description be treated as a streaming instead of HTML"
    )

    verify(
      """test <a href="link"> text </a>""",
      <span class="description-input">{"""test <a href="link"> text </a>"""}</span>,
      "Non-relative links should make the description be treated as a string instead of HTML"
    )

    verify(
      """test<a><img></img></a>""",
      <span class="description-input">{"""test<a><img></img></a>"""}</span>,
      "Non-anchor elements should make the description be treated as a string instead of HTML"
    )

    verify(
      """test <a href="/link"> text </a>""",
      <span class="description-input">test <a href="base/link"> text </a></span>,
      baseUrl = "base",
      errorMsg = "Base URL should be prepended to html links"
    )
  }

  test("SPARK-11906: Progress bar should not overflow because of speculative tasks") {
    val generated = makeProgressBar(2, 3, 0, 0, 4).head.child.filter(_.label == "div")
    val expected = Seq(
      <div class="bar bar-completed" style="width: 75.0%"></div>,
      <div class="bar bar-running" style="width: 25.0%"></div>
    )
    assert(generated.sameElements(expected),
      s"\nRunning progress bar should round down\n\nExpected:\n$expected\nGenerated:\n$generated")
  }

  test("decodeURLParameter (SPARK-12708: Sorting task error in Stages Page when yarn mode.)") {
    val encoded1 = "%252F"
    val decoded1 = "/"
    val encoded2 = "%253Cdriver%253E"
    val decoded2 = "<driver>"

    assert(decoded1 === decodeURLParameter(encoded1))
    assert(decoded2 === decodeURLParameter(encoded2))

    // verify that no affect to decoded URL.
    assert(decoded1 === decodeURLParameter(decoded1))
    assert(decoded2 === decodeURLParameter(decoded2))
  }

  private def verify(
      desc: String, expected: Elem, errorMsg: String = "", baseUrl: String = ""): Unit = {
    val generated = makeDescription(desc, baseUrl)
    assert(generated.sameElements(expected),
      s"\n$errorMsg\n\nExpected:\n$expected\nGenerated:\n$generated")
  }
} 
Example 7
Source File: SbtMavenPlugin.scala    From akka-grpc   with Apache License 2.0 5 votes vote down vote up
package akka.grpc

import java.io.File

import sbt.{ CrossVersion, IO, Logger, ModuleID, ModuleInfo, _ }
import sbt.Keys._
import sbt.plugins.JvmPlugin

import scala.util.Try
import scala.xml.{ Elem, PrettyPrinter, XML }


object SbtMavenPlugin extends AutoPlugin {
  override def trigger = noTrigger

  override def requires = JvmPlugin

  object autoImport {
    val mavenGeneratePluginXml = taskKey[Seq[File]]("Generate the maven plugin xml")
  }

  import autoImport._

  override def projectSettings: Seq[Setting[_]] = inConfig(Compile)(unscopedSettings)

  def unscopedSettings =
    Seq(
      sourceDirectory in mavenGeneratePluginXml := sourceDirectory.value / "maven",
      sources in mavenGeneratePluginXml :=
        Seq((sourceDirectory in mavenGeneratePluginXml).value / "plugin.xml").filter(_.exists()),
      target in mavenGeneratePluginXml := target.value / "maven-plugin-xml",
      managedResourceDirectories += (target in mavenGeneratePluginXml).value,
      mavenGeneratePluginXml := {
        val files = (sources in mavenGeneratePluginXml).value
        val outDir = (target in mavenGeneratePluginXml).value / "META-INF" / "maven"
        IO.createDirectory(outDir)

        val pid = projectID.value
        val pi = projectInfo.value
        val deps = allDependencies.value
        val sv = scalaVersion.value
        val sbv = scalaBinaryVersion.value
        val log = streams.value.log

        val configHash = Seq(pid.toString, pi.toString, deps.toString, sv, sbv).hashCode()
        val cacheFile = streams.value.cacheDirectory / "maven.plugin.xml.cache"
        val cachedHash = Some(cacheFile).filter(_.exists()).flatMap { file => Try(IO.read(file).toInt).toOption }
        val configChanged = cachedHash.forall(_ != configHash)

        val outFiles = files.map { file =>
          val outFile = outDir / file.getName

          if (file.lastModified() > outFile.lastModified() || configChanged) {
            log.info(s"Generating $outFile from template")
            val template = XML.loadFile(file)
            val processed = processTemplate(template, pid, pi, deps, CrossVersion(sv, sbv), log)
            IO.write(outFile, new PrettyPrinter(120, 2).format(processed))
          }
          outFile
        }

        IO.write(cacheFile, configHash.toString)

        outFiles
      },
      resourceGenerators += mavenGeneratePluginXml.taskValue)

  def processTemplate(
      xml: Elem,
      moduleID: ModuleID,
      moduleInfo: ModuleInfo,
      dependencies: Seq[ModuleID],
      crossVersion: ModuleID => ModuleID,
      log: Logger) = {
    // Add project meta data
    val withProjectInfo = Seq(
      "name" -> moduleInfo.nameFormal,
      "description" -> moduleInfo.description,
      "groupId" -> moduleID.organization,
      "artifactId" -> moduleID.name,
      "version" -> moduleID.revision).foldRight(xml) {
      case ((label, value), elem) => prependIfAbsent(elem, createElement(label, value))
    }

    withProjectInfo
  }

  private def createElement(label: String, value: String): Elem =
    <elem>{value}</elem>.copy(label = label)

  private def prependIfAbsent(parent: Elem, elem: Elem) =
    if (parent.child.exists(_.label == elem.label)) {
      parent
    } else {
      parent.copy(child = elem +: parent.child)
    }
} 
Example 8
Source File: RestServiceActor.scala    From fraud   with Apache License 2.0 5 votes vote down vote up
package fraud.main

import akka.actor.{ Actor, ActorRef }
import com.datastax.driver.core._
import fraud.main.RandomTransaction._
import spray.http.MediaTypes.{ `application/json`, `text/html` }
import spray.httpx.SprayJsonSupport.{ sprayJsonMarshaller, sprayJsonUnmarshaller }
import spray.json.JsonParser
import spray.routing._
import scala.collection.JavaConversions._
import scala.xml.Elem


trait RestService extends HttpService {
  import TransactionJsonProtocol._
  def communicate(t: Transaction)
  def indexHtml(): Elem
  def cleanHtml(): Elem 
  def fraudHtml(): Elem 

  val route =
    path("") {
      get {
        respondWithMediaType(`text/html`) {
          complete {
            indexHtml()
          }
        }
      }
    } ~ path("transaction") {
      post {
        entity(as[Transaction]) { transaction =>
          complete {
            communicate(transaction)
            transaction
          }
        }
      }
    } ~ path("transactions") {
      get {
        respondWithMediaType(`application/json`) {
          complete { randomTransactions(10) }
        }
      }
    } ~ path("fraud") {
      get {
        respondWithMediaType(`text/html`) {
          complete {
            fraudHtml()
          }
        }
      }
    } ~ path("clean") {
      get {
        respondWithMediaType(`text/html`) {
          complete {
            cleanHtml()
          }
        }
      }
    }
} 
Example 9
Source File: UIUtilsSuite.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.ui

import scala.xml.Elem

import org.apache.spark.SparkFunSuite

class UIUtilsSuite extends SparkFunSuite {
  import UIUtils._

  test("makeDescription") {//标记描述
    verify(
      """test <a href="/link"> text </a>""",
      <span class="description-input">test <a href="/link"> text </a></span>,
      "Correctly formatted text with only anchors and relative links should generate HTML"
    )

    verify(
      """test <a href="/link" text </a>""",
      <span class="description-input">{"""test <a href="/link" text </a>"""}</span>,
      "Badly formatted text should make the description be treated as a streaming instead of HTML"
    )

    verify(
      """test <a href="link"> text </a>""",
      <span class="description-input">{"""test <a href="link"> text </a>"""}</span>,
      "Non-relative links should make the description be treated as a string instead of HTML"
    )

    verify(
      """test<a><img></img></a>""",
      <span class="description-input">{"""test<a><img></img></a>"""}</span>,
      "Non-anchor elements should make the description be treated as a string instead of HTML"
    )

    verify(
      """test <a href="/link"> text </a>""",
      <span class="description-input">test <a href="base/link"> text </a></span>,
      baseUrl = "base",
      errorMsg = "Base URL should be prepended to html links"
    )
  }

  private def verify(
      desc: String, expected: Elem, errorMsg: String = "", baseUrl: String = ""): Unit = {
    val generated = makeDescription(desc, baseUrl)
    assert(generated.sameElements(expected),
      s"\n$errorMsg\n\nExpected:\n$expected\nGenerated:\n$generated")
  }
} 
Example 10
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 11
Source File: ScalaXMLSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.scalaxml

import scala.xml.Elem

import com.typesafe.config.ConfigFactory.parseString
import org.scalatest.EitherValues
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import pureconfig.generic.auto._
import pureconfig.syntax._

class ScalaXMLSuite extends AnyFlatSpec with Matchers with EitherValues {

  case class Config(people: Elem)

  val sampleXML: Elem =
    <people>
      <person firstName="foo" lastName="bar"/>
      <person firstName="blah" lastName="stuff"/>
    </people>

  it should "be able to read a config with XML" in {
    val config = parseString(
      s"""{ people =
         |    \"\"\"$sampleXML\"\"\"
         | }""".stripMargin)
    config.to[Config] shouldEqual Right(Config(sampleXML))
  }

  it should "return an error when reading invalid XML " in {
    val config = parseString("{ people: <people> }")
    config.to[Config] shouldBe 'left
  }
} 
Example 12
Source File: BintrayClientTests.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index.bintray

import akka.stream.scaladsl.Source
import akka.util.ByteString
import ch.epfl.scala.index.data.bintray.BintrayClient
import org.scalatest._
import play.api.libs.json.JsValue
import play.api.libs.ws.{WSCookie, WSResponse}

import scala.xml.Elem

class BintrayClientTests extends FlatSpec with Matchers {
  "BintrayClient" should "calculate pagination properly" in {
    getPagination(startPos = 50, endPos = 59, total = 100) should contain theSameElementsAs List(
      60,
      70,
      80,
      90
    )
    getPagination(startPos = 0, endPos = 49, total = 100) should contain theSameElementsAs List(
      50
    )
    getPagination(startPos = 50, endPos = 99, total = 100) should contain theSameElementsAs Nil
    getPagination(startPos = 0, endPos = 49, total = 50) should contain theSameElementsAs Nil
    getPagination(startPos = 0, endPos = 49, total = 51) should contain theSameElementsAs List(
      50
    )
    getPagination(startPos = 0, endPos = 0, total = 10) should contain theSameElementsAs List(
        1, 2, 3, 4, 5, 6, 7, 8, 9)
  }

  def getPagination(startPos: Int, endPos: Int, total: Int): Seq[Int] = {
    val wsResponse = wsResponseWithHeaders(
      Map("X-RangeLimit-Total" -> Seq(total.toString),
          "X-RangeLimit-StartPos" -> Seq(startPos.toString),
          "X-RangeLimit-EndPos" -> Seq(endPos.toString))
    )

    BintrayClient.remainingPages(wsResponse)
  }

  private def wsResponseWithHeaders(providedHeaders: Map[String, Seq[String]]) =
    new WSResponse {
      override def status: Int = ???

      override def statusText: String = ???

      override def underlying[T]: T = ???

      override def cookies: Seq[WSCookie] = ???

      override def cookie(name: String): Option[WSCookie] = ???

      override def body: String = ???

      override def bodyAsBytes: ByteString = ???

      override def bodyAsSource: Source[ByteString, _] = ???

      override def allHeaders: Map[String, Seq[String]] = ???

      override def xml: Elem = ???

      override def json: JsValue = ???

      override def headers: Map[String, Seq[String]] = providedHeaders
    }
} 
Example 13
Source File: PrintBench.scala    From xml-lens   with MIT License 5 votes vote down vote up
package pl.msitko.xml.bench

import java.io.StringWriter
import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations._
import pl.msitko.xml.entities.XmlDocument
import pl.msitko.xml.parsing.XmlParser
import pl.msitko.xml.printing.XmlPrinter

import scala.xml.{Elem, XML}

object PrintBenchParams {
  val lensElement: XmlDocument =
    XmlParser.parse(SmallRoundtrip.example.input).right.get

  val stdElement: Elem =
    XML.loadString(SmallRoundtrip.example.input)
}

@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
class PrintBench {
  import PrintBenchParams._

  @Benchmark def printWithLens: String = {
    XmlPrinter.print(lensElement)
  }

  @Benchmark def prettyPrintWithLens: String = {
    XmlPrinter.print(lensElement)
  }

  @Benchmark def prettyPrintWithStd: String = {
    val writer = new StringWriter
    XML.write(writer, stdElement, "UTF-8", true, null)
    writer.toString
  }

} 
Example 14
Source File: SimpleTransformationStd.scala    From xml-lens   with MIT License 5 votes vote down vote up
package pl.msitko.xml.bench

import java.io.StringWriter

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

object SimpleTransformationStd extends SimpleTransformation {
  def transform(el: Elem): Elem = {
    if(el.child.size == 1) {
      val replaceWith = el.child.head match {
        case t: Text =>
          Text(t.text.toUpperCase)
        case a => a
      }
      el.copy(child = List(replaceWith))
    } else {
      el
    }
  }

  override def transform(input: String): String = {
    val xml = XML.loadString(input)

    val transformed = xml.map {
      case el: Elem if el.label == "a" =>
        el.copy(child = el.child.flatMap {
          case el: Elem if el.label == "interesting" =>
            el.copy(child = el.child.flatMap {
              case el: Elem if el.label == "special" =>
                transform(el)
              case a => a
            })
          case a => a
        })
      case a => a
    }

    val writer = new StringWriter

    XML.write(writer, transformed.head, "UTF-8", true, null)
    writer.toString
  }
} 
Example 15
Source File: XmlTest.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.formats

import com.twitter.io.{Buf, Bufs}
import io.fintrospect.parameters.BodySpec

import scala.xml.{Elem, XML}

class XmlAutoTest extends AutoSpec(Xml.Auto) {
  override def toBuf(l: Letter): Buf = Bufs.utf8Buf(transform()(l).toString())

  override def transform(): (Letter => Elem) = (letter: Letter) => <letter>
    <from>{letter.from.address}</from>
    <to>{letter.to.address}</to>
    <message>{letter.message}</message>
  </letter>

  override def fromBuf(s: Buf): Letter = from(XML.loadString(Bufs.asUtf8String(s)))

  private def from(x: Elem) =  Letter(
    StreetAddress((x \ "to").head.text),
    StreetAddress((x \ "from").head.text),
    (x \ "message").head.text
  )

  override def bodySpec: BodySpec[Letter] = BodySpec.xml().map[Letter](from(_))
}

class XmlResponseBuilderTest extends ResponseBuilderSpec(Xml.ResponseBuilder) {
  override val customError = <message>{message}</message>
  override val customErrorSerialized = Bufs.utf8Buf(s"<message>$message</message>")
  override val customType = <okThing>theMessage</okThing>
  override val customTypeSerialized = Bufs.utf8Buf(customType.toString())
} 
Example 16
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 17
Source File: SpaceTokenizerAnnotator.scala    From jigg   with Apache License 2.0 5 votes vote down vote up
package jigg.pipeline


class SpaceTokenizerAnnotator(override val name: String, override val props: Properties)
    extends SentencesAnnotator {

  override def newSentenceAnnotation(sentence: Node): Node = {

    val sindex = sentence \@ "id"
    val text = sentence.text
    val range = (0 until text.size)

    def isSpace(c: Char) = c == ' ' || c == '\t'

    val begins = 0 +: (1 until text.size).filter { i => isSpace(text(i-1)) && !isSpace(text(i)) }

    val ends = begins map {
      range indexWhere (i=>isSpace(text(i)), _) match {
        case -1 => text.size
        case e => e
      }
    }

    val tokenSeq = begins.zip(ends).zipWithIndex map { case ((b, e), i) =>
      <token
        id={ sindex + "_tok" + i }
        form={ text.substring(b, e) }
        characterOffsetBegin={ b+"" }
        characterOffsetEnd={ e+"" }/>
    }
    val tokens = <tokens annotators={ name }>{ tokenSeq }</tokens>
    sentence addChild tokens
  }

  override def requires = Set(Requirement.Ssplit)
  override def requirementsSatisfied = Set(Requirement.Tokenize)
} 
Example 18
Source File: JUnitReportParser.scala    From sonar-scala   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.mwz.sonar.scala
package junit

import java.io.File
import java.nio.file.Path

import scala.jdk.CollectionConverters._
import scala.util.Try
import scala.xml.{Elem, XML}

import com.mwz.sonar.scala.util.Log
import org.sonar.api.batch.fs.{FilePredicate, FileSystem, InputFile}
import org.sonar.api.scanner.ScannerSide

trait JUnitReportParserAPI {

  
  private[junit] def resolveFiles(
    tests: List[Path],
    reports: List[JUnitReport]
  ): Map[InputFile, JUnitReport] =
    reports
      .groupBy(_.name)
      .flatMap {
        case (name, reports) =>
          val path: String = name.replace(".", "/")
          val files: List[Path] = tests.map(_.resolve(s"$path.scala"))
          val predicates: List[FilePredicate] =
            files.map(f => fileSystem.predicates.hasPath(f.toString))

          val inputFiles: Iterable[InputFile] =
            fileSystem
              .inputFiles(
                fileSystem.predicates.or(predicates.asJava)
              )
              .asScala

          if (files.isEmpty)
            log.error(s"The following files were not found: ${files.mkString(", ")}")

          // Collect all of the input files.
          inputFiles.flatMap(file => reports.headOption.map((file, _)))
      }
} 
Example 19
Source File: Dep.scala    From Converter   with GNU General Public License v3.0 5 votes vote down vote up
package org.scalablytyped.converter.internal.scalajs

import io.circe013.{Decoder, Encoder}
import org.scalablytyped.converter.internal.stringUtils.quote

import scala.xml.Elem

sealed trait Dep {
  def org:     String
  def version: String

  def mangledArtifact(versions: Versions): String =
    this match {
      case Dep.Java(_, artifact, _) =>
        artifact
      case Dep.Scala(_, artifact, _) =>
        s"${artifact}_${versions.scala.binVersion}"
      case Dep.ScalaJs(_, artifact, _) =>
        versions.sjs(artifact)
      case Dep.ScalaFullVersion(_, artifact, _) =>
        s"${artifact}_${versions.scala.scalaVersion}"
    }

  def asSbt: String =
    this match {
      case Dep.Java(_, artifact, _) =>
        s"${quote(org)} % ${quote(artifact)} % ${quote(version)}"
      case Dep.Scala(_, artifact, _) =>
        s"${quote(org)} %% ${quote(artifact)} % ${quote(version)}"
      case Dep.ScalaJs(_, artifact, _) =>
        s"${quote(org)} %%% ${quote(artifact)} % ${quote(version)}"
      case Dep.ScalaFullVersion(_, artifact, _) =>
        s"${quote(org)} % ${quote(artifact)} % ${quote(version)} cross CrossVersion.Full()"
    }

  def asMangledSbt(versions: Versions): String =
    s"${quote(org)} % ${quote(mangledArtifact(versions))} % ${quote(version)}"

  def asIvy(versions: Versions, config: String = "compile->default(compile)"): Elem =
    <dependency org={org} name={mangledArtifact(versions)} rev={version} conf={config}/>

  def asMaven(versions: Versions): Elem =
    <dependency>
      <groupId>{org}</groupId>
      <artifactId>{mangledArtifact(versions)}</artifactId>
      <version>{version}</version>
    </dependency>

  def asMavenTest(versions: Versions): Elem =
    <dependency>
      <groupId>{org}</groupId>
      <artifactId>{mangledArtifact(versions)}</artifactId>
      <version>{version}</version>
      <scope>test</scope>
    </dependency>
}

object Dep {
  case class Java(org:             String, artifact: String, version: String) extends Dep
  case class Scala(org:            String, artifact: String, version: String) extends Dep
  case class ScalaFullVersion(org: String, artifact: String, version: String) extends Dep
  case class ScalaJs(org:          String, artifact: String, version: String) extends Dep

  implicit val DepDecoder: Decoder[Dep] = io.circe013.generic.semiauto.deriveDecoder[Dep]
  implicit val DepEncoder: Encoder[Dep] = io.circe013.generic.semiauto.deriveEncoder[Dep]
} 
Example 20
Source File: ChartJson.scala    From temperature-machine   with Apache License 2.0 5 votes vote down vote up
package bad.robot.temperature.rrd

import bad.robot.temperature.rrd.ChartJson._
import io.circe._

import scala.collection.immutable.Seq
import scala.xml.Elem

object ChartJson {

  type Time = String
  type Sensor = String
  type Celsius = String

  def parse(rdd: Elem): List[Series] = {
    val series = (rdd \\ "datasources" \ "name").map(_.text)
    val rows = rdd \\ "data" \ "row"
    val data = rows.map { row =>
      val time = row \ "timestamp"
      val values = row \ "values" \ "v"
      (time.text, values.map(_.text))
    }

    val measurements = data.flatMap { case (time, temperatures) =>
      series.zip(temperatures).map { case (label, temperature) => (time, label, temperature) }
    }

    val bySeries: PartialFunction[(Time, Sensor, Celsius), Sensor] = {
      case (_, sensor, _) => sensor
    }

    val toSeries: PartialFunction[(Sensor, Seq[(Time, Sensor, Celsius)]), Series] = {
      case series => Series(series._1, series._2.map { case (time, _, celsius) => (time, celsius) }.toList)
    }

    measurements
      .groupBy(bySeries)
      .map(toSeries)
      .toList
      .sortBy(_.name)
  }
}

case class Series(name: Sensor, data: List[(Time, Celsius)])

object Series {

  implicit def seriesEncoder: Encoder[Series] = new Encoder[Series] {
    def apply(series: Series): Json = Json.obj(
      ("label", Json.fromString(series.name)),
      ("data" -> data(series.data))
    )
  }

  private def data(measurements: List[(Time, Celsius)]): Json = {
    Json.arr(measurements.map(point): _*)
  }

  private def point(measurement: (Time, Celsius)): Json = {
    Json.obj(
      "x" -> Json.fromLong(Seconds.secondsToDuration(Seconds(measurement._1.toLong)).toMillis),
      "y" -> Json.fromString(measurement._2)
    )
  }

} 
Example 21
Source File: Xml.scala    From temperature-machine   with Apache License 2.0 5 votes vote down vote up
package bad.robot.temperature.rrd

import java.io.{BufferedWriter, FileWriter}

import bad.robot.temperature.{FileOps, Files, JsonOps, encode}
import bad.robot.temperature.rrd.ChartJson._
import org.rrd4j.ConsolFun._
import org.rrd4j.core.RrdDb
import bad.robot.temperature.Files._
import scala.collection.JavaConverters._
import scala.xml.{Elem, XML}

case class Xml(xml: Elem) {
  def exportXml(filename: String) = {
    XML.save(Files.path / filename, xml)
  }

  def exportJson(filename: String) = {
    val writer = new BufferedWriter(new FileWriter(Files.path / filename))
    writer.write(toJson())
    writer.close()
  }

  def toJson(): String = {
    val series = parse(xml)
    encode(series).spaces2ps
  }

}

object Xml {
  def apply(start: Seconds, end: Seconds, hosts: List[Host]): Xml = {
    val database = new RrdDb(RrdFile.file)
    val request = database.createFetchRequest(AVERAGE, start, end)
    val sensors = for {
      host   <- hosts
      sensor <- 1 to RrdFile.MaxSensors
    } yield {
      s"${host.name}-sensor-$sensor"
    }
    request.setFilter(nonEmpty(sensors, database).asJava)
    val data = request.fetchData()
    val xml = data.exportXml()
    new Xml(XML.loadString(xml))
  }

  def nonEmpty(sensors: List[String], database: RrdDb) = sensors.filter(database.hasValuesFor).toSet

} 
Example 22
Source File: OsmRecordReader.scala    From magellan   with Apache License 2.0 5 votes vote down vote up
package magellan.mapreduce

import magellan.io.{OsmKey, OsmShape, OsmNode, OsmWay, OsmRelation}
import org.apache.hadoop.mapreduce.lib.input.FileSplit
import org.apache.hadoop.mapreduce.{InputSplit, RecordReader, TaskAttemptContext}
import scala.xml.{XML, Elem, Node}

private[magellan] class OsmRecordReader
  extends RecordReader[OsmKey, OsmShape] {
 
  val definedNodeLabels = Set("node", "way", "relation")
  var nodes : Seq[Node] = _
  
  var current : Int = 0
  lazy val total = nodes.length
  
  override def initialize(genericSplit: InputSplit, context: TaskAttemptContext) : Unit = {
    val split: FileSplit = genericSplit.asInstanceOf[FileSplit]
    val job = MapReduceUtils.getConfigurationFromContext(context)
    
    val file = split.getPath()
    val fs = file.getFileSystem(job)
    val fileIn = fs.open(file)
    
    val doc = XML.load(fileIn)
    fileIn.close()
    nodes = doc.child.filter(n => definedNodeLabels contains n.label)
  }
  
  override def nextKeyValue() : Boolean = {
    if (!nodes.isEmpty) { 
      if (current != 0) nodes = nodes.tail
      current += 1
    }
    !nodes.isEmpty
  }
  
  override def getCurrentKey() : OsmKey = {
    val current = nodes.head
    new OsmKey(current.label, (current \ "@id").text)
  }
  
  def getTags(shape: Node) = {
    (shape \ "tag").map(t => (t \ "@k").text -> (t \ "@v").text).toMap
  }
  
  def getOsmNode(shape: Node) = {
    new OsmNode(
        (shape \ "@id").text,
        (shape \ "@lat").text.toDouble,
        (shape \ "@lon").text.toDouble,
        getTags(shape))
  }
  
  def getOsmWay(shape: Node) = {
    new OsmWay((shape \ "@id").text, (shape \ "nd").map(w => (w \ "@ref").text), getTags(shape))
  }
  
  def getOsmRelation(shape: Node) = {
    new OsmRelation(
        (shape \ "@id").text,
        (shape \ "member").map(r => (r \ "@ref").text), getTags(shape)
    )
  }
  
  override def getCurrentValue() : OsmShape = {
    val current = nodes.head
    current.label match {
      case "node" => getOsmNode(current)
      case "way" => getOsmWay(current)
      case "relation" => getOsmRelation(current)
    }
  }
  
  override def getProgress() : Float = {
    current.toFloat / total
  }
  
  override def close() : Unit = { }
} 
Example 23
Source File: RegexSentenceAnnotator.scala    From jigg   with Apache License 2.0 5 votes vote down vote up
package jigg.pipeline



import java.util.Properties

import scala.io.Source
import scala.xml.{Node, Elem, Text, Atom}
import jigg.util.XMLUtil.RichNode

class RegexSentenceAnnotator(override val name: String, override val props: Properties) extends Annotator {

  @Prop(gloss = "Regular expression to segment lines (if omitted, specified method is used)") var pattern = ""
  @Prop(gloss = "Use predefined segment pattern newLine|point|pointAndNewLine") var method = "pointAndNewLine"
  readProps()

  val splitRegex = pattern match {
    case "" =>
      method match {
        case "newLine" => RegexSentenceAnnotator.newLine
        case "point" => RegexSentenceAnnotator.point
        case "pointAndNewLine" => RegexSentenceAnnotator.pointAndNewLine
        case other => argumentError("method")
      }
    case pattern =>
      pattern.r
  }

  private[this] val sentenceIDGen = jigg.util.IDGenerator("s")

  override def annotate(annotation: Node): Node = {

    annotation.replaceAll("document") { e =>
      val line = e.text
      val sentenceBoundaries = 0 +: splitRegex.findAllMatchIn(line).map(_.end).toVector :+ line.length
      val sentences: Vector[Node] =
        sentenceBoundaries.sliding(2).toVector flatMap { case Seq(begin_, end_) =>

          def isSpace(c: Char) = c == ' ' || c == '\t' || c == '\n'

          val snippet = line.substring(begin_, end_)
          val begin = snippet.indexWhere(!isSpace(_)) match {
            case -1 => begin_ // space only
            case offset => begin_ + offset
          }
          val end = snippet.lastIndexWhere(!isSpace(_)) match {
            case -1 => begin_
            case offset => begin_ + offset + 1
          }

          // val sentence: String = line.substring(begin, end).trim()
          val sentence: String = line.substring(begin, end)
          if (sentence.isEmpty)
            None
          else {
            Option(<sentence
              id={ sentenceIDGen.next }
              characterOffsetBegin={ begin+"" }
              characterOffsetEnd={ end+"" }>{ sentence }</sentence>)
          }
        }
      // val textRemoved = XMLUtil.removeText(e)
      // XMLUtil.addChild(textRemoved, <sentences>{ sentences }</sentences>)
      e addChild <sentences>{ sentences }</sentences>
    }
  }

  override def requires = Set()
  override def requirementsSatisfied = Set(Requirement.Ssplit)

}

object RegexSentenceAnnotator extends AnnotatorCompanion[RegexSentenceAnnotator] {
  val newLine = """\n+""".r
  val point = """。+""".r
  val pointAndNewLine = """\n+|。\n*""".r
} 
Example 24
Source File: BodySpec.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package io.fintrospect.parameters

import com.twitter.io.Buf
import com.twitter.io.Buf.ByteArray.Shared.extract
import io.fintrospect.ContentType
import io.fintrospect.ContentTypes.{APPLICATION_JSON, APPLICATION_XML, TEXT_PLAIN}
import io.fintrospect.formats.{Argo, JsonLibrary}

import scala.xml.{Elem, XML}


  def map[O](in: T => O) = BodySpec[O](contentType, paramType, s => in(deserialize(s)))
}

object BodySpec {
  def string(contentType: ContentType = TEXT_PLAIN, validation: StringValidations.Rule = StringValidations.EmptyIsInvalid): BodySpec[String] =
    BodySpec[String](contentType, StringParamType, b => validation(new String(extract(b))))

  def json[T](jsonLib: JsonLibrary[T, _] = Argo): BodySpec[T] =
    BodySpec[T](APPLICATION_JSON, ObjectParamType, b => jsonLib.JsonFormat.parse(new String(extract(b))), t => Buf.Utf8(jsonLib.JsonFormat.compact(t)))

  def xml(): BodySpec[Elem] = string(APPLICATION_XML).map(XML.loadString, _.toString())

  def binary(contentType: ContentType): BodySpec[Buf] = BodySpec(contentType, FileParamType,
    b => {
      require(b.length > 0)
      b
    },
    b => {
      require(b.length > 0)
      b
    }
  )
} 
Example 25
Source File: DocumentAnnotator.scala    From jigg   with Apache License 2.0 5 votes vote down vote up
package jigg.pipeline


trait DocumentAnnotator extends Annotator {
  override def annotate(annotation: Node): Node = {

    annotation.replaceAll("root") { case e: Elem =>
      val newChild = Annotator.makePar(e.child, nThreads).map { c =>
        c match {
          case c if c.label == "document" =>
            try newDocumentAnnotation(c) catch {
              case e: AnnotationError =>
                System.err.println(s"Failed to annotate a document by $name.")
                Annotator.annotateError(c, name, e)
            }
          case c => c
        }
      }.seq
      e.copy(child = newChild)
    }
  }

  def newDocumentAnnotation(sentence: Node): Node
}

trait SeqDocumentAnnotator extends DocumentAnnotator {
  override def nThreads = 1
} 
Example 26
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 27
Source File: XmlSerialization.scala    From ike   with Apache License 2.0 5 votes vote down vote up
package org.allenai.ike.index

import scala.xml.{ Elem, Node, Text }

object XmlSerialization {
  def xml(text: IndexableText): Elem = {
    val children = addSpaces(text.sentences map xml)
    <document>{ children }</document>
  }
  def xml(tokens: Seq[IndexableToken]): Elem = {
    val children = addSpaces(tokens map xml)
    <sentence>{ children }</sentence>
  }
  def xml(token: IndexableToken): Elem =
    <word pos={ token.pos } lemma={ token.lemma } chunk={ token.chunk }>{ token.word }</word>
  def addSpaces(elems: Seq[Elem]): Seq[Node] = {
    val n = elems.size
    val spaces = List.fill(n)(Text(" "))
    for {
      (elem, space) <- elems.zip(spaces)
      node <- List(elem, space)
    } yield node
  }
} 
Example 28
Source File: XMLParseExample.scala    From Hands-On-Data-Analysis-with-Scala   with MIT License 5 votes vote down vote up
package handson.example.xml

import handson.example.common.Person

import scala.xml.{Elem, XML}


  def parsePerson(xml: Elem): Person = {
    val id = xml \@ "id"
    val fname = xml \ "fname"
    val lname = xml \ "lname"
    val age = xml \ "age"
    val ageInYears = if (age.isEmpty) None else Some(age.text.toInt)
    Person(id, fname.text, lname.text, ageInYears)
  }

  def parsePerson(xmlStr: String): Person = {
    val xml = XML.loadString(xmlStr)
    parsePerson(xml)
  }

  def main(args: Array[String]): Unit = {
    val personXml: Elem = <person id="123"><fname>John</fname><lname>Doe</lname><age>21</age></person>
    val id = personXml \@ "id"
    println(s"id=${id}, isEmpty=${id.isEmpty}")
    val fname = personXml \ "fname"
    println(fname)
    val lname = personXml \ "lname"
    println(lname.text)
    val age = personXml \ "age"
    println(age.text)
    val person = Person(id, fname.text, lname.text, Some(age.text.toInt))
    println(person)
    print(parsePerson(personXml))
  }
} 
Example 29
Source File: XMLParseExampleSpec.scala    From Hands-On-Data-Analysis-with-Scala   with MIT License 5 votes vote down vote up
package handson.example.xml

import handson.example.common.Person
import org.scalatest.{FlatSpec, Matchers}

import scala.io.Source
import scala.xml.Elem


class XMLParseExampleSpec extends FlatSpec with Matchers {
  "XMLParseExample's parsePerson"  should "handle a simple XML" in {
    val xml: Elem = <person id="123"><fname>John</fname><lname>Doe</lname><age>21</age></person>
    val person = XMLParseExample.parsePerson(xml)
    assert(person === Person("123", "John", "Doe", Some(21)))
  }
  it should "handle missing age" in {
    val xml: Elem = <person id="123"><fname>John</fname><lname>Doe</lname></person>
    val person = XMLParseExample.parsePerson(xml)
    assert(person === Person("123", "John", "Doe"))
  }
  it should "throw NumberFormatException if age is non-integer" in {
    a [NumberFormatException] should be thrownBy {
      val xml: Elem = <person id="123"><fname>John</fname> <lname>Doe</lname> <age>21x</age></person>
      XMLParseExample.parsePerson(xml)
    }
  }
  it should "throw NumberFormatException if age is negative" in {
    a[IllegalArgumentException] should be thrownBy {
      val xml: Elem = <person id="123">
        <fname>John</fname> <lname>Doe</lname> <age>-21</age>
      </person>
      XMLParseExample.parsePerson(xml)
    }
  }
  it should "handle string input" in {
    val xmlStr = Source.fromURL(getClass.getResource("/xml/person.xml")).mkString
    val person = XMLParseExample.parsePerson(xmlStr)
    assert(person === Person("123", "John", "Doe", Some(21)))
  }
} 
Example 30
Source File: Simple_XML_Example.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package cookbook.core

// fintrospect-core
object Simple_XML_Example extends App {

  import com.twitter.finagle.http.Method.Post
  import com.twitter.finagle.http.path.Root
  import com.twitter.finagle.http.{Request, Response}
  import com.twitter.finagle.{Http, Service}
  import com.twitter.util.Await.ready
  import io.fintrospect.formats.Xml.ResponseBuilder._
  import io.fintrospect.parameters.Body
  import io.fintrospect.{Module, RouteModule, RouteSpec, ServerRoute}

  import scala.xml.Elem

  val document: Body[Elem] = Body.xml()

  val analyse: Service[Request, Response] = Service.mk[Request, Response] {
    req => {
      val postedDoc: Elem = document <-- req
      Ok(
        <document>
          <number-of-root-elements>
            {postedDoc.length}
          </number-of-root-elements>
        </document>
      )
    }
  }

  val route: ServerRoute[Request, Response] = RouteSpec().body(document).at(Post) bindTo analyse

  val module: Module = RouteModule(Root).withRoute(route)

  ready(Http.serve(":9999", module.toService))
}

//curl -v -XPOST http://localhost:9999/ --data '<person name="david" age="100"/>' 
Example 31
Source File: Accepting_Multiple_Body_Types_Example.scala    From fintrospect   with Apache License 2.0 5 votes vote down vote up
package cookbook.core

// fintrospect-core
object Accepting_Multiple_Body_Types_Example extends App {

  import argo.jdom.JsonNode
  import com.twitter.finagle.http.Method.Post
  import com.twitter.finagle.http.path.Root
  import com.twitter.finagle.http.{Request, Response}
  import com.twitter.finagle.{Http, Service}
  import com.twitter.util.Await.ready
  import io.fintrospect.parameters.Body
  import io.fintrospect.util.MultiBodyType
  import io.fintrospect.{Module, RouteModule, RouteSpec, ServerRoute}

  import scala.xml.Elem

  val json: Body[JsonNode] = Body.json()

  val echoJson: Service[Request, Response] = Service.mk[Request, Response] { req =>
    import io.fintrospect.formats.Argo.ResponseBuilder._
    Ok(json <-- req)
  }

  val xml: Body[Elem] = Body.xml()

  val echoXml: Service[Request, Response] = Service.mk[Request, Response] { req =>
    import io.fintrospect.formats.Xml.ResponseBuilder._
    Ok(xml <-- req)
  }

  val route: ServerRoute[Request, Response] = RouteSpec("echo posted content in either JSON or XML")
    .at(Post) bindTo MultiBodyType(json -> echoJson, xml -> echoXml)

  val module: Module = RouteModule(Root).withRoute(route)

  ready(Http.serve(":9999", module.toService))
}

//curl -v -XPOST -H"Content-Type: application/json" http://localhost:9999/ --data '{"name":"David"}'
//curl -v -XPOST -H"Content-Type: application/xml" http://localhost:9999/ --data '<name>David</name>' 
Example 32
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 33
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")
      }
   }
} 
Example 34
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
    }
}