summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2007-02-27 09:55:23 +0000
committerBurak Emir <emir@epfl.ch>2007-02-27 09:55:23 +0000
commit8cd3a8fcd5bab2acf77f246519a9a71727710d7e (patch)
tree72fb78efe2e45d5cc3d270691997ee24ee9affd2
parente30503f10056bdff6eb066122607c5e42e2cabb9 (diff)
downloadscala-8cd3a8fcd5bab2acf77f246519a9a71727710d7e.tar.gz
scala-8cd3a8fcd5bab2acf77f246519a9a71727710d7e.tar.bz2
scala-8cd3a8fcd5bab2acf77f246519a9a71727710d7e.zip
xml.Elem has now {apply,unapply} !
+ Utility.trim function to remove whitespace + tests
-rw-r--r--src/library/scala/xml/Elem.scala11
-rw-r--r--src/library/scala/xml/NodeBuffer.scala21
-rw-r--r--src/library/scala/xml/Utility.scala23
-rw-r--r--test/files/jvm/unittest_xml.scala27
4 files changed, 66 insertions, 16 deletions
diff --git a/src/library/scala/xml/Elem.scala b/src/library/scala/xml/Elem.scala
index c5794dbcd0..027a1120b0 100644
--- a/src/library/scala/xml/Elem.scala
+++ b/src/library/scala/xml/Elem.scala
@@ -11,7 +11,16 @@
package scala.xml
+object Elem {
+ def apply(prefix: String,label: String, attributes: MetaData, scope: NamespaceBinding, child: Node*) =
+ new Elem(prefix,label,attributes,scope,child:_*)
+
+ def unapplySeq(n:Node) = if(n.isInstanceOf[SpecialNode]) None else
+ Some(Tuple5(n.prefix,n.label,n.attributes,n.scope,n.child))
+
+
+}
/** The case class <code>Elem</code> extends the <code>Node</code> class,
* providing an immutable data object representing an XML element.
*
@@ -23,7 +32,7 @@ package scala.xml
* @param child the children of this node
*/
// "val" is redundant for non-overriding arguments
-case class Elem(override val prefix: String,
+/*case*/ class Elem(override val prefix: String,
val label: String,
override val attributes: MetaData,
override val scope: NamespaceBinding,
diff --git a/src/library/scala/xml/NodeBuffer.scala b/src/library/scala/xml/NodeBuffer.scala
index 4bc4b61ab1..0e7e1b665e 100644
--- a/src/library/scala/xml/NodeBuffer.scala
+++ b/src/library/scala/xml/NodeBuffer.scala
@@ -37,24 +37,19 @@ class NodeBuffer extends scala.collection.mutable.ArrayBuffer[Node] {
*/
def &+(o: Any): NodeBuffer = {
o match {
- case null => // ignore null
-
- case _:Unit =>
+ case null | _:Unit | Text("")=>
// ignore
- case it:Iterator[_] =>
- while(it.hasNext) {
+
+ case it:Iterator[_] =>
+ while(it.hasNext)
this &+ it.next
- }
+
case n:Node =>
super.+(n)
+
case ns:Iterable[_] =>
- val it = ns.elements
- while (it.hasNext) {
- this &+ it.next
- //if (it.hasNext)
- // this &+ " ";
- }
- //case s:String => super.+(Text(o.toString()));
+ this &+ ns.elements
+
case d =>
super.+(new Atom(d))
}
diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala
index 94e03d9502..c0be4bc280 100644
--- a/src/library/scala/xml/Utility.scala
+++ b/src/library/scala/xml/Utility.scala
@@ -22,7 +22,28 @@ import collection.mutable.{Set, HashSet}
*/
object Utility extends AnyRef with parsing.TokenTests {
- def view(s: String): Text = Text(s)
+
+ /** precondition: node is not a text node (it might be trimmed)
+ */
+
+ def trim(x:Node): Node = x match {
+ case Elem(pre,lab,md,scp,child@_*) =>
+ Elem(pre,lab,md,scp, (child flatMap trimProper):_*)
+ }
+
+ /** attribute values and Atom nodes that are not Text nodes are unaffected */
+ def trimProper(x:Node): Seq[Node] = x match {
+ case Elem(pre,lab,md,scp,child@_*) =>
+ Elem(pre,lab,md,scp, (child flatMap trimProper):_*)
+ case Text(s) =>
+ new TextBuffer().append(s).toText
+ case _ =>
+ x
+ }
+
+ /** @deprecated a string might also be Atom(s) - define your own conversion
+ */
+ @deprecated def view(s: String): Text = Text(s)
/**
* Escapes the characters &lt; &gt; &amp; and &quot; from string.
diff --git a/test/files/jvm/unittest_xml.scala b/test/files/jvm/unittest_xml.scala
index 99284f144e..a6032e257c 100644
--- a/test/files/jvm/unittest_xml.scala
+++ b/test/files/jvm/unittest_xml.scala
@@ -47,10 +47,35 @@ object Test {
}
}
+ class UtilityTest extends TestCase("scala.xml.Utility") with Assert {
+ val x = <foo>
+ <toomuchws/>
+ </foo>
+
+ val y = xml.Utility.trim(x)
+
+ assertEquals("trim 1 ", 1, y match { case <foo><toomuchws/></foo> => 1 })
+
+ val x2 = <foo>
+ <toomuchws> a b b a </toomuchws>
+ </foo>
+
+ val y2 = xml.Utility.trim(x2)
+
+ assertEquals("trim 2 ", 2, y2 match { case <foo><toomuchws>a b b a</toomuchws></foo> => 2 })
+
+
+ val z = <bar>''</bar>
+ val z1 = z.toString
+
+ assertEquals("apos unescaped", "<bar>''</bar>", z1)
+ }
+
def main(args:Array[String]) = {
val ts = new TestSuite(
new ParsingTest,
- new MetaDataTest //,
+ new MetaDataTest,
+ new UtilityTest
)
val tr = new TestResult()
ts.run(tr)