From 7831970b25664e49bc97d72cad60b83b553a8459 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 4 May 2009 18:32:47 +0000 Subject: StringBuilder helper function to reduce lots of... StringBuilder helper function to reduce lots of code duplication. --- src/library/scala/xml/EntityRef.scala | 2 +- src/library/scala/xml/Group.scala | 8 ---- src/library/scala/xml/MetaData.scala | 14 ++---- src/library/scala/xml/NamespaceBinding.scala | 14 ++---- src/library/scala/xml/PrettyPrinter.scala | 69 +++++++++++++--------------- src/library/scala/xml/Utility.scala | 28 ++++++----- src/library/scala/xml/Xhtml.scala | 15 ++---- src/library/scala/xml/dtd/ContentModel.scala | 13 ++---- src/library/scala/xml/dtd/Decl.scala | 9 ++-- 9 files changed, 65 insertions(+), 107 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/xml/EntityRef.scala b/src/library/scala/xml/EntityRef.scala index 9ce63d81f9..f660695ae1 100644 --- a/src/library/scala/xml/EntityRef.scala +++ b/src/library/scala/xml/EntityRef.scala @@ -42,7 +42,7 @@ case class EntityRef(entityName: String) extends SpecialNode { case "amp" => "&" case "apos" => "'" case "quot" => "\"" - case _ => val sb = new StringBuilder(); buildString(sb).toString() + case _ => Utility.sbToString(buildString) } /** Appends "& entityName;" to this string buffer. diff --git a/src/library/scala/xml/Group.scala b/src/library/scala/xml/Group.scala index 4831514fd4..9e18ecb5f0 100644 --- a/src/library/scala/xml/Group.scala +++ b/src/library/scala/xml/Group.scala @@ -60,12 +60,4 @@ case class Group(val nodes: Seq[Node]) extends Node { def buildString(sb: StringBuilder) = throw new UnsupportedOperationException( "class Group does not support method toString(StringBuilder)") - - override def text = { // same impl as NodeSeq - val sb = new StringBuilder() - val it = elements - while (it.hasNext) - sb.append(it.next.text) - sb.toString() - } } diff --git a/src/library/scala/xml/MetaData.scala b/src/library/scala/xml/MetaData.scala index 71d6befc20..bca5fe08a1 100644 --- a/src/library/scala/xml/MetaData.scala +++ b/src/library/scala/xml/MetaData.scala @@ -11,6 +11,8 @@ package scala.xml +import Utility.sbToString + /** * Copyright 2008 Google Inc. All Rights Reserved. * @author Burak Emir @@ -234,20 +236,12 @@ abstract class MetaData extends Collection[MetaData] { override def hashCode(): Int - def toString1(): String = { - val sb = new StringBuilder() - toString1(sb) - sb.toString() - } + def toString1(): String = sbToString(toString1) //appends string representations of single attribute to StringBuilder def toString1(sb:StringBuilder): Unit - override def toString(): String = { - val sb = new StringBuilder() - buildString(sb) - sb.toString() - } + override def toString(): String = sbToString(buildString) def buildString(sb: StringBuilder): StringBuilder = { sb.append(' ') diff --git a/src/library/scala/xml/NamespaceBinding.scala b/src/library/scala/xml/NamespaceBinding.scala index 7605fbacd2..9af256501c 100644 --- a/src/library/scala/xml/NamespaceBinding.scala +++ b/src/library/scala/xml/NamespaceBinding.scala @@ -12,6 +12,7 @@ package scala.xml import Predef._ +import Utility.sbToString /** The class NamespaceBinding represents namespace bindings * and scopes. The binding for the default namespace is treated as a null @@ -42,18 +43,9 @@ class NamespaceBinding(val prefix: String, def getPrefix(_uri: String): String = if (_uri == uri) uri else parent.getURI(_uri) - override def toString(): String = { - val sb = new StringBuilder() - buildString(sb, TopScope) - sb.toString() - } - - def buildString(stop: NamespaceBinding): String = { - val sb = new StringBuilder() - buildString(sb, stop) - sb.toString() - } + override def toString(): String = sbToString(buildString(_, TopScope)) + def buildString(stop: NamespaceBinding): String = sbToString(buildString(_, stop)) def buildString(sb: StringBuilder, stop: NamespaceBinding): Unit = { if (this ne stop) { // contains? sb.append(" xmlns") diff --git a/src/library/scala/xml/PrettyPrinter.scala b/src/library/scala/xml/PrettyPrinter.scala index 7e09d58c9b..c16e8a0da9 100644 --- a/src/library/scala/xml/PrettyPrinter.scala +++ b/src/library/scala/xml/PrettyPrinter.scala @@ -12,6 +12,7 @@ package scala.xml import scala.collection.Map +import Utility.sbToString /** Class for pretty printing. After instantiating, you can use the * toPrettyXML methods to convert XML to a formatted string. The class @@ -113,41 +114,43 @@ class PrettyPrinter( width:Int, step:Int ) { * @return ... */ protected def leafTag(n: Node) = { - val sb = new StringBuilder("<") - n.nameToString(sb) - //Utility.appendPrefixedName( n.prefix, n.label, pmap, sb ); - n.attributes.buildString(sb) - //Utility.attr2xml( n.scope, n.attributes, pmap, sb ); - sb.append("/>") - sb.toString() + def mkLeaf(sb: StringBuilder) { + sb append '<' + n nameToString sb + n.attributes buildString sb + sb append "/>" + } + sbToString(mkLeaf) } protected def startTag(n: Node, pscope: NamespaceBinding): (String, Int) = { - val sb = new StringBuilder("<") - n.nameToString(sb) //Utility.appendPrefixedName( n.prefix, n.label, pmap, sb ); - val i = sb.length + 1 - n.attributes.buildString(sb) - n.scope.buildString(sb, pscope) - sb.append('>') - (sb.toString(), i) + var i = 0 + def mkStart(sb: StringBuilder) { + sb append '<' + n nameToString sb + i = sb.length + 1 + n.attributes buildString sb + n.scope.buildString(sb, pscope) + sb append '>' + } + (sbToString(mkStart), i) } protected def endTag(n: Node) = { - val sb = new StringBuilder("') - sb.toString() + def mkEnd(sb: StringBuilder) { + sb append "' + } + sbToString(mkEnd) } protected def childrenAreLeaves(n: Node): Boolean = { - val it = n.child.elements - while (it.hasNext) - it.next match { - case _:Atom[_] | _:Comment | _:EntityRef | _:ProcInstr => - case _:Node => - return false - } - true + def isLeaf(l: Node) = l match { + case _:Atom[_] | _:Comment | _:EntityRef | _:ProcInstr => true + case _ => false + } + n.child forall isLeaf } protected def fits(test: String) = @@ -268,11 +271,8 @@ class PrettyPrinter( width:Int, step:Int ) { * @param pmap the namespace to prefix mapping * @return ... */ - def format(n: Node, pscope: NamespaceBinding): String = { - val sb = new StringBuilder() - format(n, pscope, sb) - sb.toString() - } + def format(n: Node, pscope: NamespaceBinding): String = + sbToString(format(n, pscope, _)) /** Returns a formatted string containing well-formed XML nodes with * default namespace prefix mapping. @@ -288,11 +288,8 @@ class PrettyPrinter( width:Int, step:Int ) { * @param nodes the sequence of nodes to be serialized * @param pmap the namespace to prefix mapping */ - def formatNodes(nodes: Seq[Node], pscope: NamespaceBinding): String = { - var sb = new StringBuilder() - formatNodes(nodes, pscope, sb) - sb.toString() - } + def formatNodes(nodes: Seq[Node], pscope: NamespaceBinding): String = + sbToString(formatNodes(nodes, pscope, _)) /** Appends a formatted string containing well-formed XML with * the given namespace to prefix mapping to the given stringbuffer. diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala index 4d8f99fa9e..4b749db9e6 100644 --- a/src/library/scala/xml/Utility.scala +++ b/src/library/scala/xml/Utility.scala @@ -19,8 +19,15 @@ import collection.mutable.{Set, HashSet} * * @author Burak Emir */ -object Utility extends AnyRef with parsing.TokenTests { - +object Utility extends AnyRef with parsing.TokenTests +{ + // helper for the extremely oft-repeated sequence of creating a + // StringBuilder, passing it around, and then grabbing its String. + private [xml] def sbToString(f: (StringBuilder) => Unit): String = { + val sb = new StringBuilder + f(sb) + sb.toString + } /** trims an element - call this method, when you know that it is an * element (and not a text node) so you know that it will not be trimmed @@ -72,8 +79,7 @@ object Utility extends AnyRef with parsing.TokenTests { * @param text ... * @return ... */ - final def escape(text: String): String = - escape(text, new StringBuilder()).toString() + final def escape(text: String): String = sbToString(escape(text, _)) /** * Appends escaped string to s. @@ -168,12 +174,8 @@ object Utility extends AnyRef with parsing.TokenTests { * * @todo define a way to escape literal characters to &xx; references */ - def toXML(n: Node, stripComment: Boolean): String = { - val sb = new StringBuilder() - toXML(n, TopScope, sb, stripComment) - sb.toString() - } - + def toXML(n: Node, stripComment: Boolean): String = + sbToString(toXML(n, TopScope, _, stripComment)) /** * Appends a tree to the given stringbuffer within given namespace scope. @@ -274,11 +276,7 @@ object Utility extends AnyRef with parsing.TokenTests { } ) - def appendQuoted(s: String): String = { - val sb = new StringBuilder() - appendQuoted(s, sb) - sb.toString() - } + def appendQuoted(s: String): String = sbToString(appendQuoted(s, _)) /** * Appends "s" if string s does not contain ", diff --git a/src/library/scala/xml/Xhtml.scala b/src/library/scala/xml/Xhtml.scala index 1fe16c71b3..8438f91173 100644 --- a/src/library/scala/xml/Xhtml.scala +++ b/src/library/scala/xml/Xhtml.scala @@ -3,6 +3,7 @@ package scala.xml import parsing.XhtmlEntities +import Utility.sbToString /* (c) David Pollak 2007 WorldWide Conferencing, LLC */ @@ -21,11 +22,8 @@ object Xhtml * * @param nodeSeq the node sequence */ - def toXhtml(nodeSeq: NodeSeq): String = { - val sb = new StringBuilder - sequenceToXML(nodeSeq, TopScope, sb, false, false) - sb.toString - } + def toXhtml(nodeSeq: NodeSeq): String = + sbToString(sequenceToXML(nodeSeq, TopScope, _, false, false)) /** * Convenience function: amounts to calling toXhtml(node, TopScope, ...) @@ -33,11 +31,8 @@ object Xhtml * * @param nodeSeq the node sequence */ - def toXhtml(n: Node, stripComment: Boolean, convertAmp: Boolean): String = { - val sb = new StringBuilder() - toXhtml(n, TopScope, sb, stripComment, convertAmp) - sb.toString() - } + def toXhtml(n: Node, stripComment: Boolean, convertAmp: Boolean): String = + sbToString(toXhtml(n, TopScope, _, stripComment, convertAmp)) /** * Appends a tree to the given stringbuffer within given namespace scope. diff --git a/src/library/scala/xml/dtd/ContentModel.scala b/src/library/scala/xml/dtd/ContentModel.scala index 013f3d0414..a1e2ea0aa5 100644 --- a/src/library/scala/xml/dtd/ContentModel.scala +++ b/src/library/scala/xml/dtd/ContentModel.scala @@ -13,6 +13,7 @@ package scala.xml.dtd import scala.util.regexp.WordExp import scala.util.automata.{DetWordAutom, SubsetConstruction, WordBerrySethi} +import Utility.sbToString object ContentModel extends WordExp { type _labelT = ElemName @@ -49,11 +50,7 @@ object ContentModel extends WordExp { return s } - def buildString(r: RegExp): String = { - val sb = new StringBuilder() - buildString(r, sb) - sb.toString() - } + def buildString(r: RegExp): String = sbToString(buildString(r, _)) /* precond: rs.length >= 1 */ private def buildString(rs: Seq[RegExp], sb: StringBuilder, sep: Char) { @@ -95,11 +92,7 @@ object ContentModel extends WordExp { sealed abstract class ContentModel { - override def toString(): String = { - val sb = new StringBuilder() - buildString(sb) - sb.toString() - } + override def toString(): String = sbToString(buildString) def buildString(sb:StringBuilder): StringBuilder; /* diff --git a/src/library/scala/xml/dtd/Decl.scala b/src/library/scala/xml/dtd/Decl.scala index b94c56062b..50385c69a6 100644 --- a/src/library/scala/xml/dtd/Decl.scala +++ b/src/library/scala/xml/dtd/Decl.scala @@ -10,7 +10,7 @@ package scala.xml.dtd - +import Utility.sbToString abstract class Decl @@ -49,8 +49,7 @@ extends MarkupDecl with DtdTypeSymbol { * directly. */ case class AttrDecl(name: String, tpe: String, default: DefaultDecl) { - override def toString(): String = - buildString(new StringBuilder()).toString(); + override def toString(): String = sbToString(buildString) def buildString(sb: StringBuilder): StringBuilder = { sb.append(" ").append(name).append(' ').append(tpe).append(' '); @@ -157,9 +156,7 @@ case object IMPLIED extends DefaultDecl { } case class DEFAULT(fixed: Boolean, attValue: String) extends DefaultDecl { - override def toString(): String = - buildString(new StringBuilder()).toString(); - + override def toString(): String = sbToString(buildString) override def buildString(sb: StringBuilder): StringBuilder = { if (fixed) sb.append("#FIXED ") Utility.appendEscapedQuoted(attValue, sb) -- cgit v1.2.3