Advertisement
Patosho

XML joints to table

Feb 28th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. local dom
  2.  
  3. function main()
  4.   io.input("input.txt")
  5.   local xml = io.read("*all")
  6.   dom = parseXml(xml, true)
  7.   local joint
  8.   local code = "joints = {"
  9.   print(code)
  10.  
  11.   for i,j in ipairs(path(dom, "Z", "L", "JD")[1][2][4]) do
  12.     jj = j.attribute
  13.    
  14.     x1,y1 = jj.P1:match("(.+),(.+)")
  15.     x2,y2 = jj.P2:match("(.+),(.+)")
  16.     color,width,alpha,foreground = jj.c:match("(.+),?(.+),?(.+),?(.+)")
  17.    
  18.     if foreground == "1" then
  19.       foreground = true
  20.     else
  21.       foreground = false
  22.     end
  23.    
  24.     joint = {
  25.       m1 = tonumber(jj.M1),
  26.       m2 = tonumber(jj.M2),
  27.       x1 = tonumber(x1),
  28.       y1 = tonumber(y1),
  29.       x2 = tonumber(x2),
  30.       y2 = tonumber(y2),
  31.       color = tonumber(color, 16),
  32.       width = tonumber(width),
  33.       alpha = tonumber(alpha),
  34.       foreground = foreground
  35.     }
  36.    
  37.     code = "\t{"
  38.     local first = true
  39.     for k,v in pairs(joint) do
  40.       local val = v
  41.       if type(v) == "boolean" then
  42.         val = tostring(v)
  43.       end
  44.      
  45.       if first then
  46.         first = false
  47.       else
  48.         code = code .. ", "
  49.       end
  50.       code = code .. k .. " = " .. val
  51.     end
  52.     code = code .. "};"
  53.     print(code)
  54.   end
  55.   print("}\n")
  56. end
  57.  
  58. --  Makinit's xml library
  59. do
  60.   local namePattern = "[%a_:][%w%.%-_:]*"
  61.   function parseXml(xml, fast)
  62.     if not fast then
  63.         xml = string.gsub(xml, "<!%[CDATA%[(.-)%]%]>", xmlEscape) -- replace CDATA with escaped text
  64.         xml = string.gsub(xml, "<%?.-%?>", "") -- remove processing instructions
  65.         xml = string.gsub(xml, "<!%-%-.-%-%->", "") -- remove comments
  66.         xml = string.gsub(xml, "<!.->", "")
  67.     end
  68.     local root = {}
  69.     local parents = {}
  70.     local element = root
  71.     for closing, name, attributes, empty, text in string.gmatch(xml, "<(/?)(" .. namePattern .. ")(.-)(/?)>%s*([^<]*)%s*") do
  72.         if closing == "/" then
  73.             local parent = parents[element]
  74.             if parent and name == element.name then
  75.                 element = parent
  76.             end
  77.         else
  78.             local child = {name = name, attribute = {}}
  79.             table.insert(element, child)
  80.             parents[child] = element
  81.             if empty ~= "/" then
  82.                 element = child
  83.             end
  84.             for name, value in string.gmatch(attributes, "(" .. namePattern .. ")%s*=%s*\"(.-)\"") do
  85.                 child.attribute[name] = fast and value or xmlUnescape(value)
  86.             end
  87.         end
  88.         if text ~= "" then
  89.             local child = {text = fast and text or xmlUnescape(text)}
  90.             table.insert(element, child)
  91.             parents[child] = element
  92.         end
  93.     end
  94.     return root[1]
  95.   end
  96.  
  97.   function generateXml(element, fast)
  98.     if element.name then
  99.         local xml = "<" .. element.name
  100.         for name, value in pairs(element.attribute) do
  101.             xml = xml .. " " .. name .. "=\"" .. (fast and tostring(value) or xmlEscape(tostring(value))) .. "\""
  102.         end
  103.         if #element == 0 then
  104.             xml = xml .. " />"
  105.         else
  106.             xml = xml .. ">"
  107.             for i, child in ipairs(element) do
  108.                 xml = xml .. generateXml(child, fast)
  109.             end
  110.             xml = xml .. "</" .. element.name .. ">"
  111.         end
  112.         return xml
  113.     elseif element.text then
  114.         return fast and tostring(element.text) or xmlEscape(tostring(element.text))
  115.     end
  116.   end
  117.  
  118.   function path(nodes, ...)
  119.     nodes = {nodes}
  120.     for i, name in ipairs(arg) do
  121.         local match = {}
  122.         for i, node in ipairs(nodes) do
  123.             for i, child in ipairs(node) do
  124.                 if child.name == name then
  125.                     table.insert(match, child)
  126.                 end
  127.             end
  128.         end
  129.         nodes = match
  130.     end
  131.     return nodes
  132.   end
  133.  
  134.   --(un)escape functions
  135.   local escapeCache = {}
  136.   function xmlEscape(s)
  137.     local r = escapeCache[s]
  138.     if not r then  
  139.         local g = string.gsub
  140.         r = g(s, "&", "&amp;")
  141.         r = g(r, "\"", "&quot;")
  142.         r = g(r, "'", "&apos;")
  143.         r = g(r, "<", "&lt;")
  144.         r = g(r, ">", "&gt;")
  145.         escapeCache[s] = r
  146.     end
  147.     return r
  148.   end
  149.   local unescapeCache = {}
  150.   function xmlUnescape(s)
  151.     local r = unescapeCache[s]
  152.     if not r then
  153.         local g = string.gsub
  154.         r = g(s, "&quot;", "\"")
  155.         r = g(r, "&apos;", "'")
  156.         r = g(r, "&lt;", "<")
  157.         r = g(r, "&gt;", ">")
  158.         r = g(r, "&#(%d%d?%d?%d?);", dec2char)
  159.         r = g(r, "&#x(%x%x?%x?%x?);", hex2char)
  160.         r = g(r, "&amp;", "&")
  161.         unescapeCache[s] = r
  162.     end
  163.     return r
  164.   end
  165.   function dec2char(code)
  166.     code = tonumber(code)
  167.     return string.char(code > 255 and 0 or code)
  168.   end
  169.   function hex2char(code)
  170.     code = tonumber(code, 16)
  171.     return string.char(code > 255 and 0 or code)
  172.   end
  173. end
  174.  
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement