/
m_sax_tokenizer.F90
1139 lines (1069 loc) · 36.4 KB
/
m_sax_tokenizer.F90
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module m_sax_tokenizer
#ifndef DUMMYLIB
use fox_m_fsys_array_str, only: vs_str, str_vs, vs_str_alloc
use fox_m_fsys_varstr
use m_common_charset, only: XML_WHITESPACE, &
upperCase, isInitialNameChar
use m_common_error, only: add_error, in_error
use m_common_entities, only: entity_t, existing_entity, &
expand_entity_text, expand_char_entity, &
add_internal_entity, pop_entity_list, getEntityByName
use m_common_namecheck, only: checkName, checkCharacterEntityReference
use m_sax_reader, only: file_buffer_t, open_new_file, pop_buffer_stack, &
push_chars, get_character, get_all_characters, &
parse_text_declaration, add_error_position
use m_sax_types ! everything, really
#ifdef PGF90
use fox_m_utils_uri, only: URI
#endif
implicit none
private
public :: sax_tokenize
public :: normalize_attribute_text
public :: expand_pe_text
contains
subroutine sax_tokenize(fx, fb, eof)
type(sax_parser_t), intent(inout) :: fx
type(file_buffer_t), intent(inout) :: fb
logical, intent(out) :: eof
character :: c, q
integer :: xv, phrase
logical :: firstChar, ws_discard
character, pointer :: tempString(:)
xv = fx%xds%xml_version
call set_varstr_empty(fx%token)
if (fx%nextTokenType/=TOK_NULL) then
eof = .false.
fx%tokenType = fx%nextTokenType
fx%nextTokenType = TOK_NULL
return
endif
fx%tokentype = TOK_NULL
q = " "
phrase = 0
firstChar = .true.
ws_discard = .false.
do
c = get_character(fb, eof, fx%error_stack)
if (eof) then
if (fx%state==ST_CHAR_IN_CONTENT) then
if (phrase==1) then
call append_varstr(fx%token,']')
elseif (phrase==2) then
call append_varstr(fx%token,']]')
endif
fx%tokenType = TOK_CHAR
endif
if (fx%tokenType/=TOK_NULL) then
! make sure we pass back this token before eof'ing
eof = .false.
return
endif
endif
if (eof.or.in_error(fx%error_stack)) return
if (fx%inIntSubset) then
tempString => fx%xds%intSubset
fx%xds%intSubset => vs_str_alloc(str_vs(tempString)//c)
deallocate(tempString)
endif
select case (fx%state)
case (ST_MISC)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c, XML_WHITESPACE)>0) then
if (c=="<") then
ws_discard = .false.
else
call add_error(fx%error_stack, "Unexpected character found outside content")
call add_error_position(fx%error_stack,fb)
endif
endif
else
if (c=="?") then
fx%tokenType = TOK_PI_TAG
elseif (c=="!") then
fx%tokenType = TOK_BANG_TAG
elseif (isInitialNameChar(c, xv)) then
call push_chars(fb, c)
fx%tokenType = TOK_OPEN_TAG
else
call add_error(fx%error_stack, "Unexpected character after <")
call add_error_position(fx%error_stack,fb)
endif
endif
case (ST_BANG_TAG)
if (firstChar) then
if (c=="-") then
phrase = 1
elseif (c=="[") then
fx%tokenType = TOK_OPEN_SB
elseif (verify(c,upperCase)==0) then
call varstr_str(fx%token,c)
else
call add_error(fx%error_stack, "Unexpected character after <!")
call add_error_position(fx%error_stack,fb)
endif
elseif (phrase==1) then
if (c=="-") then
fx%tokenType = TOK_OPEN_COMMENT
else
call add_error(fx%error_stack, "Unexpected character after <!-")
call add_error_position(fx%error_stack,fb)
endif
elseif (verify(c,XML_WHITESPACE)>0) then
call append_varstr(fx%token,c)
else
call push_chars(fb, c)
fx%tokenType = TOK_NAME
endif
case (ST_START_PI)
! grab until whitespace or ?
if (verify(c, XML_WHITESPACE//"?")>0) then
call append_varstr( fx%token, c )
else
fx%tokenType = TOK_NAME
if (c=="?") call push_chars(fb, c)
endif
case (ST_PI_CONTENTS)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c, XML_WHITESPACE)/=0) then
ws_discard = .false.
else
cycle
endif
endif
if (phrase==1) then
if (c==">") then
fx%tokenType = TOK_CHAR
fx%nextTokenType = TOK_PI_END
elseif (c=="?") then
! The last ? didn't mean anything, but this one might.
call append_varstr( fx%token, '?' )
else
phrase = 0
call append_varstr( fx%token, '?' )
call append_varstr( fx%token, c )
endif
elseif (c=="?") then
phrase = 1
else
call append_varstr( fx%token, c )
endif
case (ST_START_COMMENT)
select case(phrase)
case (0)
if (c=="-") then
phrase = 1
else
call append_varstr( fx%token, c )
endif
case (1)
if (c=="-") then
phrase = 2
else
call append_varstr( fx%token, '-' )
call append_varstr( fx%token, c )
phrase = 0
endif
case (2)
if (c==">") then
fx%tokenType = TOK_CHAR
fx%nextTokenType = TOK_COMMENT_END
exit
else
call add_error(fx%error_stack, &
"Expecting > after -- inside a comment.")
call add_error_position(fx%error_stack,fb)
endif
end select
case (ST_START_TAG)
! grab until whitespace or /, >
if (verify(c, XML_WHITESPACE//"/>")>0) then
call append_varstr( fx%token, c )
else
fx%tokenType = TOK_NAME
if (c==">") then
fx%nextTokenType = TOK_END_TAG
else
call push_chars(fb, c)
endif
endif
case (ST_START_CDATA_DECLARATION)
if (firstChar) then
if (verify(c, XML_WHITESPACE)==0) then
call add_error(fx%error_stack, &
"Whitespace not allowed around CDATA in section declaration")
call add_error_position(fx%error_stack,fb)
else
call varstr_str(fx%token, c)
ws_discard = .false.
endif
else
if (verify(c, XML_WHITESPACE)==0) then
call add_error(fx%error_stack, &
"Whitespace not allowed around CDATA in section declaration")
call add_error_position(fx%error_stack,fb)
elseif (c=="[") then
fx%tokenType = TOK_NAME
if (c=="[") fx%nextTokenType = TOK_OPEN_SB
else
call append_varstr( fx%token, c )
endif
endif
case (ST_CDATA_CONTENTS)
select case(phrase)
case (0)
if (c=="]") then
phrase = 1
else
call append_varstr( fx%token, c )
endif
case (1)
if (c=="]") then
phrase = 2
else
call append_varstr( fx%token, ']' )
call append_varstr( fx%token, c )
phrase = 0
endif
case (2)
if (c==">") then
fx%tokenType = TOK_CHAR
fx%nextTokenType = TOK_SECTION_END
elseif (c=="]") then
call append_varstr( fx%token, ']' )
else
call append_varstr( fx%token, ']]' )
call append_varstr( fx%token, c )
phrase = 0
endif
end select
case (ST_IN_TAG)
if (firstChar) then
if (verify(c,XML_WHITESPACE//"/>")>0) then
call add_error(fx%error_stack, &
"Whitespace required inside tag")
call add_error_position(fx%error_stack,fb)
exit
endif
ws_discard = .true.
endif
if (ws_discard) then
if (verify(c,XML_WHITESPACE)>0) then
if (c==">") then
fx%tokenType = TOK_END_TAG
elseif (c=="/") then
phrase = 1
ws_discard = .false.
else
call varstr_str(fx%token,c)
ws_discard = .false.
endif
endif
else
if (phrase==1) then
if (c==">") then
fx%tokenType = TOK_END_TAG_CLOSE
else
call add_error(fx%error_stack, &
"Unexpected character after '/' ("//c//") in tag for element '"//str_varstr(fx%name)//"'")
call add_error_position(fx%error_stack,fb)
exit
endif
else
if (verify(c,XML_WHITESPACE//"=/>")==0) then
fx%tokenType = TOK_NAME
if (c=="=") then
fx%nextTokenType = TOK_EQUALS
elseif (c==">") then
fx%nextTokenType = TOK_END_TAG
else
call push_chars(fb, c)
endif
else
call append_varstr( fx%token, c )
endif
endif
endif
case (ST_ATT_NAME)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c,XML_WHITESPACE)>0) then
if (c=="=") then
fx%tokenType = TOK_EQUALS
else
call add_error(fx%error_stack, &
"Unexpected character in element tag, expected =")
call add_error_position(fx%error_stack,fb)
endif
endif
endif
case (ST_ATT_EQUALS)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c,XML_WHITESPACE)>0) then
if (verify(c,"'""")==0) then
q = c
ws_discard = .false.
else
call add_error(fx%error_stack, "Expecting "" or '")
call add_error_position(fx%error_stack,fb)
endif
endif
else
if (c==q) then
fx%tokenType = TOK_CHAR
else
call append_varstr( fx%token, c )
endif
endif
case (ST_CHAR_IN_CONTENT)
if (c=="<".or.c=="&") then
if (phrase==1) then
call append_varstr( fx%token, ']' )
elseif (phrase==2) then
call append_varstr( fx%token, ']]' )
endif
fx%tokenType = TOK_CHAR
if (c=="<") then
call push_chars(fb, c)
elseif (c=="&") then
fx%nextTokenType = TOK_ENTITY
endif
elseif (c=="]") then
if (phrase==0) then
phrase = 1
elseif (phrase==1) then
phrase = 2
else
call append_varstr( fx%token, ']' )
endif
elseif (c==">") then
if (phrase==1) then
phrase = 0
call append_varstr( fx%token, ']>' )
elseif (phrase==2) then
call add_error(fx%error_stack, "]]> forbidden in character context")
call add_error_position(fx%error_stack,fb)
else
call append_varstr( fx%token, '>' )
endif
elseif (phrase==1) then
call append_varstr( fx%token, ']' )
call append_varstr( fx%token, c )
phrase = 0
elseif (phrase==2) then
call append_varstr( fx%token, ']]' )
call append_varstr( fx%token, c )
phrase = 0
else
call append_varstr( fx%token, c )
endif
case (ST_TAG_IN_CONTENT)
if (phrase==0) then
if (c=="<") then
phrase = 1
ws_discard = .false.
elseif (c=="&") then
fx%tokenType = TOK_ENTITY
else
call add_error(fx%error_stack, "Unexpected character found in content")
call add_error_position(fx%error_stack,fb)
endif
elseif (phrase==1) then
if (c=="?") then
fx%tokenType = TOK_PI_TAG
elseif (c=="!") then
fx%tokenType = TOK_BANG_TAG
elseif (c=="/") then
fx%tokenType = TOK_CLOSE_TAG
elseif (isInitialNameChar(c, xv)) then
call push_chars(fb, c)
fx%tokenType = TOK_OPEN_TAG
else
call add_error(fx%error_stack, "Unexpected character after <")
call add_error_position(fx%error_stack,fb)
endif
endif
case (ST_START_ENTITY)
if (verify(c,XML_WHITESPACE//";")>0) then
call append_varstr( fx%token, c )
elseif (c==";") then
fx%tokenType = TOK_NAME
else
call add_error(fx%error_stack, "Entity reference must be terminated with a ;")
call add_error_position(fx%error_stack,fb)
endif
case (ST_CLOSING_TAG)
if (verify(c,XML_WHITESPACE//">")>0) then
call append_varstr( fx%token, c )
else
fx%tokenType = TOK_NAME
if (c==">") fx%nextTokenType = TOK_END_TAG
endif
case (ST_IN_CLOSING_TAG)
if (verify(c, XML_WHITESPACE)>0) then
if (c==">") then
fx%tokenType = TOK_END_TAG
else
call add_error(fx%error_stack, "Unexpected character - expecting >")
call add_error_position(fx%error_stack,fb)
endif
endif
case (ST_IN_DOCTYPE, ST_DOC_NAME, ST_DOC_SYSTEM, ST_DOC_PUBLIC, &
ST_DOC_DECL, ST_CLOSE_DOCTYPE)
if (firstChar) then
if (verify(c, XML_WHITESPACE)>0) then
if (c==">") then
fx%tokenType = TOK_END_TAG
elseif (c=="[") then
fx%tokenType = TOK_OPEN_SB
else
call add_error(fx%error_stack, &
"Missing whitespace in doctype delcaration.")
call add_error_position(fx%error_stack,fb)
endif
else
ws_discard = .true.
endif
endif
if (ws_discard) then
if (verify(c, XML_WHITESPACE)>0) then
if (verify(c, "'""")==0) then
q = c
call set_varstr_empty(fx%token)
ws_discard = .false.
elseif (c=="[") then
fx%tokenType = TOK_OPEN_SB
elseif (c==">") then
fx%tokenType = TOK_END_TAG
else
call varstr_str(fx%token, c)
ws_discard = .false.
endif
endif
else
if (q/=" ".and.c==q) then
fx%tokenType = TOK_CHAR
elseif (q==" ".and.verify(c, ">")==0.and.(fx%state==ST_IN_DOCTYPE)) then
fx%nextTokenType = TOK_END_TAG
fx%tokenType = TOK_NAME
elseif (q==" ".and.verify(c, XML_WHITESPACE)==0) then
call push_chars(fb, c)
fx%tokenType = TOK_NAME
else
call append_varstr( fx%token, c )
endif
endif
case (ST_IN_SUBSET)
call tokenizeDTD
case (ST_START_PE)
if (verify(c,XML_WHITESPACE//";")>0) then
call append_varstr( fx%token, c )
elseif (c==";") then
fx%tokenType = TOK_NAME
else
call add_error(fx%error_stack, "Entity reference must be terminated with a ;")
call add_error_position(fx%error_stack,fb)
endif
end select
firstChar = .false.
if (fx%tokenType/=TOK_NULL) exit
enddo
contains
subroutine tokenizeDTD
if (c=="%") then
if (fx%state_dtd==ST_DTD_START_COMMENT &
.or.fx%state_dtd==ST_DTD_START_PI &
.or.fx%state_dtd==ST_DTD_PI_CONTENTS &
.or.fx%state_dtd==ST_DTD_ENTITY &
.or.fx%state_dtd==ST_DTD_NOTATION_SYSTEM &
.or.fx%state_dtd==ST_DTD_NOTATION_PUBLIC &
.or.fx%state_dtd==ST_DTD_NOTATION_PUBLIC_2 &
.or.fx%state_dtd==ST_DTD_ENTITY_PUBLIC &
.or.fx%state_dtd==ST_DTD_ENTITY_SYSTEM) then
! % is perfectly legitimate
continue
elseif (fx%state_dtd==ST_DTD_SUBSET) then
if (.not.fx%spaceBeforeEntity) then
fx%spaceBeforeEntity = .true.
call push_chars(fb, c)
c = " "
else
fx%spaceBeforeEntity = .false.
fx%tokenType = TOK_ENTITY
return
endif
elseif (fx%state_dtd==ST_DTD_ATTLIST_CONTENTS &
.and. q/="") then
! We are inside a ATTLIST attvalue, so apparent PErefs arent.
continue
elseif (fx%inIntSubset) then
call add_error(fx%error_stack, &
"Parameter entity reference not permitted inside markup for internal subset")
call add_error_position(fx%error_stack,fb)
return
elseif (fx%state_dtd==ST_DTD_ATTLIST_CONTENTS &
.or.fx%state_dtd==ST_DTD_ELEMENT_CONTENTS) then
if (is_varstr_null(fx%content)) then
! content will not always be empty here;
! if we have two PErefs bang next to each other.
call move_varstr_varstr(fx%token,fx%content)
endif
fx%tokenType = TOK_ENTITY
return
elseif (fx%state_dtd==ST_DTD_ENTITY_ID) then
! % is ok if we are in the external subset
continue
else
if (.not.fx%spaceBeforeEntity) then
fx%spaceBeforeEntity = .true.
call push_chars(fb, c)
c = " "
else
fx%spaceBeforeEntity = .false.
fx%tokenType = TOK_ENTITY
return
endif
endif
endif
select case(fx%state_dtd)
case (ST_DTD_SUBSET)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c, XML_WHITESPACE)>0) then
if (c=="]") then
phrase = 1
q = c
ws_discard = .false.
elseif (c=="<") then
phrase = 1
q = c
ws_discard = .false.
else
call add_error(fx%error_stack, "Unexpected character found in document subset")
call add_error_position(fx%error_stack,fb)
endif
endif
elseif (phrase==1) then
if (q=="<") then
if (c=="?") then
fx%tokenType = TOK_PI_TAG
elseif (c=="!") then
fx%tokenType = TOK_BANG_TAG
else
call add_error(fx%error_stack, "Unexpected character, expecting ! or ?")
call add_error_position(fx%error_stack,fb)
endif
elseif (q=="]") then
if (c=="]") then
phrase = 2
else
fx%tokenType = TOK_CLOSE_SB
call push_chars(fb, c)
if (fx%inIntSubset) then
tempString => fx%xds%intSubset
allocate(fx%xds%intSubset(size(tempString)-2))
fx%xds%intSubset = tempString(:size(tempString)-2)
deallocate(tempString)
endif
endif
endif
elseif (phrase==2) then
if (c==">") then
fx%tokenType = TOK_SECTION_END
else
call add_error(fx%error_stack, "Unexpected character, expecting >")
call add_error_position(fx%error_stack,fb)
endif
endif
case (ST_DTD_BANG_TAG)
if (firstChar) then
if (c=="-") then
phrase = 1
elseif (c=="[") then
fx%tokenType = TOK_OPEN_SB
elseif (verify(c,upperCase)==0) then
call varstr_str(fx%token,c)
else
call add_error(fx%error_stack, "Unexpected character after <!")
call add_error_position(fx%error_stack,fb)
endif
elseif (phrase==1) then
if (c=="-") then
fx%tokenType = TOK_OPEN_COMMENT
else
call add_error(fx%error_stack, "Unexpected character after <!-")
call add_error_position(fx%error_stack,fb)
endif
elseif (verify(c,XML_WHITESPACE)>0) then
call append_varstr( fx%token, c )
else
call push_chars(fb, c)
fx%tokenType = TOK_NAME
endif
case (ST_DTD_START_SECTION_DECL)
if (firstChar) then
ws_discard = .true.
endif
if (ws_discard) then
if (verify(c, XML_WHITESPACE)/=0) then
call varstr_str(fx%token, c)
ws_discard = .false.
endif
else
if (verify(c, XML_WHITESPACE//"[")>0) then
call append_varstr( fx%token, c )
else
fx%tokenType = TOK_NAME
if (c=="[") fx%nextTokenType = TOK_OPEN_SB
endif
endif
case (ST_DTD_FINISH_SECTION_DECL)
if (verify(c, XML_WHITESPACE)>0) then
if (c/="[") then
call add_error(fx%error_stack, &
"Unexpected token found, expecting [")
call add_error_position(fx%error_stack,fb)
else
fx%tokenType = TOK_OPEN_SB
endif
endif
case (ST_DTD_IN_IGNORE_SECTION)
select case(phrase)
case (0)
if (c=="<".or.c=="]") then
phrase = 1
q = c
endif
case (1)
if ((q=="<".and.c=="!").or.(q=="]".and.c=="]")) then
phrase = 2
else
phrase = 0
endif
case (2)
if (q=="<".and.c=="[") then
fx%tokenType = TOK_SECTION_START
elseif (q=="]".and.c==">") then
fx%tokenType = TOK_SECTION_END
else
phrase = 0
endif
end select
case (ST_DTD_START_PI)
! grab until whitespace or ?
if (verify(c, XML_WHITESPACE//"?")>0) then
call append_varstr( fx%token, c )
else
fx%tokenType = TOK_NAME
if (c=="?") call push_chars(fb, c)
endif
case (ST_DTD_PI_CONTENTS)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c, XML_WHITESPACE)/=0) then
ws_discard = .false.
else
return
endif
endif
if (phrase==1) then
if (c==">") then
fx%tokenType = TOK_CHAR
fx%nextTokenType = TOK_PI_END
elseif (c=="?") then
! The last ? didn't mean anything, but this one might.
call append_varstr( fx%token, '?' )
else
phrase = 0
call append_varstr( fx%token, '?' )
call append_varstr( fx%token, c )
endif
elseif (c=="?") then
phrase = 1
else
call append_varstr( fx%token, c )
endif
case (ST_DTD_START_COMMENT)
select case(phrase)
case (0)
if (c=="-") then
phrase = 1
else
call append_varstr( fx%token, c )
endif
case (1)
if (c=="-") then
phrase = 2
else
call append_varstr( fx%token, '-' )
call append_varstr( fx%token, c )
phrase = 0
endif
case (2)
if (c==">") then
fx%tokenType = TOK_CHAR
call append_varstr( fx%token, c )
fx%nextTokenType = TOK_COMMENT_END
else
call add_error(fx%error_stack, &
"Expecting > after -- inside a comment.")
call add_error_position(fx%error_stack,fb)
endif
end select
case (ST_DTD_ATTLIST, ST_DTD_ELEMENT, ST_DTD_ENTITY, &
ST_DTD_ENTITY_PE, ST_DTD_NOTATION)
if (firstChar) ws_discard = .true.
if (ws_discard) then
if (verify(c,XML_WHITESPACE)>0) then
call varstr_str(fx%token, c)
ws_discard = .false.
endif
elseif (verify(c,XML_WHITESPACE//">")>0) then
call append_varstr( fx%token, c )
else
if (c==">") then
fx%nextTokenType = TOK_END_TAG
else
call push_chars(fb, c)
endif
fx%tokenType = TOK_NAME
endif
case (ST_DTD_ELEMENT_CONTENTS)
if (c==">") then
if (.not.is_varstr_null(fx%content)) then
call move_varstr_varstr(fx%content,fx%token)
endif
fx%tokenType = TOK_DTD_CONTENTS
fx%nextTokenType = TOK_END_TAG
else
if (.not.is_varstr_null(fx%content)) then
call move_varstr_varstr(fx%content,fx%token)
call append_varstr(fx%token, c)
else
call append_varstr(fx%token, c)
endif
if (c=="(") then
fx%tokenType = TOK_OPEN_PAR
call move_varstr_varstr(fx%token,fx%content)
elseif (c==")") then
fx%tokenType = TOK_CLOSE_PAR
call move_varstr_varstr(fx%token,fx%content)
endif
endif
case (ST_DTD_ATTLIST_CONTENTS)
if (c==">") then
fx%tokenType = TOK_DTD_CONTENTS
fx%nextTokenType = TOK_END_TAG
elseif (.not.is_varstr_null(fx%content)) then
call move_varstr_varstr(fx%content,fx%token)
call append_varstr(fx%token, c)
else
call append_varstr( fx%token, c )
endif
if (c=="'".or.c=="""") then
if (q==c) then
q = ""
else
q = c
endif
endif
case (ST_DTD_ENTITY_ID, ST_DTD_ENTITY_PUBLIC, ST_DTD_ENTITY_SYSTEM, &
ST_DTD_ENTITY_NDATA, ST_DTD_ENTITY_END, ST_DTD_ENTITY_NDATA_VALUE, &
ST_DTD_NOTATION_ID, ST_DTD_NOTATION_SYSTEM, ST_DTD_NOTATION_PUBLIC, &
ST_DTD_NOTATION_PUBLIC_2, ST_DTD_NOTATION_END)
if (firstChar) then
if (verify(c, XML_WHITESPACE)>0) then
if (c==">") then
fx%tokenType = TOK_END_TAG
else
call add_error(fx%error_stack, "Missing whitespace in DTD.")
call add_error_position(fx%error_stack,fb)
endif
else
ws_discard = .true.
endif
elseif (ws_discard) then
if (verify(c, XML_WHITESPACE)>0) then
if (verify(c, "'""")==0) then
q = c
call set_varstr_empty(fx%token)
ws_discard = .false.
elseif (c==">") then
fx%tokenType = TOK_END_TAG
else
call varstr_str( fx%token, c )
ws_discard = .false.
endif
endif
else
if (q/=" ".and.c==q) then
fx%tokenType = TOK_CHAR
elseif (q==" ".and.verify(c, XML_WHITESPACE//">")==0) then
fx%tokenType = TOK_NAME
if (c==">") then
fx%nextTokenType = TOK_END_TAG
else
call push_chars(fb, c)
endif
else
call append_varstr( fx%token, c )
endif
endif
end select
end subroutine tokenizeDTD
end subroutine sax_tokenize
recursive function normalize_attribute_text(fx, s_in, fb) result(s_out)
type(sax_parser_t), intent(inout) :: fx
character, dimension(:), intent(in) :: s_in
type(file_buffer_t), intent(in) :: fb
character, dimension(:), pointer :: s_out
character, dimension(:), pointer :: s_temp, s_temp2, s_ent, tempString
character :: dummy
integer :: i, i2, j
type(entity_t), pointer :: ent
#ifdef PGF90
type(URI), pointer :: nullURI
nullURI => null()
#endif
! Condense all whitespace, only if we are validating,
! Expand all &
! Complain about < and &
allocate(s_temp(size(s_in))) ! in the first instance
allocate(s_out(0)) ! in case we return early ...
s_ent => null()
tempString => null()
i2 = 1
i = 1
do
if (i > size(s_in)) exit
! Firstly, all whitespace must become 0x20
if (verify(s_in(i),XML_WHITESPACE)==0) then
s_temp(i2) = " "
! Then, < is always illegal
i = i + 1
i2 = i2 + 1
elseif (s_in(i)=='<') then
call add_error(fx%error_stack, "Illegal '<' found in attribute.")
call add_error_position(fx%error_stack,fb)
goto 100
! Then, expand <
elseif (s_in(i)=='&') then
j = index(str_vs(s_in(i+1:)), ';')
if (j==0) then
call add_error(fx%error_stack, "Illegal '&' found in attribute")
call add_error_position(fx%error_stack,fb)
goto 100
elseif (j==1) then
call add_error(fx%error_stack, "No entity reference found")
call add_error_position(fx%error_stack,fb)
goto 100
endif
allocate(tempString(j-1))
tempString = s_in(i+1:i+j-1)
if (existing_entity(fx%predefined_e_list, str_vs(tempString))) then
! Expand immediately
s_temp(i2) = expand_entity_text(fx%predefined_e_list, str_vs(tempString))
i = i + j + 1
i2 = i2 + 1
elseif (checkCharacterEntityReference(str_vs(tempString), fx%xds%xml_version)) then
! Expand all character entities
s_temp(i2) = expand_char_entity(str_vs(tempString))
i = i + j + 1
i2 = i2 + 1 ! fixme
elseif (checkName(str_vs(tempString), fx%xds%xml_version)) then
ent => getEntityByName(fx%forbidden_ge_list, str_vs(tempString))
if (associated(ent)) then
call add_error(fx%error_stack, 'Recursive entity expansion')
call add_error_position(fx%error_stack,fb)
goto 100
else
ent => getEntityByName(fx%xds%entityList, str_vs(tempString))
endif
if (associated(ent)) then
if (ent%wfc.and.fx%xds%standalone) then
call add_error(fx%error_stack, 'Externally declared entity referenced in standalone document')
call add_error_position(fx%error_stack,fb)
goto 100
endif
!is it the right sort of entity?
if (ent%external) then
call add_error(fx%error_stack, "External entity forbidden in attribute")
call add_error_position(fx%error_stack,fb)
goto 100
endif
#ifdef PGF90
call add_internal_entity(fx%forbidden_ge_list, str_vs(tempString), "", nullURI, .false.)
#else
call add_internal_entity(fx%forbidden_ge_list, str_vs(tempString), "", null(), .false.)
#endif
! Recursively expand entity, checking for errors.
s_ent => normalize_attribute_text(fx, &
vs_str(expand_entity_text(fx%xds%entityList, str_vs(tempString))),fb)
dummy = pop_entity_list(fx%forbidden_ge_list)
if (in_error(fx%error_stack)) then
goto 100
endif
allocate(s_temp2(size(s_temp)+size(s_ent)-j))
s_temp2(:i2-1) = s_temp(:i2-1)
s_temp2(i2:i2+size(s_ent)-1) = s_ent
deallocate(s_temp)
s_temp => s_temp2
nullify(s_temp2)
i = i + j + 1
i2 = i2 + size(s_ent)
deallocate(s_ent)
else
s_temp(i2:i2+j) = s_in(i:i+j)
i = i + j + 1
i2 = i2 + j + 1
if (.not.fx%skippedExternal.or.fx%xds%standalone) then
call add_error(fx%error_stack, "Undeclared entity encountered in standalone document.")
call add_error_position(fx%error_stack,fb)
goto 100
endif
endif
else
call add_error(fx%error_stack, "Illegal entity reference")
call add_error_position(fx%error_stack,fb)
goto 100
endif
deallocate(tempString)
else
s_temp(i2) = s_in(i)
i = i + 1
i2 = i2 + 1
endif
enddo