summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/configuration/simple_xml.lua
blob: f04dd1e2de5220a765edb83d9a96cddff1c73521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- Gemeinschaft 5 simple xml gererator class
-- (c) AMOOMA GmbH 2013
-- 

module(...,package.seeall)

SimpleXml = {}

-- Create SimpleXml object
function SimpleXml.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.class = 'simplexml';
  return object;
end

function SimpleXml.element(self, arg)
  local xml_tag = '<' .. tostring(arg[1]);
  for key, value in pairs(arg) do
    if type(key) == 'string' then
      xml_tag = xml_tag .. ' ' ..  tostring(key) .. '="' .. tostring(value) .. '"';
    end
  end
  xml_tag = xml_tag .. '>';

  for key=2, #arg do
    xml_tag = xml_tag .. '\n' .. tostring(arg[key]) .. '\n';
  end

  return xml_tag .. '</' .. tostring(arg[1]) .. '>';
end


function SimpleXml.from_hash(self, element_name, parameter_hash, key_name, value_name)
  local params_xml = '';
  for key, value in pairs(parameter_hash) do
    local arguments = { [1] = element_name };
    if key_name and value_name then
      arguments[key_name] = key;
      arguments[value_name] = value;
    else
      arguments[key] = value;
    end
    params_xml = params_xml .. tostring(self:element(arguments)) .. '\n';
  end

  return params_xml;
end