alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (xml03syntax.scala)

This example Scala source code file (xml03syntax.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, anyref, assert, atom, atom, list, list, nil, nil, node, option, option, some, test

The Scala xml03syntax.scala source code

import scala.testing.SUnit._
import scala.xml._

object Test extends AnyRef with Assert {

  private def handle[A](x: Node): A = {
    println(x)
    x.child(0).asInstanceOf[Atom[A]].data
  }

  def main(args: Array[String]) {
    test1
    test2
    test3
  }

  private def test1 {
    val xNull = <hello>{null} // these used to be Atom(unit), changed to empty children

    assertSameElements(xNull.child, Nil)

    val x0 = <hello>{} // these used to be Atom(unit), changed to empty children
    val x00 = <hello>{ } //  dto.

    val xa = <hello>{ "world" }


    assertSameElements(x0.child,  Nil)
    assertSameElements(x00.child, Nil)
    assertEquals(handle[String](xa), "world")

    val xb = <hello>{ 1.5 }

    assertEquals(handle[Double](xb), 1.5)

    val xc = <hello>{ 5 }

    assertEquals(handle[Int](xc), 5)

    val xd = <hello>{ true }

    assertEquals(handle[Boolean](xd), true)

    val xe = <hello>{ 5:Short }

    assertEquals(handle[Short](xe), 5:Short)

    val xf = <hello>{ val x = 27; x }

    assertEquals(handle[Int](xf), 27)

    val xg = <hello>{ List(1,2,3,4) }

    println(xg)
    for (z <- xg.child) {
      println(z.toString() + {if (z.isInstanceOf[Text]) "(is text node ' ')" else ""})
    }

    val xh = <hello>{ for(x <- List(1,2,3,4) if x % 2 == 0) yield x }

    println(xh)
    for (z <- xh.child) {
      println(z.toString() + {if (z.isInstanceOf[Text]) "(is text node ' ')" else ""})
    }
    println
  }

  /** see SVN r13821 (emir): support for <elem key={x:Option[Seq[Node]]} />,
   *  so that Options can be used for optional attributes.
   */
  private def test2 {
    val x1: Option[Seq[Node]] = Some(<b>hello)
    val n1 = <elem key={x1} />;
    println("node="+n1+", key="+n1.attribute("key"))

    val x2: Option[Seq[Node]] = None
    val n2 = <elem key={x2} />;
    println("node="+n2+", key="+n2.attribute("key"))
  }

  private def test3 {
    // this demonstrates how to handle entities
    val s = io.Source.fromString("<a> ")
    object parser extends xml.parsing.ConstructingParser(s, false /*ignore ws*/) {
      override def replacementText(entityName: String): io.Source = {
        entityName match {
          case "nbsp" => io.Source.fromString("\u0160");
          case _ => super.replacementText(entityName);
        }
      }
      nextch; // !!important, to initialize the parser
    }
    val parsed = parser.element(TopScope) // parse the source as element
    // alternatively, we could call document()
    parsed
  }

}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala xml03syntax.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.