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

Scala example source code file (xml02.scala)

This example Scala source code file (xml02.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

assert, assert, dodgynamespace, dodgynamespace, illegalargumentexception, illegalargumentexception, test, testcase, testcase, testconsolemain, utility, xmlex, xmley, xmlpat

The Scala xml02.scala source code

import testing.SUnit._

object Test extends TestConsoleMain {

  import scala.xml.{NodeSeq, Utility}
  import NodeSeq.seqToNodeSeq

  val ax = <hello foo="bar" x:foo="baz" xmlns:x="the namespace from outer space">
             <world/>
           </hello>

  val cx = <z:hello foo="bar" xmlns:z="z" x:foo="baz" xmlns:x="the namespace from outer space">
             crazy text world
           </z:hello>

  val bx = <hello foo="bar&x">

  object XmlEx extends TestCase("attributes") with Assert {

    override def runTest = {
      assertTrue("@one",       (ax \ "@foo") xml_== "bar")              // uses NodeSeq.view!
      assertTrue("@two",       (ax \ "@foo") xml_== xml.Text("bar"))    // dto.
      assertTrue("@three",     (bx \ "@foo") xml_== "bar&x")            // dto.
      assertTrue  ("@four", (bx \ "@foo") xml_sameElements List(xml.Text("bar&x")))
      assertEquals("@five",  "<hello foo=\"bar&x\">", bx.toString)
    }
  }

  object XmlEy extends TestCase("attributes with namespace") with Assert {
    override def runTest = {
      val z = ax \ "@{the namespace from outer space}foo"
      assertTrue("@six",   (ax \ "@{the namespace from outer space}foo") xml_== "baz")
      assertTrue("@eight", (cx \ "@{the namespace from outer space}foo") xml_== "baz")
 
      try {
        ax \ "@"
        assertTrue("wrong1", false)
      } catch {
        case _: IllegalArgumentException => 
      }
      try {
        ax \ "@{"
        assertTrue("wrong2", false)
      } catch {
        case _: IllegalArgumentException => 
      }
      try {
        ax \ "@{}"
        assertTrue("wrong3", false)
      } catch {
        case _: IllegalArgumentException => 
      }
 
    }
  }

  object XmlPat extends TestCase("patterns") with Assert {
    override def runTest = {
      assertTrue(<hello/> match { case  => true; case _ => false; })
      assertTrue(<x:ga xmlns:x="z"/> match { case  => true; case _ => false; });
      assertTrue(Utility.trim(cx) match { case n @ <hello>crazy text world if (n \ "@foo") xml_== "bar" => true; })
      assertTrue(Utility.trim(cx) match { case n @ <z:hello>crazy text world if (n \ "@foo") xml_== "bar" => true; })
    }
  }

  object DodgyNamespace extends TestCase("DodgyNamespace") with Assert {
    override def runTest = {
      val x = <flog xmlns:ee="http://ee.com">
      assertTrue(x.toString.matches(".*xmlns:dog=\"http://dog.com\".*"));
    }
  }
  def suite = new TestSuite(
    XmlEx,
    XmlEy,
    XmlPat,
    DodgyNamespace
  )
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

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.