scala.xml.XML Scala Examples
The following examples show how to use scala.xml.XML.
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: Publisher.scala From incubator-s2graph with Apache License 2.0 | 6 votes |
import sbt.Keys._ import sbt._ import scala.util.Try import scala.xml.XML object Publisher { val defaultSettings = Seq( publish := { streams.value.log.error("use publishSigned task instead, to produce code-signed artifacts") }, publishMavenStyle := true, publishTo := { if (isSnapshot.value) { Some("apache" at "https://repository.apache.org/content/repositories/snapshots") } else { Some("apache" at "https://repository.apache.org/content/repositories/releases") } }, credentials ++= { Try(XML.loadFile(new File(System.getProperty("user.home")) / ".m2" / "settings.xml")).toOption.toSeq.flatMap { xml => for (server <- xml \\ "server" if (server \ "id").text == "apache") yield { Credentials("Sonatype Nexus Repository Manager", "repository.apache.org", (server \ "username").text, (server \ "password").text) } } }, pomIncludeRepository := { _ => false }, pomExtra := { <url>https://github.com/apache/incubator-s2graph</url> <licenses> <license> <name>Apache 2</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> </license> </licenses> <scm> <connection>scm:git://git.apache.org/incubator-s2graph.git</connection> <developerConnection>scm:git:https://git-wip-us.apache.org/repos/asf/incubator-s2graph.git</developerConnection> <url>github.com/apache/incubator-s2graph</url> </scm> <developers> <developer> <id>s2graph</id> <name>S2Graph Team</name> <url>http://s2graph.incubator.apache.org/</url> </developer> </developers> <mailingLists> <mailingList> <name>Dev Mailing List</name> <post>[email protected]</post> <subscribe>[email protected]</subscribe> <unsubscribe>[email protected]</unsubscribe> </mailingList> <mailingList> <name>User Mailing List</name> <post>[email protected]</post> <subscribe>[email protected]</subscribe> <unsubscribe>[email protected]</unsubscribe> </mailingList> <mailingList> <name>Commits Mailing List</name> <post>[email protected]</post> <subscribe>[email protected]</subscribe> <unsubscribe>[email protected]</unsubscribe> </mailingList> </mailingLists> } ) }
Example 2
Source File: TestStub.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.workflow.etl import scala.xml.XML object TestStub { lazy val graphConfig = { val s = scala.io.Source .fromInputStream(getClass.getClassLoader.getResourceAsStream("mass/workflow/etl/EtlWorkflowTest.xml")) try { s.getLines().mkString } finally s.close() } lazy val graphXmlConfig = XML.loadString(graphConfig) }
Example 3
Source File: HugeRoundTripBench.scala From xml-lens with MIT License | 5 votes |
package pl.msitko.xml.bench import java.io.{File, StringWriter} import java.nio.file.Paths import java.util.concurrent.TimeUnit import org.openjdk.jmh.annotations._ import pl.msitko.xml.parsing.XmlParser import pl.msitko.xml.printing.XmlPrinter import scala.xml.XML @BenchmarkMode(Array(Mode.AverageTime)) @OutputTimeUnit(TimeUnit.MICROSECONDS) @State(Scope.Benchmark) class HugeRoundTripBench { import HugeRoundTripBench._ @Benchmark def roundTripWithLens = { val parsed = XmlParser.parsePath(path).right.get XmlPrinter.print(parsed) } @Benchmark def roundtripWithStd = { val xml = XML.loadFile(file) val writer = new StringWriter XML.write(writer, xml, "UTF-8", true, null) writer.toString } } object HugeRoundTripBench { // BEWARE: that file is not included in the repo because of its huge size // you can download some big XMLs at https://dumps.wikimedia.org/enwiki/ val path = Paths.get("src", "main", "resources", "enwiki-20180420-pages-articles26.xml") val file = { // BEWARE: that file is not included in the repo because of its huge size // you can download some big XMLs at https://dumps.wikimedia.org/enwiki/ val p = List("src", "main", "resources", "enwiki-20180420-pages-articles26.xml").mkString(File.separator) new File(p) } }
Example 4
Source File: SAXBench.scala From osmesa with Apache License 2.0 | 5 votes |
package osmesa import java.util.concurrent.TimeUnit import org.apache.commons.io.IOUtils import org.openjdk.jmh.annotations._ import vectorpipe.model.{Actions, Change} import java.util.zip.GZIPInputStream import javax.xml.parsers.{SAXParser, SAXParserFactory} import scala.xml.XML // --- // @BenchmarkMode(Array(Mode.AverageTime)) @OutputTimeUnit(TimeUnit.MICROSECONDS) @State(Scope.Thread) class SAXBench { val sequence = 0 @Setup def setup: Unit = { } def gzipInputStream(): GZIPInputStream = { // requires the addition of a gzipped OSC file in bench/src/main/resources val stream = getClass.getResourceAsStream("/942.osc.gz") new GZIPInputStream(stream) } def withScalaXML(): Int = { // requires Change.fromXML (see commit 1b04a1e81f1a88f374a086c98d58677ec537b1bf) val data = XML.loadString(IOUtils.toString(gzipInputStream)) val changes = (data \ "_").flatMap { node => (node \ "_").map(Change.fromXML(_, Actions.fromString(node.label), sequence)) } changes.length } def withSAXParser(): Int = { val factory = SAXParserFactory.newInstance val parser = factory.newSAXParser val handler = new Change.ChangeHandler(sequence) parser.parse(gzipInputStream(), handler) handler.changeSeq.length } @Benchmark def useScala: Double = withScalaXML() @Benchmark def getSAXyGirl: Double = withSAXParser() }
Example 5
Source File: Config.scala From opencv-darts with GNU General Public License v3.0 | 5 votes |
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 6
Source File: SbtMavenPlugin.scala From akka-grpc with Apache License 2.0 | 5 votes |
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 7
Source File: StockPriceDemo.scala From s4ds with Apache License 2.0 | 5 votes |
import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global import scala.io._ import scala.xml.XML import scala.util._ object StockPriceDemo extends App { def urlFor(stockSymbol:String) = ( "http://dev.markitondemand.com/MODApis/Api/v2/Quote?" + s"symbol=${stockSymbol}") def fetchStockPrice(stockSymbol:String):Future[BigDecimal] = { val url = urlFor(stockSymbol) val strResponse = Future { Source.fromURL(url).mkString } val xmlResponse = strResponse.map { s => XML.loadString(s) } val price = xmlResponse.map { r => BigDecimal((r \ "LastPrice").text) } price } println("Enter symbol at prompt.") while (true) { val symbol = readLine("> ") fetchStockPrice(symbol).onComplete { future => println() future match { case Success(price) => println(s"$symbol: USD $price") case Failure(e) => println(s"Error fetching $symbol: $e") } print("> ") } } }
Example 8
Source File: StackBootstraping.scala From Mastering-Spark-for-Data-Science with MIT License | 5 votes |
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 9
Source File: SQLConnectorTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.connector.sql import fusion.testkit.FusionWordSpecLike import org.scalatest.BeforeAndAfterAll import scala.xml.XML class SQLConnectorTest extends FusionWordSpecLike with BeforeAndAfterAll { val postgresConfig = """ <connector id="postgres" type="jdbc"> | <props> | <prop key="poolName" value="postgres"/> | <prop key="maximumPoolSize" value="2"/> | <prop key="username" value="massdata"/> | <prop key="password" value="massdata"/> | <prop key="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/> | <prop key="dataSource.serverName" value="localhost"/> | <prop key="dataSource.portNumber" value="5432"/> | <prop key="dataSource.databaseName" value="massdata"/> | </props> | </connector> |""".stripMargin val mysqlConfig = """ <connector id="mysql" type="jdbc"> | <props> | <prop key="poolName" value="mysql"/> | <prop key="maximumPoolSize" value="2"/> | <prop key="jdbcUrl"> | <value><![CDATA[jdbc:mysql://127.0.0.1:3306/massdata?useSSL=false&characterEncoding=utf8]]></value> | </prop> | <prop key="username" value="massdata"/> | <prop key="password" value="Massdata.2018"/> | </props> | </connector> |""".stripMargin var postgresConnector: SQLConnector = _ var mysqlConnector: SQLConnector = _ override protected def beforeAll(): Unit = { super.beforeAll() val parser = new SQLConnectorParser() postgresConnector = parser.parseFromXML(XML.loadString(postgresConfig)) postgresConnector.setting.parameters.get[Map[String, String]](null).foreach(println) mysqlConnector = parser.parseFromXML(XML.loadString(mysqlConfig)) mysqlConnector.setting.parameters.get[Map[String, String]](null).foreach(println) } override protected def afterAll(): Unit = { if (postgresConnector ne null) { postgresConnector.close() } if (mysqlConnector ne null) { mysqlConnector.close() } super.afterAll() } "SQLConnector" should {} }
Example 10
Source File: PrintBench.scala From xml-lens with MIT License | 5 votes |
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 11
Source File: EtlJob.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.rdp.etl import mass.core.job.{ JobResult, SchedulerContext, SchedulerJob } import mass.rdp.RdpSystem import mass.rdp.etl.graph.EtlGraphException import scala.concurrent.Future import scala.xml.XML class EtlJob extends SchedulerJob { import EtlJob._ override def run(context: SchedulerContext): Future[JobResult] = { val rdpSystem = RdpSystem(context.system) val xmlString = context.data .getOrElse(WORKFLOW_STRING, throw new EtlGraphException(s"流程配置未设置,SchedulerJob.data.key = $WORKFLOW_STRING")) val workflow = EtlWorkflow.fromXML(XML.loadString(xmlString), rdpSystem).get workflow .run() .future .map { result => EtlJobResult(result) }(rdpSystem.classicSystem.dispatcher) } } object EtlJob { val WORKFLOW_STRING = "WORKFLOW_STRING" }
Example 12
Source File: EtlWorkflow.scala From fusion-data with Apache License 2.0 | 5 votes |
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 13
Source File: BrokerCommandsTests.scala From activemq-cli with Apache License 2.0 | 5 votes |
package activemq.cli.command import activemq.cli.ActiveMQCLI import activemq.cli.command.CommandsTests._ import activemq.cli.command.MessageCommandsTests._ import activemq.cli.util.Console._ import java.io.File import org.junit.After import org.junit.AfterClass import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Before import org.junit.BeforeClass import org.junit.Rule import org.junit.rules.TemporaryFolder import org.junit.Test import org.springframework.shell.Bootstrap import org.springframework.shell.core.CommandResult import org.springframework.shell.core.JLineShellComponent import scala.xml.XML class BrokerCommandsTests { @Before def before = { assertTrue(shell.executeCommand("remove-all-queues --force").isSuccess) assertTrue(shell.executeCommand("remove-all-topics --force").isSuccess) } @Test def testExportBrokerFileAlreadyExists = { val testExportBrokerFileAlreadyExists = File.createTempFile("MessageCommandsTests_testExportBrokerFileAlreadyExists", ".xml") try assertEquals(warn(s"File '${testExportBrokerFileAlreadyExists.getCanonicalPath}' already exists"), shell.executeCommand(s"export-broker --file ${testExportBrokerFileAlreadyExists.getAbsolutePath}").getResult) finally testExportBrokerFileAlreadyExists.delete } @Test def testAvailabilityIndicators: Unit = { assertTrue(shell.executeCommand("disconnect").isSuccess) try { List("info", "disconnect", "export-broker").map(command ⇒ { assertCommandFailed(shell.executeCommand(command)) }) } finally { assertTrue(shell.executeCommand("connect --broker test").isSuccess) } } } object BrokerCommandsTests { val shell = createShell @BeforeClass def beforeClass() = startAndConnectToEmbeddedBroker(shell) @AfterClass def afterClass() = stopEmbeddedBroker(shell) }
Example 14
Source File: ForgeInstallSpec.scala From PackUpdate with Apache License 2.0 | 5 votes |
package at.chaosfield.packupdate.json import java.io.{File, FileNotFoundException} import java.net.URI import scala.xml.XML import at.chaosfield.packupdate.common.{FileManager, Log, MavenPath} import org.json4s.jackson.JsonMethods case class ForgeInstallSpec( install: InstallInformation, versionInfo: VersionInformation, spec: Int = 0 ) case class InstallInformation( profileName: String, target: String, path: MavenPath, version: String, filePath: String, welcome: String, minecraft: String, mirrorList: URI, logo: String, modList: String ) case class VersionInformation( id: String, `type`: String, minecraftArguments: String, mainClass: String, inheritsFrom: String, jar: String, libraries: Array[LibraryInformation] ) case class LibraryInformation( name: MavenPath, url: Option[URI], checksums: Array[String], serverreq: Boolean = false, clientreq: Boolean = false ) { def getPom(mavenPath: MavenPath, log: Log): xml.Elem = { var lastException: Option[Exception] = None val tryUrls = url.toList ++ LibraryInformation.RepoList for (url <- tryUrls) { try { val pomUrl = url.resolve(mavenPath.getPom.getFilePath).toURL val data = FileManager.readStreamToString(FileManager.retrieveUrl(pomUrl, log)._1) return XML.loadString(data) } catch { case e: FileNotFoundException => lastException = Some(e) log.debug(s"File not found at $url, trying next...") } } throw lastException.get } } object LibraryInformation { final val RepoList = List("https://repo.maven.apache.org/maven2/", "https://libraries.minecraft.net/").map(new URI(_)) }
Example 15
Source File: XMLParser.scala From scalando with MIT License | 5 votes |
package com.jcranky.scalando.cap11 case class Foto(id: Long, owner: String, server: Int, title: String) sealed trait ResponseParser { def parse(str: String): Seq[Foto] } class XMLParser extends ResponseParser { import scala.xml.XML override def parse(str: String): Seq[Foto] = { val xmlResp = XML.loadString(str) xmlResp \\ "photo" map { p => Foto( (p \ "@id" text).toLong, p \ "@owner" text, (p \ "@server" text).toInt, p \ "@title" text) } } }
Example 16
Source File: ResponseParser.scala From scalando with MIT License | 5 votes |
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 17
Source File: ScalastyleQualityProfile.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.ncredinburgh.sonar.scalastyle import org.slf4j.LoggerFactory import org.sonar.api.profiles.{ProfileDefinition, RulesProfile} import org.sonar.api.rules.{RuleFinder, ActiveRule} import org.sonar.api.utils.ValidationMessages import org.scalastyle.ScalastyleError import scala.xml.XML import collection.JavaConversions._ import org.sonar.api.rules.RuleQuery import org.sonar.api.rules.Rule class ScalastyleQualityProfile(ruleFinder: RuleFinder) extends ProfileDefinition { private val log = LoggerFactory.getLogger(classOf[ScalastyleQualityProfile]) private val defaultConfigRules = xmlFromClassPath("/default_config.xml") \\ "scalastyle" \ "check" override def createProfile(validation: ValidationMessages): RulesProfile = { val profile = RulesProfile.create(Constants.ProfileName, Constants.ScalaKey) val enabledRules = defaultConfigRules filter (x => (x \ "@enabled").text.equals("true")) val defaultRuleClasses = enabledRules map (x => (x \ "@class").text) // currently findAll is buggy (sonar 4.5-5.1 https://jira.sonarsource.com/browse/SONAR-6390) // will still work but won't add all possible rule to the default profile val query = RuleQuery.create().withRepositoryKey(Constants.RepositoryKey) val repoRules = ruleFinder.findAll(query) for {clazz <- defaultRuleClasses} { val ruleOption = repoRules.find(clazzMatch(_, clazz)) ruleOption match { case None => validation.addWarningText(s"Rule for $clazz not found in ${Constants.RepositoryKey} repository! Rule won't be activated.") case Some(rule) => { if (!rule.isTemplate()) { val activated = profile.activateRule(rule, rule.getSeverity) setParameters(activated, clazz) } } } } profile } def setParameters(activeRule: ActiveRule, clazz: String) { // set parameters defaultConfigRules.find(x => (x \ "@class").text.equals(clazz)) match { case Some(rule) => { val params = (rule \ "parameters" \ "parameter").map(n => ((n \ "@name").text, n.text)).toMap params foreach { case (key, value) => activeRule.setParameter(key, value) } } case _ => log.warn("Default rule with key " + activeRule.getRuleKey + " could not found in default_config.xml") } // set synthetic parameter activeRule.setParameter(Constants.ClazzParam, clazz) } private def clazzMatch(rule: Rule, clazz: String): Boolean = { Option(rule.getParam(Constants.ClazzParam)) match { case Some(param) => { param.getDefaultValue.equals(clazz) } case None => { log.warn(s"Could not find required parameter ${Constants.ClazzParam}, rule for $clazz cannot be activated") false } } } private def xmlFromClassPath(s: String) = XML.load(classOf[ScalastyleError].getResourceAsStream(s)) }
Example 18
Source File: Poets.scala From Scalaprof with GNU General Public License v2.0 | 5 votes |
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 19
Source File: History.scala From sbt-flaky with Apache License 2.0 | 5 votes |
package flaky.history import java.io.{File, FileFilter, InputStream} import java.text.SimpleDateFormat import java.util.Date import flaky.{Flaky, FlakyTestReport, Io} import org.apache.commons.vfs2.VFS import scala.xml.XML class History(project: String, historyDir: File, flakyReportDir: File, projectDir: File) { private val zipFileFilter = new FileFilter { override def accept(pathname: File): Boolean = pathname.getName.endsWith(".zip") } private def runFiles(historyDir: File): List[File] = historyDir.listFiles(zipFileFilter).toList def addCurrentToHistory(): Unit = { val timestamp = System.currentTimeMillis() val date = new SimpleDateFormat(History.dateFormat).format(new Date(timestamp)) val gitCommit = Git(projectDir).currentId().toOption val historyReportDescription = HistoryReportDescription(timestamp, gitCommit) HistoryReportDescription.save(historyReportDescription, new File(flakyReportDir, History.descriptorFile)) Zip.compressFolder(new File(historyDir, s"$date.zip"), flakyReportDir) } def removeToOldFromHistory(maxToKeep: Int): Unit = { runFiles(historyDir) .take(Math.max(runFiles(historyDir).size - maxToKeep, 0)) .foreach(_.delete()) } def createHistoryReport(): HistoryReport = { val historicalRuns: List[HistoricalRun] = runFiles(historyDir) .map(History.loadHistory) val date = new SimpleDateFormat("HH:mm dd-MM-YYYY").format(new Date()) HistoryReport(project, date, historicalRuns) } def processHistory(): HistoryReport = { historyDir.mkdirs() addCurrentToHistory() removeToOldFromHistory(20) createHistoryReport() } } case class HistoryReportDescription(timestamp: Long, gitCommitHash: Option[String]) object HistoryReportDescription { def load(in: InputStream): HistoryReportDescription = { val descriptorXml = XML.load(in) val timestamp = (descriptorXml \ "timestamp").text.trim.toLong val gitHash = (descriptorXml \ "gitCommitHash").text.trim HistoryReportDescription(timestamp, Some(gitHash)) } def save(historyReportDescription: HistoryReportDescription, file: File): Unit = { val xml = <HistoryReportDescription> <timestamp> {historyReportDescription.timestamp} </timestamp> <gitCommitHash> {historyReportDescription.gitCommitHash.getOrElse("")} </gitCommitHash> </HistoryReportDescription> val prettyXml = new scala.xml.PrettyPrinter(80, 2).format(xml) Io.writeToFile(file, prettyXml) } } object History { val descriptorFile = "descriptor.xml" val dateFormat = "yyyyMMdd-HHmmss" def loadHistory: (File) => HistoricalRun = { file => { val manager = VFS.getManager val uri = file.toURI.toString.replace("file:/", "zip:/") val fo = manager.resolveFile(uri) val report: FlakyTestReport = Flaky.createReportFromHistory(fo) val descriptorFile = Option(fo.getChild(History.descriptorFile)) val dateFromFileName = file.getName.replace(".zip","") val hrd = descriptorFile .filter(_.exists()) .map(f => HistoryReportDescription.load(f.getContent.getInputStream)) .getOrElse(HistoryReportDescription(new SimpleDateFormat(dateFormat).parse(dateFromFileName).getTime, None)) HistoricalRun(hrd, report) } } }
Example 20
Source File: XmlParserGenerateNewField.scala From piflow with BSD 2-Clause "Simplified" License | 5 votes |
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 21
Source File: ScalastyleInspectionsGenerator.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
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 22
Source File: JUnitReportParser.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
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 23
Source File: ScoverageReportParser.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala package scoverage import java.nio.file.{Path, Paths} import scala.xml.{Node, XML} import cats.syntax.semigroup.catsSyntaxSemigroup import com.mwz.sonar.scala.util.PathUtils import org.sonar.api.scanner.ScannerSide private[scoverage] def extractScoverageFromNode(node: Node): Scoverage = { val branches = (node \\ "statement") .filter(node => !(node \@ "ignored").toBoolean && (node \@ "branch").toBoolean) val coveredBranches = branches.filter(statement => (statement \@ "invocation-count").toInt > 0) Scoverage( statements = (node \@ "statement-count").toInt, coveredStatements = (node \@ "statements-invoked").toInt, statementCoverage = (node \@ "statement-rate").toDouble, branches = branches.size, coveredBranches = coveredBranches.size, branchCoverage = (node \@ "branch-rate").toDouble ) } }
Example 24
Source File: ScapegoatReportParser.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala package scapegoat import java.nio.file.{Path, Paths} import scala.xml.XML import org.sonar.api.scanner.ScannerSide trait ScapegoatReportParserAPI { def parse(scapegoatReportPath: Path): Map[String, Seq[ScapegoatIssue]] } override def parse(scapegoatReportPath: Path): Map[String, Seq[ScapegoatIssue]] = { val scapegoatXMLReport = XML.loadFile(scapegoatReportPath.toFile) val scapegoatIssues = for { issue <- scapegoatXMLReport \\ "warning" line = (issue \@ "line").toInt text = issue \@ "text" file = replaceAllDotsButLastWithSlashes(issue \@ "file") inspectionId = issue \@ "inspection" } yield ScapegoatIssue(line, text, file, inspectionId) scapegoatIssues.groupBy(issue => issue.file) } }
Example 25
Source File: SpotXmlParser.scala From dbpedia-spotlight-model with Apache License 2.0 | 5 votes |
package org.dbpedia.spotlight.spot import org.dbpedia.spotlight.model.{SurfaceForm, SurfaceFormOccurrence, Text} import scala.collection.JavaConversions._ import scala.xml.{Node, XML} def extract(spotsXml: Text): java.util.List[SurfaceFormOccurrence] = { val xml = XML.loadString(spotsXml.text) val text = (xml \\ "annotation" \ "@text").toString val surfaceForms = xml \\"annotation" \ "surfaceForm" val occs = surfaceForms.map(buildOcc(_, new Text(text))) occs.toList } def buildOcc(sf: Node, text: Text) = { val offset = (sf \ "@offset").toString.toInt val name = (sf \ "@name").toString new SurfaceFormOccurrence(new SurfaceForm(name), text, offset) } def getName() = name def setName(n: String) { name = n; } } object SpotXmlParser { def main(args: Array[String]) { val xml = "<annotation text=\"The research, which is published online May 22 in the European Heart Journal, opens up the prospect of treating heart failure patients with their own, human-induced pluripotent stem cells (hiPSCs) to repair their damaged hearts.\">\n<surfaceForm name=\"published\" offset=\"23\"/>\n<surfaceForm name=\"May 22\" offset=\"40\"/>\n<surfaceForm name=\"European\" offset=\"54\"/>\n<surfaceForm name=\"Heart\" offset=\"63\"/>\n<surfaceForm name=\"Journal\" offset=\"69\"/>\n<surfaceForm name=\"prospect\" offset=\"91\"/>\n<surfaceForm name=\"heart failure\" offset=\"112\"/>\n<surfaceForm name=\"patients\" offset=\"126\"/>\n<surfaceForm name=\"human\" offset=\"151\"/>\n<surfaceForm name=\"stem cells\" offset=\"177\"/>\n<surfaceForm name=\"hearts\" offset=\"221\"/>\n</annotation>" val spotter = new SpotXmlParser() spotter.extract(new Text(xml)).foreach(println) } }
Example 26
Source File: File.scala From nescala with GNU General Public License v2.0 | 5 votes |
package com.owlandrews.nescala.helpers import com.owlandrews.nescala.Console object File { import java.io.File import java.net.URL import java.io.{FileFilter, FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream} import javax.imageio.ImageIO import scala.util.Try import scala.xml.XML import scala.language.postfixOps import sys.process._ import com.typesafe.config.ConfigFactory def Download(url: String, filename: String) = (for{ url <- Try(new URL(url)) conn <- Try(url.openConnection().connect()) file <- Try(new File(filename)) } yield Try(url #> file !!)) map {x => new File(filename)} def Writer(filename: String)(op: java.io.PrintWriter => Unit) = { val p = new java.io.PrintWriter(new File(filename)) try op(p) finally p.close() } def Write(filename: String, content: String) = { val res = new java.io.PrintWriter(new File(filename)) res.write(content) res.close() } def Filter = new FileFilter { override def accept(pathname: File): Boolean = pathname.getName.toLowerCase.endsWith(".nes") } def Image(file:Try[File]) = file.map(ImageIO.read) def Image(filename:String) = Try(ImageIO.read(resource(filename))) def Xml(filename:String) = XML.load(resource("/database.xml")) def Config(filename:String) = { val file = new File(filename) file.exists() match { case true => ConfigFactory.parseFile(file) case false => ConfigFactory.empty() } } def SaveState(console:Console) = { val fos = new FileOutputStream(s"$ApplicationFolder/${console.cartridge.CRC}.save") val oos = new ObjectOutputStream(fos) oos.writeObject(console) oos.close() } def LoadState(crc:String):Try[Console] = Try { val fis = new FileInputStream(s"$ApplicationFolder/$crc.save") val ois = new ObjectInputStreamWithCustomClassLoader(fis) val console = ois.readObject.asInstanceOf[Console] ois.close() console } // Taken from: https://gist.github.com/ramn/5566596 private class ObjectInputStreamWithCustomClassLoader(fileInputStream: FileInputStream) extends ObjectInputStream(fileInputStream) { override def resolveClass(desc: java.io.ObjectStreamClass): Class[_] = { try { Class.forName(desc.getName, false, getClass.getClassLoader) } catch { case ex: ClassNotFoundException => super.resolveClass(desc) } } } lazy val ApplicationFolder: File = { val settingDirectory = System.getProperty("user.home") + "/.nescala" val settings = new java.io.File(settingDirectory) if (!settings.exists()) settings.mkdir() settings } private def resource(filename:String) = getClass.getResourceAsStream(filename) }
Example 27
Source File: BrokerResources.scala From reactive-activemq with Apache License 2.0 | 5 votes |
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 28
Source File: Xml.scala From temperature-machine with Apache License 2.0 | 5 votes |
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 29
Source File: SimpleTransformationStd.scala From xml-lens with MIT License | 5 votes |
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 30
Source File: OsmRecordReader.scala From magellan with Apache License 2.0 | 5 votes |
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 31
Source File: OrderServiceApi.scala From 006877 with MIT License | 5 votes |
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 32
Source File: XMLParseExample.scala From Hands-On-Data-Analysis-with-Scala with MIT License | 5 votes |
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 33
Source File: BodySpec.scala From fintrospect with Apache License 2.0 | 5 votes |
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 34
Source File: XmlTest.scala From fintrospect with Apache License 2.0 | 5 votes |
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 35
Source File: VersionInfo.scala From chatoverflow with Eclipse Public License 2.0 | 5 votes |
package org.codeoverflow.chatoverflow import org.codeoverflow.chatoverflow.api.APIVersion._ import org.codeoverflow.chatoverflow.ui.web.GUIServlet._ import scala.io.Source import scala.xml.XML object VersionInfo extends WithLogger { val api: String = s"$MAJOR_VERSION.$MINOR_VERSION" val framework: String = { try { (XML.load(getClass.getResourceAsStream("/dependencies.pom")) \ "version").text } catch { case _: Exception => "unknown" } } val rest: String = "3.0.0-3" val gui: String = getGUIVersionFile("/version_gui.txt") val usedRest: String = getGUIVersionFile("/version_gui_rest.txt") def logSummary(): Unit = { logger info s"Running Chat Overflow version $framework with api version $api." logger info s"Providing rest interface with version $rest." if (gui != "unknown") { logger info s"GUI version $gui using rest interface version $usedRest detected." } else { logger warn "No GUI detected!" } if (usedRest != "unknown" && usedRest != rest) { logger warn "Provided rest interface version and the used one of the GUI differ. " + "GUI functionality may be restricted!" } } private def getGUIVersionFile(name: String): String = try { guiJar.map(jar => Source.fromInputStream(jar.getInputStream(jar.getJarEntry(name))).mkString).getOrElse("unknown") } catch { case _: Exception => "unknown" } }
Example 36
Source File: XMLUtils.scala From infinispan-spark with Apache License 2.0 | 4 votes |
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 37
Source File: Scapegoat.scala From ReactiveMongo-Streaming with Apache License 2.0 | 4 votes |
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") } } }