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

Overview
Comment:wip
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | wip
Files: files | file ages | folders
SHA3-256:f8161e258e68a8f8d52ce9f0d6ea88fd0a4b0aba02350ce8956eb1f4dea85ea0
User & Date: rolf 2019-08-17 01:40:36
Context
2019-08-17
13:54
wip check-in: 0625bcaa4c user: rolf tags: wip
01:40
wip check-in: f8161e258e user: rolf tags: wip
2019-08-16
15:20
Merged from trunk. check-in: 5cf43bf74a user: rolf tags: schema
Changes

Changes to generic/schema.c.

785
786
787
788
789
790
791








































792
793
794
795
796
797
798


799
800
801
802
803
804
805
...
815
816
817
818
819
820
821




822
823
824
825
826
827
828
...
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
...
997
998
999
1000
1001
1002
1003
1004

1005
1006
1007
1008
1009
1010
1011
....
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
....
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
....
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
....
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
....
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
....
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
....
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
....
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
....
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
....
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
....
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
....
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
....
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
....
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
....
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
....
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
....
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
....
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
....
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
....
5322
5323
5324
5325
5326
5327
5328

5329
5330
5331
5332
5333
5334
5335
        sdata->stack->interleaveState = NULL;
    }
    se = sdata->stack->down;
    sdata->stack->down = sdata->stackPool;
    sdata->stackPool = sdata->stack;
    sdata->stack = se;
}









































static int 
recover (
    Tcl_Interp *interp,
    SchemaData *sdata,
    const char *errType,
    int len


    )
{
    Tcl_Obj *cmdPtr;
    int rc;

    if (!sdata->reportCmd) return 0;
    cmdPtr = Tcl_DuplicateObj (sdata->reportCmd);
................................................................................
                        TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
    sdata->currentEvals--;
    Tcl_DecrRefCount (cmdPtr);
    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }




    return 1;
}

/* The cp argument must be type SCHEMA_CTYPE_TEXT */
static int
checkText (
    Tcl_Interp *interp,
................................................................................
        while (ac < cp->nc) {
            candidate = cp->content[ac];
            mayskip = 0;
            switch (candidate->type) {
            case SCHEMA_CTYPE_TEXT:
                if (candidate->nc) {
                    if (!checkText (interp, candidate, "")) {
                        if (recover (interp, sdata, S("MISSING_TEXT"))) {
                            mayskip = 1;
                            break;
                        }                        
                        return 0;
                    }
                }
                break;

            case SCHEMA_CTYPE_ANY:
                if (candidate->namespace &&
................................................................................
                popStack (sdata);
                break;

            case SCHEMA_CTYPE_KEYSPACE_END:
                candidate->keySpace->active--;
                if (!candidate->keySpace->active) {
                    if (candidate->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata, S("UNKNOWN_KEYREF"))) {

                            return 0;
                        }
                        candidate->keySpace->unknownIDrefs = 0;
                    }
                    Tcl_DeleteHashTable (&candidate->keySpace->ids);
                }
                ac++;
................................................................................
                    candidate->keySpace->active++;
                }
                ac++;
                hm = 0;
                continue;
            }
            if (!mayskip && mustMatch (cp->quants[ac], hm)) {
                if (recover (interp, sdata, S("MISSING_CP"))) {
                    /* Skip the just opened element tag and the following
                     * content of the current. */
                    sdata->skipDeep = 2;
                    return 1;
                }
                return 0;
            }
            ac++;
            hm = 0;
        }
        if (isName) {
            if (recover (interp, sdata, "UNEXPECTED_ELEMENT", 15)) {
                /* Skip the just opened element tag and the following
                 * content of the current. */
                sdata->skipDeep = 2;
                return 1;
            }
            return 0;
        }
        return -1;

    case SCHEMA_CTYPE_KEYSPACE:
................................................................................
                Tcl_Panic ("Keyspace constraint child of INTERLEAVE");
                break;

            }
        }
                
        if (mayskip) break;
        if (recover (interp, sdata, S("UNCOMPLET_CP"))) {
            sdata->skipDeep = 2;
            return 1;
        }
    }
    
    return -1;
}

................................................................................
    }

    if (sdata->stack) {
        SchemaValidationStack *se;
        se = sdata->stack;
        if (se->pattern->type == SCHEMA_CTYPE_NAME
            && se->activeChild >= se->pattern->nc) {
            if (recover (interp, sdata, S("UNEXPECTED_ELEMENT"))) {
                sdata->skipDeep = 1;
                return TCL_OK;
            }
            SetResult ("Unexpected child element \"");
            if (namespacePtr) {
                Tcl_AppendResult (interp, namespacePtr, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\" for element \"", NULL);
................................................................................
                Tcl_AppendResult (interp, namespace, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\"", NULL);
            return TCL_ERROR;
        }
    } else {
        if (!pattern) {
            if (recover (interp, sdata, S("UNKNOWN_ROOT_ELEMENT"))) {
                sdata->skipDeep = 1;
                return TCL_OK;
            }
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        pushToStack (sdata, pattern);
................................................................................
            }
            if (strcmp (ln, cp->attrs[i]->name) == 0) {
                found = 1;
                if (cp->attrs[i]->cp) {
                    if (!checkText (interp, cp->attrs[i]->cp,
                                    (char *) atPtr[1])) {
                        if (!recover (interp, sdata,
                                      S("WRONG_ATTRIBUTE_VALUE"))) {
                            if (nsatt) namespace[j] = '\xFF';
                            SetResult3 ("Attribute value doesn't match for "
                                        "attribute '", atPtr[0], "'");
                            return TCL_ERROR;
                        }
                    }
                }
                if (cp->attrs[i]->required) reqAttr++;
                break;
            }
        }
        if (nsatt) namespace[j] = '\xFF';
        if (!found) {
            if (!recover (interp, sdata, S("UNKNOWN_ATTRIBUTE"))) {
                SetResult3 ("Unknown attribute \"", atPtr[0], "\"");
                return TCL_ERROR;
            }
        }
    }
    if (reqAttr != cp->numReqAttr) {
        /* Lookup the missing attribute(s) */
................................................................................
                }
                if (strcmp (atPtr[0], cp->attrs[i]->name) == 0) {
                    found = 1;
                    break;
                }
            }
            if (!found) {
                if (!recover (interp, sdata, S("MISSING_ATTRIBUTE"))) {
                    if (cp->attrs[i]->namespace) {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->namespace,
                                          ":", cp->attrs[i]->name, NULL);
                    } else {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->name, NULL);
                    }
                }
................................................................................
                if (cp->attrs[i]->namespace) continue;
            }
            if (strcmp (ln, cp->attrs[i]->name) == 0) {
                if (cp->attrs[i]->cp) {
                    if (!checkText (interp, cp->attrs[i]->cp,
                                    (char *) atPtr->nodeValue)) {
                        if (!recover (interp, sdata,
                                      S("WRONG_ATTRIBUTE_VALUE"))) {
                            SetResult3 ("Attribute value doesn't match for "
                                        "attribute '", ln, "'");
                            return TCL_ERROR;
                        }
                    }
                }
                found = 1;
                if (cp->attrs[i]->required) reqAttr++;
                break;
            }
        }
        if (!found) {
            if (!recover (interp, sdata, S("UNKNOWN_ATTRIBUTE"))) {
                if (ns) {
                    SetResult ("Unknown attribute \"");
                    Tcl_AppendResult (interp, ns, ":", atPtr->nodeName,
                                      "\"");
                } else {
                    SetResult3 ("Unknown attribute \"", atPtr->nodeName, "\"");
                }
................................................................................
                    found = 1;
                    break;
                }
            nextAttr2:
                atPtr = atPtr->nextSibling;
            }
            if (!found) {
                if (!recover (interp, sdata, S("MISSING_ATTRIBUTE"))) {
                    if (cp->attrs[i]->namespace) {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->namespace,
                                          ":", cp->attrs[i]->name, NULL);
                    } else {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->name,
                                          NULL);
                    }
................................................................................
            }
            
            switch (cp->content[ac]->type) {
            case SCHEMA_CTYPE_KEYSPACE_END:
                cp->content[ac]->keySpace->active--;
                if (!cp->content[ac]->keySpace->active) {
                    if (cp->content[ac]->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata, S("UNKNOWN_KEYREF"))) {
                            return 0;
                        }
                        cp->content[ac]->keySpace->unknownIDrefs = 0;
                    }
                    Tcl_DeleteHashTable (&cp->content[ac]->keySpace->ids);
                }
                break;
................................................................................
                    cp->content[ac]->keySpace->active++;
                }
                break;
                
            case SCHEMA_CTYPE_TEXT:
                if (cp->content[ac]->nc) {
                    if (!checkText (interp, cp->content[ac], "")) {
                        if (recover (interp, sdata, S("MISSING_TEXT"))) {
                            break;
                        }
                        return 0;
                    }
                }
                break;

................................................................................
                    case SCHEMA_CTYPE_CHOICE:
                        Tcl_Panic ("Invalid CTYPE in MIXED or CHOICE");
                        
                    }
                    if (mayMiss) break;
                }
                if (mayMiss) break;
                if (!recover (interp, sdata, S("MISSING_ONE_OF_CHOICE"))) {
                    return 0;
                }
                break;
                
            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, cp->content[ac])) break;
                else return 0;
................................................................................
                pushToStack (sdata, cp->content[ac]);
                rc = checkElementEnd (interp, sdata);
                popStack (sdata);
                if (rc) break;
                return 0;
                
            case SCHEMA_CTYPE_ANY:
                if (recover (interp, sdata, S("MISSING_ANY"))) {
                    break;
                }
                return 0;
            case SCHEMA_CTYPE_NAME:
                if (recover (interp, sdata, S("MISSING_ELEMENT"))) {
                    break;
                }
                return 0;
            }
            ac++;
        }
        if (isName) return 1;
................................................................................
        /* Never pushed onto stack */
        Tcl_Panic ("Invalid CTYPE onto the validation stack!");
        return 0;

    case SCHEMA_CTYPE_INTERLEAVE:
        for (i = 0; i < cp->nc; i++) {
            if (mustMatch (cp->quants[i], se->interleaveState[i])) {
                if (recover (interp, sdata, S("MISSING_ONE_OF_INTERLEAVE"))) {
                    break;
                }
                return 0;
            }
        }
        return -1;
    }
................................................................................
                    }
                    break;
                    
                case SCHEMA_CTYPE_KEYSPACE_END:
                    cp->content[ac]->keySpace->active--;
                    if (!cp->content[ac]->keySpace->active) {
                        if (cp->content[ac]->keySpace->unknownIDrefs) {
                            if (!recover (interp, sdata, S("UNKNOWN_KEYREF"))) {
                                return 0;
                            }
                            cp->content[ac]->keySpace->unknownIDrefs = 0;
                        }
                        Tcl_DeleteHashTable (&cp->content[ac]->keySpace->ids);
                    }
                    break;
................................................................................
            break;
        }
        if (only_whites)  return TCL_OK;
        if (matchText (interp, sdata, text)) {
            return TCL_OK;
        }
    }
    if (recover (interp, sdata, S("WRONG_VALUE"))) {
        return TCL_OK;
    }
    if (!sdata->evalError) {
        SetResult ("Text content doesn't match");
    }
    return TCL_ERROR;
}
................................................................................
    Tcl_DStringInit (&dStr);
    while (kc) {
        xpathRSReset (&rs, NULL);
        xpathRSReset (&nodeList, node);
        Tcl_InitHashTable (&htable, TCL_STRING_KEYS);
        rc = xpathEvalAst (kc->selector, &nodeList, node, &rs, &errMsg);
        if (rc) {
            if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                goto nextConstraint;
            }
            goto errorCleanup;
        }
        if (rs.type == EmptyResult) goto nextConstraint;
        if (rs.type != xNodeSetResult) {
            if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                goto nextConstraint;
            }
            SetResult ("INVALID_DOM_KEYCONSTRAINT");
            goto errorCleanup;
        }
        for (i = 0; i < rs.nr_nodes; i++) {
            n = rs.nodes[i];
            if (n->nodeType != ELEMENT_NODE) {
                if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                    break;
                }
                SetResult ("INVALID_DOM_KEYCONSTRAINT");
                goto errorCleanup;
            }
            xpathRSReset (&nodeList, n);
            if (kc->nrFields == 1) {
                xpathRSReset (&frs, NULL);
                rc = xpathEvalAst (kc->fields[0], &nodeList, n, &frs,
                                   &errMsg);
                if (rc) {
                    if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                        break;
                    }
                    SetResult ("INVALID_DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.type != xNodeSetResult
                    && frs.type != EmptyResult) {
                    if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                        break;
                    }
                    SetResult ("INVALID_DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.type == EmptyResult || frs.nr_nodes == 0) {
                    if (kc->flags & DKC_FLAG_IGNORE_EMPTY_FIELD_SET) {
                        continue;
                    }
                    Tcl_CreateHashEntry (&htable, "", &hnew);
                    if (!hnew) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"))) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    continue;
                }
                if (frs.nr_nodes != 1) {
                    if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"))) {
                        break;
                    }
                    SetResult ("DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.nodes[0]->nodeType != ELEMENT_NODE
                    && frs.nodes[0]->nodeType != ATTRIBUTE_NODE) {
                    if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                        break;
                    }
                    SetResult ("INVALID_DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.nodes[0]->nodeType == ATTRIBUTE_NODE) {
                    attr = (domAttrNode *) frs.nodes[0];
                    Tcl_CreateHashEntry (&htable, attr->nodeValue, &hnew);
                    if (!hnew) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"))) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                } else {
                    keystr = xpathGetStringValue (frs.nodes[0], &len);
                    Tcl_CreateHashEntry (&htable, keystr, &hnew);
                    FREE(keystr);
                    if (!hnew) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"))) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                }
            } else {
................................................................................
                skip = 0;
                first = 1;
                for (j = 0; j < kc->nrFields; j++) {
                    xpathRSReset (&frs, NULL);
                    rc = xpathEvalAst (kc->fields[j], &nodeList, n, &frs,
                                       &errMsg);
                    if (rc) {
                        if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                            skip = 1;
                            break;
                        }
                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.type != xNodeSetResult
                        && frs.type != EmptyResult) {
                        if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                            skip = 1;
                            break;
                        }
                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.type == EmptyResult || frs.nr_nodes == 0) {
................................................................................
                            continue;
                        }
                        if (first) first = 0;
                        else Tcl_DStringAppend (&dStr, ":", 1);
                        continue;
                    }
                    if (frs.nr_nodes != 1) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"))) {
                            skip = 1;
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.nodes[0]->nodeType != ELEMENT_NODE
                        && frs.nodes[0]->nodeType != ATTRIBUTE_NODE) {
                        if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"))) {
                            skip = 1;
                            break;
                        }
                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (first) first = 0;
................................................................................
                        keystr = xpathGetStringValue (frs.nodes[0], &len);
                        Tcl_DStringAppend (&dStr, keystr, len);
                    }
                }
                if (skip) break;
                Tcl_CreateHashEntry (&htable, Tcl_DStringValue (&dStr), &hnew);
                if (!hnew) {
                    if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"))) {
                        break;
                    }
                    SetResult ("DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
            }
        }
................................................................................
    char *text
    )
{
    SchemaKeySpace *ks = (SchemaKeySpace *) constraintData;
    int hnew;
    Tcl_HashEntry *h;


    if (!ks->active) return 1;
    h = Tcl_CreateHashEntry (&ks->ids, text, &hnew);
    if (hnew) {
        Tcl_SetHashValue (h, 1);
        return 1;
    }
    if (Tcl_GetHashValue (h) == 0) {







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






|
>
>







 







>
>
>
>







 







|
|
|
|







 







|
>







 







|


<








|


<







 







|
<







 







|
|







 







|







 







|













|







 







|







 







|












|







 







|







 







|







 







|







 







|







 







|




|







 







|







 







|







 







|







 







|






|








|











|







|











|








|







|









|










|







 







|








|







 







|








|







 







|







 







>







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
...
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
...
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
....
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
....
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078

1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089

1090
1091
1092
1093
1094
1095
1096
....
1163
1164
1165
1166
1167
1168
1169
1170

1171
1172
1173
1174
1175
1176
1177
....
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
....
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
....
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
....
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
....
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
....
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
....
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
....
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
....
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
....
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
....
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
....
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
....
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
....
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
....
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
....
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
....
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
....
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
        sdata->stack->interleaveState = NULL;
    }
    se = sdata->stack->down;
    sdata->stack->down = sdata->stackPool;
    sdata->stackPool = sdata->stack;
    sdata->stack = se;
}

static void
finalizeElement (
    SchemaData *sdata,
    int ac
    )
{
    SchemaValidationStack *se;
    SchemaCP *cp, *cp1;
    int i;

    se = sdata->stack;
    cp = se->pattern;
    if (cp->type == SCHEMA_CTYPE_NAME || cp->type == SCHEMA_CTYPE_PATTERN) {
        for (i = ac; i < cp->nc; i++) {
            cp1 = cp->content[ac];
            if (cp1->type == SCHEMA_CTYPE_KEYSPACE) {
                if (!cp1->keySpace->active) {
                    Tcl_InitHashTable (&cp1->keySpace->ids,
                                       TCL_STRING_KEYS);
                    cp1->keySpace->active = 1;
                    cp1->keySpace->unknownIDrefs = 0;
                } else {
                    cp1->keySpace->active++;
                }                    
            } else if (cp->content[ac]->type == SCHEMA_CTYPE_KEYSPACE_END) {
                cp1->keySpace->active--;
                if (!cp1->keySpace->active) {
                    cp1->keySpace->unknownIDrefs = 0;
                    Tcl_DeleteHashTable (&cp1->keySpace->ids);
                }
            }
        }
    }
    popStack (sdata);
    if (cp->type != SCHEMA_CTYPE_NAME) {
        finalizeElement (sdata, sdata->stack->activeChild + 1);
    }
}


static int 
recover (
    Tcl_Interp *interp,
    SchemaData *sdata,
    const char *errType,
    int len,
    int popStack,
    int ac
    )
{
    Tcl_Obj *cmdPtr;
    int rc;

    if (!sdata->reportCmd) return 0;
    cmdPtr = Tcl_DuplicateObj (sdata->reportCmd);
................................................................................
                        TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
    sdata->currentEvals--;
    Tcl_DecrRefCount (cmdPtr);
    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }
    if (popStack) {
        finalizeElement (sdata, ac+1);
        sdata->skipDeep = 2;
    }
    return 1;
}

/* The cp argument must be type SCHEMA_CTYPE_TEXT */
static int
checkText (
    Tcl_Interp *interp,
................................................................................
        while (ac < cp->nc) {
            candidate = cp->content[ac];
            mayskip = 0;
            switch (candidate->type) {
            case SCHEMA_CTYPE_TEXT:
                if (candidate->nc) {
                    if (!checkText (interp, candidate, "")) {
                        if (recover (interp, sdata, S("MISSING_TEXT"),
                                     1, ac)) {
                            return 1;
                        }            
                        return 0;
                    }
                }
                break;

            case SCHEMA_CTYPE_ANY:
                if (candidate->namespace &&
................................................................................
                popStack (sdata);
                break;

            case SCHEMA_CTYPE_KEYSPACE_END:
                candidate->keySpace->active--;
                if (!candidate->keySpace->active) {
                    if (candidate->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata, S("UNKNOWN_KEYREF"),
                                      0, 0)) {
                            return 0;
                        }
                        candidate->keySpace->unknownIDrefs = 0;
                    }
                    Tcl_DeleteHashTable (&candidate->keySpace->ids);
                }
                ac++;
................................................................................
                    candidate->keySpace->active++;
                }
                ac++;
                hm = 0;
                continue;
            }
            if (!mayskip && mustMatch (cp->quants[ac], hm)) {
                if (recover (interp, sdata, S("MISSING_CP"), 1, ac)) {
                    /* Skip the just opened element tag and the following
                     * content of the current. */

                    return 1;
                }
                return 0;
            }
            ac++;
            hm = 0;
        }
        if (isName) {
            if (recover (interp, sdata, S("UNEXPECTED_ELEMENT"), 1, ac)) {
                /* Skip the just opened element tag and the following
                 * content of the current. */

                return 1;
            }
            return 0;
        }
        return -1;

    case SCHEMA_CTYPE_KEYSPACE:
................................................................................
                Tcl_Panic ("Keyspace constraint child of INTERLEAVE");
                break;

            }
        }
                
        if (mayskip) break;
        if (recover (interp, sdata, S("UNCOMPLET_CP"), 1, ac)) {

            return 1;
        }
    }
    
    return -1;
}

................................................................................
    }

    if (sdata->stack) {
        SchemaValidationStack *se;
        se = sdata->stack;
        if (se->pattern->type == SCHEMA_CTYPE_NAME
            && se->activeChild >= se->pattern->nc) {
            if (recover (interp, sdata, S("UNEXPECTED_ELEMENT"), 1,
                         se->activeChild)) {
                return TCL_OK;
            }
            SetResult ("Unexpected child element \"");
            if (namespacePtr) {
                Tcl_AppendResult (interp, namespacePtr, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\" for element \"", NULL);
................................................................................
                Tcl_AppendResult (interp, namespace, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\"", NULL);
            return TCL_ERROR;
        }
    } else {
        if (!pattern) {
            if (recover (interp, sdata, S("UNKNOWN_ROOT_ELEMENT"), 0, 0)) {
                sdata->skipDeep = 1;
                return TCL_OK;
            }
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        pushToStack (sdata, pattern);
................................................................................
            }
            if (strcmp (ln, cp->attrs[i]->name) == 0) {
                found = 1;
                if (cp->attrs[i]->cp) {
                    if (!checkText (interp, cp->attrs[i]->cp,
                                    (char *) atPtr[1])) {
                        if (!recover (interp, sdata,
                                      S("WRONG_ATTRIBUTE_VALUE"), 0, 0)) {
                            if (nsatt) namespace[j] = '\xFF';
                            SetResult3 ("Attribute value doesn't match for "
                                        "attribute '", atPtr[0], "'");
                            return TCL_ERROR;
                        }
                    }
                }
                if (cp->attrs[i]->required) reqAttr++;
                break;
            }
        }
        if (nsatt) namespace[j] = '\xFF';
        if (!found) {
            if (!recover (interp, sdata, S("UNKNOWN_ATTRIBUTE"), 0, 0)) {
                SetResult3 ("Unknown attribute \"", atPtr[0], "\"");
                return TCL_ERROR;
            }
        }
    }
    if (reqAttr != cp->numReqAttr) {
        /* Lookup the missing attribute(s) */
................................................................................
                }
                if (strcmp (atPtr[0], cp->attrs[i]->name) == 0) {
                    found = 1;
                    break;
                }
            }
            if (!found) {
                if (!recover (interp, sdata, S("MISSING_ATTRIBUTE"), 0, 0)) {
                    if (cp->attrs[i]->namespace) {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->namespace,
                                          ":", cp->attrs[i]->name, NULL);
                    } else {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->name, NULL);
                    }
                }
................................................................................
                if (cp->attrs[i]->namespace) continue;
            }
            if (strcmp (ln, cp->attrs[i]->name) == 0) {
                if (cp->attrs[i]->cp) {
                    if (!checkText (interp, cp->attrs[i]->cp,
                                    (char *) atPtr->nodeValue)) {
                        if (!recover (interp, sdata,
                                      S("WRONG_ATTRIBUTE_VALUE"), 0, 0)) {
                            SetResult3 ("Attribute value doesn't match for "
                                        "attribute '", ln, "'");
                            return TCL_ERROR;
                        }
                    }
                }
                found = 1;
                if (cp->attrs[i]->required) reqAttr++;
                break;
            }
        }
        if (!found) {
            if (!recover (interp, sdata, S("UNKNOWN_ATTRIBUTE"), 0, 0)) {
                if (ns) {
                    SetResult ("Unknown attribute \"");
                    Tcl_AppendResult (interp, ns, ":", atPtr->nodeName,
                                      "\"");
                } else {
                    SetResult3 ("Unknown attribute \"", atPtr->nodeName, "\"");
                }
................................................................................
                    found = 1;
                    break;
                }
            nextAttr2:
                atPtr = atPtr->nextSibling;
            }
            if (!found) {
                if (!recover (interp, sdata, S("MISSING_ATTRIBUTE"), 0, 0)) {
                    if (cp->attrs[i]->namespace) {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->namespace,
                                          ":", cp->attrs[i]->name, NULL);
                    } else {
                        Tcl_AppendResult (interp, " ", cp->attrs[i]->name,
                                          NULL);
                    }
................................................................................
            }
            
            switch (cp->content[ac]->type) {
            case SCHEMA_CTYPE_KEYSPACE_END:
                cp->content[ac]->keySpace->active--;
                if (!cp->content[ac]->keySpace->active) {
                    if (cp->content[ac]->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata, S("UNKNOWN_KEYREF"), 0, 0)) {
                            return 0;
                        }
                        cp->content[ac]->keySpace->unknownIDrefs = 0;
                    }
                    Tcl_DeleteHashTable (&cp->content[ac]->keySpace->ids);
                }
                break;
................................................................................
                    cp->content[ac]->keySpace->active++;
                }
                break;
                
            case SCHEMA_CTYPE_TEXT:
                if (cp->content[ac]->nc) {
                    if (!checkText (interp, cp->content[ac], "")) {
                        if (recover (interp, sdata, S("MISSING_TEXT"), 0, 0)) {
                            break;
                        }
                        return 0;
                    }
                }
                break;

................................................................................
                    case SCHEMA_CTYPE_CHOICE:
                        Tcl_Panic ("Invalid CTYPE in MIXED or CHOICE");
                        
                    }
                    if (mayMiss) break;
                }
                if (mayMiss) break;
                if (!recover (interp, sdata, S("MISSING_ONE_OF_CHOICE"), 0, 0)) {
                    return 0;
                }
                break;
                
            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, cp->content[ac])) break;
                else return 0;
................................................................................
                pushToStack (sdata, cp->content[ac]);
                rc = checkElementEnd (interp, sdata);
                popStack (sdata);
                if (rc) break;
                return 0;
                
            case SCHEMA_CTYPE_ANY:
                if (recover (interp, sdata, S("MISSING_ANY"), 0, 0)) {
                    break;
                }
                return 0;
            case SCHEMA_CTYPE_NAME:
                if (recover (interp, sdata, S("MISSING_ELEMENT"), 0, 0)) {
                    break;
                }
                return 0;
            }
            ac++;
        }
        if (isName) return 1;
................................................................................
        /* Never pushed onto stack */
        Tcl_Panic ("Invalid CTYPE onto the validation stack!");
        return 0;

    case SCHEMA_CTYPE_INTERLEAVE:
        for (i = 0; i < cp->nc; i++) {
            if (mustMatch (cp->quants[i], se->interleaveState[i])) {
                if (recover (interp, sdata, S("MISSING_ONE_OF_INTERLEAVE"), 0, 0)) {
                    break;
                }
                return 0;
            }
        }
        return -1;
    }
................................................................................
                    }
                    break;
                    
                case SCHEMA_CTYPE_KEYSPACE_END:
                    cp->content[ac]->keySpace->active--;
                    if (!cp->content[ac]->keySpace->active) {
                        if (cp->content[ac]->keySpace->unknownIDrefs) {
                            if (!recover (interp, sdata, S("UNKNOWN_KEYREF"), 0, 0)) {
                                return 0;
                            }
                            cp->content[ac]->keySpace->unknownIDrefs = 0;
                        }
                        Tcl_DeleteHashTable (&cp->content[ac]->keySpace->ids);
                    }
                    break;
................................................................................
            break;
        }
        if (only_whites)  return TCL_OK;
        if (matchText (interp, sdata, text)) {
            return TCL_OK;
        }
    }
    if (recover (interp, sdata, S("WRONG_VALUE"), 0, 0)) {
        return TCL_OK;
    }
    if (!sdata->evalError) {
        SetResult ("Text content doesn't match");
    }
    return TCL_ERROR;
}
................................................................................
    Tcl_DStringInit (&dStr);
    while (kc) {
        xpathRSReset (&rs, NULL);
        xpathRSReset (&nodeList, node);
        Tcl_InitHashTable (&htable, TCL_STRING_KEYS);
        rc = xpathEvalAst (kc->selector, &nodeList, node, &rs, &errMsg);
        if (rc) {
            if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                goto nextConstraint;
            }
            goto errorCleanup;
        }
        if (rs.type == EmptyResult) goto nextConstraint;
        if (rs.type != xNodeSetResult) {
            if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                goto nextConstraint;
            }
            SetResult ("INVALID_DOM_KEYCONSTRAINT");
            goto errorCleanup;
        }
        for (i = 0; i < rs.nr_nodes; i++) {
            n = rs.nodes[i];
            if (n->nodeType != ELEMENT_NODE) {
                if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                    break;
                }
                SetResult ("INVALID_DOM_KEYCONSTRAINT");
                goto errorCleanup;
            }
            xpathRSReset (&nodeList, n);
            if (kc->nrFields == 1) {
                xpathRSReset (&frs, NULL);
                rc = xpathEvalAst (kc->fields[0], &nodeList, n, &frs,
                                   &errMsg);
                if (rc) {
                    if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                        break;
                    }
                    SetResult ("INVALID_DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.type != xNodeSetResult
                    && frs.type != EmptyResult) {
                    if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                        break;
                    }
                    SetResult ("INVALID_DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.type == EmptyResult || frs.nr_nodes == 0) {
                    if (kc->flags & DKC_FLAG_IGNORE_EMPTY_FIELD_SET) {
                        continue;
                    }
                    Tcl_CreateHashEntry (&htable, "", &hnew);
                    if (!hnew) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"), 0, 0)) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    continue;
                }
                if (frs.nr_nodes != 1) {
                    if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"), 0, 0)) {
                        break;
                    }
                    SetResult ("DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.nodes[0]->nodeType != ELEMENT_NODE
                    && frs.nodes[0]->nodeType != ATTRIBUTE_NODE) {
                    if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                        break;
                    }
                    SetResult ("INVALID_DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.nodes[0]->nodeType == ATTRIBUTE_NODE) {
                    attr = (domAttrNode *) frs.nodes[0];
                    Tcl_CreateHashEntry (&htable, attr->nodeValue, &hnew);
                    if (!hnew) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"), 0, 0)) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                } else {
                    keystr = xpathGetStringValue (frs.nodes[0], &len);
                    Tcl_CreateHashEntry (&htable, keystr, &hnew);
                    FREE(keystr);
                    if (!hnew) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"), 0, 0)) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                }
            } else {
................................................................................
                skip = 0;
                first = 1;
                for (j = 0; j < kc->nrFields; j++) {
                    xpathRSReset (&frs, NULL);
                    rc = xpathEvalAst (kc->fields[j], &nodeList, n, &frs,
                                       &errMsg);
                    if (rc) {
                        if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                            skip = 1;
                            break;
                        }
                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.type != xNodeSetResult
                        && frs.type != EmptyResult) {
                        if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                            skip = 1;
                            break;
                        }
                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.type == EmptyResult || frs.nr_nodes == 0) {
................................................................................
                            continue;
                        }
                        if (first) first = 0;
                        else Tcl_DStringAppend (&dStr, ":", 1);
                        continue;
                    }
                    if (frs.nr_nodes != 1) {
                        if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"), 0, 0)) {
                            skip = 1;
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.nodes[0]->nodeType != ELEMENT_NODE
                        && frs.nodes[0]->nodeType != ATTRIBUTE_NODE) {
                        if (recover (interp, sdata, S("INVALID_DOM_KEYCONSTRAINT"), 0, 0)) {
                            skip = 1;
                            break;
                        }
                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (first) first = 0;
................................................................................
                        keystr = xpathGetStringValue (frs.nodes[0], &len);
                        Tcl_DStringAppend (&dStr, keystr, len);
                    }
                }
                if (skip) break;
                Tcl_CreateHashEntry (&htable, Tcl_DStringValue (&dStr), &hnew);
                if (!hnew) {
                    if (recover (interp, sdata, S("DOM_KEYCONSTRAINT"), 0, 0)) {
                        break;
                    }
                    SetResult ("DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
            }
        }
................................................................................
    char *text
    )
{
    SchemaKeySpace *ks = (SchemaKeySpace *) constraintData;
    int hnew;
    Tcl_HashEntry *h;

    fprintf (stderr, "keyImpl called: '%s'\n", text);
    if (!ks->active) return 1;
    h = Tcl_CreateHashEntry (&ks->ids, text, &hnew);
    if (hnew) {
        Tcl_SetHashValue (h, 1);
        return 1;
    }
    if (Tcl_GetHashValue (h) == 0) {

Changes to tests/schema.test.

4831
4832
4833
4834
4835
4836
4837




























4838
4839
4840
4841
4842
4843
4844

    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s WRONG_VALUE 1 s WRONG_VALUE s MISSING_TEXT 1 1}





























proc validatedSAX {g xml {keepEmpties 1}} {
    set args [list -validateCmd $g]
    if {!$keepEmpties} {
        lappend args -ignorewhitespace 1
    }
    xml::parser p {*}$args







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







4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872

    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s WRONG_VALUE 1 s WRONG_VALUE s MISSING_TEXT 1 1}

test schema-18.6 {reportcmd} {
    tdom::schema s
    s define {
        defelement item {
            text {key lang}
        }
        defelement items {
            element item +
        }
        defelement doc {
            element items + 
        }
    }
    s reportcmd schema-18
    set result [list]
    foreach xml {
        {<doc><items><item>12</item><item>ab</item></items></doc>}
        {<doc><items><item>12</item><item>12</item></items></doc>}
        {<doc><items><item>12</item><item>12</item></items><items><item>ab</item><item>cd</item></items></doc>}
        {<doc><items><item>12</item><item>12</item></items><items><item>ab</item><item>cd</item></items><items><item>12</item><item>12</item></items></doc>}
    } {
        lappend result [s validate $xml errMsg]
        puts $errMsg
    }
    s delete
    set result
} {}

proc validatedSAX {g xml {keepEmpties 1}} {
    set args [list -validateCmd $g]
    if {!$keepEmpties} {
        lappend args -ignorewhitespace 1
    }
    xml::parser p {*}$args