//############################################################################
// XML Literals
//############################################################################
// $Id$
import scala.testing.UnitTest._ ;
import scala.xml._ ;
import scala.collection.immutable ;
object Test {
val e = immutable.TreeMap.Empty[String,String];
/* a helper function to compare up to whitespace */
def noWS(x:String):String = {
val res = new StringBuffer();
var i = 0; while( i < x.length() ) {
val c = x.charAt( i );
c match {
case ' ' | '\n' | '\t' =>
case _ => res.append( c )
}
i = i + 1;
}
res.toString();
}
object Test01Literals {
/* === tags, elements === */
assertEquals( .toString(),
.toString()); /* ws in tags */
assertEquals( .toString(),
noWS( .toString()));
val x3 =
.toString(); /* ws in element content */
assertEquals( noWS( x3 ),
Elem("hello",e,
Elem("world",e),
Elem("test",e),
Elem("mars",e)).toString() );
/* === attributes === */
val z =
Hello World
Check the scala page!
.toString();
assertEquals( noWS( z ), noWS(
Elem("html",e,
Elem("body",e,
Elem("h1",e,Text("Hello World")),
Elem("p",e,Text("Check the "),
Elem("a", e,Text("scala"))
% Pair("href","scala.epfl.ch"),
Text("page!"))
) % Pair("background","#FFFFFF")
).toString()
));
/* === attributes are Scala expr === */
val testValue = "This is a test.";
val testValue2 = 42;
val page = ;
val page2 = ;
Console.println( page.toString() );
Console.println( page2.toString() );
}
object Test02Embed {
/* === embedded Scala blocks === */
def computeDate() = {
Elem("date", e, Text("now!"));
}
/* embedding Scala strings as text and elements */
val sc = { "World" }{ Text("42") }{ computeDate() };
assertEquals( sc.child.elements.toList,
List( Text("World"), Text("42"), Elem( "date", e,Text("now!") ) ) );
assertEquals( sc.toString(),
Elem("hello",e,Text("World42"),Elem("date",e,Text("now!"))).toString() );
def foo( m:Node ):String = m match {
case => "hello node"
case => "hallo node"
case { z } => "test node:"+z
case { e1:Node }{ e2:Node }{ _* }
=> e1.toString() + e2.toString();
}
assertEquals( foo(), "hello node" );
assertEquals( foo(), "hallo node" );
assertEquals( foo(42), "test node:42" );
assertEquals( foo(
),
.toString() + .toString() );
val rows =
1.1 | 1.2 |
2.1 | 2.2 |
;
assertEquals( noWS( rows.toList.toString() ),
noWS( List(Elem("tr",e,
Elem("td",e,Text("1.1")),Elem("td",e,Text("1.2"))
),
Elem("tr",e,
Elem("td",e,Text("2.1")),Elem("td",e,Text("2.2"))
)
).toString() )
);
val rows2 =
;
val rows3 = a b c d
;
// these are not equal as comments are valid XML Info items.
assertEquals( rows2, Elem("tr",e,Comment(" an XML comment "),ProcInstr("pinotext",None),ProcInstr("pi",Some("text"))));
assertEquals( rows3, Elem("tr",e,Text("a"),Comment(" an XML comment "),Text("b"),ProcInstr("pinotext",None),Text("c"),ProcInstr("pi",Some("text")),Text("d")));
}
object Test03Servlet {
val headerMsg = Text("What follows is an example of modular formatting.");
val footerMsg = Text("Complicated layout tasks can be encapsulated and outsourced.");
/** helper function for the main page, provides header and a footer
*/
def page( ns:Seq[Node] ) = {
ModularFormatting
Welcome
{ headerMsg }
{ ns:_* }
{ footerMsg }
Bye!
}
/** applies beautify to every element in a sequence
*/
def beautify( xs:Seq[Node] ):Seq[Node] = xs.toList.map { beautify }
/** this is a recursive procedure that adds some attributes to the tree
*/
def beautify( n:Node ):Node = n match {
case { xs @ _* } | =>
{ xs:_* } |
case =>
case Elem( label, _, xs @ _* ) =>
new Elem( label, beautify( xs ):_*)
case _ => n
}
/** this function will take a node and put it in a table
*/
def format( msg:Node ):Node = {
}
/** returns a highlighted text node with the string given as arguemnt. if it
* is null, supplies a default string.
*/
def getMessage( x:String ) = {
if( x == null )
This could be YOUR message !
else
{ Text( x ) }
}
/** the entry method
*/
def doGetXML() = {
beautify( page( List( format( getMessage( "message" ) )) ));
/* page( List( format( theMessage ))); */
}
def main( args:Array[String] ) = {
val x = doGetXML();
Console.println( x );
Console.println( new PrettyPrinter( 80, 2 ).format( x ));
}
}
object Test04 {
val sequence =
Text
;
Console.println( sequence );
val onlyOne = ;
Console.println( onlyOne );
val tryBrace = Now escaped {{ braces } ;
assertEquals( tryBrace, Elem("try",e,Text("Now escaped { braces }")));
val tryBrace2 = cool ?;
assertEquals( tryBrace2.attribute("myAttrib"), "7" );
/* Scala comments are not allowed in XML literals. see neg(2) */
val zzz = /* no comment */;
assertEquals( zzz, Elem("hello", e, Text("/* no comment */")));
}
def test05main = {
val x1s = {.toString();
Console.println( x1s );
val x2 = {.toString();
Console.println( x2 );
}
def main( args:Array[String] ):Unit = {
Console.println("Test01Literals");
Test01Literals;
Console.println("Test02Embed");
Test02Embed;
Console.println("Test03Servlet");
Test03Servlet.main( args );
Console.println("Test04");
Test04;
Console.println("Test05Ref");
test05main;{
}
}
}