• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala PrettyPrinter类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Transaction

//设置package包名称以及导入依赖的类
package net.shiroka.tools.ofx

import java.io._
import java.security.MessageDigest
import scala.xml.PrettyPrinter
import org.joda.time.DateTime
import org.joda.time.format._
import Transaction._

case class Transaction(
    dateTime: DateTime,
    `type`: Type,
    description: String,
    amount: BigDecimal,
    balance: BigDecimal
) {
  lazy val date: DateTime = dateBase(dateTime)
  lazy val moveToLastMinuteOfTheDay = copy(dateTime = dateTime.plusDays(1).minusMinutes(1))
  lazy val dateTimeOfx = dateTime.toString(ofxDateFormat)

  def uniquifyTime(previousOpt: Option[DateTime]) = previousOpt match {
    case None => moveToLastMinuteOfTheDay
    case Some(previous) if (date != dateBase(previous)) => moveToLastMinuteOfTheDay
    case Some(previous) => copy(dateTime = previous.minusMinutes(1))
  }

  def writeOfx(sink: PrintStream, ofxKeyPrefix: String = ""): Unit = {
    sink.print(ppXml.format(
      <STMTTRN>
        <TRNTYPE>{ `type`.name }</TRNTYPE>
        <DTPOSTED>{ dateTimeOfx }</DTPOSTED>
        <TRNAMT>{ amount }</TRNAMT>
        <FITID>{ ofxId(ofxKeyPrefix) }</FITID>
        <MEMO>{ description }</MEMO>
      </STMTTRN>
    ))
  }

  def ofxId(prefix: String) = {
    val text = List(prefix, dateTimeOfx, description, `type`.name, amount, balance).mkString(":")
    MessageDigest.getInstance("MD5").digest(text.getBytes).map("%02x".format(_)).mkString
  }
}

object Transaction {
  val ofxDateFormat = DateTimeFormat.forPattern("yyyyMMddHHmmss")
  val ppXml = new PrettyPrinter(120, 2)

  sealed abstract class Type(val name: String)
  case object Debit extends Type("DEBIT")
  case object Deposit extends Type("DEP")
  case object Interest extends Type("INT")
  case object Credit extends Type("CREDIT")

  def dateBase(dt: DateTime) = dt.dayOfMonth.roundFloorCopy
} 
开发者ID:ikuo,项目名称:ofx-tools,代码行数:57,代码来源:Transaction.scala


示例2: ConnectionProperties

//设置package包名称以及导入依赖的类
package info.armado.ausleihe.client.model

import better.files.File
import scala.xml.XML
import scala.xml.PrettyPrinter

object ConnectionProperties {
  def apply(propertiesFile: File): ConnectionProperties = {
    val contentNode = XML.loadFile(propertiesFile.toJava)

    val url = (contentNode \ "url").text
    val port = (contentNode \ "port").text.toInt
    val basePath = (contentNode \ "basepath").text
    val operator = Operators.parse((contentNode \ "operator").text)

    ConnectionProperties(url, port, basePath, operator)
  }
  
  def createDefault = ConnectionProperties("localhost", 8080, "ausleihe-rest-server", Some(Operators.Spielekreis))
}

case class ConnectionProperties(val url: String, val port: Int, val basePath: String, operator: Option[Operators.Value]) {
  val baseURL = s"http://${url}:${port}/${basePath}" 
  
  def saveToFile(propertiesFile: File) = {
    val content =
      <connection>
        <url>{ url }</url>
        <port>{ port }</port>
        <basepath>{ basePath }</basepath>
        <operator>{
          operator match {
            case Some(operator) => operator.toString.toLowerCase
            case None => "unknown"
          }
        }</operator>        
      </connection>
    
    val prettyPrinter = new PrettyPrinter(80, 2)
    propertiesFile.overwrite(prettyPrinter.format(content))
  }
} 
开发者ID:Spielekreis-Darmstadt,项目名称:lending,代码行数:43,代码来源:ConnectionProperties.scala


示例3: Util

//设置package包名称以及导入依赖的类
package trap.xml
//import java.io.BufferedInputStream
//import java.io.BufferedOutputStream
//import java.io.BufferedReader
//import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader

import scala.xml.Elem
import scala.xml.Attribute
import scala.xml.Text
import scala.xml.Node
import scala.xml.NodeSeq
import scala.xml.Null
import scala.xml.PrettyPrinter
import scala.xml.XML
import trap.Util._
import trap.file.Util._
object Util {
  
  def formatNicely(xmlFile:String) = 
    writeToTextFile(xmlFile, new PrettyPrinter(200, 2).format(loadXML(xmlFile)))

  def formatNicely(xml:Elem) = new PrettyPrinter(200, 2).format(xml)
    
  def getAttributeText(xml:NodeSeq, attrName:String) = {
    printdbg ("    ===> getAttributeTest "+xml+": "+attrName)
    (xml \ ("@"+attrName)).text
  }
  private val r = <root/>
  def getNode(n:String) = r.copy(label=n)
  def addChild(n: Node, newChild: Node) = n match {
    case Elem(prefix, label, attribs, scope, child @ _*) =>
      Elem(prefix, label, attribs, scope, child ++ newChild : _*)
    case _ =>  throw new Exception("Can only add children to elements!")
  }
  def addChildren(n:Node, children:NodeSeq) = children.foldLeft(n)((x, y) => addChild(x, y))
  def addAttribute(n:Elem, attrName:String, attrValue:String) = n % Attribute(None, attrName, Text(attrValue), Null)
  def attr(xml:Node, attrName:String) = (xml \ ("@"+attrName)).text
  def loadXML(fileName:String) =  
    using(new FileInputStream(fileName)){
      fis => using(new InputStreamReader(fis)){
        isr => XML.load(isr)
      }
    }

  def getElementName (x:Node) = x.nameToString(new StringBuilder).toString
  def filterByAttr(xml:NodeSeq, attrName:String, attrValue:String) = 
    xml.filter(x  => (x \ ("@"+attrName)).text == attrValue)
  
  def getElementsWithAttribute(xml:NodeSeq, elementName:String, attrName:String, attrVal:String) = 
    xml \ elementName collect { x => (x \ ("@"+attrName)).map(_.text).contains(attrVal) match {
        case true => x
      }   
    }
  def getElementWithAttribute(xml:NodeSeq, elementName:String, attrName:String, attrVal:String) = 
    ((xml \ elementName).find{ x => (x \ ("@"+attrName)).map(_.text).contains(attrVal) }).get
  def getChildren(xml:NodeSeq) = xml \ "_"
} 
开发者ID:scalahub,项目名称:acc,代码行数:60,代码来源:Util.scala


示例4: PersonPickleExample

//设置package包名称以及导入依赖的类
package com.github.rvanheest.spickle.example.xml.person

import scala.util.Success
import scala.xml.PrettyPrinter

object PersonPickleExample extends App with Person with PersonXml with PersonPickle {

  val (Success(person1), rest1) = picklePerson.parse(xml1)
  val Success(person1Xml) = picklePerson.pickle(person1, Seq.empty)

  println(person1)
  println(rest1)
  for (xml <- person1Xml) {
    println(new PrettyPrinter(160, 4).format(xml))
  }
  println(xml1.toString() == person1Xml.toString())
  println

  val (Success(person2), rest2) = picklePerson.parse(xml2)
  val Success(person2Xml) = picklePerson.pickle(person2, Seq.empty)

  println(person2)
  println(rest2)
  for (xml <- person2Xml) {
    println(new PrettyPrinter(160, 4).format(xml))
  }
  println(xml2.toString() == person2Xml.toString())
  println

  val (Success(person3), rest3) = picklePerson.parse(xml3)
  val Success(person3Xml) = picklePerson.pickle(person3, Seq.empty)

  println(person3)
  println(rest3)
  for (xml <- person3Xml) {
    println(new PrettyPrinter(160, 4).format(xml))
  }
  println(xml3.toString() == person3Xml.toString())
} 
开发者ID:rvanheest,项目名称:spickle,代码行数:40,代码来源:PersonPickleExample.scala


示例5: KmlCoordinate

//设置package包名称以及导入依赖的类
package parku30.kml

import scala.xml.{Elem, PrettyPrinter}

case class KmlCoordinate(lat: String, lon: String) {
  def toKmlString():String = { s"$lon,$lat"}
}

case class KmlPoint(coordinate: KmlCoordinate) {
  def toXml: Elem = <Point><coordinates>{coordinate.toKmlString()},0.0</coordinates></Point>
}

case class KmlPlacemark(name: String, description: String, point: KmlPoint) {
  def toXml: Elem = <Placemark><name>{name}</name><description>{scala.xml.PCData(description)}</description>{point.toXml}</Placemark>
}

case class KmlFolder(name: String, placemarks: Seq[KmlPlacemark]) {
  def toXml: Elem = <Folder>
    <name>{name}</name>
    {placemarks.map(_.toXml)}
  </Folder>
}

case class KmlDoc(name: String, description: String, folders: Seq[KmlFolder]) {

  def toXml: Elem =
    <kml xmlns='http://www.opengis.net/kml/2.2'>
      <Document>
        <name>{name}</name>
        <description>{scala.xml.PCData(description)}</description>
        {folders.map(_.toXml)}
      </Document>
    </kml>

  def toXmlString: String = {
    val pretty = new PrettyPrinter(120, 2)
    "<?xml version='1.0' encoding='UTF-8'?>\n" + pretty.format(toXml)
  }
} 
开发者ID:pukomuko,项目名称:mapslt-to-kml,代码行数:40,代码来源:Kml.scala


示例6: GpxWriter

//设置package包名称以及导入依赖的类
package com.pds.poi4s.gpx

import java.io.OutputStream

import com.pds.poi4s.gpx.GpxVersion._

import scala.xml.{Elem, PrettyPrinter}

object GpxWriter {
  private val prettyPrinter = new PrettyPrinter(80, 4)

  def write(gpxFile: GpxFile, os: OutputStream, version: GpxVersion = Version11): Unit = {
    val xml = version match {
      case Version10 => generateVersion10(gpxFile)
      case Version11 => generateVersion11(gpxFile)
    }

    os.write(prettyPrinter.format(xml).getBytes("UTF-8"))
  }

  private def generateVersion10(gpxFile: GpxFile): Elem = {
    <gpx version="1.0" creator="poi4s">
      {gpxFile.name.map(n => <name>{n}</name>).orNull}

      {
        gpxFile.waypoints map { wpt =>
          <wpt lat={wpt.lat.toString} lon={wpt.lon.toString}>
            {wpt.name.map(n => <name>{n}</name>).orNull}
            {wpt.elevation.map(e => <ele>{e}</ele>).orNull}
            {wpt.comment.map(c => <cmt>{c}</cmt>).orNull}
            {wpt.description.map(d => <desc>{d}</desc>).orNull}
            {wpt.link.map(l => <url>{l}</url>).orNull}
            {wpt.source.map(s => <src>{s}</src>).orNull}
          </wpt>
        }
      }
    </gpx>
  }

  private def generateVersion11(gpxFile: GpxFile): Elem = {
    <gpx version="1.1" creator="poi4s">
      <metadata>
        {gpxFile.name.map(n => <name>{n}</name>).orNull}
      </metadata>

      {
      gpxFile.waypoints map { wpt =>
        <wpt lat={wpt.lat.toString} lon={wpt.lon.toString}>
          {wpt.name.map(n => <name>{n}</name>).orNull}
          {wpt.elevation.map(e => <ele>{e}</ele>).orNull}
          {wpt.comment.map(c => <cmt>{c}</cmt>).orNull}
          {wpt.description.map(d => <desc>{d}</desc>).orNull}
          {wpt.link.map(l => <link>{l}</link>).orNull}
          {wpt.source.map(s => <src>{s}</src>).orNull}
        </wpt>
      }
      }
    </gpx>
  }
} 
开发者ID:stringbean,项目名称:poi4s,代码行数:61,代码来源:GpxWriter.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Response类代码示例发布时间:2022-05-23
下一篇:
Scala CharSequenceReader类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap