File: create_element.ml

package info (click to toggle)
pxp 1.2.9-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,844 kB
  • sloc: ml: 28,666; xml: 2,597; makefile: 822; sh: 691
file content (831 lines) | stat: -rw-r--r-- 17,293 bytes parent folder | download | duplicates (6)
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
(* $Id: create_element.ml,v 1.3 2001/12/15 18:06:41 gerd Exp $ *)

(* This test checks whether create_element processes the attribute list
 * correctly. The attributes are passed using the ~att_values argument, and
 * not by the main argument list. The latter is already implicitly tested
 * by the normal parser tests.
 *)

open Pxp_types
open Pxp_yacc
open Pxp_document

let conf =
  { default_config with
      encoding = `Enc_utf8;
  }
;;

let dtd =
  parse_dtd_entity 
    conf
    (from_string 
       "<!NOTATION m PUBLIC 'text/m'>
        <!NOTATION n PUBLIC 'text/n'>
        <!ENTITY e PUBLIC 'e' '' NDATA m>
        <!ENTITY f PUBLIC 'f' '' NDATA n>
        <!ELEMENT x ANY>
        <!ATTLIST x c_req CDATA #REQUIRED
                    c_def CDATA '42'
                    c_fix CDATA #FIXED 'Q'
                    c_imp CDATA #IMPLIED>
        <!ELEMENT y ANY>
        <!ATTLIST y c CDATA #IMPLIED
                    nm NMTOKEN #IMPLIED
                    nms NMTOKENS #IMPLIED
                    id ID #IMPLIED
                    enum (r|s|t) #IMPLIED
                    ent ENTITY #IMPLIED
                    ents ENTITIES #IMPLIED
                    nots NOTATION (m|n) #IMPLIED>
       ")
;;

let spec = default_spec;;

let sorted_atts e =
  let atts = e # attributes in
  List.sort (fun (a,_) (b,_) -> Pervasives.compare a b) atts
;;

let dotest name f creator =
  print_string ("Test " ^ name ^ ": ");
  flush stdout;
  try
    if f creator then
      print_endline "OK"
    else
      print_endline "FAILED (returns false)"
  with
      ex ->
	print_endline ("FAILED (exception " ^ Printexc.to_string ex ^ ")")
;;


(**********************************************************************)
(* 00x: several possibilities to create x *)


let test001 create =
  let e = create
	    ~att_values:["c_req", Value "rv"]
	    spec dtd "x" []
  in
  let atts = sorted_atts e in
  atts = ["c_def", Value "42";
	  "c_fix", Value "Q";
	  "c_imp", Implied_value;
	  "c_req", Value "rv"]
;;


let test002 create =
  let e = create
	    ~att_values:["c_req", Value "rv"; "c_fix", Value "Q"]
	    spec dtd "x" []
  in
  let atts = sorted_atts e in
  atts = ["c_def", Value "42";
	  "c_fix", Value "Q";
	  "c_imp", Implied_value;
	  "c_req", Value "rv"]
;;


let test003 create =
  let e = create
	    ~att_values:["c_req", Value "rv"; "c_imp", Implied_value]
	    spec dtd "x" []
  in
  let atts = sorted_atts e in
  atts = ["c_def", Value "42";
	  "c_fix", Value "Q";
	  "c_imp", Implied_value;
	  "c_req", Value "rv"]
;;


let test004 create =
  let e = create
	    ~att_values:["c_req", Value "rv"; "c_def", Value "43"]
	    spec dtd "x" []
  in
  let atts = sorted_atts e in
  atts = ["c_def", Value "43";
	  "c_fix", Value "Q";
	  "c_imp", Implied_value;
	  "c_req", Value "rv"]
;;

(**********************************************************************)
(* 01x: several error conditions when creating x *)

let test010 create =
  (* Missing required att *)
  try
    let e = create
	      ~att_values:[]
	      spec dtd "x" []
    in
    false
  with
      Validation_error("Required attribute `c_req' is missing") 
    | Validation_error("Attribute `c_req' has Implied_value, but is declared as #REQUIRED") ->
	true
;;


let test011 create =
  (* Bad fixed att *)
  try
    let e = create
	      ~att_values:["c_req", Value "rv"; "c_fix", Value "bad" ]
	      spec dtd "x" []
    in
    false
  with
      Validation_error("Attribute `c_fix' is fixed, but has here a different value") ->
	true
;;


let test012 create =
  (* Implied_value does not count as required value *)
  try
    let e = create
	      ~att_values:["c_req", Implied_value]
	      spec dtd "x" []
    in
    false
  with
      Validation_error("Attribute `c_req' has Implied_value, but is declared as #REQUIRED") ->
	true
;;

let test013 create =
  (* Bad fixed att (Implied_value) *)
  try
    let e = create
	      ~att_values:["c_req", Value "rv"; "c_fix", Implied_value ]
	      spec dtd "x" []
    in
    false
  with
      Validation_error("Attribute `c_fix' has Implied_value, but is not declared as #IMPLIED") ->
	true
;;


let test014 create =
  (* Implied_value not possible when default specified *)
  try
    let e = create 
	      ~att_values:["c_req", Value "rv"; "c_def", Implied_value ]
	      spec dtd "x" []
    in
    false
  with
      Validation_error("Attribute `c_def' has Implied_value, but is not declared as #IMPLIED") ->
	true
;;


let test015 create =
  (* Attributes must only occur once *)
  try
    let e = create 
	      ~att_values:["c_req", Value "rv"; "c_req", Value "rv" ]
	      spec dtd "x" []
    in
    false
  with
      WF_error("Attribute `c_req' occurs twice in element `x'")
    | WF_error("Attribute `c_req' occurs twice") ->
	true
;;


let test016 create =
  (* Attributes must only occur once *)
  try
    let e = create 
	      ~att_values:["c_req", Value "rv"; 
			   "c_imp", Implied_value; "c_imp", Value "imp" ]
	      spec dtd "x" []
    in
    false
  with
      WF_error("Attribute `c_imp' occurs twice in element `x'")
    | WF_error("Attribute `c_imp' occurs twice") ->
	true
;;


let test017 create =
  (* Attributes must only occur once *)
  try
    let e = create 
	      ~att_values:["c_req", Value "rv"; 
			   "c_imp", Implied_value; "c_imp", Implied_value ]
	      spec dtd "x" []
    in
    false
  with
      WF_error("Attribute `c_imp' occurs twice in element `x'")
    | WF_error("Attribute `c_imp' occurs twice") ->
	true
;;


let test018 create =
  (* Attributes must only occur once *)
  try
    let e = create 
	      ~att_values:["c_req", Value "rv"; 
			   "c_imp", Implied_value ]
	      spec dtd "x" [ "c_imp", "X" ]
    in
    false
  with
      WF_error("Attribute `c_imp' occurs twice in element `x'")
    | WF_error("Attribute `c_imp' occurs twice") ->
	true
;;


let test019 create =
  (* Attributes must be declared *)
  try
    let e = create 
	      ~att_values:["c_req", Value "rv"; "foo", Value "y" ]
	      spec dtd "x" [ "c_imp", "X" ]
    in
    false
  with
      Validation_error("The following attributes are not declared: foo") ->
	true
;;

(**********************************************************************)
(* 1xx: several possibilities to create y, and several error conditions *)

let test100 create =
  let e = create 
	    ~att_values:[]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test101 create =
  (* CDATA pos *)
  let e = create 
	    ~att_values:[ "c", Value "X" ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Value "X";
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test102 create =
  (* CDATA neg *)
  try
    let e = create 
	      ~att_values:[ "c", Valuelist [ "X"; "Y" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A list value cannot be assigned to attribute `c'") ->
	true
;;


let test103 create =
  (* ENTITY pos *)
  let e = create 
	    ~att_values:[ "ent", Value "e" ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Value "e";
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test104 create =
  (* ENTITY neg *)
  try
    let e = create 
	      ~att_values:[ "ent", Value "g" ]
	      spec dtd "y" []
    in
    false
  with
      WF_error("Reference to undeclared general entity `g'") ->
	true
;;


let test105 create =
  (* ENTITY neg *)
  try
    let e = create 
	      ~att_values:[ "ent", Valuelist [ "X"; "Y" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A list value cannot be assigned to attribute `ent'") ->
	true
;;


let test106 create =
  (* ENTITY neg *)
  try
    let e = create 
	      ~att_values:[ "ent", Value " e" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `ent' has leading or trailing whitespace") ->
	true
;;


let test107 create =
  (* ENTITIES pos *)
  let e = create 
	    ~att_values:[ "ents", Valuelist [ "e"; "f" ] ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Valuelist [ "e"; "f" ];
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test108 create =
  (* ENTITIES neg *)
  try
    let e = create 
	      ~att_values:[ "ents", Valuelist [ "e"; "g" ] ]
	      spec dtd "y" []
    in
    false
  with
      WF_error("Reference to undeclared general entity `g'") ->
	true
;;


let test109 create =
  (* ENTITIES neg *)
  try
    let e = create 
	      ~att_values:[ "ents", Value "X" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A non-list value cannot be assigned to attribute `ents'") ->
	true
;;


let test110 create =
  (* ENTITIES neg *)
  try
    let e = create 
	      ~att_values:[ "ents", Valuelist [ "e"; "f " ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `ents' has leading or trailing whitespace") ->
	true
;;


let test111 create =
  (* enum pos *)
  let e = create 
	    ~att_values:[ "enum", Value "s" ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Value "s";
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test112 create =
  (* enum neg *)
  try
    let e = create 
	      ~att_values:[ "enum", Value "q" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `enum' does not match one of the declared enumerator tokens") ->
	true
;;


let test113 create =
  (* enum neg *)
  try
    let e = create 
	      ~att_values:[ "enum", Value " t" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `enum' has leading or trailing whitespace") ->
	true
;;


let test114 create =
  (* enum neg *)
  try
    let e = create 
	      ~att_values:[ "enum", Valuelist [ "X"; "Y" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A list value cannot be assigned to attribute `enum'") ->
	true
;;


let test115 create =
  (* ID pos *)
  let e = create 
	    ~att_values:[ "id", Value "five" ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Value "five";
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test116 create =
  (* ID neg *)
  try
    let e = create 
	      ~att_values:[ "id", Value " t" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `id' has leading or trailing whitespace") ->
	true
;;


let test117 create =
  (* ID neg *)
  try
    let e = create 
	      ~att_values:[ "id", Value "5" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `id' is lexically malformed") ->
	true
;;


let test118 create =
  (* ID neg *)
  try
    let e = create 
	      ~att_values:[ "id", Valuelist [ "X"; "Y" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A list value cannot be assigned to attribute `id'") ->
	true
;;


let test119 create =
  (* NMTOKEN pos *)
  let e = create 
	    ~att_values:[ "nm", Value "5" ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Value "5";
	  "nms", Implied_value;
	  "nots", Implied_value
	 ]
;;


let test120 create =
  (* NMTOKEN neg *)
  try
    let e = create 
	      ~att_values:[ "nm", Value " t" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `nm' has leading or trailing whitespace") ->
	true
;;


let test121 create =
  (* NMTOKEN neg *)
  try
    let e = create 
	      ~att_values:[ "nm", Value "+5" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `nm' is lexically malformed") ->
	true
;;


let test122 create =
  (* NMTOKEN neg *)
  try
    let e = create 
	      ~att_values:[ "nm", Valuelist [ "X"; "Y" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A list value cannot be assigned to attribute `nm'") ->
	true
;;


let test123 create =
  (* NMTOKENS pos *)
  let e = create 
	    ~att_values:[ "nms", Valuelist [ "_six"; "5" ] ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Valuelist ["_six"; "5"];
	  "nots", Implied_value
	 ]
;;


let test124 create =
  (* NMTOKENS neg *)
  try
    let e = create 
	      ~att_values:[ "nms", Valuelist [ "x"; "t "] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `nms' has leading or trailing whitespace") ->
	true
;;


let test125 create =
  (* NMTOKENS neg *)
  try
    let e = create 
	      ~att_values:[ "nms", Valuelist [ "5 6" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `nms' is lexically malformed") ->
	true
;;


let test126 create =
  (* NMTOKENS neg *)
  try
    let e = create 
	      ~att_values:[ "nms", Value "X" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A non-list value cannot be assigned to attribute `nms'") ->
	true
;;


let test127 create =
  (* NOTATION pos *)
  let e = create 
	    ~att_values:[ "nots", Value "m" ]
	    spec dtd "y" []
  in
  let atts = sorted_atts e in
  atts = ["c", Implied_value;
	  "ent", Implied_value;
	  "ents", Implied_value;
	  "enum", Implied_value;
	  "id", Implied_value;
	  "nm", Implied_value;
	  "nms", Implied_value;
	  "nots", Value "m"
	 ]
;;


let test128 create =
  (* NOTATION neg *)
  try
    let e = create 
	      ~att_values:[ "nots", Value "q" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `nots' does not match one of the declared notation names") ->
	true
;;


let test129 create =
  (* NOTATION neg *)
  try
    let e = create 
	      ~att_values:[ "nots", Value " t" ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("Attribute `nots' has leading or trailing whitespace") ->
	true
;;


let test130 create =
  (* NOTATION neg *)
  try
    let e = create 
	      ~att_values:[ "nots", Valuelist [ "X"; "Y" ] ]
	      spec dtd "y" []
    in
    false
  with
      Validation_error("A list value cannot be assigned to attribute `nots'") ->
	true
;;

(**********************************************************************)

let test_series create =
  dotest "001" test001 create;
  dotest "002" test002 create;
  dotest "003" test003 create;
  dotest "004" test004 create;

  dotest "010" test010 create;
  dotest "011" test011 create;
  dotest "012" test012 create;
  dotest "013" test013 create;
  dotest "014" test014 create;
  dotest "015" test015 create;
  dotest "016" test016 create;
  dotest "017" test017 create;
  dotest "018" test018 create;
  dotest "019" test019 create;
  
  dotest "100" test100 create;
  dotest "101" test101 create;
  dotest "102" test102 create;
  dotest "103" test103 create;
  dotest "104" test104 create;
  dotest "105" test105 create;
  dotest "106" test106 create;
  dotest "107" test107 create;
  dotest "108" test108 create;
  dotest "109" test109 create;
  dotest "110" test110 create;
  dotest "111" test111 create;
  dotest "112" test112 create;
  dotest "113" test113 create;
  dotest "114" test114 create;
  dotest "115" test115 create;
  dotest "116" test116 create;
  dotest "117" test117 create;
  dotest "118" test118 create;
  dotest "119" test119 create;
  dotest "120" test120 create;
  dotest "121" test121 create;
  dotest "122" test122 create;
  dotest "123" test123 create;
  dotest "124" test124 create;
  dotest "125" test125 create;
  dotest "126" test126 create;
  dotest "127" test127 create;
  dotest "128" test128 create;
  dotest "129" test129 create;
  dotest "130" test130 create;
  ()
;;

print_endline "Series: create_element, early validation";
test_series 
  (fun ~att_values spec dtd name atts ->
     create_element_node ~att_values spec dtd name atts);

print_endline "Series: create_element, deferred validation";
test_series
  (fun ~att_values spec dtd name atts ->
     let e =
       try
	 create_element_node 
	   ~att_values
	   ~valcheck:false
	   spec dtd name atts
       with
	 | WF_error _ as e ->
	     (* Well-formedness errors will be raised here *)
	     raise e
	 | e ->
	     (* Validation errors must not happen here. So catch them
	      * and report them.
	      *)
	     failwith ("Early exception: " ^ Printexc.to_string e)
     in
     e # complement_attlist();
     e # validate_attlist();
     e
  );
()
;;