summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2006-10-14 13:31:12 +0000
committerBurak Emir <emir@epfl.ch>2006-10-14 13:31:12 +0000
commit470f990722777041a475de2e5cf02ca4504a2237 (patch)
tree4527d5a708f563c53ffcc1e37f908c76062cd201 /docs
parentf1208fc000438bb03bbb18011659dd4ca25d1127 (diff)
downloadscala-470f990722777041a475de2e5cf02ca4504a2237.tar.gz
scala-470f990722777041a475de2e5cf02ca4504a2237.tar.bz2
scala-470f990722777041a475de2e5cf02ca4504a2237.zip
xml improvements for 2.2.1 (see changes)
Diffstat (limited to 'docs')
-rw-r--r--docs/examples/xml/phonebook/embeddedBook.scala26
-rw-r--r--docs/examples/xml/phonebook/phonebook.scala38
-rw-r--r--docs/examples/xml/phonebook/phonebook1.scala21
-rw-r--r--docs/examples/xml/phonebook/phonebook2.scala25
-rw-r--r--docs/examples/xml/phonebook/phonebook3.scala96
-rw-r--r--docs/examples/xml/phonebook/verboseBook.scala24
6 files changed, 230 insertions, 0 deletions
diff --git a/docs/examples/xml/phonebook/embeddedBook.scala b/docs/examples/xml/phonebook/embeddedBook.scala
new file mode 100644
index 0000000000..8ea9628212
--- /dev/null
+++ b/docs/examples/xml/phonebook/embeddedBook.scala
@@ -0,0 +1,26 @@
+/* examples/phonebook/embeddedBook.scala */
+package phonebook
+
+object embeddedBook {
+
+ val company = <a href="http://acme.org">ACME</a>
+ val first = "Burak"
+ val last = "Emir"
+ val location = "work"
+
+ val embBook =
+ <phonebook>
+ <descr>
+ This is the <b>phonebook</b> of the
+ {company} corporation.
+ </descr>
+ <entry>
+ <name>{ first+" "+last }</name>
+ <phone where={ location }>+41 21 693 68 {val x = 60 + 7; x}</phone>
+ </entry>
+ </phonebook>;
+
+ def main(args: Array[String]) =
+ Console.println( embBook )
+
+}
diff --git a/docs/examples/xml/phonebook/phonebook.scala b/docs/examples/xml/phonebook/phonebook.scala
new file mode 100644
index 0000000000..4813c2d20d
--- /dev/null
+++ b/docs/examples/xml/phonebook/phonebook.scala
@@ -0,0 +1,38 @@
+package phonebook ;
+
+object phonebook {
+
+ val labPhoneBook =
+ <phonebook>
+ <descr>
+ This is the <b>phonebook</b> of the
+ <a href="http://acme.org">ACME</a> corporation.
+ </descr>
+ <entry>
+ <name>Burak</name>
+ <phone where="work"> +41 21 693 68 67</phone>
+ <phone where="mobile">+41 79 602 23 23</phone>
+ </entry>
+ </phonebook>;
+
+ Console.println( labPhoneBook );
+
+ // XML is immutable - adding an element
+
+ import scala.xml.{ Node, Text };
+
+ def add( phonebook:Node, newEntry:Node ):Node = phonebook match {
+ case <phonebook>{ ch @ _* }</phonebook> =>
+ <phonebook>{ ch }{ newEntry }</phonebook>
+ }
+
+ val pb2 =
+ add( labPhoneBook,
+ <entry>
+ <name>Kim</name>
+ <phone where="work"> +41 21 111 11 11</phone>
+ </entry> );
+
+ def main(args:Array[String]) = Console.println( pb2 );
+
+}
diff --git a/docs/examples/xml/phonebook/phonebook1.scala b/docs/examples/xml/phonebook/phonebook1.scala
new file mode 100644
index 0000000000..3a7a165202
--- /dev/null
+++ b/docs/examples/xml/phonebook/phonebook1.scala
@@ -0,0 +1,21 @@
+/* examples/phonebook/phonebook1.scala */
+package phonebook
+
+object phonebook1 {
+
+ val labPhoneBook =
+ <phonebook>
+ <descr>
+ This is the <b>phonebook</b> of the
+ <a href="http://acme.org">ACME</a> corporation.
+ </descr>
+ <entry>
+ <name>Burak Emir</name>
+ <phone where="work">+41 21 693 68 67</phone>
+ </entry>
+ </phonebook>;
+
+ def main(args: Array[String]) =
+ Console.println( labPhoneBook )
+
+}
diff --git a/docs/examples/xml/phonebook/phonebook2.scala b/docs/examples/xml/phonebook/phonebook2.scala
new file mode 100644
index 0000000000..ba50379369
--- /dev/null
+++ b/docs/examples/xml/phonebook/phonebook2.scala
@@ -0,0 +1,25 @@
+/* examples/xml/phonebook/phonebook2.scala */
+package phonebook;
+
+object phonebook2 {
+
+ import scala.xml.Node
+
+ /** adds an entry to a phonebook */
+ def add( p: Node, newEntry: Node ): Node = p match {
+
+ case <phonebook>{ ch @ _* }</phonebook> =>
+
+ <phonebook>{ ch }{ newEntry }</phonebook>
+ }
+
+ val pb2 =
+ add( phonebook1.labPhoneBook,
+ <entry>
+ <name>Kim</name>
+ <phone where="work">+41 21 111 11 11</phone>
+ </entry> );
+
+ def main( args: Array[String] ) =
+ Console.println( pb2 )
+}
diff --git a/docs/examples/xml/phonebook/phonebook3.scala b/docs/examples/xml/phonebook/phonebook3.scala
new file mode 100644
index 0000000000..ec49d450f4
--- /dev/null
+++ b/docs/examples/xml/phonebook/phonebook3.scala
@@ -0,0 +1,96 @@
+/* examples/xml/phonebook/phonebook3.scala */
+package phonebook
+
+object phonebook3 {
+
+ import scala.xml.{Elem, Node, Text}
+ import scala.xml.Utility.view
+ import scala.xml.PrettyPrinter
+ import Node.NoAttributes
+
+ /* finds entry for Name, Where, changes it, or adds if not present */
+ def change ( phonebook: Node, Name: String, Where: String, newPhone: String ) = {
+
+ /** returns true if this element's first child is the right 'name'
+ * x is treated a if it was a singleton sequence here.
+ */
+ def hasName ( x: Seq[Node] ) = x(0).child.elements.next match {
+ case <name>{ Text(Name) }</name> => true
+ case _ => false
+ }
+
+ /** returns true if this element has the right 'where' attribute
+ * y is treated a if it was a singleton sequence here.
+ * n.attribute(s) is the same as n.attributes.get(s)
+ */
+ def hasWhere ( y: Node ) = y.attribute("where") match {
+ case Some(Text(Where)) => true
+ case None => false
+ }
+
+ /** returns true if this element has the right 'where' attribute
+ * the apply method of MetaData (n.attributes) returns raw a
+ * sequence of nodes.
+ */
+ def hasWhere2 ( y: Node ) = y.attributes("where") match {
+ case null => false
+ case Text(Where) => true
+ case _ => false
+ }
+
+
+ /** walks through tree, returns changed/copied updated tree */
+ def copyOrChange ( ch: Iterator[Node] ):List[Node] = {
+ for( val c <- ch ) yield c match {
+
+ case x @ <entry>{ ch1 @ _* }</entry> if hasName( x ) =>
+ val it = ch1.elements;
+ val nameElem:Seq[Node] = it.next; // grab 'name' element
+ val ch2 = nameElem concat copyOrChange( it ); // concat with updated seq
+
+ if( ch1 == ch2 ) // not present: add as first entry
+
+ <entry>
+ <name>{ Name }</name>
+ <phone where={ Where }>{ newPhone }</phone>
+ { ch1 }
+ </entry>
+
+ else // was present and changed
+
+ <entry>
+ { ch2 }
+ </entry>
+
+ case y @ <phone>{ _* }</phone> if hasWhere( y ) =>
+ Console.println("c = "+c);
+ <phone where={ Where }>{ newPhone }</phone>
+
+ case _ =>
+ Console.println("c = "+c);
+ Console.println("c.attributes= "+c.attributes);
+ c
+
+ }
+ }.toList ; // for ... yield ... returns Iterator, convert to list
+
+ // decompose phonebook, apply updates
+ phonebook match {
+ case <phonebook>{ ch @ _* }</phonebook> =>
+ <phonebook>{ copyOrChange( ch.elements ) }</phonebook>
+ }
+
+ }
+
+ val pb2 =
+ change( phonebook1.labPhoneBook, "John", "work", "+41 55 555 55 55" );
+
+ val pp = new PrettyPrinter( 80, 5 );
+
+ def main( args:Array[String] ) = {
+ Console.println("---before---");
+ Console.println( pp.format( phonebook1.labPhoneBook ));
+ Console.println("---after---");
+ Console.println( pp.format( pb2 ));
+ }
+}
diff --git a/docs/examples/xml/phonebook/verboseBook.scala b/docs/examples/xml/phonebook/verboseBook.scala
new file mode 100644
index 0000000000..611cf5370e
--- /dev/null
+++ b/docs/examples/xml/phonebook/verboseBook.scala
@@ -0,0 +1,24 @@
+/* examples/xml/phonebook/verboseBook.scala */
+package phonebook
+
+object verboseBook {
+
+ import scala.xml.{ UnprefixedAttribute, Elem, Node, Null, Text, TopScope }
+
+ val pbookVerbose =
+ Elem(null, "phonebook", Null, TopScope,
+ Elem(null, "descr", Null, TopScope,
+ Text("This is a "),
+ Elem(null, "b", Null, TopScope, Text("sample")),
+ Text("description")
+ ),
+ Elem(null, "entry", Null, TopScope,
+ Elem(null, "name", Null, TopScope, Text("Burak Emir")),
+ Elem(null, "phone", new UnprefixedAttribute("where","work", Null), TopScope,
+ Text("+41 21 693 68 67"))
+ )
+ )
+
+ def main(args: Array[String]) =
+ Console.println( pbookVerbose )
+}