~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to test/files/jvm/xmlstuff.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import java.io.StringReader
2
2
import org.xml.sax.InputSource
3
 
 
4
 
import scala.testing.SUnit.Assert
5
3
import scala.xml.{Node, NodeSeq, Elem, Text, XML}
6
4
 
7
 
object Test extends AnyRef with Assert {
 
5
object Test {
8
6
 
9
7
  /** returns true if exception was thrown */
10
8
  def catcher(att: Function1[Unit, scala.xml.MetaData]): Boolean = {
21
19
 
22
20
  def main(args: Array[String]) {
23
21
 
24
 
  //val e:  scala.xml.MetaData         = null; //Node.NoAttributes;
25
 
  //val sc: scala.xml.NamespaceBinding = null;
26
 
 
27
 
    // ------------------------------------------ tests for class NodeSeq
28
 
 
29
 
    /**
30
 
    println("checking wellformed attributes");
31
 
    {
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 &
37
 
 
38
 
    assertFalse(catcher(x:Unit => new UnprefixedAttribute("key", "a&a; &lt;&lt;", Null)));
39
 
    }
40
 
*/
41
 
 
42
 
/*
43
 
checking wellformed attributes
44
 
< not allowed in attribute value
45
 
passed ok
46
 
malformed entity reference in attribute value [&]
47
 
passed ok
48
 
malformed entity reference in attribute value [a&a]
49
 
passed ok
50
 
malformed entity reference in attribute value [a&a;&]
51
 
passed ok
52
 
passed ok
53
 
*/
54
 
 
55
 
  println("NodeSeq")
 
22
    println("NodeSeq")
56
23
 
57
24
    val p = <foo>
58
 
              <bar gt='ga' value="3"/>
59
 
              <baz bazValue="8"/>
60
 
              <bar value="5" gi='go'/>
61
 
            </foo>;
62
 
  
63
 
    val pelems_1 = for( val x <- p \ "bar"; val y <- p \ "baz" ) yield {
 
25
    <bar gt='ga' value="3"/>
 
26
    <baz bazValue="8"/>
 
27
    <bar value="5" gi='go'/>
 
28
    </foo>;
 
29
    
 
30
    val pelems_1 = for (x <- p \ "bar"; y <- p \ "baz" ) yield {
64
31
      Text(x.attributes("value").toString + y.attributes("bazValue").toString+ "!")
65
32
    };
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)
68
35
 
69
 
    assertEqualsXML(p \\ "@bazValue", Text("8"))
 
36
    assert(Text("8") sameElements (p \\ "@bazValue"))
70
37
 
71
38
    val books = 
72
 
    <bks>
73
 
      <book><title>Blabla</title></book>
74
 
      <book><title>Blubabla</title></book>
75
 
      <book><title>Baaaaaaalabla</title></book>
 
39
      <bks>
 
40
    <book><title>Blabla</title></book>
 
41
    <book><title>Blubabla</title></book>
 
42
    <book><title>Baaaaaaalabla</title></book>
76
43
    </bks>;
77
44
 
78
 
  val reviews = 
79
 
    <reviews>
80
 
      <entry><title>Blabla</title>
81
 
      <remarks>
82
 
        Hallo Welt.
83
 
      </remarks>
84
 
    </entry>
85
 
      <entry><title>Blubabla</title>
86
 
      <remarks>
87
 
        Hello Blu
88
 
      </remarks>
89
 
  </entry>
90
 
      <entry><title>Blubabla</title>
91
 
      <remarks>
92
 
        rem 2
93
 
      </remarks>
94
 
  </entry>
 
45
    val reviews = 
 
46
      <reviews>
 
47
    <entry><title>Blabla</title>
 
48
    <remarks>
 
49
    Hallo Welt.
 
50
    </remarks>
 
51
    </entry>
 
52
    <entry><title>Blubabla</title>
 
53
    <remarks>
 
54
    Hello Blu
 
55
    </remarks>
 
56
    </entry>
 
57
    <entry><title>Blubabla</title>
 
58
    <remarks>
 
59
    rem 2
 
60
    </remarks>
 
61
    </entry>
95
62
    </reviews>;
96
63
 
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
101
 
          <result>
102
 
    { t }
103
 
    { r \ "remarks" }
104
 
    </result>
105
 
  ));
 
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
 
68
             <result>
 
69
      { t }
 
70
      { r \ "remarks" }
 
71
      </result>
 
72
    ));
106
73
 
107
 
  // example
108
 
  println( 
109
 
    for (t @ <book><title>Blabla</title></book> <- new NodeSeq { val theSeq = books.child }.toList)
110
 
    yield t
111
 
  );
112
 
val phoneBook =  
113
 
  <phonebook>
114
 
      <descr>
115
 
        This is the <b>phonebook</b> of the 
116
 
        <a href="http://acme.org">ACME</a> corporation.
117
 
      </descr>
118
 
      <entry>
119
 
        <name>John</name> 
120
 
        <phone where="work">  +41 21 693 68 67</phone>
121
 
        <phone where="mobile">+41 79 602 23 23</phone>
122
 
      </entry>
 
74
    // example
 
75
    println( 
 
76
      for (t @ <book><title>Blabla</title></book> <- new NodeSeq { val theSeq = books.child }.toList)
 
77
      yield t
 
78
    );
 
79
    val phoneBook =  
 
80
      <phonebook>
 
81
    <descr>
 
82
    This is the <b>phonebook</b> of the 
 
83
    <a href="http://acme.org">ACME</a> corporation.
 
84
    </descr>
 
85
    <entry>
 
86
    <name>John</name> 
 
87
    <phone where="work">  +41 21 693 68 67</phone>
 
88
    <phone where="mobile">+41 79 602 23 23</phone>
 
89
    </entry>
123
90
    </phonebook>;
124
91
 
125
92
 
126
 
val addrBook =  
127
 
  <addrbook>
128
 
      <descr>
129
 
        This is the <b>addressbook</b> of the 
130
 
        <a href="http://acme.org">ACME</a> corporation.
131
 
      </descr>
132
 
      <entry>
133
 
        <name>John</name> 
134
 
        <street> Elm Street</street>
135
 
        <city>Dolphin City</city>
136
 
      </entry>
 
93
    val addrBook =  
 
94
      <addrbook>
 
95
    <descr>
 
96
    This is the <b>addressbook</b> of the 
 
97
    <a href="http://acme.org">ACME</a> corporation.
 
98
    </descr>
 
99
    <entry>
 
100
    <name>John</name> 
 
101
    <street> Elm Street</street>
 
102
    <city>Dolphin City</city>
 
103
    </entry>
137
104
    </addrbook>;
138
105
 
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
143
 
          <result>
144
 
    { t.child }
145
 
    { r \ "phone" }
146
 
    </result>
147
 
  ));
 
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
 
110
             <result>
 
111
      { t.child }
 
112
      { r \ "phone" }
 
113
      </result>
 
114
    ));
148
115
 
149
 
  
150
 
  /* namespaces */
151
 
   // begin tmp
152
 
  println("namespaces")
153
 
  val cuckoo = <cuckoo xmlns="http://cuckoo.com">
 
116
    
 
117
    /* namespaces */
 
118
    // begin tmp
 
119
    println("namespaces")
 
120
    val cuckoo = <cuckoo xmlns="http://cuckoo.com">
154
121
    <foo/>
155
122
    <bar/>
156
 
  </cuckoo>;
157
 
  assertEquals(cuckoo.namespace, "http://cuckoo.com")
158
 
  for (n <- cuckoo \ "_" ) {
159
 
    //println("n = "+n);
160
 
    //println("n.prefix = "+n.prefix);
161
 
    //.println("n.scope = "+n.scope);
162
 
    assertEquals( n.namespace, "http://cuckoo.com")
163
 
  }
164
 
 
165
 
  println("validation - elements")
166
 
  val vtor = new scala.xml.dtd.ElementValidator();
167
 
  {
168
 
    import scala.xml.dtd.ELEMENTS
169
 
    import scala.xml.dtd.ContentModel._
170
 
    vtor.setContentModel(
171
 
          ELEMENTS( 
172
 
            Sequ(
173
 
                  Letter(ElemName("bar")), 
174
 
                  Star(Letter(ElemName("baz"))) )));
175
 
 
176
 
  }
177
 
  assertEquals(vtor( <foo><bar/><baz/><baz/></foo> ), true);
178
 
  {
179
 
    import scala.xml.dtd.MIXED
180
 
    import scala.xml.dtd.ContentModel._
181
 
    
182
 
    vtor.setContentModel(
183
 
      MIXED(
184
 
        Alt(Letter(ElemName("bar")), 
185
 
            Letter(ElemName("baz")), 
186
 
            Letter(ElemName("bal")))));
187
 
  }
188
 
 
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)
192
 
 
193
 
  println("validation - attributes")
194
 
  vtor.setContentModel(null)
195
 
  vtor.setMetaData(List())
196
 
  assertEquals(vtor( <foo bar="hello"/> ), false)
197
 
  
198
 
  { 
199
 
    import scala.xml.dtd._ 
200
 
    vtor setMetaData List(AttrDecl("bar", "CDATA", IMPLIED))
201
 
  }
202
 
  assertEquals(vtor(<foo href="http://foo.com" bar="hello"/>), false)
203
 
  assertEquals(vtor(<foo bar="hello"/>), true)
204
 
 
205
 
  { 
206
 
    import scala.xml.dtd._
207
 
    vtor.setMetaData(List(AttrDecl("bar","CDATA",REQUIRED)))
208
 
  }
209
 
  assertEquals( vtor( <foo href="http://foo.com" /> ), false )
210
 
  assertEquals( vtor( <foo bar="http://foo.com" /> ), true )
211
 
  
 
123
    </cuckoo>;
 
124
    assert(cuckoo.namespace == "http://cuckoo.com")
 
125
    for (n <- cuckoo \ "_" ) {
 
126
      //println("n = "+n);
 
127
      //println("n.prefix = "+n.prefix);
 
128
      //.println("n.scope = "+n.scope);
 
129
      assert( n.namespace == "http://cuckoo.com")
 
130
    }
 
131
 
 
132
    println("validation - elements")
 
133
    val vtor = new scala.xml.dtd.ElementValidator();
 
134
    {
 
135
      import scala.xml.dtd.ELEMENTS
 
136
      import scala.xml.dtd.ContentModel._
 
137
      vtor.setContentModel(
 
138
        ELEMENTS( 
 
139
          Sequ(
 
140
            Letter(ElemName("bar")), 
 
141
            Star(Letter(ElemName("baz"))) )));
 
142
 
 
143
    }
 
144
    assert(vtor( <foo><bar/><baz/><baz/></foo> ))
 
145
 
 
146
    {
 
147
      import scala.xml.dtd.MIXED
 
148
      import scala.xml.dtd.ContentModel._
 
149
      
 
150
      vtor.setContentModel(
 
151
        MIXED(
 
152
          Alt(Letter(ElemName("bar")), 
 
153
              Letter(ElemName("baz")), 
 
154
              Letter(ElemName("bal")))));
 
155
    }
 
156
 
 
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> ))
 
160
 
 
161
    println("validation - attributes")
 
162
    vtor.setContentModel(null)
 
163
    vtor.setMetaData(List())
 
164
    assert(!vtor( <foo bar="hello"/> ))
 
165
    
 
166
    { 
 
167
      import scala.xml.dtd._ 
 
168
      vtor setMetaData List(AttrDecl("bar", "CDATA", IMPLIED))
 
169
    }
 
170
    assert(!vtor(<foo href="http://foo.com" bar="hello"/>))
 
171
    assert(vtor(<foo bar="hello"/>))
 
172
 
 
173
    { 
 
174
      import scala.xml.dtd._
 
175
      vtor.setMetaData(List(AttrDecl("bar","CDATA",REQUIRED)))
 
176
    }
 
177
    assert(!vtor( <foo href="http://foo.com" /> ))
 
178
    assert( vtor( <foo bar="http://foo.com" /> ))
 
179
    
212
180
  }
213
181
}