the list of duplicates is ready (still not used)
[lqt.git] / old_bindings / xml.lua
blobdf74d673881d677c39c65fa5d2ff841d646121a2
1 #!/usr/bin/lua
2 --[[
4 Copyright (c) 2007-2008 Mauro Iazzi
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation
8 files (the "Software"), to deal in the Software without
9 restriction, including without limitation the rights to use,
10 copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following
13 conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
27 --]]
30 local xml_parser = {
32 strip_escapes = function (self, s)
33 s = string.gsub(s, '>', '>')
34 s = string.gsub(s, '&lt;', '<')
35 return s
36 end,
38 parseargs = function (self, s)
39 local arg = {}
40 string.gsub(s, "([%w_]+)=([\"'])(.-)%2", function (w, _, a)
41 arg[self:strip_escapes(w)] = self:strip_escapes(a)
42 end)
43 return arg
44 end,
46 collect = function (self, s)
47 local stack = {}
48 local top = {}
49 table.insert(stack, top)
50 local ni,c,label,xarg, empty
51 local i, j = 1, 1
52 while true do
53 ni,j,c,label,xarg, empty = string.find(s, "<(%/?)(%w+)(.-)(%/?)>", j)
54 if not ni then break end
55 local text = string.sub(s, i, ni-1)
56 if not string.find(text, "^%s*$") then
57 table.insert(top, text)
58 end
59 local inserted = nil
60 if empty == "/" then -- empty element tag
61 inserted = {tag=label, attr=self:parseargs(xarg), empty=1}
62 table.insert(top, inserted)
63 elseif c == "" then -- start tag
64 top = {tag=label, attr=self:parseargs(xarg)}
65 inserted = top
66 table.insert(stack, top) -- new level
67 else -- end tag
68 local toclose = table.remove(stack) -- remove top
69 top = stack[#stack]
70 if #stack < 1 then
71 error("nothing to close with "..label)
72 end
73 if toclose.tag ~= label then
74 error("trying to close "..toclose.label.." with "..label)
75 end
76 table.insert(top, toclose)
77 end
78 if inserted then
79 for a, v in pairs(inserted.attr) do
80 if type(self[a])=='table' then
81 self[a][v] = inserted
82 end
83 end
84 end
85 i = j+1
86 end
87 local text = string.sub(s, i)
88 if not string.find(text, "^%s*$") then
89 table.insert(stack[stack.n], text)
90 end
91 if #stack > 1 then
92 error("unclosed "..stack[stack.n].label)
93 end
94 return stack[1]
95 end,
98 return xml_parser