scala.xml.Utility 代码实例
实例 1
package actors import akka.actor.Actor import akka.actor.ActorLogging import akka.event.LoggingReceive import play.api.libs.json.JsValue import play.api.libs.json.Json import akka.actor.ActorRef import akka.actor.Props import scala.xml.Utility class UserActor(nick: String, userId: Int, board: ActorRef, out: ActorRef) extends Actor with ActorLogging { override def preStart() = { board ! Subscribe import UserActor._ val js = Json.obj("type" -> "info", "img" -> userId % AvatarCount) out ! js } def receive = LoggingReceive { case Message(nickname, id, s) if sender == board => import UserActor._ if (nickname != nick) { val js = Json.obj("type" -> "message", "nickname" -> nickname, "msg" -> s, "img" -> id % AvatarCount) out ! js } case js: JsValue => (js "msg").validate[String] map { Utility.escape } foreach { board ! Message(nick, userId, _) } case other => log.error("unhandled: " + other) } } object UserActor { val AvatarCount = 11 var userCount = 0 def props(nick: String)(out: ActorRef) = { userCount += 1 Props(new UserActor(nick, userCount - 1, BoardActor(), out)) } }
实例 2
package com.github.rvanheest.spickle.example.xml.person import scala.xml.{ Elem, Node, Utility } trait PersonXml { // @formatter:off private val person1: Elem = <person age="24" xlink:age="24"> <name>Jonathan Moreno</name> <address> <street>Atwood</street> <number>6689</number> <zip-code>1234AB</zip-code> <city>Tembladera</city> </address> <mail>[email protected]</mail> </person> private val person2: Elem = <person age="48" xlink:age="48"> <name>Jonathan Moreno</name> <address> <street>Atwood</street> <number addition="a">6689</number> <zip-code>1234AB</zip-code> <city>Tembladera</city> </address> <mail>[email protected]</mail> </person> private val person3: Elem = <person age="68" xlink:age="68"> <name>Jonathan Moreno</name> <address> <freepost-number>70</freepost-number> <zip-code>1234AB</zip-code> <city>Tembladera</city> </address> </person> // @formatter:on val xml1: Node = Utility.trim(person1) val xml2: Node = Utility.trim(person2) val xml3: Node = Utility.trim(person3) }
实例 3
package actors import actors.UserSocket.{ChatMessage, Message} import actors.UserSocket.Message.messageReads import akka.actor.{Actor, ActorLogging, ActorRef, Props} import akka.cluster.pubsub.DistributedPubSub import akka.cluster.pubsub.DistributedPubSubMediator.{Publish, Subscribe} import akka.event.LoggingReceive import play.api.libs.json.{Writes, JsValue, Json} import play.twirl.api.HtmlFormat import scala.xml.Utility object UserSocket { def props(user: String)(out: ActorRef) = Props(new UserSocket(user, out)) case class Message(msg: String) object Message { implicit val messageReads = Json.reads[Message] } case class ChatMessage(user: String, text: String) object ChatMessage { implicit val chatMessageWrites = new Writes[ChatMessage] { def writes(chatMessage: ChatMessage): JsValue = { Json.obj( "type" -> "message", "user" -> chatMessage.user, "text" -> multiLine(chatMessage.text) ) } } private def multiLine(text: String) = { HtmlFormat.raw(text).body.replace("n", "<br/>") } } } class UserSocket(uid: String, out: ActorRef) extends Actor with ActorLogging { val topic = "chat" val mediator = DistributedPubSub(context.system).mediator mediator ! Subscribe(topic, self) def receive = LoggingReceive { case js: JsValue => js.validate[Message](messageReads) .map(message => Utility.escape(message.msg)) .foreach { msg => mediator ! Publish(topic, ChatMessage(uid, msg))} case c:ChatMessage => out ! Json.toJson(c) } }
实例 4
package edu.ncrn.cornell.xml import scala.annotation.tailrec import scala.xml.{Node, Utility} import ScalaXmlExtra._ import XpathEnumerator._ class XpathXmlEnumerator( protected val nodesIn: List[Node] ) extends XpathEnumerator { @tailrec final def enumerateXml( nodes: List[(Node, String)], pathData: List[(String, String)] )(implicit nodeFilters: NodeFilters): List[(String, String)] = nodes.filter(x => nodeFilters(x._2, x._1)) match { case (node, currentPath) +: rest => val newElementData = if(node.child.isEmpty) List((cleanXpath(currentPath), node.text)) else Nil val newAttributeData = node.attributes.asAttrMap.map{ case (key, value) => (currentPath + "/@" + key, value) }.toList enumerateXml( rest ++ pathifyNodes(node.child.toList, currentPath + "/", nonEmpty), newElementData ::: newAttributeData ::: pathData ) case Nil => pathData } def enumerate( nonEmpty: Boolean, newNodeFilters: NodeFilters ): List[(String, String)] = { implicit val nodeFilters = newNodeFilters this.nonEmpty = nonEmpty enumerateXml(pathifyNodes( nodesIn.map(x => Utility.trim(x)), "/", nonEmpty ), Nil) } } object XpathXmlEnumerator { //def apply() ... }