Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
scala, xml and gae
     mark@ryall.name
xml literals
val document =
<root>
  <child>
    <grandchild an_attribute=quot;value1quot; />
    <grandchild an_attribute=quot;value2q...
val document =
scala.xml.Elem(null, quot;rootquot;, scala.xml.Null, scala.xml.TopScope,
  scala.xml.Elem(null, quot;childq...
extraction operators
// immediate descendant of document called 'child'
document  quot;childquot;

// any descendant of document called 'anothe...
pattern matching
• match single node with {variable}
• match node sequence with {variable @ _*}
• cannot match attribute directly
val a = document match {
  case Elem(_, quot;rootquot;, _, _, _*) => true
  case _ => null
}
val a = document match {
  case <root>{ _* }</root> => true
  case _ => false
}
def process(node: scala.xml.Node): Unit = {
  println(node)
  node match {
    case <root>{contents @ _* }</root> =>
     ...
Concatenation


def add(p: Node, newEntry: Node ): Node =
  p match {
  case <div>{ ch @ _* }</div> =>
    <div>{ ch }{ ne...
Extracting attributes



document match {
  case n @ <grandchild>{ _* }</grandchild> =>
    println(n.attribute(quot;an_at...
persisting and restoring
scala.xml.XML.saveFull(quot;filename.xmlquot;, document,
  quot;UTF-8quot;, true, null)

val document2 = scala.xml.XML.loa...
stream parsing
val src =
  scala.io.Source.fromString(quot;<root><child></child></root>quot;)
val er = new scala.xml.pull.XMLEventReader(...
building xml
var variable1 = quot;value1quot;

val variable2 = <another_grandchild an_attribute=quot;value2quot; />

var document =
  <...
google app engine
• ‘cloud’ web hosting environment on
  google’s infrastructure

• uses non relational ‘bigtable’ for data
  persistance

•...
• working example:
• code: http://bitbucket.org/markryall/scalagae
• live: http://pairs.appspot.com
• use latest datanucle...
basics
class Servlet extends HttpServlet {
  override def doGet(request: HttpServletRequest, response: HttpServletResponse):
    ...
authentication
var userService = UserServiceFactory.getUserService();
var user = userService.getCurrentUser();

if (user == null) {
  res...
persistence
@PersistenceCapable {val identityType = IdentityType.APPLICATION}
class Person(@Persistent var firstName: String,@Persiste...
Upcoming SlideShare
Loading in …5
×

Scala, XML and GAE

3,860 views

Published on

Presentation at first Melbourne Scala User Group 27th April, 2009

Published in: Technology, News & Politics

Scala, XML and GAE

  1. 1. scala, xml and gae mark@ryall.name
  2. 2. xml literals
  3. 3. val document = <root> <child> <grandchild an_attribute=quot;value1quot; /> <grandchild an_attribute=quot;value2quot; /> </child> </root>
  4. 4. val document = scala.xml.Elem(null, quot;rootquot;, scala.xml.Null, scala.xml.TopScope, scala.xml.Elem(null, quot;childquot;, scala.xml.Null, scala.xml.TopScope, scala.xml.Elem(null, quot;grandchildquot;, new scala.xml.UnprefixedAttribute(quot;an_attributequot;,quot;value1quot;, scala.xml.Null), scala.xml.TopScope, scala.xml.Text(quot;content1quot;) ), scala.xml.Elem(null, quot;grandchildquot;, new scala.xml.UnprefixedAttribute(quot;an_attributequot;,quot;value2quot;, scala.xml.Null), scala.xml.TopScope, scala.xml.Text(quot;content2quot;) ) ) )
  5. 5. extraction operators
  6. 6. // immediate descendant of document called 'child' document quot;childquot; // any descendant of document called 'another_grandchild' document quot;another_grandchildquot; // any descendant attribute called 'an_attribute' document quot;@an_attributequot;
  7. 7. pattern matching
  8. 8. • match single node with {variable} • match node sequence with {variable @ _*} • cannot match attribute directly
  9. 9. val a = document match { case Elem(_, quot;rootquot;, _, _, _*) => true case _ => null }
  10. 10. val a = document match { case <root>{ _* }</root> => true case _ => false }
  11. 11. def process(node: scala.xml.Node): Unit = { println(node) node match { case <root>{contents @ _* }</root> => for (a <- contents) process(a) case <child>{contents @ _*}</child> => for (a <- contents) process(a) case <grandchild>{contents @ _*}</grandchild> => for (a <- contents) process(a) case _ => println(quot;leafquot;) } }
  12. 12. Concatenation def add(p: Node, newEntry: Node ): Node = p match { case <div>{ ch @ _* }</div> => <div>{ ch }{ newEntry }</div> }
  13. 13. Extracting attributes document match { case n @ <grandchild>{ _* }</grandchild> => println(n.attribute(quot;an_attributequot;)) }
  14. 14. persisting and restoring
  15. 15. scala.xml.XML.saveFull(quot;filename.xmlquot;, document, quot;UTF-8quot;, true, null) val document2 = scala.xml.XML.loadFile(quot;filename.xmlquot;)
  16. 16. stream parsing
  17. 17. val src = scala.io.Source.fromString(quot;<root><child></child></root>quot;) val er = new scala.xml.pull.XMLEventReader().initialize(src) er.next ...
  18. 18. building xml
  19. 19. var variable1 = quot;value1quot; val variable2 = <another_grandchild an_attribute=quot;value2quot; /> var document = <root> <child> <grandchild an_attribute={variable1} /> {variable2} </child> </root>
  20. 20. google app engine
  21. 21. • ‘cloud’ web hosting environment on google’s infrastructure • uses non relational ‘bigtable’ for data persistance • user authentication uses google accounts or openid • also has email (sending), memcache and image manipulation services
  22. 22. • working example: • code: http://bitbucket.org/markryall/scalagae • live: http://pairs.appspot.com • use latest datanucleus enhancer from http:// www.datanucleus.org/downloads/maven2- nightly/org/datanucleus/datanucleus- enhancer/1.1.2/
  23. 23. basics
  24. 24. class Servlet extends HttpServlet { override def doGet(request: HttpServletRequest, response: HttpServletResponse): Unit = { val out = response.getWriter() ... out.println(<html>{head}<body>{body}</body></html>) } }
  25. 25. authentication
  26. 26. var userService = UserServiceFactory.getUserService(); var user = userService.getCurrentUser(); if (user == null) { response.sendRedirect(userService.createLoginURL(request.getRequestURI())) return }
  27. 27. persistence
  28. 28. @PersistenceCapable {val identityType = IdentityType.APPLICATION} class Person(@Persistent var firstName: String,@Persistent var lastName: String) { @PrimaryKey @Persistent {val valueStrategy = IdGeneratorStrategy.IDENTITY} var id: java.lang.Long = null }

×