22
20
def main(args: Array[String]) {
24
//val e: scala.xml.MetaData = null; //Node.NoAttributes;
25
//val sc: scala.xml.NamespaceBinding = null;
27
// ------------------------------------------ tests for class NodeSeq
30
println("checking wellformed attributes");
32
import scala.xml.{ UnprefixedAttribute, Null }
33
assertTrue(catcher {x:Unit => new UnprefixedAttribute("key", "<", Null)}); // < illegal
34
assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "&", Null))); // & illegal
35
assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "a&a", Null))); // & illegal
36
assertTrue(catcher(x:Unit => new UnprefixedAttribute("key", "a&a;&", Null))); // 2nd &
38
assertFalse(catcher(x:Unit => new UnprefixedAttribute("key", "a&a; <<", Null)));
43
checking wellformed attributes
44
< not allowed in attribute value
46
malformed entity reference in attribute value [&]
48
malformed entity reference in attribute value [a&a]
50
malformed entity reference in attribute value [a&a;&]
58
<bar gt='ga' value="3"/>
60
<bar value="5" gi='go'/>
63
val pelems_1 = for( val x <- p \ "bar"; val y <- p \ "baz" ) yield {
25
<bar gt='ga' value="3"/>
27
<bar value="5" gi='go'/>
30
val pelems_1 = for (x <- p \ "bar"; y <- p \ "baz" ) yield {
64
31
Text(x.attributes("value").toString + y.attributes("bazValue").toString+ "!")
66
33
val pelems_2 = new NodeSeq { val theSeq = List(Text("38!"),Text("58!")) };
67
assertSameElementsXML(pelems_1, pelems_2)
34
assert(pelems_1 sameElements pelems_2)
69
assertEqualsXML(p \\ "@bazValue", Text("8"))
36
assert(Text("8") sameElements (p \\ "@bazValue"))
73
<book><title>Blabla</title></book>
74
<book><title>Blubabla</title></book>
75
<book><title>Baaaaaaalabla</title></book>
40
<book><title>Blabla</title></book>
41
<book><title>Blubabla</title></book>
42
<book><title>Baaaaaaalabla</title></book>
80
<entry><title>Blabla</title>
85
<entry><title>Blubabla</title>
90
<entry><title>Blubabla</title>
47
<entry><title>Blabla</title>
52
<entry><title>Blubabla</title>
57
<entry><title>Blubabla</title>
97
println( new scala.xml.PrettyPrinter(80, 5).formatNodes (
98
for (t <- books \\ "title";
99
r <- reviews \\ "entry"
100
if (r \ "title") xml_== t) yield
64
println( new scala.xml.PrettyPrinter(80, 5).formatNodes (
65
for (t <- books \\ "title";
66
r <- reviews \\ "entry"
67
if (r \ "title") xml_== t) yield
109
for (t @ <book><title>Blabla</title></book> <- new NodeSeq { val theSeq = books.child }.toList)
115
This is the <b>phonebook</b> of the
116
<a href="http://acme.org">ACME</a> corporation.
120
<phone where="work"> +41 21 693 68 67</phone>
121
<phone where="mobile">+41 79 602 23 23</phone>
76
for (t @ <book><title>Blabla</title></book> <- new NodeSeq { val theSeq = books.child }.toList)
82
This is the <b>phonebook</b> of the
83
<a href="http://acme.org">ACME</a> corporation.
87
<phone where="work"> +41 21 693 68 67</phone>
88
<phone where="mobile">+41 79 602 23 23</phone>
129
This is the <b>addressbook</b> of the
130
<a href="http://acme.org">ACME</a> corporation.
134
<street> Elm Street</street>
135
<city>Dolphin City</city>
96
This is the <b>addressbook</b> of the
97
<a href="http://acme.org">ACME</a> corporation.
101
<street> Elm Street</street>
102
<city>Dolphin City</city>
139
println( new scala.xml.PrettyPrinter(80, 5).formatNodes (
140
for (t <- addrBook \\ "entry";
141
r <- phoneBook \\ "entry"
142
if (t \ "name") xml_== (r \ "name")) yield
106
println( new scala.xml.PrettyPrinter(80, 5).formatNodes (
107
for (t <- addrBook \\ "entry";
108
r <- phoneBook \\ "entry"
109
if (t \ "name") xml_== (r \ "name")) yield
152
println("namespaces")
153
val cuckoo = <cuckoo xmlns="http://cuckoo.com">
119
println("namespaces")
120
val cuckoo = <cuckoo xmlns="http://cuckoo.com">
157
assertEquals(cuckoo.namespace, "http://cuckoo.com")
158
for (n <- cuckoo \ "_" ) {
160
//println("n.prefix = "+n.prefix);
161
//.println("n.scope = "+n.scope);
162
assertEquals( n.namespace, "http://cuckoo.com")
165
println("validation - elements")
166
val vtor = new scala.xml.dtd.ElementValidator();
168
import scala.xml.dtd.ELEMENTS
169
import scala.xml.dtd.ContentModel._
170
vtor.setContentModel(
173
Letter(ElemName("bar")),
174
Star(Letter(ElemName("baz"))) )));
177
assertEquals(vtor( <foo><bar/><baz/><baz/></foo> ), true);
179
import scala.xml.dtd.MIXED
180
import scala.xml.dtd.ContentModel._
182
vtor.setContentModel(
184
Alt(Letter(ElemName("bar")),
185
Letter(ElemName("baz")),
186
Letter(ElemName("bal")))));
189
assertEquals(vtor(<foo><bar/><baz/><baz/></foo> ), true)
190
assertEquals(vtor(<foo>ab<bar/>cd<baz/>ed<baz/>gh</foo> ), true)
191
assertEquals(vtor(<foo> <ugha/> <bugha/> </foo> ), false)
193
println("validation - attributes")
194
vtor.setContentModel(null)
195
vtor.setMetaData(List())
196
assertEquals(vtor( <foo bar="hello"/> ), false)
199
import scala.xml.dtd._
200
vtor setMetaData List(AttrDecl("bar", "CDATA", IMPLIED))
202
assertEquals(vtor(<foo href="http://foo.com" bar="hello"/>), false)
203
assertEquals(vtor(<foo bar="hello"/>), true)
206
import scala.xml.dtd._
207
vtor.setMetaData(List(AttrDecl("bar","CDATA",REQUIRED)))
209
assertEquals( vtor( <foo href="http://foo.com" /> ), false )
210
assertEquals( vtor( <foo bar="http://foo.com" /> ), true )
124
assert(cuckoo.namespace == "http://cuckoo.com")
125
for (n <- cuckoo \ "_" ) {
127
//println("n.prefix = "+n.prefix);
128
//.println("n.scope = "+n.scope);
129
assert( n.namespace == "http://cuckoo.com")
132
println("validation - elements")
133
val vtor = new scala.xml.dtd.ElementValidator();
135
import scala.xml.dtd.ELEMENTS
136
import scala.xml.dtd.ContentModel._
137
vtor.setContentModel(
140
Letter(ElemName("bar")),
141
Star(Letter(ElemName("baz"))) )));
144
assert(vtor( <foo><bar/><baz/><baz/></foo> ))
147
import scala.xml.dtd.MIXED
148
import scala.xml.dtd.ContentModel._
150
vtor.setContentModel(
152
Alt(Letter(ElemName("bar")),
153
Letter(ElemName("baz")),
154
Letter(ElemName("bal")))));
157
assert(vtor(<foo><bar/><baz/><baz/></foo> ))
158
assert(vtor(<foo>ab<bar/>cd<baz/>ed<baz/>gh</foo> ))
159
assert(!vtor(<foo> <ugha/> <bugha/> </foo> ))
161
println("validation - attributes")
162
vtor.setContentModel(null)
163
vtor.setMetaData(List())
164
assert(!vtor( <foo bar="hello"/> ))
167
import scala.xml.dtd._
168
vtor setMetaData List(AttrDecl("bar", "CDATA", IMPLIED))
170
assert(!vtor(<foo href="http://foo.com" bar="hello"/>))
171
assert(vtor(<foo bar="hello"/>))
174
import scala.xml.dtd._
175
vtor.setMetaData(List(AttrDecl("bar","CDATA",REQUIRED)))
177
assert(!vtor( <foo href="http://foo.com" /> ))
178
assert( vtor( <foo bar="http://foo.com" /> ))