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

Overview
Comment:Added simple recovery, with infrastructure to add more fancy recovery features without too much fall out. Added info expected, which returns the expected (possible) events, even in a validation error report handler.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | schema
Files: files | file ages | folders
SHA3-256: a16fad774fdba14e334979e738513df09cb38c0b8e74ace9c4848dd378475b7a
User & Date: rolf 2019-11-04 18:08:59
Context
2019-11-05
22:16
Another attempt to get info expected right. check-in: 03f4151498 user: rolf tags: schema
2019-11-04
18:08
Added simple recovery, with infrastructure to add more fancy recovery features without too much fall out. Added info expected, which returns the expected (possible) events, even in a validation error report handler. check-in: a16fad774f user: rolf tags: schema
17:47
Fixes: Don't try to check attributes if in skip mode - it doesn't make sense to check attributes of an unknown element. Don't fumble with activeChild before calling out to virual matches. Report also element end as possible event (if that is possible, of course) for info expected. Closed-Leaf check-in: 65eff1573f user: rolf tags: wip
2019-10-24
14:12
Added xsd as new format (and default) to the text constraint command "number". check-in: c2b6dd2267 user: rolf tags: schema
Changes

Changes to generic/schema.c.

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80




























































81
82
83
84
85
86
87
..
89
90
91
92
93
94
95




96
97




98
99
100
101
102
103
104
...
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
...
535
536
537
538
539
540
541


542
543
544







545
546


547
548
549
550
551
552
553
...
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
...
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
...
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
...
969
970
971
972
973
974
975
976
977
978

979
980
981
982
983
984
985
....
1074
1075
1076
1077
1078
1079
1080
1081
1082

1083
1084
1085
1086
1087
1088
1089
....
1100
1101
1102
1103
1104
1105
1106
1107

1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118

1119
1120
1121
1122
1123
1124
1125
....
1195
1196
1197
1198
1199
1200
1201
1202

1203
1204
1205
1206
1207
1208
1209
....
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
....
1288
1289
1290
1291
1292
1293
1294





1295
1296
1297
1298
1299
1300





1301
1302
1303
1304
1305





1306
1307
1308
1309
1310
1311
1312
....
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347

1348
1349
1350
1351
1352
1353
1354
....
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
....
1457
1458
1459
1460
1461
1462
1463
1464

1465
1466
1467
1468
1469
1470
1471
1472

1473

1474
1475
1476
1477
1478
1479
1480
....
1497
1498
1499
1500
1501
1502
1503
1504


1505
1506
1507
1508
1509
1510
1511
....
1560
1561
1562
1563
1564
1565
1566
1567

1568
1569
1570
1571
1572
1573

1574
1575
1576

1577
1578
1579
1580
1581
1582
1583

1584

1585
1586
1587
1588
1589
1590
1591
....
1609
1610
1611
1612
1613
1614
1615
1616



1617
1618

1619
1620
1621
1622

1623
1624
1625
1626
1627
1628
1629
....
1667
1668
1669
1670
1671
1672
1673
1674


1675
1676
1677
1678
1679
1680
1681
....
1723
1724
1725
1726
1727
1728
1729
1730


1731
1732
1733
1734
1735
1736
1737
....
1746
1747
1748
1749
1750
1751
1752
1753

1754
1755
1756
1757
1758
1759
1760
....
1795
1796
1797
1798
1799
1800
1801
1802

1803
1804
1805
1806
1807
1808
1809
....
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825

1826
1827
1828
1829
1830
1831
1832
....
1841
1842
1843
1844
1845
1846
1847
1848

1849
1850
1851
1852
1853
1854
1855
....
1863
1864
1865
1866
1867
1868
1869
1870
1871

1872
1873
1874
1875
1876
1877
1878
1879
1880

1881
1882
1883
1884
1885
1886
1887
1888
1889


1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905

1906
1907
1908
1909
1910
1911
1912
....
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
....
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013

2014





2015
2016
2017
2018
2019
2020
2021
....
2040
2041
2042
2043
2044
2045
2046
2047

2048
2049
2050

2051
2052
2053
2054

2055
2056
2057
2058




2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072




2073
2074
2075
2076
2077
2078
2079
2080
....
2090
2091
2092
2093
2094
2095
2096
2097


2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108




2109
2110
2111
2112
2113
2114
2115
2116
2117




2118
2119
2120
2121
2122
2123
2124
2125
....
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
....
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
....
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503

2504
2505
2506
2507
2508
2509
2510
2511
2512

2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530

2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541

2542
2543
2544
2545
2546
2547
2548
....
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582

2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
....
2605
2606
2607
2608
2609
2610
2611
2612

2613
2614
2615
2616
2617
2618
2619
....
2665
2666
2667
2668
2669
2670
2671

2672
2673
2674
2675
2676
2677
2678
2679
2680
2681

2682
2683
2684
2685
2686
2687
2688
....
2747
2748
2749
2750
2751
2752
2753

2754
2755
2756

2757
2758
2759
2760
2761
2762
2763
....
2870
2871
2872
2873
2874
2875
2876














2877
2878
2879
2880
2881
2882
2883
....
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
....
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
....
2973
2974
2975
2976
2977
2978
2979
2980
2981

2982
2983
2984
2985
2986
2987
2988
....
3002
3003
3004
3005
3006
3007
3008

3009
3010
3011
3012

3013

3014





3015
3016
3017
3018
3019
3020
3021
....
3025
3026
3027
3028
3029
3030
3031






















































3032

3033











3034
3035




3036
3037
3038
3039
3040




3041

3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065

3066
3067
3068
3069
3070
3071
3072
3073







3074
3075
3076
3077
3078
3079
3080
....
3108
3109
3110
3111
3112
3113
3114




3115
3116
3117
3118
3119
3120
3121
3122
3123















3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158


3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
....
3237
3238
3239
3240
3241
3242
3243





























3244
3245
3246
3247
3248
3249
3250
....
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
....
3602
3603
3604
3605
3606
3607
3608



3609

3610
3611
3612
3613
3614
3615
3616
....
4479
4480
4481
4482
4483
4484
4485

4486
4487
4488
4489
4490
4491
4492
/*----------------------------------------------------------------------------
|   Local typedefs
|
\---------------------------------------------------------------------------*/

typedef struct
{
    SchemaData *sdata;
    Tcl_Interp    *interp;
    XML_Parser     parser;
    Tcl_DString   *cdata;
    int            onlyWhiteSpace;
    char          *uri;
    int            maxUriLen;
} ValidateMethodData;





























































/*----------------------------------------------------------------------------
|   domKeyConstraint related flage
|
\---------------------------------------------------------------------------*/

#define DKC_FLAG_IGNORE_EMPTY_FIELD_SET 1

................................................................................
|   Macros
|
\---------------------------------------------------------------------------*/
#define TMALLOC(t) (t*)MALLOC(sizeof(t))

#define SetResult(str) Tcl_ResetResult(interp); \
                     Tcl_SetStringObj(Tcl_GetObjResult(interp), (str), -1)




#define SetResult3(str1,str2,str3) Tcl_ResetResult(interp);     \
                     Tcl_AppendResult(interp, (str1), (str2), (str3), NULL)




#define SetIntResult(i) Tcl_ResetResult(interp);                        \
                     Tcl_SetIntObj(Tcl_GetObjResult(interp), (i))
#define SetBooleanResult(i) Tcl_ResetResult(interp); \
                     Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (i))

#define SPACE(c) ((c) == ' ' || (c) == '\n' || (c) == '\t' || (c) == '\r')
    
................................................................................
#define hasMatched(quant,hm) \
    (hm) == 0 ?  mayMiss(quant) :  1

#define mustMatch(quant,hm) \
    (hm) == 0 ? minOne(quant) : 0


#define getContext(cp, ac, hm)        \
    cp = se->pattern;                 \
    ac = se->activeChild;             \
    hm = se->hasMatched;






#define updateStack(se,cp,ac)                     \
    if (maxOne (cp->quants[ac])) {                \
        se->activeChild = ac + 1;                 \
        se->hasMatched = 0;                       \
    } else {                                      \
        se->activeChild = ac;                     \
        se->hasMatched = 1;                       \
    }

#define S(str)  str, sizeof (str) -1

static SchemaCP*
initSchemaCP (
    Schema_CP_Type type,
    void *namespace,
    char *name
    )
................................................................................
    for (i = 0; i < sdata->numPatternList; i++) {
        freeSchemaCP (sdata->patternList[i]);
    }
    FREE (sdata->patternList);
    FREE (sdata->quants);
    while (sdata->stack) {
        down = sdata->stack->down;


        FREE (sdata->stack);
        sdata->stack = down;
    }







    while (sdata->stackPool) {
        down = sdata->stackPool->down;


        FREE (sdata->stackPool);
        sdata->stackPool = down;
    }
    Tcl_DecrRefCount (sdata->evalStub[0]);
    Tcl_DecrRefCount (sdata->evalStub[1]);
    Tcl_DecrRefCount (sdata->evalStub[2]);
    FREE (sdata->evalStub);
................................................................................
        stackElm = TMALLOC (SchemaValidationStack);
    }
    memset (stackElm, 0, sizeof (SchemaValidationStack));
    stackElm->pattern = pattern;
    return stackElm;
}

/* The ac argument is the currend looked at child of the stack top
 * (which is not pattern, in case the looked ad child of the stack top
 * is SCHEMA_CTYPE_CHOICE). */











static void
pushToStack (
    SchemaData *sdata,
    SchemaCP *pattern,
    int ac
    )
{
    SchemaValidationStack *stackElm, *se;

    DBG(fprintf(stderr, "push to Stack:\n");serializeCP(pattern));
    if (sdata->stackPool) {
        stackElm = sdata->stackPool;
        sdata->stackPool = stackElm->down;
    } else {
        stackElm = TMALLOC (SchemaValidationStack);

    }
    memset (stackElm, 0, sizeof (SchemaValidationStack));
    se = sdata->stack;
    if (se) se->activeChild = ac;


    stackElm->down = se;
    stackElm->pattern = pattern;
    if (pattern->type == SCHEMA_CTYPE_INTERLEAVE) {
        stackElm->interleaveState = MALLOC (sizeof (int) * pattern->nc);
        memset (stackElm->interleaveState, 0, sizeof (int) * pattern->nc);
    }
    sdata->stack = stackElm;
}

static void
repoolStackElement (
    SchemaData *sdata,
    SchemaValidationStack *se
    ) 
{
    se->down = sdata->stackPool;
    sdata->stackPool = se;



}

static void
popStack (
    SchemaData *sdata
    )
{
    SchemaValidationStack *se;
    DBG(fprintf(stderr, "pop from Stack:\n");serializeCP(sdata->stack->pattern));
    if (sdata->stack->interleaveState) {
        FREE (sdata->stack->interleaveState);
        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
    )
................................................................................
    popStack (sdata);
    /* cp is still the pattern from stack top before the popStack */
    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, savedac;


    if (!sdata->reportCmd) return 0;
    cmdPtr = Tcl_DuplicateObj (sdata->reportCmd);
    Tcl_IncrRefCount(cmdPtr);
    Tcl_ListObjAppendElement (interp, cmdPtr,
                              sdata->self);
    Tcl_ListObjAppendElement (
        interp, cmdPtr,
        Tcl_NewStringObj (errType, len)
        );

    sdata->currentEvals++;


















    if (sdata->stack) {
        savedac = sdata->stack->activeChild;














        sdata->stack->activeChild = ac;


    }














    rc = Tcl_EvalObjEx (interp, cmdPtr,
                        TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
    if (sdata->stack) {
        sdata->stack->activeChild = savedac;
    }
    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 (
................................................................................
static int
evalVirtual (
    Tcl_Interp *interp,
    SchemaData *sdata,
    int ac
    )
{

    SchemaCP *cp;
    int savedac, savedhm, rc;

    cp = sdata->stack->pattern->content[ac];
    savedac = sdata->stack->activeChild;
    savedhm = sdata->stack->hasMatched;
    sdata->stack->activeChild = ac;
    sdata->stack->hasMatched = 1;
    cp->content[cp->nc-1] = (SchemaCP *) sdata->self;
    sdata->currentEvals++;
    rc = Tcl_EvalObjv (interp, cp->nc, (Tcl_Obj **) cp->content,
                       TCL_EVAL_GLOBAL);
    sdata->currentEvals--;
    sdata->stack->activeChild = savedac;
    sdata->stack->hasMatched = savedhm;    
    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }
    return 1;
}

................................................................................
        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 (rc == -1) mayskip = 1;
                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("MISSING_ONE_OF_INTERLEAVE"), 1, ac)) {

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

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

    DBG(
        fprintf (stderr, "probeElement: look if '%s' in ns '%s' match\n",
                 name, (char *)namespace);
        );

    /* This is problematic. If feeded with continuously new namespaces */
    if (namespace) {
        h = Tcl_FindHashEntry (&sdata->namespace, namespace);
    } else {
        h = NULL;
    }
    if (h) {
        namespacePtr = Tcl_GetHashKey (&sdata->namespace, h);
................................................................................
        }
    }
    
    if (sdata->validationState == VALIDATION_READY) {
        /* The root of the tree to check. */
        if (sdata->start) {
            if (strcmp (name, sdata->start) != 0) {





                SetResult ("Root element doesn't match");
                return TCL_ERROR;
            }
            if (namespace) {
                if (!sdata->startNamespace||
                    strcmp (namespace, sdata->startNamespace) != 0) {





                    SetResult ("Root element namespace doesn't match");
                    return TCL_ERROR;
                }
            } else {
                if (sdata->startNamespace) {





                    SetResult ("Root element namespace doesn't match");
                    return TCL_ERROR;
                }
            }
        }
    }
    if (h) {
................................................................................
            }
            pattern = pattern->next;
        }
    } else {
        pattern = NULL;
    }

    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);
            if (se->pattern->namespace) {
                Tcl_AppendResult (interp, namespace, ":", NULL);
            }
            Tcl_AppendResult (interp, name, "\"", NULL);
            return TCL_ERROR;
        }
    } else {
        sdata->validationState = VALIDATION_STARTED;
        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, 0);
................................................................................
    cp = sdata->stack->pattern;
    *isrequiered = 0;
    for (i = 0; i < cp->numAttr; i++) {
        if (cp->attrs[i]->namespace == ns
            && cp->attrs[i]->name == name) {
            if (cp->attrs[i]->cp) {
                if (!checkText (interp, cp->attrs[i]->cp, value)) {
                    if (!recover (interp, sdata,
                                  S("WRONG_ATTRIBUTE_VALUE"), 0, 0)) {
                        SetResult3 ("Attribute value doesn't match for "
                                    "attribute '", name , "'");
                        return 0;
                    }
                }
            }
            if (cp->attrs[i]->required) *isrequiered = 1;
            return 1;
................................................................................
        } else {
            ns = NULL;
        }
        found = probeAttribute (interp, sdata, ln, ns, atPtr[1], &req);
        reqAttr += req;
    unknowncleanup:
        if (!found) {
            if (!recover (interp, sdata, S("UNKNOWN_ATTRIBUTE"), 0, 0)) {

                SetResult3 ("Unknown attribute \"", atPtr[0], "\"");
                return TCL_ERROR;
            }
        }
        if (nsatt) namespace[j] = '\xFF';
    }
    if (reqAttr != cp->numReqAttr) {
        /* Lookup the missing attribute(s) */

        SetResult ("Missing mandatory attribute(s):");

        for (i = 0; i < cp->numAttr; i++) {
            if (!cp->attrs[i]->required) continue;
            found = 0;
            for (atPtr = (char **) attr; atPtr[0] && atPtr[1]; atPtr += 2) {
                if (cp->attrs[i]->namespace) {
                    ln = atPtr[0];
                    j = 0;
................................................................................
                }
                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);
                    }
                }
................................................................................
        } else {
            ns = NULL;
        }
        found = probeAttribute (interp, sdata, ln, ns, atPtr->nodeValue, &req);
        reqAttr += req;
    unknown:
        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, "\"");

                }
                sdata->validationState = VALIDATION_ERROR;
                return TCL_ERROR;

            }
        }
    nextAttr:
        atPtr = atPtr->nextSibling;
    }
    if (reqAttr != cp->numReqAttr) {
        /* Lookup the missing attribute(s) */

        SetResult ("Missing mandatory attribute(s):");

        for (i = 0; i < cp->numAttr; i++) {
            if (!cp->attrs[i]->required) continue;
            found = 0;
            atPtr = attr;
            while (atPtr) {
                if (cp->attrs[i]->namespace) {
                    if (!atPtr->namespace) goto nextAttr2;
................................................................................
                    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);

                    }
                }
            }
        }
        if (!sdata->reportCmd) {
            sdata->validationState = VALIDATION_ERROR;
            return TCL_ERROR;
................................................................................
            ns = Tcl_GetHashKey (&sdata->namespace, h);
        }
        found = probeAttribute (interp, sdata, name, ns,
                                Tcl_GetString (attvalue), &req);
        reqAttr += req;
    unknown:
        if (!found) {
            if (!recover (interp, sdata, S("UNKNOWN_ATTRIBUTE"), 0, 0)) {


                if (ns) {
                    SetResult ("Unknown attribute \"");
                    Tcl_AppendResult (interp, ns, ":", name,
                                      "\"");
                } else {
                    SetResult3 ("Unknown attribute \"", name, "\"");
                }
................................................................................
            }
            
            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, ac)) break;
                else return 0;
................................................................................
                pushToStack (sdata, cp->content[ac], 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;
    }
................................................................................
    ) 
{
    Tcl_HashEntry *h, *h1;
    Tcl_HashSearch search, search1;
    int haveErrMsg = 0;
    SchemaDocKey *dk;

    /* TODO: add recovering */
    if (sdata->unknownIDrefs) {

        haveErrMsg = 1;
        SetResult ("References to unknown IDs:");
        for (h = Tcl_FirstHashEntry (&sdata->ids, &search);
             h != NULL;
             h = Tcl_NextHashEntry (&search)) {
            if (Tcl_GetHashValue (h) == 0) {
                Tcl_AppendResult (interp, " '",
                                  Tcl_GetHashKey (&sdata->ids, h),
                                  "'", NULL);

            }
        }
    }
    if (sdata->idTables.numEntries) {
        for (h = Tcl_FirstHashEntry (&sdata->idTables, &search);
             h != NULL;
             h = Tcl_NextHashEntry (&search)) {
            dk = Tcl_GetHashValue (h);
            if (dk->unknownIDrefs) {


                if (haveErrMsg) {
                    Tcl_AppendResult (interp, "\n", NULL);
                } else {
                    haveErrMsg = 1;
                }
                Tcl_AppendResult (interp,
                                  "References to unknown IDs in ID space '",
                                  Tcl_GetHashKey (&sdata->idTables, h),
                                  "':", NULL);
                for (h1 = Tcl_FirstHashEntry (&dk->ids, &search1);
                     h1 != NULL;
                     h1 = Tcl_NextHashEntry (&search1)) {
                    if (Tcl_GetHashValue (h1) == 0) {
                        Tcl_AppendResult (interp, " '",
                                          Tcl_GetHashKey (&dk->ids, h1),
                                          "'", NULL);

                    }
                }
            }
        }
    }
    if (haveErrMsg) {
        sdata->validationState = VALIDATION_ERROR;
................................................................................
        popStack (sdata);
        if (sdata->stack == NULL) {
            /* End of the first pattern (the tree root) without error. */
            /* Check for unknown ID references */
            if (!checkDocKeys (interp, sdata)) {
                return TCL_ERROR;
            }
            /*  We have successfully ended validation */
            sdata->validationState = VALIDATION_FINISHED;
        }
        DBG(
            fprintf(stderr, "probeElementEnd: _CAN_ end here.\n");
            serializeStack (sdata);
            );
        return TCL_OK;
    }
    if (!sdata->evalError) {
        SetResult ("Missing mandatory content");
    }
    sdata->validationState = VALIDATION_ERROR;
    DBG(
        fprintf(stderr, "probeElementEnd: CAN'T end here.\n");
        serializeStack (sdata);
        );
    return TCL_ERROR;
}
................................................................................
                candidate = cp->content[ac];
                switch (candidate->type) {
                case SCHEMA_CTYPE_TEXT:
                    if (checkText (interp, candidate, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    if (!sdata->evalError) {
                        SetResult ("Invalid text content");
                    }

                    updateStack (se, cp, ac);





                    return 0;

                case SCHEMA_CTYPE_CHOICE:
                    if (candidate->flags & MIXED_CONTENT) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
................................................................................
                                updateStack (se, cp, ac);
                                return 1;
                            }
                            popStack (sdata);
                            break;

                        case SCHEMA_CTYPE_VIRTUAL:
                            Tcl_Panic ("Virtual constrain in MIXED or CHOICE");

                            
                        case SCHEMA_CTYPE_CHOICE:
                            Tcl_Panic ("MIXED or CHOICE child of MIXED or CHOICE");


                        case SCHEMA_CTYPE_KEYSPACE_END:
                        case SCHEMA_CTYPE_KEYSPACE:
                            Tcl_Panic ("Keyspace constrain in MIXED or CHOICE");

                            
                        }
                    }
                    if (mustMatch (cp->quants[ac], hm)) {




                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_INTERLEAVE:
                case SCHEMA_CTYPE_PATTERN:
                    pushToStack (sdata, candidate, ac);
                    if (matchText (interp, sdata, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    popStack (sdata);
                    if (mustMatch (cp->quants[ac], hm)) {




                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_VIRTUAL:
                    if (!evalVirtual (interp, sdata, ac)) return 0;
                    break;
................................................................................
                    }
                    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;
                    
                case SCHEMA_CTYPE_NAME:
                case SCHEMA_CTYPE_ANY:
                    if (mustMatch (cp->quants[ac], hm)) {




                        SetResult ("Unexpected text content");
                        return 0;
                    }
                    break;

                }
                ac++;
            }
            if (isName) {




                SetResult ("Unexpected text content");
                return 0;
            }
            popStack (sdata);
            continue;

        case SCHEMA_CTYPE_KEYSPACE:
        case SCHEMA_CTYPE_KEYSPACE_END:
................................................................................
            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;
}

static void
................................................................................

    if (probeElement (vdata->interp, sdata, s, namespace)
        != TCL_OK) {
        sdata->validationState = VALIDATION_ERROR;
        XML_StopParser (vdata->parser, 0);
        return;
    }
    if (atts[0] || (sdata->stack
                    && sdata->stack->pattern->attrs)) {
        if (probeAttributes (vdata->interp, sdata, atts)
            != TCL_OK) {
            sdata->validationState = VALIDATION_ERROR;
            XML_StopParser (vdata->parser, 0);
        }
    }
}
................................................................................
    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) {
                        if (kc->flags & DKC_FLAG_IGNORE_EMPTY_FIELD_SET) {
                            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;
                    else Tcl_DStringAppend (&dStr, ":", 1);
                    if (frs.nodes[0]->nodeType == ATTRIBUTE_NODE) {
                        attr = (domAttrNode *) frs.nodes[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;
                }
            }
        }
................................................................................
    if (probeElement (interp, sdata, ln,
                      node->namespace ?
                      node->ownerDocument->namespaces[node->namespace-1]->uri
                      : NULL)
        != TCL_OK) {
        return TCL_ERROR;
    }

    if (node->firstAttr) {
        if (probeDomAttributes (interp, sdata, node->firstAttr) != TCL_OK) {
            return TCL_ERROR;
        }
    } else {
        if (sdata->stack->pattern->numReqAttr) {
            /* probeDomAttributes fills interp result with a msg which
             * required attributes are missing. */
            probeDomAttributes (interp, sdata, NULL);
            return TCL_ERROR;

        }
    }

    if (sdata->stack->pattern->domKeys) {
        if (checkdomKeyConstraints (interp, sdata, node) != TCL_OK)
            return TCL_ERROR;
    }
................................................................................
{
    Tcl_HashEntry *h;
    Tcl_HashSearch search;
    SchemaDocKey *dk;
    SchemaKeySpace *ks;
    
    while (sdata->stack) popStack (sdata);

    sdata->validationState = VALIDATION_READY;
    sdata->skipDeep = 0;
    sdata->evalError = 0;

    Tcl_DStringSetLength (sdata->cdata, 0);
    if (sdata->ids.numEntries) {
        Tcl_DeleteHashTable (&sdata->ids);
        Tcl_InitHashTable (&sdata->ids, TCL_STRING_KEYS);
        sdata->unknownIDrefs = 0;
    }
    if (sdata->idTables.numEntries) {
................................................................................
    Tcl_Obj *rObj;

    rObj = Tcl_NewObj();
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewStringObj ("#text", 5));
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewObj());
    return rObj;
}















static void
definedElements (
    SchemaData *sdata,
    Tcl_Interp *interp
    )
{
................................................................................
            || cp->flags & PLACEHOLDER_PATTERN_DEF) continue;
        elmObj = serializeElementName (interp, cp);
        Tcl_ListObjAppendElement (interp, rObj, elmObj);
    }
}

static int
getNextExpected (
    SchemaData *sdata,
    SchemaValidationStack *se,
    Tcl_Interp *interp,
    Tcl_HashTable *seenCPs,
    Tcl_Obj *rObj
    )
{
    int ac, hm, i, hnew, mustMatch, mayskip, rc;
    SchemaCP *cp, *ic, *jc;
    SchemaValidationStack *se1;

    getContext (cp, ac, hm);
    if (hm && maxOne(cp->quants[ac])) ac++;
    switch (cp->type) {
    case SCHEMA_CTYPE_INTERLEAVE:
................................................................................
                                          serializeElementName (interp, ic));
                break;
            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                Tcl_CreateHashEntry (seenCPs, ic, &hnew);
                if (hnew) {
                    se1 = getStackElement (sdata, ic);
                    mayskip = getNextExpected (sdata, se1, interp, seenCPs,
                                               rObj);
                    repoolStackElement (sdata, se1);
                }
                break;

            case SCHEMA_CTYPE_ANY:
                Tcl_ListObjAppendElement (interp, rObj,
                                          serializeAnyCP (interp, ic));
................................................................................
                            );
                        break;
                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        Tcl_CreateHashEntry (seenCPs, jc, &hnew);
                        if (hnew) {
                            se1 = getStackElement (sdata, ic);
                            mayskip = getNextExpected (sdata, se1, interp,
                                                       seenCPs, rObj);

                            repoolStackElement (sdata, se1);
                        }
                        break;
                    case SCHEMA_CTYPE_ANY:
                        Tcl_ListObjAppendElement (
                            interp, rObj, serializeAnyCP (interp, jc)
                            );
................................................................................
                    }
                }
                break;

            case SCHEMA_CTYPE_VIRTUAL:
            case SCHEMA_CTYPE_KEYSPACE:
            case SCHEMA_CTYPE_KEYSPACE_END:

                break;
            }
            if (cp->type == SCHEMA_CTYPE_INTERLEAVE && minOne(cp->quants[ac]))
                mustMatch = 1;

            else if (!mayskip && minOne (cp->quants[ac])) break;

            ac++;





        }
        break;
        
    case SCHEMA_CTYPE_ANY:
    case SCHEMA_CTYPE_CHOICE:
    case SCHEMA_CTYPE_TEXT:
    case SCHEMA_CTYPE_VIRTUAL:
................................................................................
    }
    if (cp->type == SCHEMA_CTYPE_NAME) {
        return 0;
    }
    if (cp->type == SCHEMA_CTYPE_INTERLEAVE && mustMatch) {
        return 0;
    }






















































    if (se->down) {

        hm = se->down->hasMatched;











        se->down->hasMatched = 1;
        rc = getNextExpected (sdata, se->down, interp, seenCPs, rObj);




        se->down->hasMatched = hm;
        return rc;
    }
    return 1;
}






static int
schemaInstanceInfoCmd (
    SchemaData *sdata,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    int methodIndex, len, i, hnew;
    Tcl_HashEntry *h;
    SchemaCP *cp;
    SchemaValidationStack *se;
    void *ns;
    Tcl_Obj *rObj, *r2Obj, *thisObj;
    Tcl_HashTable localHash;
    Tcl_HashSearch search;
    
    static const char *schemaInstanceInfoMethods[] = {
        "validationstate", "vstate", "definedElements", "stack", "toplevel",
        "pastexpected", "nextexpected", "definition", NULL
    };
    enum schemaInstanceInfoMethod {
        m_validationstate, m_vstate, m_definedElements, m_stack, m_toplevel,
        m_pastexpected, m_nextexpected, m_definition

    };

    static const char *schemaInstanceInfoStackMethods[] = {
        "top", "inside", NULL
    };
    enum schemaInstanceInfoStackMethod {
        m_top, m_inside
    };







    
    if (objc < 2) {
        Tcl_WrongNumArgs (interp, 1, objv, "subcommand ?arguments?");
        return TCL_ERROR;
    }

    if (Tcl_GetIndexFromObj (interp, objv[1], schemaInstanceInfoMethods,
................................................................................
            Tcl_WrongNumArgs (interp, 1, objv, "definedElements");
            return TCL_ERROR;
        }
        definedElements (sdata, interp);
        break;

    case m_stack:




        if (Tcl_GetIndexFromObj (interp, objv[2],
                                 schemaInstanceInfoStackMethods,
                                 "method", 0, &methodIndex)
            != TCL_OK) {
            return TCL_ERROR;
        }
        switch ((enum schemaInstanceInfoStackMethod) methodIndex) {
        case m_top:
            break;















        case m_inside:
            if (!sdata->stack) {
                Tcl_ResetResult (interp);
                return TCL_OK;
            }
            se = sdata->stack;
            while (se->pattern->type != SCHEMA_CTYPE_NAME) {
                se = se->down;
            }
            rObj = serializeElementName (interp, se->pattern);
            Tcl_SetObjResult (interp, rObj);
            return TCL_OK;
            break;
        }
        
    case m_toplevel:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 1, objv, "no argument expected");
            return TCL_ERROR;
        }
        if (!sdata->currentEvals) {
            SetResult ("not called while schema evaluation");
            return TCL_ERROR;
        }
        if (!sdata->defineToplevel && sdata->currentEvals > 1) {
            SetBooleanResult (0);
        } else {
            SetBooleanResult (1);
        }
        return TCL_OK;

    case m_nextexpected:
        if (sdata->validationState == VALIDATION_ERROR) {
            SetResult ("Validation command in error state.");
            return TCL_ERROR;


        } else if (sdata->validationState == VALIDATION_FINISHED) {
            return TCL_OK;
        }
        if (!sdata->stack) {
            if (sdata->start) {
                Tcl_AppendElement (interp, sdata->start);
                if (sdata->startNamespace) {
                    Tcl_AppendElement (interp, sdata->startNamespace);
                }
            } else {
                definedElements (sdata, interp);
            }
        } else {
            rObj = Tcl_NewObj();
            Tcl_InitHashTable (&localHash, TCL_ONE_WORD_KEYS);
            getNextExpected (sdata, sdata->stack, interp, &localHash, rObj);
            Tcl_DeleteHashTable (&localHash);
            Tcl_ListObjLength (interp, rObj, &len);
            if (len <= 1) {
                Tcl_SetObjResult (interp, rObj);
            } else {
                Tcl_InitHashTable (&localHash, TCL_STRING_KEYS);
                for (i = 0; i < len; i++) {
                    Tcl_ListObjIndex (interp, rObj, i, &thisObj);
                    h = Tcl_CreateHashEntry (
                        &localHash, Tcl_GetString (thisObj), &hnew
                        );
                    if (hnew) {
                        Tcl_SetHashValue (h, thisObj);
                    }
                }
                r2Obj = Tcl_NewObj();
                len = 0;
                for (h = Tcl_FirstHashEntry (&localHash, &search);
                     h != NULL;
                     h = Tcl_NextHashEntry (&search)) {
                    Tcl_ListObjAppendElement (interp, r2Obj,
                                              Tcl_GetHashValue (h));
                    len++;
                }
                Tcl_DeleteHashTable (&localHash);
                Tcl_DecrRefCount (rObj);
                Tcl_SetObjResult (interp, r2Obj);
            }
        }
        return TCL_OK;
        
    case m_pastexpected:
        break;
        
    case m_definition:
        if (objc < 3 && objc > 4) {
            Tcl_WrongNumArgs (interp, 1, objv, "name ?namespace?");
            return TCL_ERROR;
        }
................................................................................
        if (cp->namespace) {
            Tcl_AppendElement (interp, cp->namespace);
        }
        if (cp->defScript) {
            Tcl_AppendElement (interp, Tcl_GetString (cp->defScript));
        }
        break;





























    }
    return TCL_OK;
}

int
schemaInstanceCmd (
    ClientData clientData,
................................................................................
                    } else {
                        attData = objv[4];
                    }
                }
            }
            result = probeElement (interp, sdata, Tcl_GetString (objv[3]),
                                   namespacePtr);
            if (result == TCL_OK) {
                result = probeEventAttribute (interp, sdata, attData, len);
            }
            break;
            
        case k_elementend:
            if (objc != 3) {
                Tcl_WrongNumArgs (interp, 3, objv, "No arguments expected.");
................................................................................
                Tcl_SetVar (interp, Tcl_GetString (objv[3]), "", 0);
            }
        } else {
            if (objc == 4) {
                Tcl_SetVar (interp, Tcl_GetString (objv[3]),
                            Tcl_GetStringResult (interp), 0);
            }



            SetBooleanResult (0);

        }
        schemaReset (sdata);
        break;

    case m_domvalidate:
        CHECK_EVAL
        if (objc < 3 || objc > 4) {
................................................................................
    }
    for (i = 0; i < nrKeyspaces; i++) {
        Tcl_ListObjIndex (interp, objv[1], i, &ksObj);
        h = Tcl_CreateHashEntry (&sdata->keySpaces,
                                 Tcl_GetString (ksObj), &hnew);
        if (hnew) {
            ks = TMALLOC (SchemaKeySpace);

            ks->active = 0;
            ks->unknownIDrefs = 0;
            Tcl_SetHashValue (h, ks);
        } else {
            ks = Tcl_GetHashValue (h);
        }
        pattern = initSchemaCP (SCHEMA_CTYPE_KEYSPACE,







|








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







 







>
>
>
>


>
>
>
>







 







|
|
|
|
>
>
>
>



<
<
<
<
|
|
<
<
<







 







>
>



>
>
>
>
>
>
>


>
>







 







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







|


|
|
|
|
|
>
|
<
|
<
>
>
|
<

|
|

|



|

|
|

|
|
>
>
>







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







 







<




>
|
<
|
>




|
>

|






|

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


<
<
<

>
>
>
>





|
>


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







 







>

<


<
<
<
<





<
<







 







|
|
|
>







 







|
|
>







 







|
>










|
>







 







|
>







 







<







 







>
>
>
>
>






>
>
>
>
>





>
>
>
>
>







 







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


|
>







 







|
|
|







 







|
>
|







>
|
>







 







|
>
>







 







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







>
|
>







 







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







 







|
>
>







 







|
>
>







 







|
>







 







|
>







 







<
<
<
<

|
>







 







|
>







 







|

>
|
|
|
|
|
|
|
|
|
>









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







 







|








<
|
<







 







|
|
<
>
|
>
>
>
>
>







 







|
>


|
>



|
>




>
>
>
>
|













>
>
>
>
|







 







|
>
>











>
>
>
>
|








>
>
>
>
|







 







<
<
<







 







|
|







 







|
<
<




<
<
<






<
<
<






|
<

<
<
<
|


|
<
<
<
<









|
>








|
>







<
<
<







|
>










|
>







 







<
<
<
<
|




<
<
<
<












|
>








<
<
<
<







 







|
>







 







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







 







>



>







 







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







 







|







|







 







|
|







 







|
|
>







 







>


|
|
>
|
>

>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
|
<
|
<
|
>
>
>
>
|
>








|




|
<
<



|



|
>








>
>
>
>
>
>
>







 







>
>
>
>







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











<




|













|
|
|

>
>
|












<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







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







 







|







 







>
>
>
|
>







 







>







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
...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
...
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
...
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
...
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
...
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
....
1096
1097
1098
1099
1100
1101
1102
1103
1104

1105
1106




1107
1108
1109
1110
1111


1112
1113
1114
1115
1116
1117
1118
....
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
....
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
....
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
....
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
....
1428
1429
1430
1431
1432
1433
1434

1435
1436
1437
1438
1439
1440
1441
....
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
....
1508
1509
1510
1511
1512
1513
1514
1515




















1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
....
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
....
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
....
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
....
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
....
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
....
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
....
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
....
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
....
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
....
2006
2007
2008
2009
2010
2011
2012




2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
....
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
....
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
....
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166

2167

2168
2169
2170
2171
2172
2173
2174
....
2198
2199
2200
2201
2202
2203
2204
2205
2206

2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
....
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
....
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
....
2438
2439
2440
2441
2442
2443
2444



2445
2446
2447
2448
2449
2450
2451
....
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
....
2665
2666
2667
2668
2669
2670
2671
2672


2673
2674
2675
2676



2677
2678
2679
2680
2681
2682



2683
2684
2685
2686
2687
2688
2689

2690



2691
2692
2693
2694




2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722



2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
....
2752
2753
2754
2755
2756
2757
2758




2759
2760
2761
2762
2763




2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785




2786
2787
2788
2789
2790
2791
2792
....
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
....
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
....
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
....
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
....
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
....
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
....
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
....
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
....
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319

3320
3321
3322
3323
3324

3325

3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346


3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
....
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451

3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488


3489













3490


















3491
3492
3493
3494
3495
3496
3497
....
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
....
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
....
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
....
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
/*----------------------------------------------------------------------------
|   Local typedefs
|
\---------------------------------------------------------------------------*/

typedef struct
{
    SchemaData    *sdata;
    Tcl_Interp    *interp;
    XML_Parser     parser;
    Tcl_DString   *cdata;
    int            onlyWhiteSpace;
    char          *uri;
    int            maxUriLen;
} ValidateMethodData;

typedef enum {
    MATCH_GLOBAL,
    MATCH_ELEMENT_START,
    MATCH_ELEMENT_END,
    MATCH_TEXT,
    MATCH_ATTRIBUTE_TEXT,
    MATCH_DOM_KEYCONSTRAINT
} ValidationAction;

static char *ValidationAction2str[] = {
    "MATCH_GLOBAL",
    "MATCH_ELEMENT_START",
    "MATCH_ELEMENT_END",
    "MATCH_TEXT",
    "MATCH_ATTRIBUTE_TEXT",
    "MATCH_DOM_KEYCONSTRAINT"
};
    
typedef enum {
    DOM_KEYCONSTRAINT,
    MISSING_ATTRIBUTE,
    MISSING_ELEMENT_MATCH_START,
    MISSING_ELEMENT_MATCH_END,
    MISSING_ELEMENT_MATCH_TEXT,
    MISSING_TEXT_MATCH_START,
    MISSING_TEXT_MATCH_END,
    UNEXPECTED_ROOT_ELEMENT,
    UNEXPECTED_ELEMENT,
    UNKNOWN_ATTRIBUTE,
    INVALID_KEYREF_MATCH_START,
    INVALID_KEYREF_MATCH_END,
    INVALID_KEYREF_MATCH_TEXT,
    UNKNOWN_ROOT_ELEMENT,
    UNKOWN_GLOBAL_ID,
    UNKOWN_ID,
    INVALID_ATTRIBUTE_VALUE,
    INVALID_VALUE
} ValidationErrorType;

static char *ValidationErrorType2str[] = {
    "DOM_KEYCONSTRAINT",
    "MISSING_ATTRIBUTE",
    "MISSING_ELEMENT",
    "MISSING_ELEMENT",
    "MISSING_ELEMENT",
    "MISSING_TEXT",
    "MISSING_TEXT",
    "UNEXPECTED_ROOT_ELEMENT",
    "UNEXPECTED_ELEMENT",
    "UNKNOWN_ATTRIBUTE",
    "INVALID_KEYREF",
    "INVALID_KEYREF",
    "INVALID_KEYREF",
    "UNKNOWN_ROOT_ELEMENT",
    "UNKOWN_GLOBAL_ID",
    "UNKOWN_ID",
    "INVALID_ATTRIBUTE_VALUE",
    "INVALID_VALUE"
};
    
/*----------------------------------------------------------------------------
|   domKeyConstraint related flage
|
\---------------------------------------------------------------------------*/

#define DKC_FLAG_IGNORE_EMPTY_FIELD_SET 1

................................................................................
|   Macros
|
\---------------------------------------------------------------------------*/
#define TMALLOC(t) (t*)MALLOC(sizeof(t))

#define SetResult(str) Tcl_ResetResult(interp); \
                     Tcl_SetStringObj(Tcl_GetObjResult(interp), (str), -1)
#define SetResultV(str) if (!sdata->evalError) { \
        Tcl_ResetResult(interp);                                        \
        Tcl_SetStringObj(Tcl_GetObjResult(interp), (str), -1);          \
    }
#define SetResult3(str1,str2,str3) Tcl_ResetResult(interp);     \
                     Tcl_AppendResult(interp, (str1), (str2), (str3), NULL)
#define SetResult3V(str1,str2,str3) if (!sdata->evalError) {            \
        Tcl_ResetResult(interp);                                        \
        Tcl_AppendResult(interp, (str1), (str2), (str3), NULL);         \
    }
#define SetIntResult(i) Tcl_ResetResult(interp);                        \
                     Tcl_SetIntObj(Tcl_GetObjResult(interp), (i))
#define SetBooleanResult(i) Tcl_ResetResult(interp); \
                     Tcl_SetBooleanObj(Tcl_GetObjResult(interp), (i))

#define SPACE(c) ((c) == ' ' || (c) == '\n' || (c) == '\t' || (c) == '\r')
    
................................................................................
#define hasMatched(quant,hm) \
    (hm) == 0 ?  mayMiss(quant) :  1

#define mustMatch(quant,hm) \
    (hm) == 0 ? minOne(quant) : 0


#define getContext(cp, ac, hm)                    \
    cp = se->pattern;                             \
    ac = se->activeChild;                         \
    hm = se->hasMatched;                          \
    if (hm && maxOne (cp->quants[ac])) {          \
        ac += + 1;                                \
        hm = 0;                                   \
    }                                             \


#define updateStack(se,cp,ac)                     \




    se->activeChild = ac;                         \
    se->hasMatched = 1;                           \




static SchemaCP*
initSchemaCP (
    Schema_CP_Type type,
    void *namespace,
    char *name
    )
................................................................................
    for (i = 0; i < sdata->numPatternList; i++) {
        freeSchemaCP (sdata->patternList[i]);
    }
    FREE (sdata->patternList);
    FREE (sdata->quants);
    while (sdata->stack) {
        down = sdata->stack->down;
        if (sdata->stack->interleaveState)
            FREE (sdata->stack->interleaveState);
        FREE (sdata->stack);
        sdata->stack = down;
    }
    while (sdata->lastMatchse) {
        down = sdata->lastMatchse->down;
        if (sdata->lastMatchse->interleaveState)
            FREE (sdata->lastMatchse->interleaveState);
        FREE (sdata->lastMatchse);
        sdata->lastMatchse = down;
    }
    while (sdata->stackPool) {
        down = sdata->stackPool->down;
        /* interleaveState always got cleand up at puting se back to
         * pool */
        FREE (sdata->stackPool);
        sdata->stackPool = down;
    }
    Tcl_DecrRefCount (sdata->evalStub[0]);
    Tcl_DecrRefCount (sdata->evalStub[1]);
    Tcl_DecrRefCount (sdata->evalStub[2]);
    FREE (sdata->evalStub);
................................................................................
        stackElm = TMALLOC (SchemaValidationStack);
    }
    memset (stackElm, 0, sizeof (SchemaValidationStack));
    stackElm->pattern = pattern;
    return stackElm;
}

static void
repoolStackElement (
    SchemaData *sdata,
    SchemaValidationStack *se
    ) 
{
    if (se->interleaveState) {
        FREE (se->interleaveState);
        se->interleaveState = NULL;
    }
    se->down = sdata->stackPool;
    sdata->stackPool = se;
}

static void
pushToStack (
    SchemaData *sdata,
    SchemaCP *pattern,
    int ac
    )
{
    SchemaValidationStack *se, *nextse;

    DBG(fprintf(stderr, "push to Stack:\n");serializeCP(pattern));
    if (pattern->type == SCHEMA_CTYPE_NAME && sdata->lastMatchse) {
        se = sdata->lastMatchse;
        while (se) {
            nextse = se->down;
            repoolStackElement (sdata, se);
            se = nextse;
        }

        sdata->lastMatchse = NULL;

    }
    se = getStackElement (sdata, pattern);
    se->down = sdata->stack;

    if (pattern->type == SCHEMA_CTYPE_INTERLEAVE) {
        se->interleaveState = MALLOC (sizeof (int) * pattern->nc);
        memset (se->interleaveState, 0, sizeof (int) * pattern->nc);
    }
    sdata->stack = se;
}

static void
popFromStack (
    SchemaData *sdata,
    SchemaValidationStack **stack
    )
{
    SchemaValidationStack *se;
    DBG(fprintf(stderr, "popFromStack:\n"); serializeCP((*stack)->pattern));
    se = (*stack)->down;
    repoolStackElement (sdata, *stack);
    *stack = se;
}

static void
popStack (
    SchemaData *sdata
    )
{
    SchemaValidationStack *se, *nextse;
    DBG(fprintf(stderr, "popStack:\n"); serializeCP(sdata->stack->pattern));
    if (sdata->stack->pattern->type == SCHEMA_CTYPE_NAME) {
        se = sdata->lastMatchse;
        while (se) {
            nextse = se->down;
            repoolStackElement (sdata, se);
            se = nextse;
        }
        sdata->lastMatchse = NULL;
        se = sdata->stack->down;
        repoolStackElement (sdata, sdata->stack);
        sdata->stack = se;
    } else {
        if (sdata->stack->activeChild > 0 || sdata->stack->hasMatched) {
            se = sdata->stack->down;
            sdata->stack->down = sdata->lastMatchse;
            sdata->lastMatchse = sdata->stack;
            sdata->stack = se;
        } else {
            se = sdata->stack->down;
            repoolStackElement (sdata, sdata->stack);
            sdata->stack = se;
        }
    }
}

static void
finalizeElement (
    SchemaData *sdata,
    int ac
    )
................................................................................
    popStack (sdata);
    /* cp is still the pattern from stack top before the popStack */
    if (cp->type != SCHEMA_CTYPE_NAME) {
        finalizeElement (sdata, sdata->stack->activeChild + 1);
    }
}


static int 
recover (
    Tcl_Interp *interp,
    SchemaData *sdata,
    ValidationErrorType errorType,
    const char *name,

    const char *ns,
    char *text,
    int ac
    )
{
    Tcl_Obj *cmdPtr;
    int rc;
    SchemaValidationStack *se;

    if (!sdata->reportCmd || sdata->evalError) return 0;
    cmdPtr = Tcl_DuplicateObj (sdata->reportCmd);
    Tcl_IncrRefCount(cmdPtr);
    Tcl_ListObjAppendElement (interp, cmdPtr,
                              sdata->self);
    Tcl_ListObjAppendElement (
        interp, cmdPtr,
        Tcl_NewStringObj (ValidationErrorType2str[errorType], -1)
        );
    sdata->vname = name;
    sdata->vns = ns;
    sdata->vtext = text;
    switch (errorType) {
    case DOM_KEYCONSTRAINT:
        sdata->vaction = MATCH_DOM_KEYCONSTRAINT;
        break;
    case MISSING_ATTRIBUTE:
    case UNKNOWN_ATTRIBUTE:
    case MISSING_ELEMENT_MATCH_START:
    case MISSING_TEXT_MATCH_START:
    case INVALID_KEYREF_MATCH_START:
    case UNEXPECTED_ROOT_ELEMENT:
    case UNKNOWN_ROOT_ELEMENT:
    case UNEXPECTED_ELEMENT:
        sdata->vaction = MATCH_ELEMENT_START;
        break;
    case MISSING_TEXT_MATCH_END:
    case INVALID_KEYREF_MATCH_END:
    case MISSING_ELEMENT_MATCH_END:
        if (sdata->stack) {
            se = sdata->stack;
            while (se->pattern->type != SCHEMA_CTYPE_NAME) {
                se = se->down;
            }
            sdata->vname = se->pattern->name;
            sdata->vns = se->pattern->namespace;
        }
        sdata->vaction = MATCH_ELEMENT_END;
        break;
    case MISSING_ELEMENT_MATCH_TEXT:
        sdata->vaction = MATCH_TEXT;
        break;
    case INVALID_KEYREF_MATCH_TEXT:
    case INVALID_VALUE:
        if (sdata->stack) {
            se = sdata->stack;
            while (se->pattern->type != SCHEMA_CTYPE_NAME) {
                se = se->down;
            }
            sdata->vname = se->pattern->name;
            sdata->vns = se->pattern->namespace;
        }
        sdata->vaction = MATCH_TEXT;
        break;
    case UNKOWN_GLOBAL_ID:
    case UNKOWN_ID:
        sdata->vaction = MATCH_TEXT;
        break;
    case INVALID_ATTRIBUTE_VALUE:
        sdata->vaction = MATCH_ATTRIBUTE_TEXT;
        break;
    }
    sdata->currentEvals++;
    rc = Tcl_EvalObjEx (interp, cmdPtr,
                        TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);



    sdata->currentEvals--;
    sdata->vaction = MATCH_GLOBAL;
    sdata->vname = NULL;
    sdata->vns = NULL;
    sdata->vtext = NULL;
    Tcl_DecrRefCount (cmdPtr);
    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }
    switch (errorType) {
    case MISSING_ELEMENT_MATCH_START:
        finalizeElement (sdata, ac+1);
        sdata->skipDeep = 2;
        break;
    case UNEXPECTED_ELEMENT:
        sdata->skipDeep = 1;
        break;        
    case DOM_KEYCONSTRAINT:
    case MISSING_ATTRIBUTE:
    case MISSING_ELEMENT_MATCH_END:
    case MISSING_ELEMENT_MATCH_TEXT:
    case MISSING_TEXT_MATCH_START:
    case MISSING_TEXT_MATCH_END:
    case UNEXPECTED_ROOT_ELEMENT:
    case UNKNOWN_ATTRIBUTE:
    case INVALID_KEYREF_MATCH_START:
    case INVALID_KEYREF_MATCH_END:
    case INVALID_KEYREF_MATCH_TEXT:
    case UNKNOWN_ROOT_ELEMENT:
    case UNKOWN_GLOBAL_ID:
    case UNKOWN_ID:
    case INVALID_ATTRIBUTE_VALUE:
    case INVALID_VALUE:
        break;
    }
    return 1;
}

/* The cp argument must be type SCHEMA_CTYPE_TEXT */
static int
checkText (
................................................................................
static int
evalVirtual (
    Tcl_Interp *interp,
    SchemaData *sdata,
    int ac
    )
{
    int rc;
    SchemaCP *cp;


    cp = sdata->stack->pattern->content[ac];




    cp->content[cp->nc-1] = (SchemaCP *) sdata->self;
    sdata->currentEvals++;
    rc = Tcl_EvalObjv (interp, cp->nc, (Tcl_Obj **) cp->content,
                       TCL_EVAL_GLOBAL);
    sdata->currentEvals--;


    if (rc != TCL_OK) {
        sdata->evalError = 1;
        return 0;
    }
    return 1;
}

................................................................................
        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, MISSING_TEXT_MATCH_START,
                                     name, namespace, NULL, ac)) {
                            mayskip = 1;
                            break;
                        }            
                        return 0;
                    }
                }
                break;

            case SCHEMA_CTYPE_ANY:
................................................................................
                if (rc == -1) mayskip = 1;
                break;

            case SCHEMA_CTYPE_KEYSPACE_END:
                candidate->keySpace->active--;
                if (!candidate->keySpace->active) {
                    if (candidate->keySpace->unknownIDrefs) {
                        if (!recover (interp, sdata,
                                      INVALID_KEYREF_MATCH_START, name,
                                      namespace, NULL, ac)) {
                            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, MISSING_ELEMENT_MATCH_START, name,
                             namespace, NULL, 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, UNEXPECTED_ELEMENT, name, namespace,
                         NULL, 0)) {
                /* 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, MISSING_ELEMENT_MATCH_START, name,
                     namespace, NULL, cp->nc)) {
            return 1;
        }
        return 0;
    }
    return -1;
}

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

    DBG(
        fprintf (stderr, "probeElement: look if '%s' in ns '%s' match\n",
                 name, (char *)namespace);
        );


    if (namespace) {
        h = Tcl_FindHashEntry (&sdata->namespace, namespace);
    } else {
        h = NULL;
    }
    if (h) {
        namespacePtr = Tcl_GetHashKey (&sdata->namespace, h);
................................................................................
        }
    }
    
    if (sdata->validationState == VALIDATION_READY) {
        /* The root of the tree to check. */
        if (sdata->start) {
            if (strcmp (name, sdata->start) != 0) {
                if (recover (interp, sdata, UNEXPECTED_ROOT_ELEMENT, name,
                             namespace, NULL, 0)) {
                    sdata->validationState = VALIDATION_FINISHED;
                    return TCL_OK;
                }
                SetResult ("Root element doesn't match");
                return TCL_ERROR;
            }
            if (namespace) {
                if (!sdata->startNamespace||
                    strcmp (namespace, sdata->startNamespace) != 0) {
                    if (recover (interp, sdata, UNEXPECTED_ROOT_ELEMENT, name,
                                 namespace, NULL, 0)) {
                        sdata->validationState = VALIDATION_FINISHED;
                        return TCL_OK;
                    }
                    SetResult ("Root element namespace doesn't match");
                    return TCL_ERROR;
                }
            } else {
                if (sdata->startNamespace) {
                    if (recover (interp, sdata, UNEXPECTED_ROOT_ELEMENT, name,
                                 namespace, NULL, 0)) {
                        sdata->validationState = VALIDATION_FINISHED;
                        return TCL_OK;
                    }
                    SetResult ("Root element namespace doesn't match");
                    return TCL_ERROR;
                }
            }
        }
    }
    if (h) {
................................................................................
            }
            pattern = pattern->next;
        }
    } else {
        pattern = NULL;
    }

    if (!sdata->stack) {




















        sdata->validationState = VALIDATION_STARTED;
        if (!pattern) {
            if (recover (interp, sdata, UNKNOWN_ROOT_ELEMENT, name, namespace,
                         NULL, 0)) {
                sdata->skipDeep = 1;
                return TCL_OK;
            }
            SetResult ("Unknown element");
            return TCL_ERROR;
        }
        pushToStack (sdata, pattern, 0);
................................................................................
    cp = sdata->stack->pattern;
    *isrequiered = 0;
    for (i = 0; i < cp->numAttr; i++) {
        if (cp->attrs[i]->namespace == ns
            && cp->attrs[i]->name == name) {
            if (cp->attrs[i]->cp) {
                if (!checkText (interp, cp->attrs[i]->cp, value)) {
                    if (!recover (interp, sdata, INVALID_ATTRIBUTE_VALUE, name,
                                  ns, value, i)) {
                        SetResult3V ("Attribute value doesn't match for "
                                    "attribute '", name , "'");
                        return 0;
                    }
                }
            }
            if (cp->attrs[i]->required) *isrequiered = 1;
            return 1;
................................................................................
        } else {
            ns = NULL;
        }
        found = probeAttribute (interp, sdata, ln, ns, atPtr[1], &req);
        reqAttr += req;
    unknowncleanup:
        if (!found) {
            if (!recover (interp, sdata, UNKNOWN_ATTRIBUTE, ln, namespace,
                          NULL, 0)) {
                SetResult3V ("Unknown attribute \"", atPtr[0], "\"");
                return TCL_ERROR;
            }
        }
        if (nsatt) namespace[j] = '\xFF';
    }
    if (reqAttr != cp->numReqAttr) {
        /* Lookup the missing attribute(s) */
        if (!sdata->evalError) {
            SetResult ("Missing mandatory attribute(s):");
        }
        for (i = 0; i < cp->numAttr; i++) {
            if (!cp->attrs[i]->required) continue;
            found = 0;
            for (atPtr = (char **) attr; atPtr[0] && atPtr[1]; atPtr += 2) {
                if (cp->attrs[i]->namespace) {
                    ln = atPtr[0];
                    j = 0;
................................................................................
                }
                if (strcmp (atPtr[0], cp->attrs[i]->name) == 0) {
                    found = 1;
                    break;
                }
            }
            if (!found) {
                if (!recover (interp, sdata, MISSING_ATTRIBUTE,
                              cp->attrs[i]->name, cp->attrs[i]->namespace,
                              NULL, i)) {
                    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);
                    }
                }
................................................................................
        } else {
            ns = NULL;
        }
        found = probeAttribute (interp, sdata, ln, ns, atPtr->nodeValue, &req);
        reqAttr += req;
    unknown:
        if (!found) {
            if (!recover (interp, sdata, UNKNOWN_ATTRIBUTE, ln, ns, NULL, 0)) {
                if (!sdata->evalError) {
                    if (ns) {
                        SetResult ("Unknown attribute \"");
                        Tcl_AppendResult (interp, ns, ":", atPtr->nodeName,
                                          "\"");
                    } else {
                        SetResult3 ("Unknown attribute \"", atPtr->nodeName,
                                    "\"");
                    }
                    sdata->validationState = VALIDATION_ERROR;
                    return TCL_ERROR;
                }
            }
        }
    nextAttr:
        atPtr = atPtr->nextSibling;
    }
    if (reqAttr != cp->numReqAttr) {
        /* Lookup the missing attribute(s) */
        if (!sdata->evalError) {
            SetResult ("Missing mandatory attribute(s):");
        }
        for (i = 0; i < cp->numAttr; i++) {
            if (!cp->attrs[i]->required) continue;
            found = 0;
            atPtr = attr;
            while (atPtr) {
                if (cp->attrs[i]->namespace) {
                    if (!atPtr->namespace) goto nextAttr2;
................................................................................
                    found = 1;
                    break;
                }
            nextAttr2:
                atPtr = atPtr->nextSibling;
            }
            if (!found) {
                if (!recover (interp, sdata, MISSING_ATTRIBUTE,
                              cp->attrs[i]->name, cp->attrs[i]->namespace,
                              NULL, i)) {
                    if (!sdata->evalError) {
                        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 (!sdata->reportCmd) {
            sdata->validationState = VALIDATION_ERROR;
            return TCL_ERROR;
................................................................................
            ns = Tcl_GetHashKey (&sdata->namespace, h);
        }
        found = probeAttribute (interp, sdata, name, ns,
                                Tcl_GetString (attvalue), &req);
        reqAttr += req;
    unknown:
        if (!found) {
            if (!recover (interp, sdata, UNKNOWN_ATTRIBUTE,
                          Tcl_GetString (attname), Tcl_GetString (attns),
                          NULL, 0)) {
                if (ns) {
                    SetResult ("Unknown attribute \"");
                    Tcl_AppendResult (interp, ns, ":", name,
                                      "\"");
                } else {
                    SetResult3 ("Unknown attribute \"", name, "\"");
                }
................................................................................
            }
            
            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, INVALID_KEYREF_MATCH_END,
                                      NULL, NULL,
                                      cp->content[ac]->keySpace->name, 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, MISSING_TEXT_MATCH_END,
                                     NULL, NULL, NULL, ac)) {
                            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, MISSING_ELEMENT_MATCH_END, NULL,
                              NULL, NULL, 0)) {
                    return 0;
                }
                break;
                
            case SCHEMA_CTYPE_VIRTUAL:
                if (evalVirtual (interp, sdata, ac)) break;
                else return 0;
................................................................................
                pushToStack (sdata, cp->content[ac], ac);
                rc = checkElementEnd (interp, sdata);
                popStack (sdata);
                if (rc) break;
                return 0;
                
            case SCHEMA_CTYPE_ANY:




            case SCHEMA_CTYPE_NAME:
                if (recover (interp, sdata, MISSING_ELEMENT_MATCH_END, NULL,
                             NULL, NULL, 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, MISSING_ELEMENT_MATCH_END, NULL,
                             NULL, NULL, 0)) {
                    break;
                }
                return 0;
            }
        }
        return -1;
    }
................................................................................
    ) 
{
    Tcl_HashEntry *h, *h1;
    Tcl_HashSearch search, search1;
    int haveErrMsg = 0;
    SchemaDocKey *dk;

    if (sdata->evalError) return 0;
    if (sdata->unknownIDrefs) {
        if (!recover (interp, sdata, UNKOWN_ID, NULL, NULL, NULL, 0)) {
            haveErrMsg = 1;
            SetResult ("References to unknown IDs:");
            for (h = Tcl_FirstHashEntry (&sdata->ids, &search);
                 h != NULL;
                 h = Tcl_NextHashEntry (&search)) {
                if (Tcl_GetHashValue (h) == 0) {
                    Tcl_AppendResult (interp, " '",
                                      Tcl_GetHashKey (&sdata->ids, h),
                                      "'", NULL);
                }
            }
        }
    }
    if (sdata->idTables.numEntries) {
        for (h = Tcl_FirstHashEntry (&sdata->idTables, &search);
             h != NULL;
             h = Tcl_NextHashEntry (&search)) {
            dk = Tcl_GetHashValue (h);
            if (dk->unknownIDrefs) {
                if (!recover (interp, sdata, UNKOWN_GLOBAL_ID, NULL, NULL,
                              NULL, 0)) {
                    if (haveErrMsg) {
                        Tcl_AppendResult (interp, "\n", NULL);
                    } else {
                        haveErrMsg = 1;
                    }
                    Tcl_AppendResult (interp,
                                      "References to unknown IDs in ID space '",
                                      Tcl_GetHashKey (&sdata->idTables, h),
                                      "':", NULL);
                    for (h1 = Tcl_FirstHashEntry (&dk->ids, &search1);
                         h1 != NULL;
                         h1 = Tcl_NextHashEntry (&search1)) {
                        if (Tcl_GetHashValue (h1) == 0) {
                            Tcl_AppendResult (interp, " '",
                                              Tcl_GetHashKey (&dk->ids, h1),
                                              "'", NULL);
                        }
                    }
                }
            }
        }
    }
    if (haveErrMsg) {
        sdata->validationState = VALIDATION_ERROR;
................................................................................
        popStack (sdata);
        if (sdata->stack == NULL) {
            /* End of the first pattern (the tree root) without error. */
            /* Check for unknown ID references */
            if (!checkDocKeys (interp, sdata)) {
                return TCL_ERROR;
            }
            /*  We have successfully finished validation */
            sdata->validationState = VALIDATION_FINISHED;
        }
        DBG(
            fprintf(stderr, "probeElementEnd: _CAN_ end here.\n");
            serializeStack (sdata);
            );
        return TCL_OK;
    }

    SetResultV ("Missing mandatory content");

    sdata->validationState = VALIDATION_ERROR;
    DBG(
        fprintf(stderr, "probeElementEnd: CAN'T end here.\n");
        serializeStack (sdata);
        );
    return TCL_ERROR;
}
................................................................................
                candidate = cp->content[ac];
                switch (candidate->type) {
                case SCHEMA_CTYPE_TEXT:
                    if (checkText (interp, candidate, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    if (sdata->evalError) return 0;
                    if (recover (interp, sdata, INVALID_VALUE, NULL, NULL,

                                 text, ac)) {
                        updateStack (se, cp, ac);
                        return 1;
                    } else {
                        return 0;
                    }
                    SetResult ("Invalid text content");
                    return 0;

                case SCHEMA_CTYPE_CHOICE:
                    if (candidate->flags & MIXED_CONTENT) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
................................................................................
                                updateStack (se, cp, ac);
                                return 1;
                            }
                            popStack (sdata);
                            break;

                        case SCHEMA_CTYPE_VIRTUAL:
                            Tcl_Panic ("Virtual constrain in MIXED or"
                                       " CHOICE");
                            
                        case SCHEMA_CTYPE_CHOICE:
                            Tcl_Panic ("MIXED or CHOICE child of MIXED or"
                                       " CHOICE");

                        case SCHEMA_CTYPE_KEYSPACE_END:
                        case SCHEMA_CTYPE_KEYSPACE:
                            Tcl_Panic ("Keyspace constrain in MIXED or"
                                       " CHOICE");
                            
                        }
                    }
                    if (mustMatch (cp->quants[ac], hm)) {
                        if (recover (interp, sdata, MISSING_ELEMENT_MATCH_TEXT,
                                     NULL, NULL, text, 0)) {
                            break;
                        }
                        SetResultV ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_INTERLEAVE:
                case SCHEMA_CTYPE_PATTERN:
                    pushToStack (sdata, candidate, ac);
                    if (matchText (interp, sdata, text)) {
                        updateStack (se, cp, ac);
                        return 1;
                    }
                    popStack (sdata);
                    if (mustMatch (cp->quants[ac], hm)) {
                        if (recover (interp, sdata, MISSING_ELEMENT_MATCH_TEXT,
                                     NULL, NULL, text, 0)) {
                            break;
                        }
                        SetResultV ("Unexpected text content");
                        return 0;
                    }
                    break;

                case SCHEMA_CTYPE_VIRTUAL:
                    if (!evalVirtual (interp, sdata, ac)) return 0;
                    break;
................................................................................
                    }
                    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,
                                          INVALID_KEYREF_MATCH_TEXT, NULL,
                                          NULL, text, ac)) {
                                return 0;
                            }
                            cp->content[ac]->keySpace->unknownIDrefs = 0;
                        }
                        Tcl_DeleteHashTable (&cp->content[ac]->keySpace->ids);
                    }
                    break;
                    
                case SCHEMA_CTYPE_NAME:
                case SCHEMA_CTYPE_ANY:
                    if (mustMatch (cp->quants[ac], hm)) {
                        if (recover (interp, sdata, MISSING_ELEMENT_MATCH_TEXT,
                                     NULL, NULL, text, ac)) {
                            break;
                        }
                        SetResultV ("Unexpected text content");
                        return 0;
                    }
                    break;

                }
                ac++;
            }
            if (isName) {
                if (recover (interp, sdata, MISSING_ELEMENT_MATCH_TEXT, NULL,
                             NULL, text, 0)) {
                    return 1;
                }
                SetResultV ("Unexpected text content");
                return 0;
            }
            popStack (sdata);
            continue;

        case SCHEMA_CTYPE_KEYSPACE:
        case SCHEMA_CTYPE_KEYSPACE_END:
................................................................................
            break;
        }
        if (only_whites)  return TCL_OK;
        if (matchText (interp, sdata, text)) {
            return TCL_OK;
        }
    }



    if (!sdata->evalError) {
        SetResult ("Text content doesn't match");
    }
    return TCL_ERROR;
}

static void
................................................................................

    if (probeElement (vdata->interp, sdata, s, namespace)
        != TCL_OK) {
        sdata->validationState = VALIDATION_ERROR;
        XML_StopParser (vdata->parser, 0);
        return;
    }
    if (sdata->skipDeep == 0
        && (atts[0] || (sdata->stack && sdata->stack->pattern->attrs))) {
        if (probeAttributes (vdata->interp, sdata, atts)
            != TCL_OK) {
            sdata->validationState = VALIDATION_ERROR;
            XML_StopParser (vdata->parser, 0);
        }
    }
}
................................................................................
    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) {
            SetResult (errMsg);


            goto errorCleanup;
        }
        if (rs.type == EmptyResult) goto nextConstraint;
        if (rs.type != xNodeSetResult) {



            SetResult ("INVALID_DOM_KEYCONSTRAINT");
            goto errorCleanup;
        }
        for (i = 0; i < rs.nr_nodes; i++) {
            n = rs.nodes[i];
            if (n->nodeType != ELEMENT_NODE) {



                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) {



                    SetResult (errMsg);
                    goto errorCleanup;
                }
                if (frs.type != xNodeSetResult && frs.type != EmptyResult) {




                    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, DOM_KEYCONSTRAINT, NULL,
                                     NULL, NULL, 0)) {
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    continue;
                }
                if (frs.nr_nodes != 1) {
                    if (recover (interp, sdata, DOM_KEYCONSTRAINT, NULL, NULL,
                                 NULL, 0)) {
                        break;
                    }
                    SetResult ("DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
                if (frs.nodes[0]->nodeType != ELEMENT_NODE
                    && frs.nodes[0]->nodeType != ATTRIBUTE_NODE) {



                    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, DOM_KEYCONSTRAINT, NULL,
                                     NULL, NULL, 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, DOM_KEYCONSTRAINT, NULL,
                                     NULL, NULL, 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) {




                        SetResult (errMsg);
                        goto errorCleanup;
                    }
                    if (frs.type != xNodeSetResult
                        && frs.type != EmptyResult) {




                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.type == EmptyResult || frs.nr_nodes == 0) {
                        if (kc->flags & DKC_FLAG_IGNORE_EMPTY_FIELD_SET) {
                            continue;
                        }
                        if (first) first = 0;
                        else Tcl_DStringAppend (&dStr, ":", 1);
                        continue;
                    }
                    if (frs.nr_nodes != 1) {
                        if (recover (interp, sdata, DOM_KEYCONSTRAINT, NULL,
                                     NULL, NULL, 0)) {
                            skip = 1;
                            break;
                        }
                        SetResult ("DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (frs.nodes[0]->nodeType != ELEMENT_NODE
                        && frs.nodes[0]->nodeType != ATTRIBUTE_NODE) {




                        SetResult ("INVALID_DOM_KEYCONSTRAINT");
                        goto errorCleanup;
                    }
                    if (first) first = 0;
                    else Tcl_DStringAppend (&dStr, ":", 1);
                    if (frs.nodes[0]->nodeType == ATTRIBUTE_NODE) {
                        attr = (domAttrNode *) frs.nodes[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, DOM_KEYCONSTRAINT, NULL, NULL,
                                 NULL, 0)) {
                        break;
                    }
                    SetResult ("DOM_KEYCONSTRAINT");
                    goto errorCleanup;
                }
            }
        }
................................................................................
    if (probeElement (interp, sdata, ln,
                      node->namespace ?
                      node->ownerDocument->namespaces[node->namespace-1]->uri
                      : NULL)
        != TCL_OK) {
        return TCL_ERROR;
    }
    if (sdata->skipDeep == 0) {
        if (node->firstAttr) {
            if (probeDomAttributes (interp, sdata, node->firstAttr) != TCL_OK) {
                return TCL_ERROR;
            }
        } else {
            if (sdata->stack->pattern->numReqAttr) {
                /* probeDomAttributes fills interp result with a msg which
                 * required attributes are missing. */
                probeDomAttributes (interp, sdata, NULL);
                return TCL_ERROR;
            }
        }
    }

    if (sdata->stack->pattern->domKeys) {
        if (checkdomKeyConstraints (interp, sdata, node) != TCL_OK)
            return TCL_ERROR;
    }
................................................................................
{
    Tcl_HashEntry *h;
    Tcl_HashSearch search;
    SchemaDocKey *dk;
    SchemaKeySpace *ks;
    
    while (sdata->stack) popStack (sdata);
    while (sdata->lastMatchse) popFromStack (sdata, &sdata->lastMatchse);
    sdata->validationState = VALIDATION_READY;
    sdata->skipDeep = 0;
    sdata->evalError = 0;
    sdata->vaction = 0;
    Tcl_DStringSetLength (sdata->cdata, 0);
    if (sdata->ids.numEntries) {
        Tcl_DeleteHashTable (&sdata->ids);
        Tcl_InitHashTable (&sdata->ids, TCL_STRING_KEYS);
        sdata->unknownIDrefs = 0;
    }
    if (sdata->idTables.numEntries) {
................................................................................
    Tcl_Obj *rObj;

    rObj = Tcl_NewObj();
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewStringObj ("#text", 5));
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewObj());
    return rObj;
}

static Tcl_Obj*
serializeElementEnd (
    Tcl_Interp *interp
    )
{
    Tcl_Obj *rObj;

    rObj = Tcl_NewObj();
    Tcl_ListObjAppendElement (interp, rObj,
                              Tcl_NewStringObj ("<elementend>", 12));
    Tcl_ListObjAppendElement (interp, rObj, Tcl_NewObj());
    return rObj;
}

static void
definedElements (
    SchemaData *sdata,
    Tcl_Interp *interp
    )
{
................................................................................
            || cp->flags & PLACEHOLDER_PATTERN_DEF) continue;
        elmObj = serializeElementName (interp, cp);
        Tcl_ListObjAppendElement (interp, rObj, elmObj);
    }
}

static int
getNextExpectedWorker (
    SchemaData *sdata,
    SchemaValidationStack *se,
    Tcl_Interp *interp,
    Tcl_HashTable *seenCPs,
    Tcl_Obj *rObj
    )
{
    int ac, hm, i, hnew, mustMatch, mayskip;
    SchemaCP *cp, *ic, *jc;
    SchemaValidationStack *se1;

    getContext (cp, ac, hm);
    if (hm && maxOne(cp->quants[ac])) ac++;
    switch (cp->type) {
    case SCHEMA_CTYPE_INTERLEAVE:
................................................................................
                                          serializeElementName (interp, ic));
                break;
            case SCHEMA_CTYPE_INTERLEAVE:
            case SCHEMA_CTYPE_PATTERN:
                Tcl_CreateHashEntry (seenCPs, ic, &hnew);
                if (hnew) {
                    se1 = getStackElement (sdata, ic);
                    mayskip = getNextExpectedWorker (sdata, se1, interp,
                                                     seenCPs, rObj);
                    repoolStackElement (sdata, se1);
                }
                break;

            case SCHEMA_CTYPE_ANY:
                Tcl_ListObjAppendElement (interp, rObj,
                                          serializeAnyCP (interp, ic));
................................................................................
                            );
                        break;
                    case SCHEMA_CTYPE_INTERLEAVE:
                    case SCHEMA_CTYPE_PATTERN:
                        Tcl_CreateHashEntry (seenCPs, jc, &hnew);
                        if (hnew) {
                            se1 = getStackElement (sdata, ic);
                            mayskip = getNextExpectedWorker (
                                sdata, se1, interp, seenCPs, rObj
                                );
                            repoolStackElement (sdata, se1);
                        }
                        break;
                    case SCHEMA_CTYPE_ANY:
                        Tcl_ListObjAppendElement (
                            interp, rObj, serializeAnyCP (interp, jc)
                            );
................................................................................
                    }
                }
                break;

            case SCHEMA_CTYPE_VIRTUAL:
            case SCHEMA_CTYPE_KEYSPACE:
            case SCHEMA_CTYPE_KEYSPACE_END:
                mayskip = 1;
                break;
            }
            if (cp->type == SCHEMA_CTYPE_INTERLEAVE) {
                if (minOne(cp->quants[ac])) mustMatch = 1;
            } else {
                if (!mayskip && minOne (cp->quants[ac])) break;
            }
            ac++;
        }
        if (cp->type == SCHEMA_CTYPE_NAME && ac == cp->nc) {
            Tcl_ListObjAppendElement (
                interp, rObj, serializeElementEnd (interp)
                );
        }
        break;
        
    case SCHEMA_CTYPE_ANY:
    case SCHEMA_CTYPE_CHOICE:
    case SCHEMA_CTYPE_TEXT:
    case SCHEMA_CTYPE_VIRTUAL:
................................................................................
    }
    if (cp->type == SCHEMA_CTYPE_NAME) {
        return 0;
    }
    if (cp->type == SCHEMA_CTYPE_INTERLEAVE && mustMatch) {
        return 0;
    }
    return 1;
}

static Tcl_Obj *
unifyMatchList (
    Tcl_Interp *interp,
    Tcl_HashTable *htable,
    Tcl_Obj *list
    )
{
    int len, i, hnew;
    Tcl_HashEntry *h;
    Tcl_Obj *rObj, *thisObj;
    Tcl_HashSearch search;
    
    rObj = Tcl_NewObj();
    Tcl_ListObjLength (interp, list, &len);
    if (len == 0) return rObj;
    if (len == 1) {
        Tcl_ListObjIndex (interp, list, 0, &thisObj);
        Tcl_ListObjAppendElement (interp, rObj, thisObj);
        return rObj;
    }
    Tcl_InitHashTable (htable, TCL_STRING_KEYS);
    for (i = 0; i < len; i++) {
        Tcl_ListObjIndex (interp, list, i, &thisObj);
        h = Tcl_CreateHashEntry (htable, Tcl_GetString (thisObj), &hnew);
        if (hnew) {
            Tcl_SetHashValue (h, thisObj);
        }
    }
    for (h = Tcl_FirstHashEntry (htable, &search);
         h != NULL;
         h = Tcl_NextHashEntry (&search)) {
        Tcl_ListObjAppendElement (interp, rObj, Tcl_GetHashValue (h));
    }
    Tcl_DeleteHashTable (htable);
    return rObj;
}

static void
getNextExpected (
    SchemaData *sdata,
    Tcl_Interp *interp
    )
{
    int remainingLastMatch, count;
    Tcl_Obj *rObj;
    Tcl_HashTable localHash;
    SchemaValidationStack *se;

    remainingLastMatch = 0;
    if (sdata->lastMatchse) {
        se = sdata->lastMatchse;
        while (se->down) {
            remainingLastMatch++;
            se = se->down;
        }
    } else {
        se = sdata->stack;
    }
    rObj = Tcl_NewObj();
    Tcl_InitHashTable (&localHash, TCL_ONE_WORD_KEYS);
    while (getNextExpectedWorker (sdata, se, interp, &localHash, rObj)) {
        if (remainingLastMatch) {
            count = 0;
            se = sdata->lastMatchse;
            while (count < remainingLastMatch) {
                se = se->down;

                count++;
            }
            remainingLastMatch--;
        } else {
            se = se->down;

        }

    }
    Tcl_DeleteHashTable (&localHash);
    Tcl_SetObjResult (interp, unifyMatchList (interp, &localHash,
                                              rObj));
    Tcl_DecrRefCount (rObj);
}

static int
schemaInstanceInfoCmd (
    SchemaData *sdata,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    int methodIndex;
    Tcl_HashEntry *h;
    SchemaCP *cp;
    SchemaValidationStack *se;
    void *ns;
    Tcl_Obj *rObj;


    
    static const char *schemaInstanceInfoMethods[] = {
        "validationstate", "vstate", "definedElements", "stack", "toplevel",
        "expected", "definition", "validationaction", "vaction", NULL
    };
    enum schemaInstanceInfoMethod {
        m_validationstate, m_vstate, m_definedElements, m_stack, m_toplevel,
        m_expected, m_definition, m_validationaction,
        m_vaction
    };

    static const char *schemaInstanceInfoStackMethods[] = {
        "top", "inside", NULL
    };
    enum schemaInstanceInfoStackMethod {
        m_top, m_inside
    };

    static const char *schemaInstanceInfoVactionMethods[] = {
        "name", "namespace", "text", NULL
    };
    enum schemaInstanceInfoVactionMethod {
        m_name, m_namespace, m_text
    };
    
    if (objc < 2) {
        Tcl_WrongNumArgs (interp, 1, objv, "subcommand ?arguments?");
        return TCL_ERROR;
    }

    if (Tcl_GetIndexFromObj (interp, objv[1], schemaInstanceInfoMethods,
................................................................................
            Tcl_WrongNumArgs (interp, 1, objv, "definedElements");
            return TCL_ERROR;
        }
        definedElements (sdata, interp);
        break;

    case m_stack:
        if (objc != 3) {
            Tcl_WrongNumArgs (interp, 2, objv, "top|inside");
            return TCL_ERROR;
        }
        if (Tcl_GetIndexFromObj (interp, objv[2],
                                 schemaInstanceInfoStackMethods,
                                 "method", 0, &methodIndex)
            != TCL_OK) {
            return TCL_ERROR;
        }
        switch ((enum schemaInstanceInfoStackMethod) methodIndex) {
        case m_inside:
            if (!sdata->stack) {
                Tcl_ResetResult (interp);
                return TCL_OK;
            }
            se = sdata->stack;
            rObj = Tcl_NewObj();
            while (se) {
                if (se->pattern->type == SCHEMA_CTYPE_NAME) {
                    Tcl_ListObjAppendElement (interp, rObj,
                        serializeElementName (interp, se->pattern));
                }
                se = se->down;
            }
            Tcl_SetObjResult (interp, rObj);
            return TCL_OK;
            
        case m_top:
            if (!sdata->stack) {
                Tcl_ResetResult (interp);
                return TCL_OK;
            }
            se = sdata->stack;
            while (se->pattern->type != SCHEMA_CTYPE_NAME) {
                se = se->down;
            }
            rObj = serializeElementName (interp, se->pattern);
            Tcl_SetObjResult (interp, rObj);
            return TCL_OK;

        }
        
    case m_toplevel:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 2, objv, "");
            return TCL_ERROR;
        }
        if (!sdata->currentEvals) {
            SetResult ("not called while schema evaluation");
            return TCL_ERROR;
        }
        if (!sdata->defineToplevel && sdata->currentEvals > 1) {
            SetBooleanResult (0);
        } else {
            SetBooleanResult (1);
        }
        return TCL_OK;

    case m_expected:
        if (objc != 2) {
            Tcl_WrongNumArgs (interp, 2, objv, "");
            return TCL_ERROR;
        }
        if (sdata->validationState == VALIDATION_ERROR
            || sdata->validationState == VALIDATION_FINISHED) {
            return TCL_OK;
        }
        if (!sdata->stack) {
            if (sdata->start) {
                Tcl_AppendElement (interp, sdata->start);
                if (sdata->startNamespace) {
                    Tcl_AppendElement (interp, sdata->startNamespace);
                }
            } else {
                definedElements (sdata, interp);
            }
        } else {


            getNextExpected (sdata, interp);













        }


















        break;
        
    case m_definition:
        if (objc < 3 && objc > 4) {
            Tcl_WrongNumArgs (interp, 1, objv, "name ?namespace?");
            return TCL_ERROR;
        }
................................................................................
        if (cp->namespace) {
            Tcl_AppendElement (interp, cp->namespace);
        }
        if (cp->defScript) {
            Tcl_AppendElement (interp, Tcl_GetString (cp->defScript));
        }
        break;

    case m_vaction:
    case m_validationaction:
        if (objc == 2) {
            SetResult (ValidationAction2str[sdata->vaction]);
            break;
        }
        if (objc != 3) {
            Tcl_WrongNumArgs (interp, 2, objv, "?name|namespace|text?");
            return TCL_ERROR;
        }
        if (Tcl_GetIndexFromObj (interp, objv[2],
                                 schemaInstanceInfoVactionMethods,
                                 "method", 0, &methodIndex)
            != TCL_OK) {
            return TCL_ERROR;
        }
        switch ((enum schemaInstanceInfoVactionMethod) methodIndex) {
        case m_name:
            SetResult (sdata->vname);
            break;
        case m_namespace:
            SetResult (sdata->vns);
            break;
        case m_text:
            SetResult (sdata->vtext);
            break;
        }
        break;
    }
    return TCL_OK;
}

int
schemaInstanceCmd (
    ClientData clientData,
................................................................................
                    } else {
                        attData = objv[4];
                    }
                }
            }
            result = probeElement (interp, sdata, Tcl_GetString (objv[3]),
                                   namespacePtr);
            if (sdata->skipDeep == 0 && result == TCL_OK) {
                result = probeEventAttribute (interp, sdata, attData, len);
            }
            break;
            
        case k_elementend:
            if (objc != 3) {
                Tcl_WrongNumArgs (interp, 3, objv, "No arguments expected.");
................................................................................
                Tcl_SetVar (interp, Tcl_GetString (objv[3]), "", 0);
            }
        } else {
            if (objc == 4) {
                Tcl_SetVar (interp, Tcl_GetString (objv[3]),
                            Tcl_GetStringResult (interp), 0);
            }
            if (sdata->evalError) {
                return TCL_ERROR;
            } else {
                SetBooleanResult (0);
            }
        }
        schemaReset (sdata);
        break;

    case m_domvalidate:
        CHECK_EVAL
        if (objc < 3 || objc > 4) {
................................................................................
    }
    for (i = 0; i < nrKeyspaces; i++) {
        Tcl_ListObjIndex (interp, objv[1], i, &ksObj);
        h = Tcl_CreateHashEntry (&sdata->keySpaces,
                                 Tcl_GetString (ksObj), &hnew);
        if (hnew) {
            ks = TMALLOC (SchemaKeySpace);
            ks->name = Tcl_GetHashKey (&sdata->keySpaces, h);
            ks->active = 0;
            ks->unknownIDrefs = 0;
            Tcl_SetHashValue (h, ks);
        } else {
            ks = Tcl_GetHashValue (h);
        }
        pattern = initSchemaCP (SCHEMA_CTYPE_KEYSPACE,

Changes to generic/schema.h.

84
85
86
87
88
89
90

91
92
93
94
95
96
97
...
152
153
154
155
156
157
158

159
160
161
162
163
164
165
...
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
    int    nrFields;
    int    flags;
    struct domKeyConstraint *next;
} domKeyConstraint;

typedef struct 
{

    int active;
    Tcl_HashTable ids;
    int unknownIDrefs;
} SchemaKeySpace;

typedef struct SchemaCP
{
................................................................................
    unsigned int patternListSize;
    unsigned int forwardPatternDefs;
    SchemaQuant *quants;
    int       currentEvals;
    int       cleanupAfterEval;
    int       evalError;
    Tcl_Obj  *reportCmd;

    Tcl_Obj **evalStub;
    Tcl_Obj **textStub;
    char *currentNamespace;
    int   defineToplevel;
    int   isTextConstraint;
    int   isAttributeConstaint;
    SchemaCP *cp;
................................................................................
    SchemaAttr **currentAttrs;
    unsigned int numAttr;
    unsigned int numReqAttr;
    unsigned int attrSize;
    SchemaValidationStack *stack;
    SchemaValidationStack *stackPool;
    ValidationState validationState;




    unsigned int skipDeep;
    Tcl_DString *cdata;
    Tcl_HashTable ids;
    int unknownIDrefs;
    Tcl_HashTable idTables;
    Tcl_HashTable keySpaces;
} SchemaData;

int 
schemaInstanceCmd (
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    );

int tDOM_SchemaObjCmd (
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    );

void tDOM_SchemaInit (







>







 







>







 







>
>
>
>










<
<
<
<
<
<
<







84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
...
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
    int    nrFields;
    int    flags;
    struct domKeyConstraint *next;
} domKeyConstraint;

typedef struct 
{
    char *name;
    int active;
    Tcl_HashTable ids;
    int unknownIDrefs;
} SchemaKeySpace;

typedef struct SchemaCP
{
................................................................................
    unsigned int patternListSize;
    unsigned int forwardPatternDefs;
    SchemaQuant *quants;
    int       currentEvals;
    int       cleanupAfterEval;
    int       evalError;
    Tcl_Obj  *reportCmd;
    SchemaValidationStack *lastMatchse;
    Tcl_Obj **evalStub;
    Tcl_Obj **textStub;
    char *currentNamespace;
    int   defineToplevel;
    int   isTextConstraint;
    int   isAttributeConstaint;
    SchemaCP *cp;
................................................................................
    SchemaAttr **currentAttrs;
    unsigned int numAttr;
    unsigned int numReqAttr;
    unsigned int attrSize;
    SchemaValidationStack *stack;
    SchemaValidationStack *stackPool;
    ValidationState validationState;
    int vaction;
    const char *vname;
    const char *vns;
    const char *vtext;
    unsigned int skipDeep;
    Tcl_DString *cdata;
    Tcl_HashTable ids;
    int unknownIDrefs;
    Tcl_HashTable idTables;
    Tcl_HashTable keySpaces;
} SchemaData;

int 
schemaInstanceCmd (







    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    );

void tDOM_SchemaInit (

Changes to tests/schema.test.

918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
...
979
980
981
982
983
984
985
















986
987
988
989
990
991
992
....
1543
1544
1545
1546
1547
1548
1549



























1550
1551
1552
1553
1554
1555
1556
....
1704
1705
1706
1707
1708
1709
1710

1711
1712
1713
1714
1715
1716
1717
....
1732
1733
1734
1735
1736
1737
1738

1739
1740
1741
1742
1743
1744
1745
....
1760
1761
1762
1763
1764
1765
1766

1767
1768
1769
1770
1771
1772
1773
....
1794
1795
1796
1797
1798
1799
1800

1801
1802
1803
1804
1805
1806
1807
....
1828
1829
1830
1831
1832
1833
1834

1835
1836
1837
1838
1839
1840
1841
....
1862
1863
1864
1865
1866
1867
1868

1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
....
1910
1911
1912
1913
1914
1915
1916

1917
1918
1919
1920
1921
1922
1923
....
1935
1936
1937
1938
1939
1940
1941

1942
1943
1944

1945
1946






























1947
1948
1949
1950
1951
1952
1953
....
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
....
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
....
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
....
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
....
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
....
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
....
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
....
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
....
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240

































































































5241
5242
5243
5244
5245
5246
5247
....
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
....
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
....
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
....
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
....
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
....
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
....
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
....
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
....
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
....
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
....
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a/><b/></doc>
    } {
        lappend result [s validate $xml errMsg]
        lappend result $errMsg
    }
    s delete
    set result
} {0 {error "This method is not allowed in nested evaluation" at line 1 character 6} 0 {error "This method is not allowed in nested evaluation" at line 1 character 15} 0 {error "This method is not allowed in nested evaluation" at line 1 character 13}}

test schema-4.6 {event start with namespace} {
    tdom::schema s
    s defelement doc http://tdom.org/test {
        element a ! text
    }
    s event start doc http://tdom.org/test
................................................................................
    }
    s event start doc http://tdom.org/test
    s event start a {{att1 http://tdom.org/test} "some data"} http://tdom.org/test
    s event end
    s event end
} {}

















test schema-5.1 {dom parse -validateCmd} {
    set result [catch {
        [dom parse -validateCmd tdom::schema <doc/>]
    }]
} 1

test schema-5.2 {dom parse -validateCmd} {
................................................................................
        <doc><a/><b/><a/><b/></doc>
    } {
        lappend result [grammar validate $xml]
    }
    grammar delete
    set result
} {1 1 1 0}




























test schema-7.12 {loop over seq} {
    tdom::schema create grammar
    grammar define {
        defelement doc {
            group {
                element a
................................................................................
        <doc><a/><a/><a/></doc>
        <doc><a/><a/><a/><c/></doc>
        <doc><b/><b/><b/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {0 0 1 0 1 1 1}

test schema-7.15a {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><a/><a/><a/></doc>
        <doc><a/><a/><a/><c/></doc>
        <doc><b/><b/><b/><c/></doc>
        <doc><b/><b/><b/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {1 1 0 1 0 0 1}

test schema-7.15b {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><a/><a/><a/></doc>
        <doc><a/><a/><a/><c/></doc>
        <doc><b/><b/><b/><c/></doc>
        <doc><b/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {1 1 0 1 0 0 1}

test schema-7.16 {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {0 0 1 0 1 1 1 1 1}

test schema-7.17 {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {0 0 1 0 1 1 0 0 1}

test schema-7.18 {choice} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {0 0 1 0 0 1 0 0 0}

        # <doc/>
        # <doc><a/></doc>
        # <doc><a/><c/></doc>
        # <doc><a/><a/><c/></doc>
        # <doc><a/><a/></doc>
        # <doc><a/><a/><a/></doc>
        # <doc><a/><a/><a/><c/></doc>
        # <doc><aa/><c/></doc>
        # <doc><aa/><ab/><aa/><c/></doc>
        # <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        # <doc><c/></doc>

test schema-7.19 {choice with quantified choice} {
    tdom::schema create s
    s define {
        defelement doc {
            choice {
                element a {0 2}
                group {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }

    set result
} {0 0 1 1 0 0 0 1 0 0 1}

test schema-7.20 {group with only optional content} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a/><c/></doc>
        <doc><a/><a/><c/></doc>
        <doc><a/><b/></doc>
        <doc><a/><b/><c/></doc>

    } {
        lappend result [s validate $xml]
    }

    set result
} {0 1 1 0 1 1}































test schema-8.1 {validate method} {
    tdom::schema create grammar
    grammar defelement doc {
        element e1
        element e2 *
    }
................................................................................
</addressBook>        
    }]
    set result [s domvalidate $doc errMsg]
    lappend result $errMsg
    $doc delete
    s delete
    set result
} {0 {Unexpected child element "some" for element "some"}}

test schema-12.3 {domvalidate w/ attribute} {
    tdom::schema s
    s define {
        defelement doc {
            attribute type {fixed "1.2"}
            element a
................................................................................
    set result [s validate {<doc><a><b>foo</b><c/></a><a><b></b><c>bar</c></a></doc>} msg]
    s delete
    lappend result $msg ${schema-15.2}
    set result
} {1 {} astartaendastartaend}

proc schema-15.3 {type cmd} {
    lappend ::schema-15.3 $type [$cmd info stack inside]
}

test schema-15.3 {constraint cmd tcl} {
    tdom::schema s
    s define {
        defelement doc {
            element a *
................................................................................
    set result
} {defelement b {
            element b1 1 text
            element a
            element b2
        }}

test schema-17.5 {info nextexpected} {
    tdom::schema s
    s define {
        defelement doc {
            choice ? {
                element a
                element c
                element b
................................................................................
            }
            element toplevel ?
            element musthave
            element aftermust
        }
    }
    s event start doc
    set result [s info nextexpected]
    s delete
    lsort $result
} {a b c musthave toplevel}

test schema-17.6 {info nextexpected} {
    tdom::schema s
    s prefixns {foo http://foo.bar}
    s define {
        defelement doc {
            choice ? {
                element a
                element c
................................................................................
            namespace foo {
                element musthave
            }
            element aftermust
        }
    }
    s event start doc
    set result [s info nextexpected]
    s delete
    lsort $result
} {a b c {musthave http://foo.bar} toplevel}

test schema-17.7 {info nextexpected} {
    tdom::schema s
    s prefixns {foo http://foo.bar}
    s define {
        defelement doc {
            mixed {
                element a
                element c
................................................................................
            namespace foo {
                element musthave
            }
            element aftermust
        }
    }
    s event start doc
    set result [s info nextexpected]
    s delete
    lsort $result
} {a b c {musthave http://foo.bar} toplevel {{#text} {}}}

test schema-17.8 {info nextexpected} {
    tdom::schema s
    s defelement doc {
        choice ? {
            element a
            element c
            element b
        }
        element toplevel ?
        element musthave
        element aftermust
    }
    set result [s info nextexpected]
    s define {
        foreach elm {a b c} {
            defelement $elm {}
        }
    }
    lappend result {*}[lsort [s info nextexpected]]
    s event start doc
    lappend result {*}[lsort [s info nextexpected]]
    s event start c
    s event end
    lappend result {*}[lsort [s info nextexpected]]
    s delete
    set result
} {doc a b c doc a b c musthave toplevel musthave toplevel}

proc schema-17.9 {scmd} {
    global result
    lappend result {*}[lsort [$scmd info nextexpected]]
}
                  
test schema-17.9 {info nextexpected from scripted constrain} {
    tdom::schema s
    s define {
        defpattern some {
            element a ?
            group ? {
                element b ?
                tcl schema-17.9
................................................................................
            element must
        }
    }
    set result ""
    lappend [s validate {<doc><must/></doc>}]
    s delete
    set result
} {c may must}

test schema-17.10 {info nextexpected interleave} {
    set defs {
        {
            interleave {
                element a ?
                element b
                element c ?
            }
................................................................................
        }
    }
    set result [list]
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s event start doc
        lappend result {*}[lsort [s info nextexpected]]
        s delete
    }
    set result
} {a b c a b c d a b c d}

test schema-17.11 {info nextexpected interleave} {
    set defs {
        {
            group + {
                element c ?
                element a ?
                element b ?
            }
................................................................................
    set result [list]
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s event start doc
        s event start b
        s event end
        set result [lsort [s info nextexpected]]
        s delete
    }
    set result
} {a b c d}

test schema-17.12 {info nextexpected interleave} {
    tdom::schema s
    s define {
        prefixns {ns1 http://foo.bar}
        defelement doc {
            element a
            any
            any ns1 ?
            element b ?
        }
    }
    s event start doc
    s event start a
    s event end
    set result [lsort [s info nextexpected]]
    s event start something
    s event end
    lappend result {*}[lsort [s info nextexpected]]
    s delete
    set result
} {{<any> {}} {<any> http://foo.bar} b}


































































































proc schema-18 {args} {
    lappend ::result {*}$args
}
test schema-18.1 {reportcmd} {
    tdom::schema s
    s define {
................................................................................
    }
    s reportcmd schema-18
    set result ""
    set rc [s validate {<doc><a/><c/></doc>}]
    lappend result $rc
    s delete
    set result
} {s MISSING_CP 1}

test schema-18.3 {reportcmd} {
    tdom::schema s
    s define {
        defelement doc {
            element a
        }
................................................................................
        {<doc><items><item ref="1"/><item ref="1"/></items><items><item ref="1"/><item ref="1"/></items></doc>}
        {<doc><items><item ref="1"/><item ref="a"/></items><items><item ref="b"/><item ref="1"/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 s WRONG_ATTRIBUTE_VALUE s WRONG_ATTRIBUTE_VALUE 1}

test schema-18.5 {reportcmd} {
    tdom::schema s
    s define {
        defelement doc {
            element items * {
                element item * {
................................................................................
        {<doc><items><item>>12</item><item>ab</item></items></doc>}

    } {
        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
................................................................................
        {<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]
    }
    s delete
    set result
} {1 s WRONG_VALUE 1 s WRONG_VALUE 1 s WRONG_VALUE s WRONG_VALUE 1}

proc schema-18.7 {args} {
    global result
    lappend result "tclcallback"
}

test schema-18.7 {reportcmd} {
................................................................................
        {<doc><items><b/><a/><c/></items><items><b/><c/></items><items><a/><b/><c/></items></doc>}
        {<doc><items><b/><a/><c/></items><items><a/><c/></items><items><b/><a/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {tclcallback 1 tclcallback tclcallback 1 tclcallback s MISSING_ONE_OF_INTERLEAVE tclcallback 1 tclcallback s MISSING_ONE_OF_INTERLEAVE tclcallback s MISSING_ELEMENT 1}

test schema-18.8 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
    foreach xml {
        {<doc><items><a/><b/><c/></items><items><a/><c/></items><items><b/><a/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s MISSING_CP s MISSING_CP 1}

test schema-18.9 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
    foreach xml {
        {<doc><a/><b/><c/><a/><c/><b/><a/></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s MISSING_CP 1}

test schema-18.10 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
    foreach xml {
        {<doc><a/><b/><c/><a/><b/><a/></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s MISSING_CP 1}

test schema-18.11 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
        <doc><a/><b/><b/><c/></doc>
        <doc><a/><b/><c/><e/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 s MISSING_CP 1 s MISSING_CP 1 s MISSING_CP 1 s MISSING_CP 1 s UNEXPECTED_ELEMENT 1}

proc validatedSAX {g xml {keepEmpties 1}} {
    set args [list -validateCmd $g]
    if {!$keepEmpties} {
        lappend args -ignorewhitespace 1
    }
    xml::parser p {*}$args
................................................................................
        {<doc><items><item ref="1"/><item ref="1"/></items><items><item ref="1"/><item ref="1"/></items></doc>}
        {<doc><items><item ref="1"/><item ref="a"/></items><items><item ref="c"/><item ref="d"/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s WRONG_ATTRIBUTE_VALUE s WRONG_ATTRIBUTE_VALUE 1 1}

test schema-19.3 {keyspace} {
    tdom::schema s
    s define {
        defelement doc {
            element items *
        }
................................................................................
        {<doc><items><item ref="a" id="1"/></items></doc>}
        {<doc><items><item ref="1"/><item id="1"/></items><items><item ref="1"/><item ref="1"/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 1 s UNKNOWN_KEYREF 1 s UNKNOWN_KEYREF 1}

test schema-20.1 {domunique} {
    set schema {
        prefixns {ns1 http://tdom.org/test}
        defelement doc {
            domunique ${::schema-20.1} @ref
        }







|




|







 







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







 







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







 







>







 







>







 







>







 







>







 







>







 







>



<
<
<
<
<
<
<
<
<
<
<
<







 







>







 







>



>

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







 







|







 







|







 







|







 







|




|







 







|




|







 







|




|











|





|

|


|






|


|







 







|

|







 







|





|







 







|





|













|


|


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







 







|







 







|







 







|







 







|







 







|







 







|







 







|







 







|







 







|







 







|







 







|







918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
...
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
....
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
....
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
....
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
....
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
....
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
....
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
....
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920












1921
1922
1923
1924
1925
1926
1927
....
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
....
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
....
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
....
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
....
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
....
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
....
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
....
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
....
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
....
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
....
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
....
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
....
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
....
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
....
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
....
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
....
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
....
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
....
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
....
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
....
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
....
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
    }
    set result [list]
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a/><b/></doc>
    } {
        lappend result [catch {s validate $xml errMsg} errMsg]
        lappend result $errMsg
    }
    s delete
    set result
} {1 {error "This method is not allowed in nested evaluation" at line 1 character 6} 1 {error "This method is not allowed in nested evaluation" at line 1 character 15} 1 {error "This method is not allowed in nested evaluation" at line 1 character 13}}

test schema-4.6 {event start with namespace} {
    tdom::schema s
    s defelement doc http://tdom.org/test {
        element a ! text
    }
    s event start doc http://tdom.org/test
................................................................................
    }
    s event start doc http://tdom.org/test
    s event start a {{att1 http://tdom.org/test} "some data"} http://tdom.org/test
    s event end
    s event end
} {}

proc schema-4.10 {scmd} {
    error "Error raised in schema-4.10"
}
test schema-4.10 {event - tcl error in called script} {
    tdom::schema s
    s defelement doc {
        tcl schema-4.10
        element a
    }
    s event start doc 
    set result [catch {s event start a} errMsg]
    lappend result $errMsg
    s delete
    set result
} {1 {Error raised in schema-4.10}}

test schema-5.1 {dom parse -validateCmd} {
    set result [catch {
        [dom parse -validateCmd tdom::schema <doc/>]
    }]
} 1

test schema-5.2 {dom parse -validateCmd} {
................................................................................
        <doc><a/><b/><a/><b/></doc>
    } {
        lappend result [grammar validate $xml]
    }
    grammar delete
    set result
} {1 1 1 0}

test schema-7.11b {loop over seq} {
    tdom::schema create grammar
    grammar define {
        defelement doc {
            group 2 {
                element a ?
                element b ?
            }
            element c
        }
        foreach e {a b c} {
            defelement $e {}
        }
    }
    set result [list]
    foreach xml {
        <doc><c/></doc>
        <doc><a/><b/><c/></doc>
        <doc><a/><b/><a/><b/><c/></doc>
        <doc><a/><b/><a/><b/></doc>
    } {
        lappend result [grammar validate $xml]
    }
    grammar delete
    set result
} {1 1 1 0}

test schema-7.12 {loop over seq} {
    tdom::schema create grammar
    grammar define {
        defelement doc {
            group {
                element a
................................................................................
        <doc><a/><a/><a/></doc>
        <doc><a/><a/><a/><c/></doc>
        <doc><b/><b/><b/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 0 1 1 1}

test schema-7.15a {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><a/><a/><a/></doc>
        <doc><a/><a/><a/><c/></doc>
        <doc><b/><b/><b/><c/></doc>
        <doc><b/><b/><b/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 1 0 1 0 0 1}

test schema-7.15b {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><a/><a/><a/></doc>
        <doc><a/><a/><a/><c/></doc>
        <doc><b/><b/><b/><c/></doc>
        <doc><b/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 1 0 1 0 0 1}

test schema-7.16 {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 0 1 1 1 1 1}

test schema-7.17 {choice with optional choices} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 0 1 1 0 0 1}

test schema-7.18 {choice} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 0 0 1 0 0 0}













test schema-7.19 {choice with quantified choice} {
    tdom::schema create s
    s define {
        defelement doc {
            choice {
                element a {0 2}
                group {
................................................................................
        <doc><aa/><c/></doc>
        <doc><aa/><ab/><aa/><c/></doc>
        <doc><aa/><ab/><aa/><aa/><ab/><c/></doc>
        <doc><c/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 1 0 0 0 1 0 0 1}

test schema-7.20 {group with only optional content} {
    tdom::schema create s
    s define {
        defelement doc {
................................................................................
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a/><c/></doc>
        <doc><a/><a/><c/></doc>
        <doc><a/><b/></doc>
        <doc><a/><b/><c/></doc>
        <doc><b/><c/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 1 1 0 1 1 0}

test schema-7.21 {group with only optional content} {
    set def {
        group + {
            element c ?
            element a ?
            element b ?
        }
        element d
    }
    set result [list]
    tdom::schema s
    s defelement doc $def
    foreach xml {
        <doc/>
        <doc><a/></doc>
        <doc><a/><c/><d/></doc>
        <doc><c/><d/></doc>
        <doc><a/><a/><c/><d/></doc>
        <doc><a/><b/><d/></doc>
        <doc><a/><b/><c/><d/></doc>
        <doc><b/><c/><d/></doc>
        <doc><d/></doc>
        <doc><d/><d/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {0 0 1 1 1 1 1 1 1 0}

test schema-8.1 {validate method} {
    tdom::schema create grammar
    grammar defelement doc {
        element e1
        element e2 *
    }
................................................................................
</addressBook>        
    }]
    set result [s domvalidate $doc errMsg]
    lappend result $errMsg
    $doc delete
    s delete
    set result
} {0 {Element "some" doesn't match}}

test schema-12.3 {domvalidate w/ attribute} {
    tdom::schema s
    s define {
        defelement doc {
            attribute type {fixed "1.2"}
            element a
................................................................................
    set result [s validate {<doc><a><b>foo</b><c/></a><a><b></b><c>bar</c></a></doc>} msg]
    s delete
    lappend result $msg ${schema-15.2}
    set result
} {1 {} astartaendastartaend}

proc schema-15.3 {type cmd} {
    lappend ::schema-15.3 $type [$cmd info stack top]
}

test schema-15.3 {constraint cmd tcl} {
    tdom::schema s
    s define {
        defelement doc {
            element a *
................................................................................
    set result
} {defelement b {
            element b1 1 text
            element a
            element b2
        }}

test schema-17.5 {info expected} {
    tdom::schema s
    s define {
        defelement doc {
            choice ? {
                element a
                element c
                element b
................................................................................
            }
            element toplevel ?
            element musthave
            element aftermust
        }
    }
    s event start doc
    set result [s info expected]
    s delete
    lsort $result
} {a b c musthave toplevel}

test schema-17.6 {info expected} {
    tdom::schema s
    s prefixns {foo http://foo.bar}
    s define {
        defelement doc {
            choice ? {
                element a
                element c
................................................................................
            namespace foo {
                element musthave
            }
            element aftermust
        }
    }
    s event start doc
    set result [s info expected]
    s delete
    lsort $result
} {a b c {musthave http://foo.bar} toplevel}

test schema-17.7 {info expected} {
    tdom::schema s
    s prefixns {foo http://foo.bar}
    s define {
        defelement doc {
            mixed {
                element a
                element c
................................................................................
            namespace foo {
                element musthave
            }
            element aftermust
        }
    }
    s event start doc
    set result [s info expected]
    s delete
    lsort $result
} {a b c {musthave http://foo.bar} toplevel {{#text} {}}}

test schema-17.8 {info expected} {
    tdom::schema s
    s defelement doc {
        choice ? {
            element a
            element c
            element b
        }
        element toplevel ?
        element musthave
        element aftermust
    }
    set result [s info expected]
    s define {
        foreach elm {a b c} {
            defelement $elm {}
        }
    }
    lappend result {*}[lsort [s info expected]]
    s event start doc
    lappend result {*}[lsort [s info expected]]
    s event start c
    s event end
    lappend result {*}[lsort [s info expected]]
    s delete
    set result
} {doc a b c doc a b c musthave toplevel musthave toplevel}

proc schema-17.9 {scmd} {
    global result
    lappend result {*}[lsort [$scmd info expected]]
}
                  
test schema-17.9 {info expected from scripted constrain} {
    tdom::schema s
    s define {
        defpattern some {
            element a ?
            group ? {
                element b ?
                tcl schema-17.9
................................................................................
            element must
        }
    }
    set result ""
    lappend [s validate {<doc><must/></doc>}]
    s delete
    set result
} {a b c may must}

test schema-17.10 {info expected interleave} {
    set defs {
        {
            interleave {
                element a ?
                element b
                element c ?
            }
................................................................................
        }
    }
    set result [list]
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s event start doc
        lappend result {*}[lsort [s info expected]]
        s delete
    }
    set result
} {a b c a b c d a b c d}

test schema-17.11 {info expected} {
    set defs {
        {
            group + {
                element c ?
                element a ?
                element b ?
            }
................................................................................
    set result [list]
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s event start doc
        s event start b
        s event end
        set result [lsort [s info expected]]
        s delete
    }
    set result
} {a b c d}

test schema-17.12 {info expected} {
    tdom::schema s
    s define {
        prefixns {ns1 http://foo.bar}
        defelement doc {
            element a
            any
            any ns1 ?
            element b ?
        }
    }
    s event start doc
    s event start a
    s event end
    set result [lsort [s info expected]]
    s event start something
    s event end
    lappend result {*}[lsort [s info expected]]
    s delete
    set result
} {{<any> {}} {<any> http://foo.bar} {<elementend> {}} b}

proc schema-17.13 {scmd args} {
    global fromReportCmd
    set fromReportCmd [lsort [$scmd info expected]]
}
test schema-17.13 {info expected} {
    set defs {
        {
            element a
            element b ?
        }
        {
            element a ?
            element b
        }
        {
            element a ?
            element b ?
        }
    }
    set xmlinput {
        <doc/>
        <doc><a/></doc>
        <doc><b/></doc>
        <doc><a/><b/></doc>
        <doc><unknown/></doc>
        <doc><a/><unknown/></doc>
    }
    set result [list]
    set defnr 0
    foreach def $defs {
        tdom::schema s
        s defelement doc $def
        s reportcmd schema-17.13
        set xmlnr 0
        foreach xml $xmlinput {
            set fromReportCmd ""
            lappend result $defnr/$xmlnr: [s validate $xml errMsg]
            lappend result {*}$fromReportCmd
            incr xmlnr
        }
        s delete
        incr defnr
    }
    set result
} {0/0: 1 a 0/1: 1 0/2: 1 a 0/3: 1 0/4: 1 a 0/5: 1 {<elementend> {}} b 1/0: 1 a b 1/1: 1 b 1/2: 1 1/3: 1 1/4: 1 a b 1/5: 1 b 2/0: 1 2/1: 1 2/2: 1 2/3: 1 2/4: 1 {<elementend> {}} a b 2/5: 1 {<elementend> {}} b}

proc schema-17.14 {scmd args} {
    global result
    lappend result {*}[lsort [$scmd info expected]]
}
test schema-17.14 {info expected} {
    set defs {
        {
            group + {
                element c ?
                element a ?
                element b ?
            }
            tcl schema-17.14
            element d
        }
    }
    set result [list]
    foreach def $defs {
        tdom::schema s
        s reportcmd schema-17.14
        s defelement doc $def
        s event start doc
        s event start unknownElement
        s delete
    }
    set result
} {a b c d a b c d}

proc schema-17.15 {type cmd} {
    lappend ::result $type [$cmd info stack inside]
}

test schema-17.15 {info inside} {
    tdom::schema s
    s define {
        defelement doc {
            element a *
        }
        defelement a {
            tcl schema-17.15 astart
            element b ! text
            element c ! text
            tcl schema-17.15 aend
        }
    }
    set result {}
    s validate {<doc><a><b>foo</b><c/></a><a><b></b><c>bar</c></a></doc>}
    s delete
    set result
} {astart {a doc} aend {a doc} astart {a doc} aend {a doc}}

proc schema-18 {args} {
    lappend ::result {*}$args
}
test schema-18.1 {reportcmd} {
    tdom::schema s
    s define {
................................................................................
    }
    s reportcmd schema-18
    set result ""
    set rc [s validate {<doc><a/><c/></doc>}]
    lappend result $rc
    s delete
    set result
} {s MISSING_ELEMENT 1}

test schema-18.3 {reportcmd} {
    tdom::schema s
    s define {
        defelement doc {
            element a
        }
................................................................................
        {<doc><items><item ref="1"/><item ref="1"/></items><items><item ref="1"/><item ref="1"/></items></doc>}
        {<doc><items><item ref="1"/><item ref="a"/></items><items><item ref="b"/><item ref="1"/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 s INVALID_ATTRIBUTE_VALUE s INVALID_ATTRIBUTE_VALUE 1}

test schema-18.5 {reportcmd} {
    tdom::schema s
    s define {
        defelement doc {
            element items * {
                element item * {
................................................................................
        {<doc><items><item>>12</item><item>ab</item></items></doc>}

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

test schema-18.6 {reportcmd} {
    tdom::schema s
    s define {
        defelement item {
            text {
                key lang
................................................................................
        {<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]
    }
    s delete
    set result
} {1 s INVALID_VALUE 1 s INVALID_VALUE 1 s INVALID_VALUE s INVALID_VALUE 1}

proc schema-18.7 {args} {
    global result
    lappend result "tclcallback"
}

test schema-18.7 {reportcmd} {
................................................................................
        {<doc><items><b/><a/><c/></items><items><b/><c/></items><items><a/><b/><c/></items></doc>}
        {<doc><items><b/><a/><c/></items><items><a/><c/></items><items><b/><a/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {tclcallback 1 tclcallback tclcallback 1 tclcallback s MISSING_ELEMENT tclcallback 1 tclcallback s MISSING_ELEMENT tclcallback s MISSING_ELEMENT 1}

test schema-18.8 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
    foreach xml {
        {<doc><items><a/><b/><c/></items><items><a/><c/></items><items><b/><a/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s MISSING_ELEMENT s MISSING_ELEMENT 1}

test schema-18.9 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
    foreach xml {
        {<doc><a/><b/><c/><a/><c/><b/><a/></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s MISSING_ELEMENT 1}

test schema-18.10 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
    foreach xml {
        {<doc><a/><b/><c/><a/><b/><a/></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s MISSING_ELEMENT 1}

test schema-18.11 {reportcmd} {
    tdom::schema s
    s define {
        defelement a {}
        defelement b {}
        defelement c {}
................................................................................
        <doc><a/><b/><b/><c/></doc>
        <doc><a/><b/><c/><e/></doc>
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 s MISSING_ELEMENT 1 s MISSING_ELEMENT 1 s MISSING_ELEMENT 1 s MISSING_ELEMENT 1 s UNEXPECTED_ELEMENT 1}

proc validatedSAX {g xml {keepEmpties 1}} {
    set args [list -validateCmd $g]
    if {!$keepEmpties} {
        lappend args -ignorewhitespace 1
    }
    xml::parser p {*}$args
................................................................................
        {<doc><items><item ref="1"/><item ref="1"/></items><items><item ref="1"/><item ref="1"/></items></doc>}
        {<doc><items><item ref="1"/><item ref="a"/></items><items><item ref="c"/><item ref="d"/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {s INVALID_ATTRIBUTE_VALUE s INVALID_ATTRIBUTE_VALUE 1 1}

test schema-19.3 {keyspace} {
    tdom::schema s
    s define {
        defelement doc {
            element items *
        }
................................................................................
        {<doc><items><item ref="a" id="1"/></items></doc>}
        {<doc><items><item ref="1"/><item id="1"/></items><items><item ref="1"/><item ref="1"/></items></doc>}
    } {
        lappend result [s validate $xml]
    }
    s delete
    set result
} {1 1 s INVALID_KEYREF 1 s INVALID_KEYREF 1}

test schema-20.1 {domunique} {
    set schema {
        prefixns {ns1 http://tdom.org/test}
        defelement doc {
            domunique ${::schema-20.1} @ref
        }