summaryrefslogtreecommitdiff
path: root/test/files/run/xmlParsing_servlet.scala
blob: 7bd622b0a6907de910c06e49ac6bc5dae60f0538 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import scala.xml._ ;

object Test {

  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] ) = {
    <html>
      <head>
        <title>ModularFormatting</title>
      </head>
      <body>
        <h2>Welcome</h2>
        <p>
          { headerMsg }
        </p>
        <p>
          { ns:_* }
        </p>
        <hr/>
        <p>
          { footerMsg }
        </p>
        <h2>Bye!</h2>
      </body>
    </html>
  }

  /** 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 <td>{ xs @ _* }</td> =>
          <td bgcolor="#AAAAFF" color="#222255">{ xs:_* }</td>

    case <table>{ xs @ _* }</table> =>
          <table align="center">{ beautify( xs ):_* }</table>

    case Elem( label, xs @ _* ) =>
          Elem( label, beautify( xs ):_*)

    case _ => n
  }

  /** this function will take a node and put it in a table
   */
  def format( msg:Node ):Node = {
    <table>
      <tr>
        <td>{ msg }</td>
      </tr>
    </table>
  }

  /** 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 )
      <h1> This could be YOUR message ! </h1>
    else
      <h1> { Text( x ) } </h1>
  }

  /** the entry method
   */
  def doGetXML() = {
    beautify( page( List( format( getMessage( "message" ) )) ));
    /* page( List( format( theMessage ))); */

  }

  def main( args:Array[String] ) = {
    Console.println( doGetXML() );
  }

}