Advertisement
zinch

Simple xml builder

Dec 24th, 2014
3,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
IO 2.42 KB | None | 0 0
  1. OperatorTable addAssignOperator(":", "addEntry")
  2.  
  3. Builder := Object clone
  4.  
  5. Builder curlyBrackets := method(
  6.   m := Map clone
  7.   call message arguments foreach(arg, m doString(arg asString))
  8.   m
  9. )
  10.  
  11. Map addEntry := method(
  12.   self atPut(
  13.     call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\""),
  14.     call evalArgAt(1))
  15. )
  16.  
  17. Builder increaseLevel := method(
  18.   if (self level == 0,
  19.   self level = 4,
  20.   self level := 4 + self level)
  21. )
  22.  
  23. Builder decreaseLevel := method(
  24.   if (self level == 0,
  25.   0, self level := self level - 4)
  26. )
  27.  
  28. Builder printStringWithOffset := method(string,
  29.   writeln(string alignRight(self level + string size))
  30. )
  31.  
  32. Builder level := 0
  33. Builder forward := method(
  34.   hasAttributes := (call message argAt(0) asString beginsWithSeq("curlyBrackets"))
  35.   (hasAttributes) ifFalse(printStringWithOffset("<" .. (call message name) .. ">"))
  36.  
  37.   tagClosed := false
  38.   call message arguments foreach(arg,
  39.     self increaseLevel
  40.     content := self doMessage(arg)
  41.     self decreaseLevel
  42.  
  43.     if (content type == "Map",
  44.       attributes := ""
  45.       content foreach(key, val,
  46.         attributes = attributes .. key .. "=\"" .. val .. "\" ")
  47.  
  48.       closingBracket := ">"
  49.       if (call message arguments size == 1,
  50.         closingBracket = "/>"
  51.         tagClosed = true
  52.       )
  53.  
  54.       printStringWithOffset("<" .. (call message name) .. " " .. attributes .. closingBracket)
  55.     )
  56.  
  57.     if (content type == "Sequence",
  58.       self increaseLevel
  59.       printStringWithOffset(content)
  60.       self decreaseLevel
  61.     )
  62.   )
  63.   (tagClosed) ifFalse(printStringWithOffset("</" .. (call message name) .. ">"))
  64. )
  65.  
  66. //Example
  67.  
  68. Builder html(
  69.   {"xmlns:c":"http://java.sun.com/jsp/jstl/core", "xmlns:fn":"http://java.sun.com/jsp/jstl/functions"},
  70.   head(title("Hello")),
  71.   body(
  72.     {"bgcolor":"white"},
  73.     img({"src":"duke.waving.gif"}),
  74.     h2("My name is Duke. What is yours?")
  75.     form(
  76.       {"method":"get"},
  77.       input({"type":"text", "name":"username", "size":"25"}),
  78.       p(""),
  79.       input({"type":"submit", "value":"Submit"}),
  80.       input({"type":"reset", "value":"Reset"})
  81.     ),
  82.     jsp:useBean({"id":"userNameBean", "class":"hello.UserNameBean", "scope":"request"}),
  83.     jsp:setProperty({"name":"userNameBean", "property":"name", "value":"${param.username}"}),
  84.     c:if(
  85.       {"test":"${fn:length(userBeanName.name) gt 0}"},
  86.       jsp:directive.include({"file":"response.jsp"})
  87.     )
  88.   )
  89. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement