]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | local base = _G\r |
2 | \r | |
3 | -- symbolic name for tag index, this allows accessing the tag by var[xml.TAG]\r | |
4 | xml.TAG = 0\r | |
5 | \r | |
6 | -- sets or returns tag of a LuaXML object\r | |
7 | function xml.tag(var,tag)\r | |
8 | if base.type(var)~="table" then return end\r | |
9 | if base.type(tag)=="nil" then \r | |
10 | return var[xml.TAG]\r | |
11 | end\r | |
12 | var[xml.TAG] = tag\r | |
13 | end\r | |
14 | \r | |
15 | -- creates a new LuaXML object either by setting the metatable of an existing Lua table or by setting its tag\r | |
16 | function xml.new(arg)\r | |
17 | if base.type(arg)=="table" then \r | |
18 | base.setmetatable(arg,{__index=xml, __tostring=xml.str})\r | |
19 | return arg\r | |
20 | end\r | |
21 | local var={}\r | |
22 | base.setmetatable(var,{__index=xml, __tostring=xml.str})\r | |
23 | if base.type(arg)=="string" then var[xml.TAG]=arg end\r | |
24 | return var\r | |
25 | end\r | |
26 | \r | |
27 | -- appends a new subordinate LuaXML object to an existing one, optionally sets tag\r | |
28 | function xml.append(var,tag)\r | |
29 | if base.type(var)~="table" then return end\r | |
30 | local newVar = xml.new(tag)\r | |
31 | var[#var+1] = newVar\r | |
32 | return newVar\r | |
33 | end\r | |
34 | \r | |
35 | -- converts any Lua var into an XML string\r | |
36 | function xml.str(var,indent,tagValue)\r | |
37 | if base.type(var)=="nil" then return end\r | |
38 | local indent = indent or 0\r | |
39 | local indentStr=""\r | |
40 | for i = 1,indent do indentStr=indentStr.." " end\r | |
41 | local tableStr=""\r | |
42 | \r | |
43 | if base.type(var)=="table" then\r | |
44 | local tag = var[0] or tagValue or base.type(var)\r | |
45 | local s = indentStr.."<"..tag\r | |
46 | for k,v in base.pairs(var) do -- attributes \r | |
47 | if base.type(k)=="string" then\r | |
48 | if base.type(v)=="table" and k~="_M" then -- otherwise recursiveness imminent\r | |
49 | tableStr = tableStr..xml.str(v,indent+1,k)\r | |
50 | else\r | |
51 | s = s.." "..k.."=\""..xml.encode(base.tostring(v)).."\""\r | |
52 | end\r | |
53 | end\r | |
54 | end\r | |
55 | if #var==0 and #tableStr==0 then\r | |
56 | s = s.." />\n"\r | |
57 | elseif #var==1 and base.type(var[1])~="table" and #tableStr==0 then -- single element\r | |
58 | s = s..">"..xml.encode(base.tostring(var[1])).."</"..tag..">\n"\r | |
59 | else\r | |
60 | s = s..">\n"\r | |
61 | for k,v in base.ipairs(var) do -- elements\r | |
62 | if base.type(v)=="string" then\r | |
63 | s = s..indentStr.." "..xml.encode(v).." \n"\r | |
64 | else\r | |
65 | s = s..xml.str(v,indent+1)\r | |
66 | end\r | |
67 | end\r | |
68 | s=s..tableStr..indentStr.."</"..tag..">\n"\r | |
69 | end\r | |
70 | return s\r | |
71 | else\r | |
72 | local tag = base.type(var)\r | |
73 | return indentStr.."<"..tag.."> "..xml.encode(base.tostring(var)).." </"..tag..">\n"\r | |
74 | end\r | |
75 | end\r | |
76 | \r | |
77 | \r | |
78 | -- saves a Lua var as xml file\r | |
79 | function xml.save(var,filename)\r | |
80 | if not var then return end\r | |
81 | if not filename or #filename==0 then return end\r | |
82 | local file = base.io.open(filename,"w")\r | |
83 | file:write("<?xml version=\"1.0\"?>\n<!-- file \"",filename, "\", generated by LuaXML -->\n\n")\r | |
84 | file:write(xml.str(var))\r | |
85 | base.io.close(file)\r | |
86 | end\r | |
87 | \r | |
88 | \r | |
89 | -- recursively parses a Lua table for a substatement fitting to the provided tag and attribute\r | |
90 | function xml.find(var, tag, attributeKey,attributeValue)\r | |
91 | -- check input:\r | |
92 | if base.type(var)~="table" then return end\r | |
93 | if base.type(tag)=="string" and #tag==0 then tag=nil end\r | |
94 | if base.type(attributeKey)~="string" or #attributeKey==0 then attributeKey=nil end\r | |
95 | if base.type(attributeValue)=="string" and #attributeValue==0 then attributeValue=nil end\r | |
96 | -- compare this table:\r | |
97 | if tag~=nil then\r | |
98 | if var[0]==tag and ( attributeValue == nil or var[attributeKey]==attributeValue ) then\r | |
99 | base.setmetatable(var,{__index=xml, __tostring=xml.str})\r | |
100 | return var\r | |
101 | end\r | |
102 | else\r | |
103 | if attributeValue == nil or var[attributeKey]==attributeValue then\r | |
104 | base.setmetatable(var,{__index=xml, __tostring=xml.str})\r | |
105 | return var\r | |
106 | end\r | |
107 | end\r | |
108 | -- recursively parse subtags:\r | |
109 | for k,v in base.ipairs(var) do\r | |
110 | if base.type(v)=="table" then\r | |
111 | local ret = xml.find(v, tag, attributeKey,attributeValue)\r | |
112 | if ret ~= nil then return ret end\r | |
113 | end\r | |
114 | end\r | |
115 | end\r | |
116 | \r |