forked from allegory-software/allegory-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.lua
178 lines (145 loc) · 5.27 KB
/
xml.lua
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
--[=[
XML formatting.
Written by Cosmin Apreutesei. Public Domain.
FEATURES
* does all necessary XML escaping.
* prevents generating text that isn't well-formed.
* generates namespace prefixes.
* produces Canonical XML, suitable for use with digital signatures.
LIMITATIONS
* only UTF8 encoding supported
* no empty element tags
* no <!DOCTYPE> declarations (write it yourself before calling w:start_doc())
* no pretty-printing (add linebreaks and indentation yourself with w:text())
API
xml() -> w Create a new genx writer.
w:free() Free the genx writer.
w:start_doc(file) Start an XML document on a Lua file object
w:start_doc(write) Start an XML document on write([s[, size]])
w:end_doc() Flush pending updates and release the file handle
w:ns(uri[, prefix]) -> ns Declare a namespace for reuse.
w:tag(name[, ns | uri,prefix]) -> elem Declare an element for reuse.
w:attr(name[, ns | uri,prefix]) -> attr Declare an attribute for reuse.
w:comment(s) Add a comment to the current XML stream.
w:pi(target, text) Add a PI to the current XML stream.
w:start_tag(elem | name [, ns | uri,prefix]) Start a new XML element.
w:end_tag() End the current element.
w:add_attr(attr, val[, ns | uri,prefix]) Add an attribute to the current element.
Attributes are sorted by name in the output stream.
w:add_ns(ns | [uri,prefix]) Add a namespace to the current element.
w:unset_default_namespace() Add a `xmlns=""` declaration to unset the default namespace declaration.
This is a no-op if no default namespace is in effect.
w:text(s[, size]) Add utf-8 text.
w:char(char) Add an unicode code point.
w:check_text(s) -> genxStatus Check utf-8 text.
w:scrub_text(s) -> s Scrub utf-8 text of invalid characters.
]=]
if not ... then require'xml_test'; return end
local ffi = require'ffi'
local C = ffi.load'genx'
require'genx_h'
local function checkh(w, statusP, h)
if h ~= nil then return h end
local s = C.genxGetErrorMessage(w, statusP[0])
error(s ~= nil and ffi.string(s) or 'unknown error')
end
local function checknz(w, status)
if status == 0 then return end
local s = C.genxGetErrorMessage(w, status)
error(s ~= nil and ffi.string(s) or 'unknown error')
end
local function nzcaller(f)
return function(w, ...)
return checknz(w, f(w, ...))
end
end
function xml(alloc, dealloc, userdata)
local w = C.genxNew(alloc, dealloc, userdata)
assert(w ~= nil, 'out of memory')
ffi.gc(w, C.genxDispose)
return w
end
local senders = {} --{[genxWriter] = genxSender}
local function free_sender(w)
local sender = senders[w]
if not sender then return end
sender.send:free()
sender.sendBounded:free()
sender.flush:free()
senders[w] = nil
end
local function free(w)
C.genxDispose(ffi.gc(w, nil))
free_sender(w)
end
ffi.metatype('genxWriter_rec', {__index = {
free = free,
start_doc = function(w, f, ...)
free_sender(w)
if type(f) == 'function' then
--f is called as either: f(s), f(s, sz), or f() to signal EOF.
local sender = ffi.new'genxSender'
sender.send = ffi.new('send_callback', function(_, s) f(s); return 0 end)
sender.sendBounded = ffi.new('sendBounded_callback', function(_, p1, p2) f(p1, p2-p1); return 0 end)
sender.flush = ffi.new('flush_callback', function() f(); return 0 end)
senders[w] = sender
checknz(w, C.genxStartDocSender(w, sender))
else
checknz(w, C.genxStartDocFile(w, f))
end
end,
end_doc = nzcaller(C.genxEndDocument),
ns = function(w, uri, prefix, statusP)
statusP = statusP or ffi.new'genxStatus[1]'
return checkh(w, statusP, C.genxDeclareNamespace(w, uri, prefix, statusP))
end,
tag = function(w, name, ns, statusP)
statusP = statusP or ffi.new'genxStatus[1]'
return checkh(w, statusP, C.genxDeclareElement(w, ns, name, statusP))
end,
attr = function(w, name, ns, statusP)
statusP = statusP or ffi.new'genxStatus[1]'
return checkh(w, statusP, C.genxDeclareAttribute(w, ns, name, statusP))
end,
comment = nzcaller(C.genxComment),
pi = nzcaller(C.genxPI),
start_tag = function(w, e, ns, prefix)
if type(ns) == 'string' then
ns = w:ns(ns, prefix)
end
if type(e) == 'string' then
e = w:tag(e, ns)
end
checknz(w, C.genxStartElement(e))
end,
add_attr = function(w, a, val, ns, prefix)
if type(ns) == 'string' then
ns = w:ns(ns, prefix)
end
if type(a) == 'string' then
a = w:attr(a, ns)
end
checknz(w, C.genxAddAttribute(a, val))
end,
add_ns = function(w, ns, prefix)
if type(ns) == 'string' then
ns = w:ns(ns, prefix)
end
checknz(w, C.genxAddNamespace(ns, prefix))
end,
unset_default_namespace = nzcaller(C.genxUnsetDefaultNamespace),
end_tag = nzcaller(C.genxEndElement),
text = function(w, s, sz)
checknz(w, C.genxAddCountedText(w, s, sz or #s))
end,
char = nzcaller(C.genxAddCharacter),
check_text = C.genxCheckText,
scrub_text = function(s_in)
s_out = ffi.new('constUtf8[?]', #s_in + 1)
if C.genxScrubText(s_in, s_out) ~= 0 then
return ffi.string(s_out)
else
return s_in
end
end,
}, __gc = free})