Sat, 28 Jul 2012 00:15:08 +0100
Add compatibility with Lua 5.2
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | -- See Copyright Notice in license.html |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | -- $Id: lom.lua,v 1.6 2005/06/09 19:18:40 tuler Exp $ |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
4 | local lxp = require "lxp" |
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
6 | local tinsert, tremove = table.insert, table.remove |
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | local assert, type, print = assert, type, print |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | local function starttag (p, tag, attr) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local stack = p:getcallbacks().stack |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | local newelement = {tag = tag, attr = attr} |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | tinsert(stack, newelement) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | local function endtag (p, tag) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | local stack = p:getcallbacks().stack |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | local element = tremove(stack) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | assert(element.tag == tag) |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
20 | local level = #stack |
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | tinsert(stack[level], element) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | local function text (p, txt) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | local stack = p:getcallbacks().stack |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
26 | local element = stack[#stack] |
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
27 | local n = #element |
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | if type(element[n]) == "string" then |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | element[n] = element[n] .. txt |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | else |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | tinsert(element, txt) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
35 | local function parse (o) |
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | local c = { StartElement = starttag, |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | EndElement = endtag, |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | CharacterData = text, |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | _nonstrict = true, |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | stack = {{}} |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | } |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | local p = lxp.new(c) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | local status, err |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | if type(o) == "string" then |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | status, err = p:parse(o) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | if not status then return nil, err end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | else |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
48 | for l in pairs(o) do |
0
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | status, err = p:parse(l) |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | if not status then return nil, err end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | status, err = p:parse() |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | if not status then return nil, err end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | p:close() |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | return c.stack[1][1] |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | end |
24d141cb2d1e
Initial commit of LuaExpat 1.1.0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | |
10
e981a82571cf
Add compatibility with Lua 5.2
Tom?s Guisasola Gorham <tomas@tecgraf.puc-rio.br>
parents:
0
diff
changeset
|
59 | return { parse = parse } |