aboutsummaryrefslogtreecommitdiff
path: root/src/libjin-lua/scripts/utils/xml.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin-lua/scripts/utils/xml.lua')
-rw-r--r--src/libjin-lua/scripts/utils/xml.lua136
1 files changed, 68 insertions, 68 deletions
diff --git a/src/libjin-lua/scripts/utils/xml.lua b/src/libjin-lua/scripts/utils/xml.lua
index 8512dd3..665e490 100644
--- a/src/libjin-lua/scripts/utils/xml.lua
+++ b/src/libjin-lua/scripts/utils/xml.lua
@@ -13,21 +13,21 @@ function XmlParser.toXmlString(value)
value = string.gsub(value, ">", ">"); -- '>' -> ">"
value = string.gsub(value, "\"", """); -- '"' -> """
value = string.gsub(value, "([^%w%&%;%p%\t% ])",
- function(c)
- return string.format("&#x%X;", string.byte(c))
- end);
+ function(c)
+ return string.format("&#x%X;", string.byte(c))
+ end);
return value;
end
function XmlParser.fromXmlString(value)
value = string.gsub(value, "&#x([%x]+)%;",
- function(h)
- return string.char(tonumber(h, 16))
- end);
+ function(h)
+ return string.char(tonumber(h, 16))
+ end);
value = string.gsub(value, "&#([0-9]+)%;",
- function(h)
- return string.char(tonumber(h, 10))
- end);
+ function(h)
+ return string.char(tonumber(h, 10))
+ end);
value = string.gsub(value, """, "\"");
value = string.gsub(value, "'", "'");
value = string.gsub(value, ">", ">");
@@ -38,7 +38,7 @@ end
function XmlParser.parseArgs(node, s)
string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a)
- node:addProperty(w, XmlParser.fromXmlString(a))
+ node:addProperty(w, XmlParser.fromXmlString(a))
end)
end
@@ -56,34 +56,34 @@ local function newNode(name)
function node:children() return self.___children end
function node:numChildren() return #self.___children end
function node:addChild(child)
- if self[child:name()] ~= nil then
- if type(self[child:name()].name) == "function" then
- local tempTable = {}
- table.insert(tempTable, self[child:name()])
- self[child:name()] = tempTable
- end
- table.insert(self[child:name()], child)
- else
- self[child:name()] = child
- end
- table.insert(self.___children, child)
+ if self[child:name()] ~= nil then
+ if type(self[child:name()].name) == "function" then
+ local tempTable = {}
+ table.insert(tempTable, self[child:name()])
+ self[child:name()] = tempTable
+ end
+ table.insert(self[child:name()], child)
+ else
+ self[child:name()] = child
+ end
+ table.insert(self.___children, child)
end
function node:properties() return self.___props end
function node:numProperties() return #self.___props end
function node:addProperty(name, value)
- local lName = "@" .. name
- if self[lName] ~= nil then
- if type(self[lName]) == "string" then
- local tempTable = {}
- table.insert(tempTable, self[lName])
- self[lName] = tempTable
- end
- table.insert(self[lName], value)
- else
- self[lName] = value
- end
- table.insert(self.___props, { name = name, value = self[name] })
+ local lName = "@" .. name
+ if self[lName] ~= nil then
+ if type(self[lName]) == "string" then
+ local tempTable = {}
+ table.insert(tempTable, self[lName])
+ self[lName] = tempTable
+ end
+ table.insert(self[lName], value)
+ else
+ self[lName] = value
+ end
+ table.insert(self.___props, { name = name, value = self[name] })
end
return node
@@ -96,58 +96,58 @@ function XmlParser.parseXmlText(xmlText)
local ni, c, label, xarg, empty
local i, j = 1, 1
while true do
- ni, j, c, label, xarg, empty = string.find(xmlText, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
- if not ni then break end
- local text = string.sub(xmlText, i, ni - 1);
- if not string.find(text, "^%s*$") then
- local lVal = (top:value() or "") .. XmlParser.fromXmlString(text)
- stack[#stack]:setValue(lVal)
- end
- if empty == "/" then -- empty element tag
- local lNode = newNode(label)
- XmlParser.parseArgs(lNode, xarg)
- top:addChild(lNode)
- elseif c == "" then -- start tag
- local lNode = newNode(label)
- XmlParser.parseArgs(lNode, xarg)
- table.insert(stack, lNode)
+ ni, j, c, label, xarg, empty = string.find(xmlText, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
+ if not ni then break end
+ local text = string.sub(xmlText, i, ni - 1);
+ if not string.find(text, "^%s*$") then
+ local lVal = (top:value() or "") .. XmlParser.fromXmlString(text)
+ stack[#stack]:setValue(lVal)
+ end
+ if empty == "/" then -- empty element tag
+ local lNode = newNode(label)
+ XmlParser.parseArgs(lNode, xarg)
+ top:addChild(lNode)
+ elseif c == "" then -- start tag
+ local lNode = newNode(label)
+ XmlParser.parseArgs(lNode, xarg)
+ table.insert(stack, lNode)
top = lNode
- else -- end tag
- local toclose = table.remove(stack) -- remove top
-
- top = stack[#stack]
- if #stack < 1 then
- error("XmlParser. nothing to close with " .. label)
- end
- if toclose:name() ~= label then
- error("XmlParser. trying to close " .. toclose.name .. " with " .. label)
- end
- top:addChild(toclose)
- end
- i = j + 1
+ else -- end tag
+ local toclose = table.remove(stack) -- remove top
+
+ top = stack[#stack]
+ if #stack < 1 then
+ error("XmlParser. nothing to close with " .. label)
+ end
+ if toclose:name() ~= label then
+ error("XmlParser. trying to close " .. toclose.name .. " with " .. label)
+ end
+ top:addChild(toclose)
+ end
+ i = j + 1
end
local text = string.sub(xmlText, i);
if #stack > 1 then
- error("XmlParser. unclosed " .. stack[#stack]:name())
+ error("XmlParser. unclosed " .. stack[#stack]:name())
end
return top
end
function XmlParser.loadFile(xmlFilename, base)
if not base then
- base = system.ResourceDirectory
+ base = system.ResourceDirectory
end
local path = system.pathForFile(xmlFilename, base)
local hFile, err = io.open(path, "r");
if hFile and not err then
- local xmlText = hFile:read("*a"); -- read file content
- io.close(hFile);
- return XmlParser.parseXmlText(xmlText), nil;
+ local xmlText = hFile:read("*a"); -- read file content
+ io.close(hFile);
+ return XmlParser.parseXmlText(xmlText), nil;
else
- print(err)
- return nil
+ print(err)
+ return nil
end
end