1 | module m_xml_parser |
---|
2 | |
---|
3 | ! |
---|
4 | ! Basic module to parse XML in the SAX spirit. |
---|
5 | ! |
---|
6 | |
---|
7 | use m_buffer |
---|
8 | use m_reader |
---|
9 | use m_fsm |
---|
10 | use m_dictionary |
---|
11 | use m_debug |
---|
12 | use m_xml_error |
---|
13 | use m_elstack ! For element nesting checks |
---|
14 | use m_entities |
---|
15 | ! |
---|
16 | private |
---|
17 | |
---|
18 | ! |
---|
19 | ! XML file handle |
---|
20 | ! |
---|
21 | type, public :: xml_t |
---|
22 | private |
---|
23 | type(file_buffer_t) :: fb |
---|
24 | type(fsm_t) :: fx |
---|
25 | character(len=200) :: path_mark |
---|
26 | end type xml_t |
---|
27 | |
---|
28 | ! |
---|
29 | public :: xml_parse |
---|
30 | public :: open_xmlfile, close_xmlfile |
---|
31 | public :: endfile_xmlfile, rewind_xmlfile |
---|
32 | public :: eof_xmlfile, sync_xmlfile |
---|
33 | public :: xml_char_count |
---|
34 | public :: xml_path, xml_mark_path, xml_get_path_mark |
---|
35 | public :: xml_name, xml_attributes |
---|
36 | |
---|
37 | CONTAINS !============================================================= |
---|
38 | |
---|
39 | subroutine open_xmlfile(fname,fxml,iostat,record_size) |
---|
40 | character(len=*), intent(in) :: fname |
---|
41 | integer, intent(out) :: iostat |
---|
42 | type(xml_t), intent(out) :: fxml |
---|
43 | integer, intent(in), optional :: record_size |
---|
44 | |
---|
45 | call open_file(fname,fxml%fb,iostat,record_size) |
---|
46 | call init_fsm(fxml%fx) |
---|
47 | fxml%path_mark = "" |
---|
48 | |
---|
49 | end subroutine open_xmlfile |
---|
50 | !------------------------------------------------------------------------- |
---|
51 | |
---|
52 | subroutine rewind_xmlfile(fxml) |
---|
53 | type(xml_t), intent(inout) :: fxml |
---|
54 | |
---|
55 | call rewind_file(fxml%fb) |
---|
56 | call reset_fsm(fxml%fx) |
---|
57 | fxml%path_mark = "" |
---|
58 | |
---|
59 | end subroutine rewind_xmlfile |
---|
60 | |
---|
61 | !----------------------------------------- |
---|
62 | subroutine endfile_xmlfile(fxml) |
---|
63 | type(xml_t), intent(inout) :: fxml |
---|
64 | |
---|
65 | call mark_eof_file(fxml%fb) |
---|
66 | |
---|
67 | end subroutine endfile_xmlfile |
---|
68 | |
---|
69 | !----------------------------------------- |
---|
70 | subroutine close_xmlfile(fxml) |
---|
71 | type(xml_t), intent(inout) :: fxml |
---|
72 | |
---|
73 | call close_file_buffer(fxml%fb) |
---|
74 | call reset_fsm(fxml%fx) ! just in case |
---|
75 | fxml%path_mark = "" ! "" |
---|
76 | |
---|
77 | end subroutine close_xmlfile |
---|
78 | |
---|
79 | !----------------------------------------- |
---|
80 | subroutine sync_xmlfile(fxml,iostat) |
---|
81 | type(xml_t), intent(inout) :: fxml |
---|
82 | integer, intent(out) :: iostat |
---|
83 | |
---|
84 | call sync_file(fxml%fb,iostat) |
---|
85 | ! Do not reset fx: that's the whole point of synching. |
---|
86 | |
---|
87 | end subroutine sync_xmlfile |
---|
88 | |
---|
89 | !---------------------------------------------------- |
---|
90 | function eof_xmlfile(fxml) result (res) |
---|
91 | type(xml_t), intent(in) :: fxml |
---|
92 | logical :: res |
---|
93 | |
---|
94 | res = eof_file(fxml%fb) |
---|
95 | |
---|
96 | end function eof_xmlfile |
---|
97 | ! |
---|
98 | !---------------------------------------------------- |
---|
99 | !---------------------------------------------------- |
---|
100 | function xml_char_count(fxml) result (nc) |
---|
101 | type(xml_t), intent(in) :: fxml |
---|
102 | integer :: nc |
---|
103 | |
---|
104 | nc = nchars_processed(fxml%fb) |
---|
105 | |
---|
106 | end function xml_char_count |
---|
107 | ! |
---|
108 | !---------------------------------------------------- |
---|
109 | ! |
---|
110 | |
---|
111 | subroutine xml_parse(fxml, begin_element_handler, & |
---|
112 | end_element_handler, & |
---|
113 | pcdata_chunk_handler, & |
---|
114 | comment_handler, & |
---|
115 | xml_declaration_handler, & |
---|
116 | cdata_section_handler, & |
---|
117 | sgml_declaration_handler, & |
---|
118 | error_handler, & |
---|
119 | signal_handler, & |
---|
120 | verbose, & |
---|
121 | empty_element_handler) |
---|
122 | |
---|
123 | type(xml_t), intent(inout), target :: fxml |
---|
124 | |
---|
125 | optional :: begin_element_handler |
---|
126 | optional :: end_element_handler |
---|
127 | optional :: pcdata_chunk_handler |
---|
128 | optional :: comment_handler |
---|
129 | optional :: xml_declaration_handler |
---|
130 | optional :: sgml_declaration_handler |
---|
131 | optional :: cdata_section_handler |
---|
132 | optional :: error_handler |
---|
133 | optional :: signal_handler |
---|
134 | logical, intent(in), optional :: verbose |
---|
135 | optional :: empty_element_handler |
---|
136 | |
---|
137 | interface |
---|
138 | subroutine begin_element_handler(name,attributes) |
---|
139 | use m_dictionary |
---|
140 | character(len=*), intent(in) :: name |
---|
141 | type(dictionary_t), intent(in) :: attributes |
---|
142 | end subroutine begin_element_handler |
---|
143 | |
---|
144 | subroutine end_element_handler(name) |
---|
145 | character(len=*), intent(in) :: name |
---|
146 | end subroutine end_element_handler |
---|
147 | |
---|
148 | subroutine pcdata_chunk_handler(chunk) |
---|
149 | character(len=*), intent(in) :: chunk |
---|
150 | end subroutine pcdata_chunk_handler |
---|
151 | |
---|
152 | subroutine comment_handler(comment) |
---|
153 | character(len=*), intent(in) :: comment |
---|
154 | end subroutine comment_handler |
---|
155 | |
---|
156 | subroutine xml_declaration_handler(name,attributes) |
---|
157 | use m_dictionary |
---|
158 | character(len=*), intent(in) :: name |
---|
159 | type(dictionary_t), intent(in) :: attributes |
---|
160 | end subroutine xml_declaration_handler |
---|
161 | |
---|
162 | subroutine sgml_declaration_handler(sgml_declaration) |
---|
163 | character(len=*), intent(in) :: sgml_declaration |
---|
164 | end subroutine sgml_declaration_handler |
---|
165 | |
---|
166 | subroutine cdata_section_handler(cdata) |
---|
167 | character(len=*), intent(in) :: cdata |
---|
168 | end subroutine cdata_section_handler |
---|
169 | |
---|
170 | subroutine error_handler(error_info) |
---|
171 | use m_xml_error |
---|
172 | type(xml_error_t), intent(in) :: error_info |
---|
173 | end subroutine error_handler |
---|
174 | |
---|
175 | subroutine signal_handler(code) |
---|
176 | logical, intent(out) :: code |
---|
177 | end subroutine signal_handler |
---|
178 | |
---|
179 | subroutine empty_element_handler(name,attributes) |
---|
180 | use m_dictionary |
---|
181 | character(len=*), intent(in) :: name |
---|
182 | type(dictionary_t), intent(in) :: attributes |
---|
183 | end subroutine empty_element_handler |
---|
184 | |
---|
185 | end interface |
---|
186 | |
---|
187 | character(len=1) :: c |
---|
188 | integer :: iostat, status |
---|
189 | |
---|
190 | character(len=150) :: message |
---|
191 | integer :: signal |
---|
192 | |
---|
193 | type(buffer_t) :: translated_pcdata |
---|
194 | type(buffer_t) :: name, oldname, dummy |
---|
195 | |
---|
196 | logical :: have_begin_handler, have_end_handler, & |
---|
197 | have_pcdata_handler, have_comment_handler, & |
---|
198 | have_xml_declaration_handler, & |
---|
199 | have_sgml_declaration_handler, & |
---|
200 | have_cdata_section_handler, have_empty_handler, & |
---|
201 | have_error_handler, have_signal_handler |
---|
202 | |
---|
203 | logical :: pause_signal |
---|
204 | |
---|
205 | type(xml_error_t) :: error_info |
---|
206 | type(file_buffer_t), pointer :: fb |
---|
207 | type(fsm_t), pointer :: fx |
---|
208 | |
---|
209 | have_begin_handler = present(begin_element_handler) |
---|
210 | have_end_handler = present(end_element_handler) |
---|
211 | have_pcdata_handler = present(pcdata_chunk_handler) |
---|
212 | have_comment_handler = present(comment_handler) |
---|
213 | have_xml_declaration_handler = present(xml_declaration_handler) |
---|
214 | have_sgml_declaration_handler = present(sgml_declaration_handler) |
---|
215 | have_cdata_section_handler = present(cdata_section_handler) |
---|
216 | have_error_handler = present(error_handler) |
---|
217 | have_signal_handler = present(signal_handler) |
---|
218 | have_empty_handler = present(empty_element_handler) |
---|
219 | |
---|
220 | fb => fxml%fb |
---|
221 | fx => fxml%fx |
---|
222 | if (present(verbose)) then |
---|
223 | debug = verbose ! For m_converters |
---|
224 | fx%debug = verbose ! job-specific flag |
---|
225 | endif |
---|
226 | |
---|
227 | if (fx%debug) print *, " Entering xml_parse..." |
---|
228 | |
---|
229 | !--------------------------------------------------------------------- |
---|
230 | do |
---|
231 | call get_character(fb,c,iostat) |
---|
232 | |
---|
233 | if (iostat /= 0) then ! End of file... |
---|
234 | if (.not. is_empty(fx%element_stack)) then |
---|
235 | call build_error_info(error_info, & |
---|
236 | "Early end of file.", & |
---|
237 | line(fb),column(fb),fx%element_stack,SEVERE_ERROR_CODE) |
---|
238 | if (have_error_handler) then |
---|
239 | call error_handler(error_info) |
---|
240 | else |
---|
241 | call default_error_handler(error_info) |
---|
242 | endif |
---|
243 | endif |
---|
244 | call endfile_xmlfile(fxml) ! Mark it as eof |
---|
245 | EXIT |
---|
246 | endif |
---|
247 | |
---|
248 | call evolve_fsm(fx,c,signal) |
---|
249 | |
---|
250 | if (fx%debug) print *, c, " ::: ", trim(fx%action) |
---|
251 | |
---|
252 | if (signal == END_OF_TAG) then |
---|
253 | ! |
---|
254 | ! We decide whether we have ended an opening tag or a closing tag |
---|
255 | ! |
---|
256 | if (fx%context == OPENING_TAG) then |
---|
257 | name = fx%element_name |
---|
258 | |
---|
259 | if (fx%debug) print *, "We have found an opening tag" |
---|
260 | if (fx%root_element_seen) then |
---|
261 | if (name .equal. fx%root_element_name) then |
---|
262 | call build_error_info(error_info, & |
---|
263 | "Duplicate root element: " // str(name), & |
---|
264 | line(fb),column(fb),fx%element_stack,SEVERE_ERROR_CODE) |
---|
265 | if (have_error_handler) then |
---|
266 | call error_handler(error_info) |
---|
267 | else |
---|
268 | call default_error_handler(error_info) |
---|
269 | endif |
---|
270 | endif |
---|
271 | if (is_empty(fx%element_stack)) then |
---|
272 | call build_error_info(error_info, & |
---|
273 | "Opening tag beyond root context: " // str(name), & |
---|
274 | line(fb),column(fb),fx%element_stack,SEVERE_ERROR_CODE) |
---|
275 | if (have_error_handler) then |
---|
276 | call error_handler(error_info) |
---|
277 | else |
---|
278 | call default_error_handler(error_info) |
---|
279 | endif |
---|
280 | endif |
---|
281 | else |
---|
282 | fx%root_element_name = name |
---|
283 | fx%root_element_seen = .true. |
---|
284 | endif |
---|
285 | call push_elstack(name,fx%element_stack) |
---|
286 | if (have_begin_handler) & |
---|
287 | call begin_element_handler(str(name),fx%attributes) |
---|
288 | |
---|
289 | else if (fx%context == CLOSING_TAG) then |
---|
290 | name = fx%element_name |
---|
291 | |
---|
292 | if (fx%debug) print *, "We have found a closing tag" |
---|
293 | if (is_empty(fx%element_stack)) then |
---|
294 | call build_error_info(error_info, & |
---|
295 | "Nesting error: End tag: " // str(name) // & |
---|
296 | " does not match -- too many end tags", & |
---|
297 | line(fb),column(fb),fx%element_stack,SEVERE_ERROR_CODE) |
---|
298 | if (have_error_handler) then |
---|
299 | call error_handler(error_info) |
---|
300 | else |
---|
301 | call default_error_handler(error_info) |
---|
302 | endif |
---|
303 | else |
---|
304 | call get_top_elstack(fx%element_stack,oldname) |
---|
305 | if (oldname .equal. name) then |
---|
306 | call pop_elstack(fx%element_stack,oldname) |
---|
307 | if (have_end_handler) call end_element_handler(str(name)) |
---|
308 | !! call pop_elstack(fx%element_stack,oldname) |
---|
309 | else |
---|
310 | call build_error_info(error_info, & |
---|
311 | "Nesting error: End tag: " // str(name) // & |
---|
312 | ". Expecting end of : " // str(oldname), & |
---|
313 | line(fb),column(fb),fx%element_stack,SEVERE_ERROR_CODE) |
---|
314 | if (have_error_handler) then |
---|
315 | call error_handler(error_info) |
---|
316 | else |
---|
317 | call default_error_handler(error_info) |
---|
318 | endif |
---|
319 | endif |
---|
320 | endif |
---|
321 | else if (fx%context == SINGLE_TAG) then |
---|
322 | name = fx%element_name |
---|
323 | |
---|
324 | if (fx%debug) print *, "We have found a single (empty) tag: ", & |
---|
325 | char(name) |
---|
326 | ! |
---|
327 | ! Push name on to stack to reveal true xpath |
---|
328 | ! |
---|
329 | call push_elstack(name,fx%element_stack) |
---|
330 | if (have_empty_handler) then |
---|
331 | if (fx%debug) print *, "--> calling empty_element_handler." |
---|
332 | call empty_element_handler(str(name),fx%attributes) |
---|
333 | call pop_elstack(fx%element_stack,dummy) |
---|
334 | else |
---|
335 | if (have_begin_handler) then |
---|
336 | if (fx%debug) print *, "--> calling begin_element_handler..." |
---|
337 | call begin_element_handler(str(name),fx%attributes) |
---|
338 | endif |
---|
339 | call pop_elstack(fx%element_stack,dummy) |
---|
340 | if (have_end_handler) then |
---|
341 | if (fx%debug) print *, "--> ... and end_element_handler." |
---|
342 | call end_element_handler(str(name)) |
---|
343 | endif |
---|
344 | endif |
---|
345 | !! call pop_elstack(fx%element_stack,dummy) |
---|
346 | |
---|
347 | else if (fx%context == CDATA_SECTION_TAG) then |
---|
348 | |
---|
349 | if (fx%debug) print *, "We found a CDATA section" |
---|
350 | if (is_empty(fx%element_stack)) then |
---|
351 | if (fx%debug) print *, & |
---|
352 | "... Warning: CDATA section outside element context" |
---|
353 | else |
---|
354 | if (have_cdata_section_handler) then |
---|
355 | call cdata_section_handler(str(fx%pcdata)) |
---|
356 | else |
---|
357 | if (have_pcdata_handler) & |
---|
358 | call pcdata_chunk_handler(str(fx%pcdata)) |
---|
359 | endif |
---|
360 | endif |
---|
361 | |
---|
362 | else if (fx%context == COMMENT_TAG) then |
---|
363 | |
---|
364 | if (fx%debug) print *, "We found a comment tag" |
---|
365 | if (have_comment_handler) & |
---|
366 | call comment_handler(str(fx%pcdata)) |
---|
367 | |
---|
368 | else if (fx%context == SGML_DECLARATION_TAG) then |
---|
369 | |
---|
370 | if (fx%debug) print *, "We found an sgml declaration" |
---|
371 | if (have_sgml_declaration_handler) & |
---|
372 | call sgml_declaration_handler(str(fx%pcdata)) |
---|
373 | |
---|
374 | else if (fx%context == XML_DECLARATION_TAG) then |
---|
375 | |
---|
376 | if (fx%debug) print *, "We found an XML declaration" |
---|
377 | name = fx%element_name |
---|
378 | if (have_xml_declaration_handler) & |
---|
379 | call xml_declaration_handler(str(name),fx%attributes) |
---|
380 | |
---|
381 | else |
---|
382 | |
---|
383 | ! do nothing |
---|
384 | |
---|
385 | endif |
---|
386 | |
---|
387 | else if (signal == CHUNK_OF_PCDATA) then |
---|
388 | |
---|
389 | if (fx%debug) print *, "We found a chunk of PCDATA" |
---|
390 | if (is_empty(fx%element_stack)) then |
---|
391 | if (fx%debug) print *, "... Warning: PCDATA outside element context" |
---|
392 | ! Just a warning |
---|
393 | call build_error_info(error_info, & |
---|
394 | "PCDATA outside of element context", & |
---|
395 | line(fb),column(fb),fx%element_stack,WARNING_CODE) |
---|
396 | if (have_error_handler) then |
---|
397 | call error_handler(error_info) |
---|
398 | else |
---|
399 | call default_error_handler(error_info) |
---|
400 | endif |
---|
401 | else |
---|
402 | ! |
---|
403 | ! Replace entities by their value |
---|
404 | ! |
---|
405 | call entity_filter(fx%pcdata,translated_pcdata,status,message) |
---|
406 | if (status < 0) then |
---|
407 | call build_error_info(error_info, message, & |
---|
408 | line(fb),-status,fx%element_stack,SEVERE_ERROR_CODE) |
---|
409 | if (have_error_handler) then |
---|
410 | call error_handler(error_info) |
---|
411 | else |
---|
412 | call default_error_handler(error_info) |
---|
413 | endif |
---|
414 | else if (status > 0) then |
---|
415 | ! Just a warning |
---|
416 | call build_error_info(error_info, message, & |
---|
417 | line(fb),status,fx%element_stack,WARNING_CODE) |
---|
418 | if (have_error_handler) then |
---|
419 | call error_handler(error_info) |
---|
420 | else |
---|
421 | call default_error_handler(error_info) |
---|
422 | endif |
---|
423 | else |
---|
424 | if (have_pcdata_handler) & |
---|
425 | call pcdata_chunk_handler(str(translated_pcdata)) |
---|
426 | endif |
---|
427 | endif |
---|
428 | |
---|
429 | else if (signal == EXCEPTION) then |
---|
430 | call build_error_info(error_info, fx%action, & |
---|
431 | line(fb),column(fb),fx%element_stack,SEVERE_ERROR_CODE) |
---|
432 | if (have_error_handler) then |
---|
433 | call error_handler(error_info) |
---|
434 | else |
---|
435 | call default_error_handler(error_info) |
---|
436 | endif |
---|
437 | else |
---|
438 | ! QUIET, do nothing |
---|
439 | endif |
---|
440 | if (signal /= QUIET) then |
---|
441 | if (have_signal_handler) then |
---|
442 | call signal_handler(pause_signal) |
---|
443 | if (pause_signal) exit |
---|
444 | endif |
---|
445 | endif |
---|
446 | |
---|
447 | enddo |
---|
448 | |
---|
449 | end subroutine xml_parse |
---|
450 | |
---|
451 | !----------------------------------------- |
---|
452 | subroutine xml_path(fxml,path) |
---|
453 | type(xml_t), intent(in) :: fxml |
---|
454 | character(len=*), intent(out) :: path |
---|
455 | |
---|
456 | call get_elstack_signature(fxml%fx%element_stack,path) |
---|
457 | |
---|
458 | end subroutine xml_path |
---|
459 | |
---|
460 | !----------------------------------------- |
---|
461 | subroutine xml_mark_path(fxml,path) |
---|
462 | ! |
---|
463 | ! Marks the current path |
---|
464 | ! |
---|
465 | type(xml_t), intent(inout) :: fxml |
---|
466 | character(len=*), intent(out) :: path |
---|
467 | |
---|
468 | call get_elstack_signature(fxml%fx%element_stack,fxml%path_mark) |
---|
469 | path = fxml%path_mark |
---|
470 | |
---|
471 | end subroutine xml_mark_path |
---|
472 | |
---|
473 | !----------------------------------------- |
---|
474 | subroutine xml_get_path_mark(fxml,path) |
---|
475 | ! |
---|
476 | ! Returns the currently markd path (or an empty string if there are no marks) |
---|
477 | ! |
---|
478 | type(xml_t), intent(in) :: fxml |
---|
479 | character(len=*), intent(out) :: path |
---|
480 | |
---|
481 | path = fxml%path_mark |
---|
482 | |
---|
483 | end subroutine xml_get_path_mark |
---|
484 | |
---|
485 | !----------------------------------------- |
---|
486 | subroutine xml_name(fxml,name) |
---|
487 | type(xml_t), intent(in) :: fxml |
---|
488 | character(len=*), intent(out) :: name |
---|
489 | |
---|
490 | name = char(fxml%fx%element_name) |
---|
491 | |
---|
492 | end subroutine xml_name |
---|
493 | !----------------------------------------- |
---|
494 | subroutine xml_attributes(fxml,attributes) |
---|
495 | type(xml_t), intent(in) :: fxml |
---|
496 | type(dictionary_t), intent(out) :: attributes |
---|
497 | |
---|
498 | attributes = fxml%fx%attributes |
---|
499 | |
---|
500 | end subroutine xml_attributes |
---|
501 | |
---|
502 | end module m_xml_parser |
---|
503 | |
---|
504 | |
---|
505 | |
---|
506 | |
---|