Permalink
Browse files

Whitespace changes: fixed formatting to be more idiomatic

  • Loading branch information...
djspiewak committed May 17, 2011
1 parent 8c9b8c5 commit c4b5ccceb2da290b9824c0453d61aacb87b7bd7c
@@ -33,66 +33,78 @@ import java.io.Writer
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStreamWriter
class XMLSerializer(val encoding : String, val outputDeclaration : Boolean) {
def serializeDocument(elem : Elem, w : Writer) {
class XMLSerializer(encoding: String, outputDeclaration: Boolean) {
def serializeDocument(elem: Elem, w: Writer) {
if (outputDeclaration) {
w.append("<?xml version=\"1.0\" encoding=\"")
w.append(encoding )
w.append(encoding)
w.append("\" standalone=\"yes\"?>")
}
serialize(elem, w);
serialize(elem, w)
}
def serializeDocument(elem : Elem, outputFile : java.io.File) {
def serializeDocument(elem: Elem, outputFile: File) {
serializeDocument(elem, new OutputStreamWriter(new FileOutputStream(outputFile), encoding))
}
def serialize(elem : Elem, w : Writer) {
var scopes : List[Map[String, String]] = Nil;
def doSerialize(node : Node, w : Writer) {
def serialize(elem: Elem, w: Writer) {
var scopes: List[Map[String, String]] = Nil
def doSerialize(node: Node, w: Writer) {
node match {
case Elem(prefix, name, attrs, scope, children) => {
val parentScope = scopes.headOption getOrElse Map()
scopes = scope :: scopes
val attrStr = if (attrs.isEmpty)
""
else
" " + (attrs map { case (key, value) => key.toString + "=\"" + Node.escapeText(value) + '"' } mkString " ")
val scopeChange = scope filter { case (key, value) => parentScope.get(key) != Some(value) }
val prefixesStr = if (scopeChange.isEmpty)
""
else
" " + (scopeChange map {
case (key, value) => (if (key == "") "xmlns" else "xmlns:" + key) + "=\"" + Node.escapeText(value) + '"' } mkString " ")
val qname = (prefix map { _ + ":" } getOrElse "") + name
val partial = "<" + qname + attrStr + prefixesStr
if (children.isEmpty) {
w append partial
w append "/>"
} else {
w append partial
w append '>'
children.foreach(doSerialize(_, w))
w append "</"
w append qname
w append '>'
}
val attrStr = if (attrs.isEmpty) {
""
} else {
val delta = attrs map {
case (key, value) => key.toString + "=\"" + Node.escapeText(value) + '"'
} mkString " "
" " + delta
}
val scopeChange = scope filter { case (key, value) => parentScope.get(key) != Some(value) }
val prefixesStr = if (scopeChange.isEmpty) {
""
} else {
val delta = scopeChange map {
case (key, value) =>
(if (key == "") "xmlns" else "xmlns:" + key) + "=\"" + Node.escapeText(value) + '"'
} mkString " "
" " + delta
}
val qname = (prefix map { _ + ":" } getOrElse "") + name
val partial = "<" + qname + attrStr + prefixesStr
if (children.isEmpty) {
w.append(partial)
w.append("/>")
} else {
w.append(partial)
w.append('>')
children foreach { doSerialize(_, w) }
w append("</")
w append(qname)
w append('>')
}
scopes = scopes.tail
}
case node => w.append(node.toString())
case node => w.append(node.toString)
}
}
doSerialize(elem, w)
}
}
object XMLSerializer {
def apply(encoding : String = "UTF-8", outputDeclaration : Boolean = false) : XMLSerializer = {
new XMLSerializer(encoding, outputDeclaration);
}
def apply(encoding: String = "UTF-8", outputDeclaration: Boolean = false): XMLSerializer = {
new XMLSerializer(encoding, outputDeclaration);
}
}
@@ -68,14 +68,14 @@ class XMLSpecs extends Specification {
fromString("<my:test xmlns:my='urn:my-urn:quux'></my:test>").name mustEqual "test"
}
"serialize prefixes minimally" in {
fromString("<my:test xmlns:my='urn:my-urn:quux'>\n<beef/>\n\t\n</my:test>").toString mustEqual "<my:test xmlns:my=\"urn:my-urn:quux\">\n<beef/>\n\t\n</my:test>"
}
"serialize unprefixed elements correctly" in {
fromString("<test xmlns='urn:my-urn:quux'>\n<beef/>\n\t\n</test>").toString mustEqual "<test xmlns=\"urn:my-urn:quux\">\n<beef/>\n\t\n</test>"
}
}
"serialize prefixes minimally" in {
fromString("<my:test xmlns:my='urn:my-urn:quux'>\n<beef/>\n\t\n</my:test>").toString mustEqual "<my:test xmlns:my=\"urn:my-urn:quux\">\n<beef/>\n\t\n</my:test>"
}
"serialize unprefixed elements correctly" in {
fromString("<test xmlns='urn:my-urn:quux'>\n<beef/>\n\t\n</test>").toString mustEqual "<test xmlns=\"urn:my-urn:quux\">\n<beef/>\n\t\n</test>"
}
}
"fromSource" should {
import scala.io.Source

0 comments on commit c4b5ccc

Please sign in to comment.