当前位置: 首页>>代码示例>>Scala>>正文


Scala Utility类代码示例

本文整理汇总了Scala中scala.xml.Utility的典型用法代码示例。如果您正苦于以下问题:Scala Utility类的具体用法?Scala Utility怎么用?Scala Utility使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Utility类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: UserActor

//设置package包名称以及导入依赖的类
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(uid: String, board: ActorRef, out: ActorRef) extends Actor with ActorLogging {


  override def preStart() = {
    BoardActor() ! Subscribe
  }

  def receive = LoggingReceive {
    case Message(muid, s) if sender == board => {
      val js = Json.obj("type" -> "message", "uid" -> muid, "msg" -> s)
      out ! js
    }
    case js: JsValue => (js \ "msg").validate[String] map { Utility.escape(_) }  map { board ! Message(uid, _ ) }
    case other => log.error("unhandled: " + other)
  }
}

object UserActor {
  def props(uid: String)(out: ActorRef) = Props(new UserActor(uid, BoardActor(), out))
} 
开发者ID:OCervantes,项目名称:websocket-play,代码行数:33,代码来源:UserActor.scala

示例2: UserSocket

//设置package包名称以及导入依赖的类
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)
  }
} 
开发者ID:onegrx,项目名称:playakkachat,代码行数:59,代码来源:UserSocket.scala

示例3: UserActor

//设置package包名称以及导入依赖的类
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))
  }
} 
开发者ID:baydindima,项目名称:ScalaChat,代码行数:48,代码来源:UserActor.scala

示例4:

//设置package包名称以及导入依赖的类
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)
} 
开发者ID:rvanheest,项目名称:spickle,代码行数:44,代码来源:PersonXml.scala

示例5: XpathXmlEnumerator

//设置package包名称以及导入依赖的类
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() ...

} 
开发者ID:ncrncornell,项目名称:XPathEnumerator,代码行数:51,代码来源:XpathXmlEnumerator.scala

示例6: Html

//设置package包名称以及导入依赖的类
package com.auginte.eventsourced

import java.io.File

import scala.io.Source
import scala.xml.Utility

object Html {
  val index = resource("index.html")

  val commonJs = resource("common.js")

  def project(project: Project, uuid: UUID, compiledJsName: String, compiledJsLauncherName: String, compiledJsDepsName: String) = resource(
    "project.html",
    Map(
      "{{project}}" -> urlEncode(project),
      "{{project|urlencoded}}" -> java.net.URLEncoder.encode(project, "utf-8"),
      "{{uuid}}" -> uuid,
      "{{compiledJsName}}" -> compiledJsName,
      "{{compiledJsLauncherName}}" -> compiledJsLauncherName,
      "{{compiledJsDepsName}}" -> compiledJsDepsName
    )
  )

  private def resource(file: String, parameters: Map[String, String] = Map()): String = {
    val resource = Html.getClass.getResourceAsStream(file)
    if (resource == null) {
      throw new RuntimeException(s"Cannot load resource: $file")
    }
    replace(Source.fromInputStream(resource).mkString(""), parameters)
  }

  private def replace(initialData: String, parameters: Map[String, String]): String =
    parameters.foldLeft(initialData)((commulative, pair) => commulative.replaceAllLiterally(pair._1, pair._2))

  private def urlEncode(data: String) = Utility.escape(data.replaceAll(" ", "-")).replaceAll("'", "%27").replaceAll("\"", "%22")

  def readJs(name: String) = {
    val file = new File(name)
    Source.fromFile(file)
  }
} 
开发者ID:Auginte,项目名称:event-sourced-notepad,代码行数:43,代码来源:Html.scala


注:本文中的scala.xml.Utility类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。