本文整理汇总了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
}
示例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))
}
}
示例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 \ "_"
}
示例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())
}
示例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)
}
}
示例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>
}
}