|
Scala example source code file (xmlstuff.scala)
The Scala xmlstuff.scala source code
import java.io.StringReader
import org.xml.sax.InputSource
import scala.testing.SUnit.Assert
import scala.xml.{Node, NodeSeq, Elem, Text, XML}
object Test extends AnyRef with Assert {
/** returns true if exception was thrown */
def catcher(att: Function1[Unit, scala.xml.MetaData]): Boolean = {
var ex = false
try {
att.apply({})
} catch {
case scala.xml.MalformedAttributeException(msg) =>
println(msg)
ex = true
}
ex
}
def main(args: Array[String]) {
//val e: scala.xml.MetaData = null; //Node.NoAttributes;
//val sc: scala.xml.NamespaceBinding = null;
// ------------------------------------------ tests for class NodeSeq
/**
println("checking wellformed attributes");
{
import scala.xml.{ UnprefixedAttribute, Null }
assertTrue(catcher {x:Unit => new UnprefixedAttribute("key", "<", Null)}); // < illegal
assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "&", Null))); // & illegal
assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "a&a", Null))); // & illegal
assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "a&a;&", Null))); // 2nd &
assertFalse(catcher(x:Unit => new UnprefixedAttribute("key", "a&a; <<", Null)));
}
*/
/*
checking wellformed attributes
< not allowed in attribute value
passed ok
malformed entity reference in attribute value [&]
passed ok
malformed entity reference in attribute value [a&a]
passed ok
malformed entity reference in attribute value [a&a;&]
passed ok
passed ok
*/
println("NodeSeq")
val p = <foo>
<bar gt='ga' value="3"/>
<baz bazValue="8"/>
<bar value="5" gi='go'/>
</foo>;
val pelems_1 = for( val x <- p \ "bar"; val y <- p \ "baz" ) yield {
Text(x.attributes("value").toString + y.attributes("bazValue").toString+ "!")
};
val pelems_2 = new NodeSeq { val theSeq = List(Text("38!"),Text("58!")) };
assertSameElementsXML(pelems_1, pelems_2)
assertEqualsXML(p \\ "@bazValue", Text("8"))
val books =
<bks>
<book>
Other Scala examples (source code examples)Here is a short list of links related to this Scala xmlstuff.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.