-
Notifications
You must be signed in to change notification settings - Fork 32
/
streaming.jl
279 lines (217 loc) · 8.68 KB
/
streaming.jl
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
277
278
type XPCallbacks
start_cdata::Function
end_cdata::Function
comment::Function
character_data::Function
default::Function
default_expand::Function
start_element::Function
end_element::Function
start_namespace::Function
end_namespace::Function
# Create an XPCallbacks instance filled with nop callbacks
function XPCallbacks()
start_cdata = (handler::XPStreamHandler) -> nothing
end_cdata = (handler::XPStreamHandler) -> nothing
comment = (handler::XPStreamHandler, txt::String) -> nothing
character_data = (handler::XPStreamHandler, txt::String) -> nothing
default = (handler::XPStreamHandler, txt::String) -> nothing
default_expand = (handler::XPStreamHandler, txt::String) -> nothing
start_element = (handler::XPStreamHandler, name::String, attrs_in::Dict{String,String}) -> nothing
end_element = (handler::XPStreamHandler, name::String) -> nothing
start_namespace = (handler::XPStreamHandler, prefix::String, uri::String) -> nothing
end_namespace = (handler::XPStreamHandler, prefix::String) -> nothing
new(start_cdata, end_cdata, comment, character_data, default,
default_expand, start_element, end_element, start_namespace,
end_namespace)
end
end
type XPStreamHandler{D}
cbs::XPCallbacks
parser::XML_Parser
data::D
end
function streaming_start_cdata (p_cbs::Ptr{Void})
@DBG_PRINT ("Found StartCdata")
h = unsafe_pointer_to_objref(p_cbs)::XPStreamHandler
h.cbs.start_cdata(h)
return
end
cb_streaming_start_cdata = cfunction(streaming_start_cdata, Void, (Ptr{Void},))
function streaming_end_cdata (p_cbs::Ptr{Void})
@DBG_PRINT ("Found EndCdata")
h = unsafe_pointer_to_objref(p_cbs)::XPStreamHandler
h.cbs.end_cdata(h)
return;
end
cb_streaming_end_cdata = cfunction(streaming_end_cdata, Void, (Ptr{Void},))
function streaming_cdata (p_cbs::Ptr{Void}, s::Ptr{Uint8}, len::Cint)
h = unsafe_pointer_to_objref(p_cbs)::XPStreamHandler
txt = bytestring(s, int(len))
@DBG_PRINT ("Found CData : " * txt)
h.cbs.character_data(h, txt)
return;
end
cb_streaming_cdata = cfunction(streaming_cdata, Void, (Ptr{Void},Ptr{Uint8}, Cint))
function streaming_start_element(p_cbs::Ptr{Void}, name::Ptr{Uint8}, attrs_in::Ptr{Ptr{Uint8}})
h = unsafe_pointer_to_objref(p_cbs)::XPStreamHandler
txt::String = bytestring(name)
attrs::Dict{String,String} = attrs_in_to_dict(attrs_in)
h.cbs.start_element(h, txt, attrs)
return
end
cb_streaming_start_element = cfunction(streaming_start_element, Void, (Ptr{Void},Ptr{Uint8}, Ptr{Ptr{Uint8}}))
function streaming_end_element(p_h::Ptr{Void}, name::Ptr{Uint8})
h = unsafe_pointer_to_objref(p_h)::XPStreamHandler
txt::String = bytestring(name)
@DBG_PRINT ("End element: $txt, current element: $(xph.pdata.name) ")
h.cbs.end_element(h, txt)
return
end
cb_streaming_end_element = cfunction(streaming_end_element, Void, (Ptr{Void},Ptr{Uint8}))
function streaming_comment (p_h::Ptr{Void}, data::Ptr{Uint8})
h = unsafe_pointer_to_objref(p_h)::XPStreamHandler
txt = bytestring(data)
@DBG_PRINT ("Found comment : " * txt)
h.cbs.comment(h, txt)
return
end
cb_streaming_comment = cfunction(streaming_comment, Void, (Ptr{Void},Ptr{Uint8}))
function streaming_default (p_h::Ptr{Void}, data::Ptr{Uint8}, len::Cint)
xph = unsafe_pointer_to_objref(p_h)::XPStreamHandler
txt = bytestring(data)
@DBG_PRINT ("Default : " * txt)
h.cbs.default(h, txt)
return;
end
cb_streaming_default = cfunction(streaming_default, Void, (Ptr{Void},Ptr{Uint8}, Cint))
function streaming_default_expand (p_h::Ptr{Void}, data::Ptr{Uint8}, len::Cint)
h = unsafe_pointer_to_objref(p_h)::XPStreamHandler
txt = bytestring(data)
@DBG_PRINT ("Default Expand : " * txt)
h.cbs.default_expand(h, txt)
return;
end
cb_streaming_default_expand = cfunction(streaming_default_expand, Void, (Ptr{Void},Ptr{Uint8}, Cint))
function streaming_start_namespace (p_h::Ptr{Void}, prefix::Ptr{Uint8}, uri::Ptr{Uint8})
h = unsafe_pointer_to_objref(p_h)::XPStreamHandler
prefix = bytestring(prefix)
uri = bytestring(uri)
@DBG_PRINT ("start namespace prefix : $prefix, uri: $uri")
h.cbs.start_namespace(h, prefix, uri)
return;
end
cb_streaming_start_namespace = cfunction(streaming_start_namespace, Void, (Ptr{Void},Ptr{Uint8}, Ptr{Uint8}))
function streaming_end_namespace (p_h::Ptr{Void}, prefix::Ptr{Uint8})
h = unsafe_pointer_to_objref(p_h)::XPStreamHandler
prefix = bytestring(prefix)
@DBG_PRINT ("end namespace prefix : $prefix")
h.cbs.end_namespace(h, prefix)
return;
end
cb_streaming_end_namespace = cfunction(streaming_end_namespace, Void, (Ptr{Void},Ptr{Uint8}))
# Unsupported callbacks: External Entity, NotationDecl, Not Stand Alone, Processing, UnparsedEntityDecl, StartDocType
# SetBase and GetBase
function make_parser(cbs::XPCallbacks,data=nothing,sep='\0')
p::XML_Parser = (sep == '\0') ? XML_ParserCreate(C_NULL) : XML_ParserCreateNS(C_NULL, sep);
if (p == C_NULL) error("XML_ParserCreate failed") end
h = XPStreamHandler(cbs, p, data)
p_h = pointer_from_objref(h)
XML_SetUserData(p, p_h);
XML_SetCdataSectionHandler(p, cb_streaming_start_cdata, cb_streaming_end_cdata)
XML_SetCharacterDataHandler(p, cb_streaming_cdata)
XML_SetCommentHandler(p, cb_streaming_comment)
XML_SetDefaultHandler(p, cb_streaming_default)
XML_SetDefaultHandlerExpand(p, cb_streaming_default_expand)
XML_SetElementHandler(p, cb_streaming_start_element, cb_streaming_end_element)
# XML_SetExternalEntityRefHandler(p, f_ExternaEntity)
XML_SetNamespaceDeclHandler(p, cb_streaming_start_namespace, cb_streaming_end_namespace)
# XML_SetNotationDeclHandler(p, f_NotationDecl)
# XML_SetNotStandaloneHandler(p, f_NotStandalone)
# XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction)
# XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl)
# XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl)
return h
end
function stop(h::XPStreamHandler)
XML_StopParser(h.parser, XML_FALSE)
# XML_ParserFree(h.parser)
end
function pause(h::XPStreamHandler)
XML_StopParser(h.parser, XML_TRUE)
end
function resume(h::XPStreamHandler)
XML_ResumeParser(h.parser)
end
function free(h::XPStreamHandler)
XML_ParserFree(h.parser)
end
function parsefile(filename::String,callbacks::XPCallbacks; bufferlines=1024, data=nothing)
h = make_parser(callbacks, data)
# TODO: Support suspending for files too
suspended = false
file = open(filename, "r")
try
io = IOBuffer()
while !eof(file)
i::Int = 0
truncate(io, 0)
while i < bufferlines && !eof(file)
write(io, readline(file))
i += 1
end
txt = bytestring(io)
rc = XML_Parse(h.parser, txt, length(txt.data), 0)
if (rc != XML_STATUS_OK) && (XML_GetErrorCode(h.parser) != XML_ERROR_ABORTED)
# Do not fail if the user aborted the parsing
error("Error parsing document : $rc")
end
if XML_GetErrorCode(h.parser) == XML_ERROR_ABORTED
break
end
end
rc = XML_Parse(h.parser, "", length(""), 1)
#if (rc == XML_STATUS_SUSPENDED)
# suspended = true
# return XPStreamHandler(callbacks, parser)
#end
if (rc != XML_STATUS_OK) && (XML_GetErrorCode(h.parser) != XML_ERROR_ABORTED)
# Do not fail if the user aborted the parsing
error("Error parsing document : $rc")
end
catch e
stre = string(e)
(err, line, column, pos) = xp_geterror(h.parser)
@DBG_PRINT ("$e, $err, $line, $column, $pos")
rethrow("$e, $err, $line, $column, $pos")
finally
if !suspended
XML_ParserFree(h.parser)
end
close(file)
end
end
function parse(txt::String,callbacks::XPCallbacks; data=nothing)
h = make_parser(callbacks, data)
suspended = false
try
rc = XML_Parse(h.parser, txt, length(txt.data), 1)
if (rc == XML_STATUS_SUSPENDED)
suspended = true
return h
end
if (rc != XML_STATUS_OK) && (XML_GetErrorCode(h.parser) != XML_ERROR_ABORTED)
# Do not fail if the user aborted the parsing
error("Error parsing document : $rc")
end
catch e
stre = string(e)
(err, line, column, pos) = xp_geterror(h.parser)
@DBG_PRINT ("$e, $err, $line, $column, $pos")
rethrow("$e, $err, $line, $column, $pos")
finally
if !suspended
XML_ParserFree(h.parser)
end
end
end