-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtnc.c
2980 lines (2811 loc) · 100 KB
/
tnc.c
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
/* This code implements a set of tDOM C Handlers that can be
dynamically loaded into a tclsh with already loaded tDOM
package. This parser extension does some tests according to the DTD
against the data within an XML document.
Copyright (c) 2001-2003 Rolf Ade */
#include <tdom.h>
#include <string.h>
#include <stdlib.h>
/*
* Beginning with 8.4, Tcl API is CONST'ified
*/
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION <= 3)
# define const
#endif
#ifndef TCL_THREADS
# define TDomThreaded(x)
#else
# define TDomThreaded(x) x
#endif
/* The inital stack sizes must be at least 1 */
#define TNC_INITCONTENTSTACKSIZE 512
/*----------------------------------------------------------------------------
| local globals
|
\---------------------------------------------------------------------------*/
/* Counter to generate unique validateCmd names */
static int uniqueCounter = 0;
TDomThreaded(static Tcl_Mutex counterMutex;) /* Protect the counter */
/* To enable some debugging output at stdout use this.
But beware: this debugging output isn't systematic
and only understandable, if you know the internals
of tnc. */
/* #define TNC_DEBUG */
/* The elements of TNC_Content carry exactly the same information
as expats XML_Content. But the element is identified by his
Tcl_HashEntry entry within the "tagNames" Hashtable (see TNC_Data)
and not the element name. This should be much more efficient. */
typedef struct TNC_cp TNC_Content;
typedef struct TNC_elemAttInfo TNC_ElemAttInfo;
struct TNC_cp
{
enum XML_Content_Type type;
enum XML_Content_Quant quant;
Tcl_HashEntry *nameId;
unsigned int numchildren;
TNC_Content *children;
TNC_ElemAttInfo *attInfo;
};
typedef struct TNC_contentStack
{
TNC_Content *model;
int activeChild;
int deep;
int alreadymatched;
} TNC_ContentStack;
typedef struct TNC_data
{
char *doctypeName; /* From DOCTYPE declaration */
int ignoreWhiteCDATAs; /* Flag: white space allowed in
current content model? */
int ignorePCDATA; /* Flag: currently mixed content
model? */
Tcl_HashTable *tagNames; /* Hash table of all ELEMENT
declarations of the DTD.
Element name is the key.
While parsing, entry points
to the XML_Content of that
Element, after finishing of
DTD parsing, entry holds a
pointer to the TNC_Content
of that element. */
TNC_ElemAttInfo *elemAttInfo; /* TncElementStartCommand stores
the elemAttInfo pointer of
the current element here for
DOM validation, to avoid two
element name lookups. */
int elemContentsRewriten; /* Signals, if the tagNames
entries point to
TNC_Contents */
int status; /* While used with expat obj:
1 after successful parsed
DTD, 0 otherwise.
For validateCmd used for
error report during
validation: 0 OK, 1 validation
error. */
int idCheck; /* Flag: check IDREF resolution*/
Tcl_HashTable *attDefsTables; /* Used to store ATTLIST
declarations while parsing.
Keys are the element names. */
Tcl_HashTable *entityDecls; /* Used to store ENTITY
declarations. */
Tcl_HashTable *notationDecls; /* Used to store NOTATION
declarations. */
Tcl_HashTable *ids; /* Used to track IDs */
Tcl_Interp *interp;
Tcl_Obj *expatObj; /* If != NULL, points to the
parserCmd structure. NULL
for ValidateCmds. Used, to
distinguish between SAX
and DOM validation. */
int contentStackSize; /* Current size of the content
stack */
int contentStackPtr; /* Points to the currently active
content model on the stack */
TNC_ContentStack *contentStack; /* Stack for the currently
nested open content models. */
} TNC_Data;
typedef enum TNC_attType {
TNC_ATTTYPE_CDATA,
TNC_ATTTYPE_ID,
TNC_ATTTYPE_IDREF,
TNC_ATTTYPE_IDREFS,
TNC_ATTTYPE_ENTITY,
TNC_ATTTYPE_ENTITIES,
TNC_ATTTYPE_NMTOKEN,
TNC_ATTTYPE_NMTOKENS,
TNC_ATTTYPE_NOTATION,
TNC_ATTTYPE_ENUMERATION,
} TNC_AttType;
struct TNC_elemAttInfo
{
Tcl_HashTable *attributes;
int nrOfreq;
int nrOfIdAtts;
};
typedef struct TNC_attDecl
{
TNC_AttType att_type;
char *dflt;
int isrequired;
Tcl_HashTable *lookupTable; /* either NotationTypes or enum values */
} TNC_AttDecl;
typedef struct TNC_entityInfo
{
int is_notation;
char *notationName;
} TNC_EntityInfo;
typedef Tcl_HashEntry TNC_NameId;
static char tnc_usage[] =
"Usage tnc <expat parser obj> <subCommand>, where subCommand can be: \n"
" enable \n"
" remove \n"
" getValidateCmd ?cmdName?\n"
;
static char validateCmd_usage[] =
"Usage validateCmd <method> <args>, where method can be: \n"
" validateDocument <domDocument> \n"
" validateTree <domNode> \n"
" validateAttributes <domNode> \n"
" delete \n"
;
enum TNC_Error {
TNC_ERROR_NONE,
TNC_ERROR_DUPLICATE_ELEMENT_DECL,
TNC_ERROR_DUPLICATE_MIXED_ELEMENT,
TNC_ERROR_UNKNOWN_ELEMENT,
TNC_ERROR_EMPTY_ELEMENT,
TNC_ERROR_DISALLOWED_PCDATA,
TNC_ERROR_DISALLOWED_CDATA,
TNC_ERROR_NO_DOCTYPE_DECL,
TNC_ERROR_WRONG_ROOT_ELEMENT,
TNC_ERROR_NO_ATTRIBUTES,
TNC_ERROR_UNKOWN_ATTRIBUTE,
TNC_ERROR_WRONG_FIXED_ATTVALUE,
TNC_ERROR_MISSING_REQUIRED_ATTRIBUTE,
TNC_ERROR_MORE_THAN_ONE_ID_ATT,
TNC_ERROR_ID_ATT_DEFAULT,
TNC_ERROR_DUPLICATE_ID_VALUE,
TNC_ERROR_UNKOWN_ID_REFERRED,
TNC_ERROR_ENTITY_ATTRIBUTE,
TNC_ERROR_ENTITIES_ATTRIBUTE,
TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED,
TNC_ERROR_NOTATION_REQUIRED,
TNC_ERROR_NOTATION_MUST_BE_DECLARED,
TNC_ERROR_IMPOSSIBLE_DEFAULT,
TNC_ERROR_ENUM_ATT_WRONG_VALUE,
TNC_ERROR_NMTOKEN_REQUIRED,
TNC_ERROR_NAME_REQUIRED,
TNC_ERROR_NAMES_REQUIRED,
TNC_ERROR_ELEMENT_NOT_ALLOWED_HERE,
TNC_ERROR_ELEMENT_CAN_NOT_END_HERE,
TNC_ERROR_ONLY_THREE_BYTE_UTF8,
TNC_ERROR_UNKNOWN_NODE_TYPE
};
const char *
TNC_ErrorString (int code)
{
static const char *message[] = {
"No error.",
"Element declared more than once.",
"The same name must not appear more than once in \n\tone mixed-content declaration.",
"No declaration for this element.",
"Element is declared to be empty, but isn't.",
"PCDATA not allowed here.",
"CDATA section not allowed here.",
"No DOCTYPE declaration.",
"Root element doesn't match DOCTYPE name.",
"No attributes defined for this element.",
"Unknown attribute for this element.",
"Attribute value must match the FIXED default.",
"Required attribute missing.",
"Only one attribute with type ID allowed.",
"No default value allowed for attribute type ID.",
"ID attribute values must be unique within the document.",
"Unknown ID referred.",
"Attribute value has to be a unparsed entity.",
"Attribute value has to be a sequence of unparsed entities.",
"The defaults of attributes with type ENTITY or ENTITIES\nhas to be unparsed entities.",
"Attribute value has to be one of the allowed notations.",
"Every used NOTATION must be declared.",
"Attribute default is not one of the allowed values",
"Attribute hasn't one of the allowed values.",
"Attribute value has to be a NMTOKEN.",
"Attribute value has to be a Name.",
"Attribute value has to match production Names.",
"Element is not allowed here.",
"Element can not end here (required element(s) missing).",
"Can only handle UTF8 chars up to 3 bytes length."
"Unknown or unexpected dom node type."
};
/* if (code > 0 && code < sizeof(message)/sizeof(message[0])) */
return message[code];
return 0;
}
#define CHECK_UTF_CHARLEN(d) if (!(d)) { \
signalNotValid (userData, TNC_ERROR_ONLY_THREE_BYTE_UTF8);\
return;\
}
#define CHECK_UTF_CHARLENR(d) if (!(d)) { \
signalNotValid (userData, TNC_ERROR_ONLY_THREE_BYTE_UTF8);\
return 0;\
}
#define CHECK_UTF_CHARLEN_COPY(d) if (!(d)) { \
signalNotValid (userData, TNC_ERROR_ONLY_THREE_BYTE_UTF8);\
FREE (copy);\
return;\
}
#define SetResult(str) Tcl_ResetResult(interp); \
Tcl_SetStringObj(Tcl_GetObjResult(interp), (str), -1)
#define SetBooleanResult(i) Tcl_ResetResult(interp); \
Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (i))
extern char *Tdom_InitStubs (Tcl_Interp *interp, char *version, int exact);
static void
signalNotValid (userData, code)
void *userData;
int code;
{
TNC_Data *tncdata = (TNC_Data *) userData;
TclGenExpatInfo *expat;
char s[1000];
if (tncdata->expatObj) {
expat = GetExpatInfo (tncdata->interp, tncdata->expatObj);
sprintf (s, "Validation error at line %ld, character %ld: %s",
XML_GetCurrentLineNumber (expat->parser),
XML_GetCurrentColumnNumber (expat->parser),
TNC_ErrorString (code));
expat->status = TCL_ERROR;
expat->result = Tcl_NewStringObj (s, -1);
Tcl_IncrRefCount (expat->result);
} else {
tncdata->status = 1;
Tcl_SetResult (tncdata->interp, (char *)TNC_ErrorString (code),
TCL_VOLATILE);
}
}
/*
*----------------------------------------------------------------------------
*
* FindUniqueCmdName --
*
* Generate new command name. Used for getValidateCmd.
*
* Results:
* Returns newly allocated Tcl object containing name.
*
* Side effects:
* Allocates Tcl object.
*
*----------------------------------------------------------------------------
*/
static void
FindUniqueCmdName(
Tcl_Interp *interp,
char *s
)
{
Tcl_CmdInfo info;
TDomThreaded(Tcl_MutexLock(&counterMutex);)
do {
sprintf(s, "DTDvalidator%d", uniqueCounter++);
} while (Tcl_GetCommandInfo(interp, s, &info));
TDomThreaded(Tcl_MutexUnlock(&counterMutex);)
}
/*
*----------------------------------------------------------------------------
*
* TncStartDoctypeDeclHandler --
*
* This procedure is called for the start of the DOCTYPE
* declaration.
*
* Results:
* None.
*
* Side effects:
* Stores the doctype Name in the TNC_data.
*
*----------------------------------------------------------------------------
*/
void
TncStartDoctypeDeclHandler (userData, doctypeName, sysid, pubid, has_internal_subset)
void *userData;
const char *doctypeName;
const char *sysid;
const char *pubid;
int has_internal_subset;
{
TNC_Data *tncdata = (TNC_Data *) userData;
#ifdef TNC_DEBUG
printf ("TncStartDoctypeDeclHandler start\n");
#endif
tncdata->doctypeName = tdomstrdup (doctypeName);
}
/*
*----------------------------------------------------------------------------
*
* TncFreeTncModel --
*
* This helper procedure frees recursively TNC_Contents.
*
* Results:
* None.
*
* Side effects:
* Frees memory.
*
*----------------------------------------------------------------------------
*/
static void
TncFreeTncModel (tmodel)
TNC_Content *tmodel;
{
unsigned int i;
if (tmodel->children) {
for (i = 0; i < tmodel->numchildren; i++) {
TncFreeTncModel (&tmodel->children[i]);
}
FREE ((char *) tmodel->children);
}
}
/*
*----------------------------------------------------------------------------
*
* TncRewriteModel --
*
* This helper procedure creates recursively a TNC_Content from
* a XML_Content.
*
* Results:
* None.
*
* Side effects:
* Allocates memory for the TNC_Content models.
*
*----------------------------------------------------------------------------
*/
static void
TncRewriteModel (emodel, tmodel, tagNames)
XML_Content *emodel;
TNC_Content *tmodel;
Tcl_HashTable *tagNames;
{
Tcl_HashEntry *entryPtr;
unsigned int i;
tmodel->type = emodel->type;
tmodel->quant = emodel->quant;
tmodel->numchildren = emodel->numchildren;
tmodel->children = NULL;
tmodel->nameId = NULL;
switch (emodel->type) {
case XML_CTYPE_MIXED:
if (emodel->quant == XML_CQUANT_REP) {
tmodel->children = (TNC_Content *)
MALLOC (sizeof (TNC_Content) * emodel->numchildren);
for (i = 0; i < emodel->numchildren; i++) {
TncRewriteModel (&emodel->children[i], &tmodel->children[i],
tagNames);
}
}
break;
case XML_CTYPE_ANY:
case XML_CTYPE_EMPTY:
/* do nothing */
break;
case XML_CTYPE_SEQ:
case XML_CTYPE_CHOICE:
tmodel->children = (TNC_Content *)
MALLOC (sizeof (TNC_Content) * emodel->numchildren);
for (i = 0; i < emodel->numchildren; i++) {
TncRewriteModel (&emodel->children[i], &tmodel->children[i],
tagNames);
}
break;
case XML_CTYPE_NAME:
entryPtr = Tcl_FindHashEntry (tagNames, emodel->name);
/* Notice, that it is possible for entryPtr to be NULL.
This means, a content model uses a not declared element.
This is legal even in valid documents. (Of course, if the
undeclared element actually shows up in the document
that would make the document invalid.) See rec 3.2
QUESTION: Should there be a flag to enable a warning,
when a declaration contains an element type for which
no declaration is provided, as rec 3.2 metioned?
This would be the appropriated place to omit the
warning. */
tmodel->nameId = entryPtr;
}
}
/*
*----------------------------------------------------------------------------
*
* TncEndDoctypeDeclHandler --
*
* This procedure is called at the end of the DOCTYPE
* declaration, after processing any external subset.
* It rewrites the XML_Content models to TNC_Content
* models and frees the XML_Content models.
*
* Results:
* None.
*
* Side effects:
* Rewrites the XML_Content models to TNC_Content
* models.
*
*----------------------------------------------------------------------------
*/
void
TncEndDoctypeDeclHandler (userData)
void *userData;
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr, *ePtr1;
Tcl_HashSearch search;
XML_Content *emodel;
TNC_Content *tmodel = NULL;
char *elementName;
entryPtr = Tcl_FirstHashEntry (tncdata->tagNames, &search);
while (entryPtr != NULL) {
#ifdef TNC_DEBUG
printf ("name: %-20s nameId: %p\n",
Tcl_GetHashKey (tncdata->tagNames, entryPtr),
entryPtr);
#endif
emodel = (XML_Content*) Tcl_GetHashValue (entryPtr);
tmodel = (TNC_Content*) MALLOC (sizeof (TNC_Content));
TncRewriteModel (emodel, tmodel, tncdata->tagNames);
elementName = Tcl_GetHashKey (tncdata->tagNames, entryPtr);
ePtr1 = Tcl_FindHashEntry (tncdata->attDefsTables, elementName);
if (ePtr1) {
tmodel->attInfo = (TNC_ElemAttInfo *) Tcl_GetHashValue (ePtr1);
} else {
tmodel->attInfo = NULL;
}
Tcl_SetHashValue (entryPtr, tmodel);
entryPtr = Tcl_NextHashEntry (&search);
}
tncdata->elemContentsRewriten = 1;
/* Checks, if every used notation name is in deed declared */
entryPtr = Tcl_FirstHashEntry (tncdata->notationDecls, &search);
while (entryPtr != NULL) {
#ifdef TNC_DEBUG
printf ("check notation name %s\n",
Tcl_GetHashKey (tncdata->notationDecls, entryPtr));
printf ("value %p\n", Tcl_GetHashValue (entryPtr));
#endif
if (!Tcl_GetHashValue (entryPtr)) {
signalNotValid (userData, TNC_ERROR_NOTATION_MUST_BE_DECLARED);
return;
}
entryPtr = Tcl_NextHashEntry (&search);
}
/* Checks, if every used entity name is indeed declared */
entryPtr = Tcl_FirstHashEntry (tncdata->entityDecls, &search);
while (entryPtr != NULL) {
if (!Tcl_GetHashValue (entryPtr)) {
signalNotValid (userData,
TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
return;
}
entryPtr = Tcl_NextHashEntry (&search);
}
tncdata->status = 1;
}
/*
*----------------------------------------------------------------------------
*
* TncEntityDeclHandler --
*
* This procedure is called for every entity declaration.
*
* Results:
* None.
*
* Side effects:
* Stores either the name of the entity and
* type information in a lookup table.
*
*----------------------------------------------------------------------------
*/
void
TncEntityDeclHandler (userData, entityName, is_parameter_entity, value,
value_length, base, systemId, publicId, notationName)
void *userData;
const char *entityName;
int is_parameter_entity;
const char *value;
int value_length;
const char *base;
const char *systemId;
const char *publicId;
const char *notationName;
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
int newPtr;
TNC_EntityInfo *entityInfo;
/* expat collects entity definitions internaly by itself. So this is
maybe superfluous, if it possible to access the expat internal
represention. To study this is left to the reader. */
if (is_parameter_entity) return;
entryPtr = Tcl_CreateHashEntry (tncdata->entityDecls, entityName, &newPtr);
/* multiple declaration of the same entity are allowed; first
definition wins (rec. 4.2) */
if (!newPtr) {
/* Eventually, an attribute declaration with type ENTITY or ENTITIES
has used this (up to the attribute declaration undeclared) ENTITY
within his default value. In this case, the hash value has to
be NULL and the entity must be a unparsed entity. */
if (!Tcl_GetHashValue (entryPtr)) {
if (notationName == NULL) {
signalNotValid (userData,
TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
return;
}
newPtr = 1;
}
}
if (newPtr) {
entityInfo = (TNC_EntityInfo *) MALLOC (sizeof (TNC_EntityInfo));
if (notationName != NULL) {
entityInfo->is_notation = 1;
Tcl_CreateHashEntry (tncdata->notationDecls,
notationName, &newPtr);
entityInfo->notationName = tdomstrdup (notationName);
}
else {
entityInfo->is_notation = 0;
}
Tcl_SetHashValue (entryPtr, entityInfo);
}
}
/*
*----------------------------------------------------------------------------
*
* TncNotationDeclHandler --
*
* This procedure is called for every notation declaration.
*
* Results:
* None.
*
* Side effects:
* Stores the notationName in the notationDecls table with value
* one.
*
*----------------------------------------------------------------------------
*/
void
TncNotationDeclHandler (userData, notationName, base, systemId, publicId)
void *userData;
const char *notationName;
const char *base;
const char *systemId;
const char *publicId;
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
int newPtr;
entryPtr = Tcl_CreateHashEntry (tncdata->notationDecls,
notationName,
&newPtr);
#ifdef TNC_DEBUG
printf ("Notation %s declared\n", notationName);
#endif
Tcl_SetHashValue (entryPtr, (char *) 1);
}
/*
*----------------------------------------------------------------------------
*
* TncElementDeclCommand --
*
* This procedure is called for every element declaration.
*
* Results:
* None.
*
* Side effects:
* Stores the tag name of the element in a lookup table.
*
*----------------------------------------------------------------------------
*/
void
TncElementDeclCommand (userData, name, model)
void *userData;
const char *name;
XML_Content *model;
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr;
int newPtr;
unsigned int i, j;
entryPtr = Tcl_CreateHashEntry (tncdata->tagNames, name, &newPtr);
/* "No element type may be declared more than once." (rec. 3.2) */
if (!newPtr) {
signalNotValid (userData, TNC_ERROR_DUPLICATE_ELEMENT_DECL);
return;
}
/* "The same name must not appear more than once in a
single mixed-content declaration." (rec. 3.2.2)
NOTE: OK, OK, doing it this way may not be optimal or even fast
in some cases. Please step in with a more fancy solution, if you
feel the need. */
if (model->type == XML_CTYPE_MIXED && model->quant == XML_CQUANT_REP) {
for (i = 0; i < model->numchildren; i++) {
for (j = i + 1; j < model->numchildren; j++) {
if (strcmp ((&model->children[i])->name,
(&model->children[j])->name) == 0) {
signalNotValid (userData,
TNC_ERROR_DUPLICATE_MIXED_ELEMENT);
return;
}
}
}
}
Tcl_SetHashValue (entryPtr, model);
return;
}
/*
*----------------------------------------------------------------------------
*
* TncAttDeclCommand --
*
* This procedure is called for *each* attribute in an XML
* ATTLIST declaration. It stores the attribute definition in
* an element specific hash table.
*
* Results:
* None.
*
* Side effects:
* Stores the tag name of the element in a lookup table.
*
*----------------------------------------------------------------------------
*/
void
TncAttDeclCommand (userData, elname, attname, att_type, dflt, isrequired)
void *userData;
const char *elname;
const char *attname;
const char *att_type;
const char *dflt;
int isrequired;
{
TNC_Data *tncdata = (TNC_Data *) userData;
Tcl_HashEntry *entryPtr, *entryPtr1;
Tcl_HashTable *elemAtts;
TNC_ElemAttInfo *elemAttInfo;
TNC_AttDecl *attDecl;
TNC_EntityInfo *entityInfo;
int newPtr, start, i, clen;
char *copy;
entryPtr = Tcl_CreateHashEntry (tncdata->attDefsTables, elname, &newPtr);
if (newPtr) {
elemAttInfo = (TNC_ElemAttInfo *) MALLOC (sizeof (TNC_ElemAttInfo));
elemAtts = (Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (elemAtts, TCL_STRING_KEYS);
elemAttInfo->attributes = elemAtts;
elemAttInfo->nrOfreq = 0;
elemAttInfo->nrOfIdAtts = 0;
Tcl_SetHashValue (entryPtr, elemAttInfo);
} else {
elemAttInfo = (TNC_ElemAttInfo *) Tcl_GetHashValue (entryPtr);
elemAtts = elemAttInfo->attributes;
}
entryPtr = Tcl_CreateHashEntry (elemAtts, attname, &newPtr);
/* Multiple Attribute declarations are allowed, but later declarations
are ignored. See rec 3.3. */
if (newPtr) {
attDecl = (TNC_AttDecl *) MALLOC (sizeof (TNC_AttDecl));
if (strcmp (att_type, "CDATA") == 0) {
attDecl->att_type = TNC_ATTTYPE_CDATA;
}
else if (strcmp (att_type, "ID") == 0) {
if (elemAttInfo->nrOfIdAtts) {
signalNotValid (userData, TNC_ERROR_MORE_THAN_ONE_ID_ATT);
return;
}
elemAttInfo->nrOfIdAtts++;
if (dflt != NULL) {
signalNotValid (userData, TNC_ERROR_ID_ATT_DEFAULT);
return;
}
attDecl->att_type = TNC_ATTTYPE_ID;
}
else if (strcmp (att_type, "IDREF") == 0) {
attDecl->att_type = TNC_ATTTYPE_IDREF;
}
else if (strcmp (att_type, "IDREFS") == 0) {
attDecl->att_type = TNC_ATTTYPE_IDREFS;
}
else if (strcmp (att_type, "ENTITY") == 0) {
attDecl->att_type = TNC_ATTTYPE_ENTITY;
}
else if (strcmp (att_type, "ENTITIES") == 0) {
attDecl->att_type = TNC_ATTTYPE_ENTITIES;
}
else if (strcmp (att_type, "NMTOKEN") == 0) {
attDecl->att_type = TNC_ATTTYPE_NMTOKEN;
}
else if (strcmp (att_type, "NMTOKENS") == 0) {
attDecl->att_type = TNC_ATTTYPE_NMTOKENS;
}
else if (strncmp (att_type, "NOTATION(", 9) == 0) {
/* This is a bit puzzling. expat returns something like
<!NOTATION gif PUBLIC "gif">
<!ATTLIST c type NOTATION (gif) #IMPLIED>
as att_type "NOTATION(gif)". */
attDecl->att_type = TNC_ATTTYPE_NOTATION;
attDecl->lookupTable =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (attDecl->lookupTable, TCL_STRING_KEYS);
copy = tdomstrdup (att_type);
start = i = 9;
while (i) {
if (copy[i] == ')') {
copy[i] = '\0';
#ifdef TNC_DEBUG
printf ("att type NOTATION: notation %s allowed\n",
©[start]);
#endif
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
entryPtr1 = Tcl_CreateHashEntry (tncdata->notationDecls,
©[start], &newPtr);
#ifdef TNC_DEBUG
if (newPtr) {
printf ("up to now unknown NOTATION\n");
} else {
printf ("NOTATION already known\n");
}
#endif
FREE (copy);
break;
}
if (copy[i] == '|') {
copy[i] = '\0';
#ifdef TNC_DEBUG
printf ("att type NOTATION: notation %s allowed\n",
©[start]);
#endif
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
entryPtr1 = Tcl_CreateHashEntry (tncdata->notationDecls,
©[start], &newPtr);
#ifdef TNC_DEBUG
if (newPtr) {
printf ("up to now unknown NOTATION\n");
} else {
printf ("NOTATION already known\n");
}
#endif
start = ++i;
continue;
}
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAMING_NMTOKEN (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
}
else {
/* expat returns something like
<!ATTLIST a type ( numbered
|bullets ) #IMPLIED>
as att_type "(numbered|bullets)", e.g. in some
"non-official" normalized way.
Makes things easier for us. */
attDecl->att_type = TNC_ATTTYPE_ENUMERATION;
attDecl->lookupTable =
(Tcl_HashTable *) MALLOC (sizeof (Tcl_HashTable));
Tcl_InitHashTable (attDecl->lookupTable, TCL_STRING_KEYS);
copy = tdomstrdup (att_type);
start = i = 1;
while (1) {
if (copy[i] == ')') {
copy[i] = '\0';
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
FREE (copy);
break;
}
if (copy[i] == '|') {
copy[i] = '\0';
Tcl_CreateHashEntry (attDecl->lookupTable,
©[start], &newPtr);
start = ++i;
continue;
}
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAMING_NMTOKEN (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NMTOKEN_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
}
if (dflt != NULL) {
switch (attDecl->att_type) {
case TNC_ATTTYPE_ENTITY:
case TNC_ATTTYPE_IDREF:
clen = UTF8_CHAR_LEN (*dflt);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAME_START (dflt, clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i = clen;
while (1) {
if (dflt[i] == '\0') {
break;
}
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAMING_NMTOKEN (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i += clen;
}
if (attDecl->att_type == TNC_ATTTYPE_ENTITY) {
entryPtr1 = Tcl_CreateHashEntry (tncdata->entityDecls,
dflt, &newPtr);
if (!newPtr) {
entityInfo =
(TNC_EntityInfo *) Tcl_GetHashValue (entryPtr1);
if (!entityInfo->is_notation) {
signalNotValid (userData,TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
}
}
}
break;
case TNC_ATTTYPE_IDREFS:
start = i = 0;
while (1) {
if (dflt[i] == '\0') {
break;
}
if (dflt[i] == ' ') {
start = ++i;
}
if (start == i) {
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAME_START (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i += clen;
}
else {
clen = UTF8_CHAR_LEN (dflt[i]);
CHECK_UTF_CHARLEN (clen);
if (!UTF8_GET_NAMING_NMTOKEN (&dflt[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
return;
}
i += clen;
}
}
break;
case TNC_ATTTYPE_ENTITIES:
copy = tdomstrdup (dflt);
start = i = 0;
while (1) {
if (copy[i] == '\0') {
FREE (copy);
break;
}
if (copy[i] == ' ') {
copy[i] = '\0';
entryPtr1 = Tcl_CreateHashEntry (tncdata->entityDecls,
©[start],
&newPtr);
if (!newPtr) {
entityInfo =
(TNC_EntityInfo *) Tcl_GetHashValue (entryPtr1);
if (!entityInfo->is_notation) {
signalNotValid (userData,TNC_ERROR_ATT_ENTITY_DEFAULT_MUST_BE_DECLARED);
}
}
start = ++i;
}
if (start == i) {
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);
if (!UTF8_GET_NAME_START (©[i], clen)) {
signalNotValid (userData, TNC_ERROR_NAME_REQUIRED);
FREE (copy);
return;
}
i += clen;
}
else {
clen = UTF8_CHAR_LEN (copy[i]);
CHECK_UTF_CHARLEN_COPY (clen);