Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Praesentation
1. XML Processing, Scalate
Seminar Softwareentwicklung mit Scala
Wintersemester 2010/11
Martin Hartmann
Friedrich Schiller Universität Jena
Prof. Dr. Wolfram Amme
Christoph Henniger
Christian Schachtzabel
24. Januar 2011
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 1/ )
2. Inhaltsverzeichnisi
1 Bearbeitung von XML
Erzeugung eines XML-Datensatz
Suchen von bestimmten Knoten
Elemente hinzufügen
Elemente ändern
zu guter Letzt
Load,Save XML
2 Scalate
Was ist Scalate
Scaml - Allgemein
Scaml - Möglichkeiten
Einbindung
3 Quellen
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 2/ )
3. Extensible Markup Language
Sprache zur hierarchischen Darstellung
von Daten
in Baumstruktur aufgebaut
perfekt zum plattformunabhängigen
Datenaustausch (z.B. übers www)
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 3/ )
4. Methoden
Als normale String-Verarbeitung
Als Objektgraph (DOM)
XML-Nodes als Events geparst (SAX)
Bearbeitung einfacher Bäume
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 4/ )
5. XML aus Strings
import scala.xml._
val phoneBook =
<phonebook>
<descr>
This is a <b>sample</b> description
</descr>
<entry>
<name>Peter Müller</name>
<phone where="work">03643/081542</phone>
</entry>
</phonebook>;
assert(phoneBook.isInstanceOf[scala.xml.Elem])
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 5/ )
6. Nodes und Elements
case class Elem(val prefix: String, // namespace prefix
val label: String, // (local) tag name
val attributes: MetaData,
val scope: NamespaceBinding, // namespace bindings
val child: Node*) extends Node
Beispiel:
val bsp = Elem(null,"Beispiel", null, TopScope, null);
⇒ <Beispiel></Beispiel>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 6/ )
7. Work with Elements
import scala.xml._
val phoneBook =
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("Peter
Müller")),
Elem(null, "phone", new
UnprefixedAttribute("where","work", Null),
TopScope,
Text("03643/082542"))
)
)
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 7/ )
8. Schleifen und Matching I
for (phone <- (phoneBook"phone")){
if ((phone@"where").text == "work")
println("At Work: " + phone.text)
}
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 8/ )
9. Schleifen und Matching II
phoneBook match {
case <phonebook>{entries @_*}</phonebook>=>
for (number @ <phone>{_*}</phone> <-entries)
println("Numbers: " + number.text)
case _ => doSmthElse
andere Möglichkeit der Selektion:
case Elem(_,"entry", _, _, _*, Elem(_, "foo", _)) => ...
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Christian Schachtzabel26
Prof. Dr. Wolfram Amme 24. Januar 2011 9/ )
10. Einträge hinzufügen I
def add( pBook: Node, newEntry: Node ): Node = pBook match {
case <phonebook>{ ch @ _*}</phonebook> =>
<phonebook>{ ch }{ newEntry}</phonebook>
}
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
10 / )
11. Einträge hinzufügen II
add( phoneBook,
<entry>
<name>Werner Schmidt</name>
<phone where="home">03643/123456</phone>
</entry>
);
Oder:
add( phoneBook,
Elem(null, "entry", Null, TopScope,
Elem(null, "name", Null, TopScope, Text("Werner
Schmidt")),
Elem(null, "phone", new
UnprefixedAttribute("where","home", Null),
TopScope,
Text("03643/123456"))
)
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
11 / )
12. Einträge ändern I
Annahme einer Funktion “copyOrChange“
phoneBook match {
case <phonebook>{ ch @ _* }</phonebook> =>
<phonebook>{ copyOrChange( ch.elements ) }</phonebook>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
12 / )
13. Einträge ändern II
def copyOrChange ( ch: Iterator[Node] )
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
13 / )
14. Einträge ändern II
def copyOrChange ( ch: Iterator[Node] )
Sprung an die entsprechende Stelle
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
13 / )
15. Einträge ändern II
def copyOrChange ( ch: Iterator[Node] )
Sprung an die entsprechende Stelle
case x @ <entry><name>{ Text(Name) }</name>{ ch1 @
_*}</entry> =>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
13 / )
16. Einträge ändern II
def copyOrChange ( ch: Iterator[Node] )
Sprung an die entsprechende Stelle
case x @ <entry><name>{ Text(Name) }</name>{ ch1 @
_*}</entry> =>
Zugriff mit
ch1.text
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
13 / )
17. Einträge ändern III
Eintrag noch nicht vorhanden:
<entry>
<name>{ Name }</name>
<phone where={ Where }>{ newPhone }</phone>
</entry>
Eintrag muss geändert werden:
<phone where={ Where}>{ newPhone }</phone>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
14 / )
18. XML Path Language
val pp = new PrettyPrinter(80, 5);
Console.println( pp.formatNodes( phoneBook "entry" ));
Ausgabe:
<entry>
<name>Peter Müller</name>
<phone where="work">03643/081542</phone>
</entry><entry>
<name>Werner Schmidt</name>
<phone where="home">03643/123456</phone>
</entry>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
15 / )
19. Fazit
Verarbeitungskriterien:
sequenzieller Zugriff
Pull Ablauf
hierarchisch strukturiert
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
16 / )
20. Load und Save XML
val x = scala.xml.XML.loadFile("myfile.xml");
scala.xml.XML.save("myfile.xml", Elem(...));
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
17 / )
21. Was ist Scalate
Template Engine zum Generieren von Text und
Markups
unterstützt verschiedene template-Formate
(Mustache, Scaml, Jade, SSP)
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
18 / )
22. Scaml Allgemein
’Dialekt’ von Haml
besser lesbar als bspw. XML
Allerdings: kein ’Begin’ und ’End’ ,Abtrennung durch einrücken
⇒ dadurch bessere Lesbarkeit
Leerzeichen und Tabs im gesamten Dokument konsistent
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
19 / )
23. Beispiel
%one
%two
%three Here Text
<one>
<two>
<three>Here Text</three>
</two>
</one>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
20 / )
24. Klassen, Attribute, IDs
%div#things
%span#spicy Chili Con Carne
%p.beans{ :food => "true" } Something about Beans
%h1.class.otherclass#more Something more
<div id="things">
<span id="spicy">Chili Con Carne</span>
<p class="beans" food="true">Something about Beans</p>
<h1 id="more" class="class otherclass">Something more</h1>
</div>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
21 / )
25. Scaml - Doctype
!!! XML
%html
%head
%title Myspace
%body
%h1 I am the international space station
<?xml version="1.0" encoding="utf-8" ?>
<html>
<head>
<title>Myspace</title>
</head>
<body>
<h1>I am the international space station</h1>
</body>
</html>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
22 / )
26. Scala-Funktionen und Scaml
object Cheese {
def foo(productId: Int) =
<a href={"/products/" + productId} title="Product link">
My Product
</a>
}
Einbindung in Scaml:
- import Cheese._
= foo(123)
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
23 / )
27. Capture-Funktion
import org.fusesource.scalate.RenderContext.capture
object Cheese {
def foo(productId: Int)(body: => Unit) =
<a href={"/products/" + productId} title="Product
link">capture(body)</a>
}
-@ val id: Int = 123
- import Cheese._
= foo(id)
product #{id}
Ergebnis:
<a href="/products/123" title="Product link">product 123</a>
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
24 / )
28. Quellen
Dean Wampler, Alex Payne
Programming Scala
sites.google.com/site/burakemir/scalaxbook.docbk.html
http://scalate.fusesource.org/documentation/user-guide.html
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
25 / )
29. Vielen Dank für
ihre Aufmerksamkeit
M. Hartmann (Friedrich Schiller Universität Jena XML Processing, Scalate Christoph Henniger Januar 2011
Prof. Dr. Wolfram Amme 24. Christian Schachtzabel26
26 / )