File size: 7,549 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
--[[------------------------------------------------------
  # Very fast xml parser for Lua <a href="https://travis-ci.org/lubyk/xml"><img src="https://travis-ci.org/lubyk/xml.png" alt="Build Status"></a> 

  This parser uses [RapidXML](http://rapidxml.sourceforge.net/) to parse XML
  content.

  <html><a href="https://github.com/lubyk/xml"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" alt="Fork me on GitHub"></a></html>

  *MIT license* &copy Marcin Kalicinski 2006, 2009, Gaspard Bucher 2014.

  ## Installation
  
  With [luarocks](http://luarocks.org):

    $ luarocks install xml
  
  ## Usage example

    local data = xml.load(some_xml)

    local xml_string = xml.dump(some_table)

--]]-----------------------------------------------------
local lub     = require 'lub'
local lib     = lub.Autoload 'xml'
local ipairs, pairs, insert, type,
      match, tostring =
      ipairs, pairs, table.insert, type,
      string.match, tostring

local parser  = lib.Parser()


-- Current version respecting [semantic versioning](http://semver.org).
lib.VERSION = '1.1.2'

lib.DEPENDS = { -- doc
  -- Compatible with Lua 5.1 to 5.3 and LuaJIT
  'lua >= 5.1, < 5.4',
  -- Uses [Lubyk base library](http://doc.lubyk.org/lub.html)
  'lub >= 1.0.3, < 2',
}

-- nodoc
lib.DESCRIPTION = {
  summary = "Very fast xml parser based on RapidXML",
  detailed = [[
    This module is part of the Lubyk project.

    Main features are:
     - Fast and easy to use
     - Complete documentation
     - Based on proven code (RapidXML)
     - Full test coverage

    Read the documentation at http://doc.lubyk.org/xml.html.
  ]],
  homepage = "http://doc.lubyk.org/"..lib.type..".html",
  author   = "Gaspard Bucher",
  license  = "MIT",
}

-- nodoc
lib.BUILD = {
  github    = 'lubyk',
  includes  = {'include', 'src/bind', 'src/vendor'},
  platlibs = {
    linux  = {'stdc++'},
    macosx = {'stdc++'},
  },
  -- FIXME: Implement platform flags for lut.Builder and see how it works with
  -- luarocks /EHsc is needed for exception handling.
  platflags = {
    win32 = {'EHsc'},
  },
}

--[[

  # Lua table format

  This xml library uses string keys in Lua tables to store
  attributes and numerical keys for sub-nodes. Since the 'xml'
  attribute is not allowed in XML, we use this key to store the
  tag. Here is an example of Lua content:

  ## Lua

    {xml='document',
      {xml = 'article',
        {xml = 'p', 'This is the first paragraph.'},
        {xml = 'h2', class = 'opt', 'Title with opt style'},
      },
      {xml = 'article',
        {xml = 'p', 'Some ', {xml = 'b', 'important'}, ' text.'},
      },
    }

  ## XML

  And the equivalent xml:

    #txt
    <document>
      <article>
        <p>This is the first paragraph.</p>
        <h2 class='opt'>Title with opt style</h2>
      </article>
      <article>
        <p>Some <b>important</b> text.</p>
      </article>
    </document>

  # Notes on speed

  RapidXML is a very fast parser that uses in-place modification of the input
  text. Since Lua strings are immutable, we have to make a copy except for the
  xml.Parser.NonDestructive and xml.Parser.Fastest settings. With these types
  some xml entities such as `&amp;lt;` are not translated.

  See [RapidXML](http://rapidxml.sourceforge.net/) for details.
--]]------------------------------------------------------

-- # Class methods

-- Parse a `string` containing xml content and return a table. Uses
-- xml.Parser with the xml.Parser.Default type.
function lib.load(string)
  return parser:load(string)
end

-- Parse the XML content of the file at `path` and return a lua table. Uses
-- xml.Parser with the xml.Parser.Default type.
function lib.loadpath(path)
  return parser:load(lub.content(path))
end

local function escape(v)
  if type(v) == 'boolean' then
    return v and 'true' or 'false'
  else
    return v:gsub('&','&amp;'):gsub('>','&gt;'):gsub('<','&lt;'):gsub("'",'&apos;')
  end
end

local function tagWithAttributes(data)
  local res = data.xml or 'table'
  for k,v in pairs(data) do
    if k ~= 'xml' and type(k) == 'string' then
      res = res .. ' ' .. k .. "='" .. escape(v) .. "'"
    end
  end
  return res
end

local function doDump(data, indent, output, last, depth, max_depth)
  if depth > max_depth then
    error(string.format("Could not dump table to XML. Maximal depth of %i reached.", max_depth))
  end

  if data[1] then
    insert(output, (last == 'n' and indent or '')..'<'..tagWithAttributes(data)..'>')
    last = 'n'
    local ind = indent..'  '
    for _, child in ipairs(data) do
      local typ = type(child)
      if typ == 'table' then
        doDump(child, ind, output, last, depth + 1, max_depth)
        last = 'n'
      elseif typ == 'number' then
        insert(output, tostring(child))
      else
        local s = escape(child)
        insert(output, s)
        last = 's'
      end
    end
    insert(output, (last == 'n' and indent or '')..'</'..(data.xml or 'table')..'>')
    last = 'n'
  else
    -- no children
    insert(output, (last == 'n' and indent or '')..'<'..tagWithAttributes(data)..'/>')
    last = 'n'
  end
end

-- Dump a lua table in the format described above and return an XML string. The
-- `max_depth` parameter is used to avoid infinite recursion in case a table
-- references one of its ancestors.
--
-- Default maximal depth is 3000.
function lib.dump(table, max_depth)
  local max_depth = max_depth or 3000
  local res = {}
  doDump(table, '\n', res, 's', 1, max_depth)
  return lub.join(res, '')
end


local function doRemoveNamespace(data, prefix)
  data.xml = match(data.xml, prefix .. ':(.*)') or data.xml
  for _, sub in ipairs(data) do
    if type(sub) == 'table' then
      doRemoveNamespace(sub, prefix)
    end
  end
end

-- This function finds the `xmlns:NAME='KEY'` declaration and removes `NAME:` from
-- the tag names.
--
-- Example:
--
--   local data = xml.load [[
--    <foo:document xmlns:foo='bar'>
--      <foo:name>Blah</foo:name>
--    </foo:document>
--   ]]
--   
--   xml.removeNamespace(data, 'bar')
--
--   -- Result
--   {xml = 'document', ['xmlns:foo'] = 'bar',
--     {xml = 'name', 'Blah'},
--   }
function lib.removeNamespace(data, key)
  local nm
  for k, v in pairs(data) do
    if v == key then
      nm = match(k, 'xmlns:(.*)')
      if nm == '' then
        -- error
        return
      else
        doRemoveNamespace(data, nm)
      end
    end
  end
end

-- Recursively find the first table with a tag equal to `tag`. This
-- search uses [lub.search](lub.html#search) to do an [Iterative deepening depth-first search](https://en.wikipedia.org/wiki/Iterative_deepening_depth-first_search)
-- because we usually search for elements close to the surface.
--
-- For more options, use [lub.search](lub.html#search) directly with a custom
-- function.
--
-- You can also pass an attribute key and attribute value to further filter the
-- searched node. This gives this function the same arguments as LuaXML's find
-- function.
--
-- Usage examples:
--
--   local sect = xml.find(elem, 'simplesect', 'kind', 'section')
--
--   print(xml.find(sect, 'title'))
function lib.find(data, tag, attr_key, attr_value)
  if attr_key then
    return lub.search(data, function(node)
      if node.xml == tag and node[attr_key] == attr_value then
        return node
      end
    end)
  else
    return lub.search(data, function(node)
      if node.xml == tag then
        return node
      end
    end)
  end
end

-- # Classes

return lib