Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added commands defelementtype and elementtype to have elements with the same name but different content models (without using local defined elements).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | schema
Files: files | file ages | folders
SHA3-256: 8327de493473602b958f8b6545face8a5550c5c352a7866f7808d1f7c9ef151e
User & Date: rolf 2019-12-28 01:09:01
Context
2019-12-31
00:49
Merged from trunk check-in: f212d3b0ba user: rolf tags: schema
2019-12-28
01:09
Added commands defelementtype and elementtype to have elements with the same name but different content models (without using local defined elements). check-in: 8327de4934 user: rolf tags: schema
01:03
Fixed problems with cleanup after errors in definition scripts. Closed-Leaf check-in: f08e4b9cb1 user: rolf tags: elementtype
2019-12-24
02:01
Fixed the test suite wrt to the last commit. Further work on elementtype. check-in: a17f75fc27 user: rolf tags: schema
Changes

Changes to generic/schema.c.

389
390
391
392
393
394
395
396



397
398
399
400
401
402
403
...
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
...
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
....
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
....
3810
3811
3812
3813
3814
3815
3816



3817
3818
3819
3820
3821
3822
3823
....
4114
4115
4116
4117
4118
4119
4120
4121



















































































4122
4123
4124
4125
4126
4127
4128
....
4389
4390
4391
4392
4393
4394
4395
4396

4397
4398
4399
4400
4401
4402
4403
....
4408
4409
4410
4411
4412
4413
4414
4415
4416

4417




4418
4419

4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
....
4438
4439
4440
4441
4442
4443
4444





4445
4446
4447
4448
4449
4450
4451
....
6768
6769
6770
6771
6772
6773
6774
6775

6776
6777
6778


6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
        if (pattern->flags & FORWARD_PATTERN_DEF) {
            fprintf (stderr, "\tAnonymously defined NAME\n");
        }
        if (pattern->flags & PLACEHOLDER_PATTERN_DEF) {
            fprintf (stderr, "\tAs placeholder defined NAME\n");
        }
        if (pattern->flags & LOCAL_DEFINED_ELEMENT) {
            fprintf (stderr, "\tAs placeholder defined NAME\n");



        }
        /* Fall thru. */
    case SCHEMA_CTYPE_CHOICE:
    case SCHEMA_CTYPE_INTERLEAVE:
        fprintf (stderr, "\t%d childs\n", pattern->nc);
        break;
    case SCHEMA_CTYPE_ANY:
................................................................................

    sdata = TMALLOC (SchemaData);
    memset (sdata, 0, sizeof(SchemaData));
    name = Tcl_GetStringFromObj (cmdNameObj, &len);
    sdata->self = Tcl_NewStringObj (name, len);
    Tcl_IncrRefCount (sdata->self);
    Tcl_InitHashTable (&sdata->element, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->elementTypes, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->prefix, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->pattern, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->attrNames, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->namespace, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->textDef, TCL_STRING_KEYS);
    sdata->emptyNamespace = Tcl_CreateHashEntry (
        &sdata->namespace, "", &hnew);
................................................................................
            FREE (sdata->prefixns[i]);
            i++;
        }
        FREE (sdata->prefixns);
    }
    Tcl_DeleteHashTable (&sdata->namespace);
    Tcl_DeleteHashTable (&sdata->element);
    Tcl_DeleteHashTable (&sdata->elementTypes);
    Tcl_DeleteHashTable (&sdata->prefix);
    Tcl_DeleteHashTable (&sdata->pattern);
    Tcl_DeleteHashTable (&sdata->attrNames);
    Tcl_DeleteHashTable (&sdata->textDef);
    for (i = 0; i < sdata->numPatternList; i++) {
        freeSchemaCP (sdata->patternList[i]);
    }
................................................................................
static void
cleanupLastPattern (
    SchemaData *sdata,
    unsigned int from
    )
{
    unsigned int i;

    Tcl_HashTable *hashTable;
    Tcl_HashEntry *h;

    SchemaCP *this, *previous, *current;

    for (i = from; i < sdata->numPatternList; i++) {
        this = sdata->patternList[i];
        hashTable = NULL;
        if (this->type == SCHEMA_CTYPE_NAME) {




            hashTable = &sdata->element;


        }
        if (this->type == SCHEMA_CTYPE_PATTERN) {
            hashTable = &sdata->pattern;

        }
        if (this->name && hashTable) {
            if (this->flags & FORWARD_PATTERN_DEF) {
                sdata->forwardPatternDefs--;
            }
            h = Tcl_FindHashEntry (hashTable, this->name);
            previous = NULL;
            current = Tcl_GetHashValue (h);
            while (current != NULL && current != this) {
                previous = current;
                current = current->next;
            }
            if (previous) {
                if (current->next) {
                    previous->next = current->next;
                } else {
                    previous->next = NULL;
                }
            } else {
                if (current) {
                    Tcl_SetHashValue (h, current->next);
                } else {
                    Tcl_DeleteHashEntry (h);
                }
            }
        }
        freeSchemaCP (sdata->patternList[i]);
................................................................................
schemaInstanceCmd (
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    int            methodIndex, keywordIndex, hnew, patternIndex;
    int            result = TCL_OK, forwardDef = 0, i = 0, j;
    int            savedDefineToplevel, type, len;
    unsigned int   savedNumPatternList;
    SchemaData    *savedsdata = NULL, *sdata = (SchemaData *) clientData;
    Tcl_HashTable *hashTable;
    Tcl_HashEntry *h, *h1;
    SchemaCP      *pattern, *current = NULL;
    void          *namespacePtr, *savedNamespacePtr;
    char          *xmlstr, *errMsg;
    domDocument   *doc;
    domNode       *node;
    Tcl_Obj       *attData;

    static const char *schemaInstanceMethods[] = {
................................................................................
                    pattern->flags &= ~FORWARD_PATTERN_DEF;
                }
                pattern->flags &= ~PLACEHOLDER_PATTERN_DEF;
            }
            pattern->defScript = objv[patternIndex];
            Tcl_IncrRefCount (pattern->defScript);
        } else {



            cleanupLastPattern (sdata, savedNumPatternList);
        }
        sdata->defineToplevel = savedDefineToplevel;
        sdata->currentNamespace = savedNamespacePtr;
        if (!savedDefineToplevel) {
            SETASI(savedsdata);
        }
................................................................................
    case m_defelementtype:
        CHECK_RECURSIVE_CALL
        if (objc != 5-i && objc != 6-i) {
            Tcl_WrongNumArgs (interp, 1-i, objv, "<type_name> <name>"
                 " ?<namespace>? pattern");
            return TCL_ERROR;
        }
        /* todo */



















































































        break;
            
    default:
        Tcl_SetResult (interp, "unknown method", NULL);
        result = TCL_ERROR;
        break;

................................................................................
        addToContent (sdata, pattern, quant, n, m);
    } else {
        freeSchemaCP (pattern);
    }
    return result;
}

/* Implements the schema definition commands "element" and "ref" */

static int
NamedPatternObjCmd (
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
................................................................................
    Tcl_HashEntry *h;
    SchemaCP *pattern = NULL, *current;
    SchemaQuant quant;
    int hnew, n, m;

    CHECK_SI
    CHECK_TOPLEVEL
    if (patternType == SCHEMA_CTYPE_NAME) {
        checkNrArgs (2,4,"Expected: elementName ?quant? ?pattern?");

        hashTable = &sdata->element;




    } else {
        checkNrArgs (2,3,"Expected: patternName ?quant?");

        hashTable = &sdata->pattern;
    }

    quant = getQuant (interp, sdata, objc == 2 ? NULL : objv[2], &n, &m);
    if (quant == SCHEMA_CQUANT_ERROR) {
        return TCL_ERROR;
    }
    h = Tcl_CreateHashEntry (hashTable, Tcl_GetString(objv[1]), &hnew);
    if (objc < 4) {
        /* Reference to an element or pattern */
        if (!hnew) {
            pattern = (SchemaCP *) Tcl_GetHashValue (h);
            while (pattern) {
                if (pattern->namespace == sdata->currentNamespace) {
                    break;
                }
                pattern = pattern->next;
................................................................................
        }
        if (!pattern) {
            pattern = initSchemaCP (
                patternType,
                sdata->currentNamespace,
                Tcl_GetHashKey (hashTable, h)
                );





            pattern->flags |= FORWARD_PATTERN_DEF;
            sdata->forwardPatternDefs++;
            if (!hnew) {
                current = (SchemaCP *) Tcl_GetHashValue (h);
                pattern->next = current;
            }
            REMEMBER_PATTERN (pattern);
................................................................................
    Tcl_CreateObjCommand (interp, "tdom::schema::prefixns",
                          schemaInstanceCmd, NULL, NULL);

    /* The "any" definition command. */
    Tcl_CreateObjCommand (interp, "tdom::schema::any",
                          AnyPatternObjCmd, NULL, NULL);

    /* The named pattern commands "element" and "ref". */

    Tcl_CreateObjCommand (interp, "tdom::schema::element",
                          NamedPatternObjCmd,
                          (ClientData) SCHEMA_CTYPE_NAME, NULL);


    Tcl_CreateObjCommand (interp, "tdom::schema::ref",
                          NamedPatternObjCmd,
                          (ClientData) SCHEMA_CTYPE_PATTERN, NULL);

    /* The anonymous pattern commands "choise", "mixed", "interleave"
     * and "group". */
    Tcl_CreateObjCommand (interp, "tdom::schema::choice",
                          AnonPatternObjCmd, (ClientData) 0, NULL);
    Tcl_CreateObjCommand (interp, "tdom::schema::mixed",
                          AnonPatternObjCmd, (ClientData) 1, NULL);







|
>
>
>







 







|







 







|







 







>









>
>
>
>
|
>
>



>

|



|













|







 







|






|







 







>
>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
>







 







|

>

>
>
>
>


>









|







 







>
>
>
>
>







 







|
>

|
<
>
>

|
<







389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
...
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
...
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
...
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
....
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
....
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
....
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
....
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
....
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
....
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
....
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887

6888
6889
6890
6891

6892
6893
6894
6895
6896
6897
6898
        if (pattern->flags & FORWARD_PATTERN_DEF) {
            fprintf (stderr, "\tAnonymously defined NAME\n");
        }
        if (pattern->flags & PLACEHOLDER_PATTERN_DEF) {
            fprintf (stderr, "\tAs placeholder defined NAME\n");
        }
        if (pattern->flags & LOCAL_DEFINED_ELEMENT) {
            fprintf (stderr, "\tLocal defined NAME\n");
        }
        if (pattern->flags & ELEMENTTYPE_DEF) {
            fprintf (stderr, "\tElementtype '%s'\n", pattern->typeName);
        }
        /* Fall thru. */
    case SCHEMA_CTYPE_CHOICE:
    case SCHEMA_CTYPE_INTERLEAVE:
        fprintf (stderr, "\t%d childs\n", pattern->nc);
        break;
    case SCHEMA_CTYPE_ANY:
................................................................................

    sdata = TMALLOC (SchemaData);
    memset (sdata, 0, sizeof(SchemaData));
    name = Tcl_GetStringFromObj (cmdNameObj, &len);
    sdata->self = Tcl_NewStringObj (name, len);
    Tcl_IncrRefCount (sdata->self);
    Tcl_InitHashTable (&sdata->element, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->elementType, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->prefix, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->pattern, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->attrNames, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->namespace, TCL_STRING_KEYS);
    Tcl_InitHashTable (&sdata->textDef, TCL_STRING_KEYS);
    sdata->emptyNamespace = Tcl_CreateHashEntry (
        &sdata->namespace, "", &hnew);
................................................................................
            FREE (sdata->prefixns[i]);
            i++;
        }
        FREE (sdata->prefixns);
    }
    Tcl_DeleteHashTable (&sdata->namespace);
    Tcl_DeleteHashTable (&sdata->element);
    Tcl_DeleteHashTable (&sdata->elementType);
    Tcl_DeleteHashTable (&sdata->prefix);
    Tcl_DeleteHashTable (&sdata->pattern);
    Tcl_DeleteHashTable (&sdata->attrNames);
    Tcl_DeleteHashTable (&sdata->textDef);
    for (i = 0; i < sdata->numPatternList; i++) {
        freeSchemaCP (sdata->patternList[i]);
    }
................................................................................
static void
cleanupLastPattern (
    SchemaData *sdata,
    unsigned int from
    )
{
    unsigned int i;
    char *name;
    Tcl_HashTable *hashTable;
    Tcl_HashEntry *h;

    SchemaCP *this, *previous, *current;

    for (i = from; i < sdata->numPatternList; i++) {
        this = sdata->patternList[i];
        hashTable = NULL;
        if (this->type == SCHEMA_CTYPE_NAME) {
            if (this->flags & ELEMENTTYPE_DEF) {
                hashTable = &sdata->elementType;
                name = this->typeName;
            } else {
                hashTable = &sdata->element;
                name = this->name;
            }
        }
        if (this->type == SCHEMA_CTYPE_PATTERN) {
            hashTable = &sdata->pattern;
            name = this->name;
        }
        if (name && hashTable) {
            if (this->flags & FORWARD_PATTERN_DEF) {
                sdata->forwardPatternDefs--;
            }
            h = Tcl_FindHashEntry (hashTable, name);
            previous = NULL;
            current = Tcl_GetHashValue (h);
            while (current != NULL && current != this) {
                previous = current;
                current = current->next;
            }
            if (previous) {
                if (current->next) {
                    previous->next = current->next;
                } else {
                    previous->next = NULL;
                }
            } else {
                if (current && current->next) {
                    Tcl_SetHashValue (h, current->next);
                } else {
                    Tcl_DeleteHashEntry (h);
                }
            }
        }
        freeSchemaCP (sdata->patternList[i]);
................................................................................
schemaInstanceCmd (
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    int            methodIndex, keywordIndex, hnew, hnew1, patternIndex;
    int            result = TCL_OK, forwardDef = 0, i = 0, j;
    int            savedDefineToplevel, type, len;
    unsigned int   savedNumPatternList;
    SchemaData    *savedsdata = NULL, *sdata = (SchemaData *) clientData;
    Tcl_HashTable *hashTable;
    Tcl_HashEntry *h, *h1;
    SchemaCP      *pattern, *thiscp, *current = NULL;
    void          *namespacePtr, *savedNamespacePtr;
    char          *xmlstr, *errMsg;
    domDocument   *doc;
    domNode       *node;
    Tcl_Obj       *attData;

    static const char *schemaInstanceMethods[] = {
................................................................................
                    pattern->flags &= ~FORWARD_PATTERN_DEF;
                }
                pattern->flags &= ~PLACEHOLDER_PATTERN_DEF;
            }
            pattern->defScript = objv[patternIndex];
            Tcl_IncrRefCount (pattern->defScript);
        } else {
            if (forwardDef) {
                pattern->nc = 0;
            }
            cleanupLastPattern (sdata, savedNumPatternList);
        }
        sdata->defineToplevel = savedDefineToplevel;
        sdata->currentNamespace = savedNamespacePtr;
        if (!savedDefineToplevel) {
            SETASI(savedsdata);
        }
................................................................................
    case m_defelementtype:
        CHECK_RECURSIVE_CALL
        if (objc != 5-i && objc != 6-i) {
            Tcl_WrongNumArgs (interp, 1-i, objv, "<type_name> <name>"
                 " ?<namespace>? pattern");
            return TCL_ERROR;
        }
        savedNumPatternList = sdata->numPatternList;
        namespacePtr = NULL;
        patternIndex = 4-i;
        if (objc == 6-i) {
            patternIndex = 5-i;
            namespacePtr = getNamespacePtr (sdata, Tcl_GetString (objv[4-i]));
        }
        h = Tcl_CreateHashEntry (&sdata->elementType , Tcl_GetString (objv[2-i]),
                                 &hnew);
        pattern = NULL;
        if (!hnew) {
            pattern = (SchemaCP *) Tcl_GetHashValue (h);
            while (pattern) {
                if (pattern->namespace == namespacePtr) {
                    if (pattern->flags & FORWARD_PATTERN_DEF) {
                        forwardDef = 1;
                        break;
                    }
                    SetResult ("Element type already defined in this "
                               "namespace");
                    return TCL_ERROR;
                }
                pattern = pattern->next;
            }
        }
        h1 = Tcl_CreateHashEntry (&sdata->element , Tcl_GetString (objv[3-i]),
                                  &hnew1);
        if (hnew1) {
            thiscp = initSchemaCP (SCHEMA_CTYPE_NAME, namespacePtr,
                                   Tcl_GetString (objv[3-i]));
            REMEMBER_PATTERN (thiscp);
            thiscp->flags |= PLACEHOLDER_PATTERN_DEF;
            Tcl_SetHashValue (h1, thiscp);
        }
        if (pattern == NULL) {
            pattern = initSchemaCP (SCHEMA_CTYPE_NAME, namespacePtr,
                                    Tcl_GetHashKey (&sdata->element, h1));
            pattern->flags |= ELEMENTTYPE_DEF;
            pattern->typeName = Tcl_GetHashKey (&sdata->elementType, h);
            if (!hnew) {
                current = (SchemaCP *) Tcl_GetHashValue (h);
                pattern->next = current;
            }
            REMEMBER_PATTERN (pattern);
            Tcl_SetHashValue (h, pattern);
        }
        
        SETASI(sdata);
        savedDefineToplevel = sdata->defineToplevel;
        savedNamespacePtr = sdata->currentNamespace;
        sdata->defineToplevel = 0;
        sdata->currentNamespace = namespacePtr;
        sdata->cp = pattern;
        sdata->numAttr = 0;
        sdata->numReqAttr = 0;
        sdata->currentAttrs = NULL;
        sdata->contentSize = CONTENT_ARRAY_SIZE_INIT;
        sdata->evalStub[3] = objv[patternIndex];
        sdata->currentEvals++;
        result = Tcl_EvalObjv (interp, 4, sdata->evalStub, TCL_EVAL_GLOBAL);
        sdata->currentEvals--;
        sdata->currentNamespace = NULL;
        pattern->attrs = sdata->currentAttrs;
        pattern->numAttr = sdata->numAttr;
        pattern->numReqAttr = sdata->numReqAttr;
        if (result == TCL_OK) {
            if (forwardDef) {
                sdata->forwardPatternDefs--;
                pattern->name = Tcl_GetHashKey (&sdata->element, h1);
                pattern->flags &= ~FORWARD_PATTERN_DEF;
            }
            pattern->defScript = objv[patternIndex];
            Tcl_IncrRefCount (pattern->defScript);
        } else {
            if (forwardDef) {
                pattern->nc = 0;
            }
            cleanupLastPattern (sdata, savedNumPatternList);
        }
        sdata->defineToplevel = savedDefineToplevel;
        sdata->currentNamespace = savedNamespacePtr;
        if (!savedDefineToplevel) {
            SETASI(savedsdata);
        }
        break;
            
    default:
        Tcl_SetResult (interp, "unknown method", NULL);
        result = TCL_ERROR;
        break;

................................................................................
        addToContent (sdata, pattern, quant, n, m);
    } else {
        freeSchemaCP (pattern);
    }
    return result;
}

/* Implements the schema definition commands "element", "elementtype"
 * and "ref" */
static int
NamedPatternObjCmd (
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
................................................................................
    Tcl_HashEntry *h;
    SchemaCP *pattern = NULL, *current;
    SchemaQuant quant;
    int hnew, n, m;

    CHECK_SI
    CHECK_TOPLEVEL
    if (clientData == 0) {
        checkNrArgs (2,4,"Expected: elementName ?quant? ?pattern?");
        patternType = SCHEMA_CTYPE_NAME;
        hashTable = &sdata->element;
    } else if (clientData == (ClientData) 1) {
        checkNrArgs (2,3,"Expected: elementtypeName ?quant?");
        patternType = SCHEMA_CTYPE_NAME;
        hashTable = &sdata->elementType;            
    } else {
        checkNrArgs (2,3,"Expected: patternName ?quant?");
        patternType = SCHEMA_CTYPE_PATTERN;
        hashTable = &sdata->pattern;
    }

    quant = getQuant (interp, sdata, objc == 2 ? NULL : objv[2], &n, &m);
    if (quant == SCHEMA_CQUANT_ERROR) {
        return TCL_ERROR;
    }
    h = Tcl_CreateHashEntry (hashTable, Tcl_GetString(objv[1]), &hnew);
    if (objc < 4) {
        /* Reference to an element, elementtype or pattern */
        if (!hnew) {
            pattern = (SchemaCP *) Tcl_GetHashValue (h);
            while (pattern) {
                if (pattern->namespace == sdata->currentNamespace) {
                    break;
                }
                pattern = pattern->next;
................................................................................
        }
        if (!pattern) {
            pattern = initSchemaCP (
                patternType,
                sdata->currentNamespace,
                Tcl_GetHashKey (hashTable, h)
                );
            if (clientData == (ClientData) 1) {
                pattern->typeName = pattern->name;
                pattern->name = NULL;
                pattern->flags |= ELEMENTTYPE_DEF;
            }
            pattern->flags |= FORWARD_PATTERN_DEF;
            sdata->forwardPatternDefs++;
            if (!hnew) {
                current = (SchemaCP *) Tcl_GetHashValue (h);
                pattern->next = current;
            }
            REMEMBER_PATTERN (pattern);
................................................................................
    Tcl_CreateObjCommand (interp, "tdom::schema::prefixns",
                          schemaInstanceCmd, NULL, NULL);

    /* The "any" definition command. */
    Tcl_CreateObjCommand (interp, "tdom::schema::any",
                          AnyPatternObjCmd, NULL, NULL);

    /* The named pattern commands "element", "elementtype" and
     * "ref". */
    Tcl_CreateObjCommand (interp, "tdom::schema::element",
                          NamedPatternObjCmd, (ClientData) 0, NULL);

    Tcl_CreateObjCommand (interp, "tdom::schema::elementtype",
                          NamedPatternObjCmd, (ClientData) 1, NULL);
    Tcl_CreateObjCommand (interp, "tdom::schema::ref",
                          NamedPatternObjCmd, (ClientData) 2, NULL);


    /* The anonymous pattern commands "choise", "mixed", "interleave"
     * and "group". */
    Tcl_CreateObjCommand (interp, "tdom::schema::choice",
                          AnonPatternObjCmd, (ClientData) 0, NULL);
    Tcl_CreateObjCommand (interp, "tdom::schema::mixed",
                          AnonPatternObjCmd, (ClientData) 1, NULL);

Changes to generic/schema.h.

71
72
73
74
75
76
77
78

79
80
81
82
83
84
85
..
95
96
97
98
99
100
101

102
103
104
105
106
107
108
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

/* The SchemaFlags flags */
#define FORWARD_PATTERN_DEF     1
#define PLACEHOLDER_PATTERN_DEF 2
#define AMBIGUOUS_PATTERN       4
#define LOCAL_DEFINED_ELEMENT   8
#define CONSTRAINT_TEXT_CHILD  16
#define MIXED_CONTENT          32 


typedef struct domKeyConstraint {
    char  *name;
    ast    selector;
    ast   *fields;
    int    nrFields;
    int    flags;
................................................................................
} SchemaKeySpace;

typedef struct SchemaCP
{
    Schema_CP_Type    type;
    char             *namespace;
    char             *name;

    struct SchemaCP  *next;
    SchemaFlags       flags;
    struct SchemaCP **content;
    SchemaQuant      *quants;
    unsigned int      nc;
    SchemaAttr      **attrs;
    unsigned int      numAttr;
................................................................................

typedef struct SchemaData_
{
    Tcl_Obj *self;
    char *start;
    char *startNamespace;
    Tcl_HashTable element;
    Tcl_HashTable elementTypes;
    Tcl_HashTable namespace;
    Tcl_HashEntry *emptyNamespace;
    char **prefixns;
    Tcl_HashTable prefix;
    Tcl_HashTable pattern;
    Tcl_HashTable attrNames;
    Tcl_HashTable textDef;







|
>







 







>







 







|







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

/* The SchemaFlags flags */
#define FORWARD_PATTERN_DEF     1
#define PLACEHOLDER_PATTERN_DEF 2
#define AMBIGUOUS_PATTERN       4
#define LOCAL_DEFINED_ELEMENT   8
#define CONSTRAINT_TEXT_CHILD  16
#define MIXED_CONTENT          32
#define ELEMENTTYPE_DEF        64

typedef struct domKeyConstraint {
    char  *name;
    ast    selector;
    ast   *fields;
    int    nrFields;
    int    flags;
................................................................................
} SchemaKeySpace;

typedef struct SchemaCP
{
    Schema_CP_Type    type;
    char             *namespace;
    char             *name;
    char             *typeName;
    struct SchemaCP  *next;
    SchemaFlags       flags;
    struct SchemaCP **content;
    SchemaQuant      *quants;
    unsigned int      nc;
    SchemaAttr      **attrs;
    unsigned int      numAttr;
................................................................................

typedef struct SchemaData_
{
    Tcl_Obj *self;
    char *start;
    char *startNamespace;
    Tcl_HashTable element;
    Tcl_HashTable elementType;
    Tcl_HashTable namespace;
    Tcl_HashEntry *emptyNamespace;
    char **prefixns;
    Tcl_HashTable prefix;
    Tcl_HashTable pattern;
    Tcl_HashTable attrNames;
    Tcl_HashTable textDef;

Changes to tests/schema.test.

18
19
20
21
22
23
24

25
26
27
28
29
30
31
...
651
652
653
654
655
656
657


























658
659
660
661
662
663
664
....
7003
7004
7005
7006
7007
7008
7009
7010




7011



































































































































#    schema-15.*: Constraint cmd tcl
#    schema-16.*: interleave
#    schema-17.*: info
#    schema-18.*: reportcmd
#    schema-19.*: keyspace
#    schema-20.*: domunique
#    schema-21.*: internal: buffers

#
# Copyright (c) 2018-2019 Rolf Ade.

source [file join [file dir [info script]] loadtdom.tcl]

if {[dom featureinfo schema]} {

................................................................................
            }
        }
    }]
    s delete
    set result
} 1
        


























test schema-2.1 {grammar definition: ref} {
    tdom::schema create grammar
    grammar defpattern thisPattern {
        element a
        element b
    }
    grammar defpattern thatPattern {
................................................................................
                     <doc>[string repeat <e/> 27]</doc>  \
                     <doc>[string repeat <e/> 37]</doc>] {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 1 1 0}





}










































































































































>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 








>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
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
....
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
#    schema-15.*: Constraint cmd tcl
#    schema-16.*: interleave
#    schema-17.*: info
#    schema-18.*: reportcmd
#    schema-19.*: keyspace
#    schema-20.*: domunique
#    schema-21.*: internal: buffers
#    schema-22.*: defelementtype, elementtype
#
# Copyright (c) 2018-2019 Rolf Ade.

source [file join [file dir [info script]] loadtdom.tcl]

if {[dom featureinfo schema]} {

................................................................................
            }
        }
    }]
    s delete
    set result
} 1
        
test schema-1.29 {defelement} {
    tdom::schema s
    catch {
        s defelement e {
            element a 
            element b
            error "some"
        }
    }
    s defelement e {
        element a 
        element b
    }
    set result [list]
    foreach xml {
        <e/>
        <e><a/></e>
        <e><a/><b/></e>
        <e><a/><b/><b/></e>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 0}

test schema-2.1 {grammar definition: ref} {
    tdom::schema create grammar
    grammar defpattern thisPattern {
        element a
        element b
    }
    grammar defpattern thatPattern {
................................................................................
                     <doc>[string repeat <e/> 27]</doc>  \
                     <doc>[string repeat <e/> 37]</doc>] {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 1 1 0}

test schema-22.1 {defelementtype} {
    tdom::schema s
    s defelementtype a_type a {
        element e1
    }
    s defelement doc {
        elementtype a_type
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a><e1/></a></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1}

test schema-22.2 {defelementtype} {
    tdom::schema s
    s defelement doc {
        elementtype a_type
    }
    s defelementtype a_type a {
        element e1
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a><e1/></a></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1}

test schema-22.3 {defelementtype} {
    tdom::schema s
    s defelement doc {
        elementtype a
        elementtype a2
    }
    s defelementtype a a {
        element e1
    }
    s defelementtype a2 a {
        element e2
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a><e1/></a></doc>
        <doc><a><e1/></a><a><e2/></a></doc>
        <doc><a><e1/></a><a><e1/></a></doc>
        <doc><a><e2/></a><a><e1/></a></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 0 1 0 0}

test schema-22.4 {defelementtype} {
    tdom::schema s
    catch {s defelement doc {
        elementtype a
        elementtype a2
        error "my"
    }}
    s defelement doc {
        elementtype a
        elementtype a2
    }
    catch {s defelementtype a a {
        element e1
        error my
    }}
    s defelementtype a a {
        element e1
    }
    catch {s defelementtype a2 a {
        element e2
        error my
    }}
    s defelementtype a2 a {
        element e2
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a><e1/></a></doc>
        <doc><a><e1/></a><a><e2/></a></doc>
        <doc><a><e1/></a><a><e1/></a></doc>
        <doc><a><e2/></a><a><e1/></a></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 0 1 0 0}

test schema-22.5 {defelementtype} {
    tdom::schema s
    s prefixns {ns http://my.foo}
    s defelement doc ns {
        elementtype a
        elementtype a2
    }
    s defelementtype a a ns {
        element e1
    }
    s defelementtype a2 a ns {
        element e2
    }
    set result [list]
    foreach xml {
        {<doc xmlns='http://my.foo'/>}
        {<doc xmlns='http://my.foo'><a/></doc>}
        {<doc xmlns='http://my.foo'><a><e1/></a></doc>}
        {<doc xmlns='http://my.foo'><a><e1/></a><a><e2/></a></doc>}
        {<doc xmlns='http://my.foo'><a><e1/></a><a><e1/></a></doc>}
        {<doc xmlns='http://my.foo'><a><e2/></a><a><e1/></a></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 0 1 0 0}

}