leptris 1.9.323.0-aarch64-linux → 1.9.331.0-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@
4
4
  * grow the model (datatypes, facets, content models, identity
5
5
  * constraints) — this slice's contract is the API shape, the
6
6
  * xs:schema identity check, and the declaration enumeration. */
7
+ #include <ctype.h>
7
8
  #include <stdio.h>
8
9
  #include <stdlib.h>
9
10
  #include <string.h>
@@ -13,6 +14,8 @@
13
14
 
14
15
  #define XSD_NS "http://www.w3.org/2001/XMLSchema"
15
16
 
17
+ void xsd_capture_ics(struct leptris_xsd_schema* s,
18
+ LeptrisElement elem, const char* element_name);
16
19
  extern int xsd_simple_valid(const XsdSimple* simple, const char* v);
17
20
  extern struct xsd_validator* xsd_validator_new(
18
21
  struct leptris_xsd_schema* s);
@@ -32,8 +35,14 @@ struct leptris_xsd_schema {
32
35
  XsdSimple* simple_types;
33
36
  XsdElementDecl* elements;
34
37
  XsdCm* complex_types; /* name-carrying model roots */
38
+ XsdGroupDef* groups; /* top-level xs:group definitions */
39
+ XsdAttrGroupDef* attr_groups;
40
+ XsdAttrDecl* top_attrs; /* top-level xs:attribute declarations */
35
41
  XsdTypeAttrs* type_attrs;
42
+ XsdElementIcs* element_ics;
36
43
  char* target_ns;
44
+ char* base_dir; /* schemaLocation resolution (file compile) */
45
+ int include_depth; /* include/import recursion guard */
37
46
  void* validator; /* slice-4 parked error list */
38
47
  char* error;
39
48
  };
@@ -53,6 +62,24 @@ static XsdCm* xsd_cm_new(XsdCmKind kind);
53
62
  static void xsd_cm_occurrence(XsdCm* cm, LeptrisElement e);
54
63
  static XsdCm* xsd_cm_parse(struct leptris_xsd_schema* s,
55
64
  LeptrisElement group, XsdCm** tail_out);
65
+ static XsdAttrDecl* xsd_capture_attr_rows(struct leptris_xsd_schema* s,
66
+ LeptrisElement container,
67
+ int depth);
68
+ extern XsdCm* xsd_find_complex(struct leptris_xsd_schema* s,
69
+ const char* type_name);
70
+ extern const XsdTypeAttrs* xsd_find_type_attrs(
71
+ struct leptris_xsd_schema* s, const char* type_name);
72
+ static XsdCm* xsd_cm_copy_list(const XsdCm* head);
73
+ static void xsd_capture_root(struct leptris_xsd_schema* s,
74
+ LeptrisElement root);
75
+ static char* xsd_read_file(const char* path, size_t* out_len);
76
+ static XsdAttrDecl* xsd_copy_attr_rows(const XsdAttrDecl* src);
77
+ static void xsd_register_complex(struct leptris_xsd_schema* s,
78
+ XsdCm* root, XsdAttrDecl* attrs,
79
+ const char* name);
80
+ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
81
+ LeptrisElement ct,
82
+ const char* name);
56
83
  static XsdSimple* xsd_capture_simple(LeptrisElement st);
57
84
 
58
85
 
@@ -73,6 +100,7 @@ static void xsd_cm_free(XsdCm* cm) {
73
100
  free(cm->type);
74
101
  free(cm->ns);
75
102
  free(cm->any_ns);
103
+ free(cm->text_type);
76
104
  free(cm);
77
105
  cm = next;
78
106
  }
@@ -104,13 +132,7 @@ static int xsd_capture_inline(struct leptris_xsd_schema* s,
104
132
  leptris_element_expanded_name(e, &local, &prefix, &uri);
105
133
  if (!local || !uri || strcmp(uri, XSD_NS) != 0) continue;
106
134
  if (strcmp(local, "complexType") == 0) {
107
- XsdCm* root = xsd_cm_new(XSD_CM_SEQ);
108
- if (!root) return 0;
109
- root->name = xsd_strdup(synth);
110
- root->first_child = xsd_cm_parse(s, e, NULL);
111
- root->next = s->complex_types;
112
- s->complex_types = root;
113
- return 1;
135
+ return xsd_capture_complex_model(s, e, synth) ? 1 : 0;
114
136
  }
115
137
  if (strcmp(local, "simpleType") == 0) {
116
138
  XsdSimple* captured = xsd_capture_simple(e);
@@ -165,6 +187,13 @@ static XsdCm* xsd_cm_parse(struct leptris_xsd_schema* s,
165
187
  cm->type = xsd_strdup(synth);
166
188
  }
167
189
  }
190
+ } else if (strcmp(local, "group") == 0) {
191
+ /* <xs:group ref="g"/> — expanded at NFA build so
192
+ * forward references need no capture order */
193
+ cm = xsd_cm_new(XSD_CM_GROUP_REF);
194
+ if (cm)
195
+ cm->name = xsd_strdup(
196
+ leptris_element_attribute(e, "ref"));
168
197
  } else if (strcmp(local, "any") == 0) {
169
198
  cm = xsd_cm_new(XSD_CM_ANY);
170
199
  if (cm) {
@@ -222,7 +251,81 @@ static XsdSimple* xsd_capture_simple(LeptrisElement st) {
222
251
  leptris_element_expanded_name(e, &local, &prefix, &uri);
223
252
  if (uri && strcmp(uri, XSD_NS) != 0) continue;
224
253
  if (!local) continue;
225
- if (strcmp(local, "restriction") == 0) {
254
+ if (strcmp(local, "list") == 0) {
255
+ simple->deriv = XSD_SIMPLE_LIST;
256
+ const char* it = leptris_element_attribute(e, "itemType");
257
+ if (it && *it) {
258
+ simple->item_type = xsd_strdup(it);
259
+ } else {
260
+ /* inline anonymous item type rides this capture */
261
+ for (LeptrisNodeRef q = leptris_node_first_child(
262
+ leptris_element_as_node(e));
263
+ q && !simple->item_def;
264
+ q = leptris_node_next_sibling(q)) {
265
+ if (leptris_node_get_type(q) !=
266
+ LEPTRIS_NODE_TYPE_ELEMENT)
267
+ continue;
268
+ LeptrisElement qe = (LeptrisElement)q;
269
+ const char* ql = NULL;
270
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
271
+ if (ql && strcmp(ql, "simpleType") == 0)
272
+ simple->item_def = xsd_capture_simple(qe);
273
+ }
274
+ }
275
+ } else if (strcmp(local, "union") == 0) {
276
+ simple->deriv = XSD_SIMPLE_UNION;
277
+ const char* mt =
278
+ leptris_element_attribute(e, "memberTypes");
279
+ if (mt && *mt) {
280
+ size_t n = 0;
281
+ for (const char* p = mt; *p;) {
282
+ while (*p && isspace((unsigned char)*p)) p++;
283
+ if (!*p) break;
284
+ while (*p && !isspace((unsigned char)*p)) p++;
285
+ n++;
286
+ }
287
+ if (n) {
288
+ simple->members = (char**)calloc(n, sizeof(char*));
289
+ if (simple->members) {
290
+ size_t i = 0;
291
+ const char* p = mt;
292
+ while (*p && i < n) {
293
+ while (*p && isspace((unsigned char)*p)) p++;
294
+ if (!*p) break;
295
+ const char* start = p;
296
+ while (*p && !isspace((unsigned char)*p)) p++;
297
+ size_t len = (size_t)(p - start);
298
+ char* tok = (char*)malloc(len + 1);
299
+ if (!tok) break;
300
+ memcpy(tok, start, len);
301
+ tok[len] = 0;
302
+ simple->members[i++] = tok;
303
+ }
304
+ simple->member_count = i;
305
+ }
306
+ }
307
+ }
308
+ /* inline anonymous members ride this capture */
309
+ for (LeptrisNodeRef q = leptris_node_first_child(
310
+ leptris_element_as_node(e));
311
+ q; q = leptris_node_next_sibling(q)) {
312
+ if (leptris_node_get_type(q) != LEPTRIS_NODE_TYPE_ELEMENT)
313
+ continue;
314
+ LeptrisElement qe = (LeptrisElement)q;
315
+ const char* ql = NULL;
316
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
317
+ if (!ql || strcmp(ql, "simpleType") != 0) continue;
318
+ XsdSimple* def = xsd_capture_simple(qe);
319
+ if (!def) continue;
320
+ XsdSimple** grown = (XsdSimple**)realloc(
321
+ simple->member_defs,
322
+ (simple->member_def_count + 1) *
323
+ sizeof(XsdSimple*));
324
+ if (!grown) break;
325
+ simple->member_defs = grown;
326
+ simple->member_defs[simple->member_def_count++] = def;
327
+ }
328
+ } else if (strcmp(local, "restriction") == 0) {
226
329
  simple->base =
227
330
  xsd_strdup(leptris_element_attribute(e, "base"));
228
331
  for (LeptrisNodeRef f = leptris_node_first_child(
@@ -284,6 +387,71 @@ static const XsdSimple* xsd_find_simple(struct leptris_xsd_schema* s,
284
387
  return NULL;
285
388
  }
286
389
 
390
+ extern int xsd_builtin_valid(const char* type, const char* v);
391
+ extern int rng_regex_matches(const char* pat, const char* text);
392
+ static int xsd_valid_chain(struct leptris_xsd_schema* s,
393
+ const XsdSimple* t, const char* v, int depth);
394
+
395
+ /* One list item or union member: a builtin reference, a local
396
+ * schema type (chain-walked), or an inline capture. Unresolved
397
+ * names accept (the compile-time leniency the corpus gate pins). */
398
+ static int xsd_item_valid(struct leptris_xsd_schema* s,
399
+ const char* name, const XsdSimple* def,
400
+ const char* v, int depth) {
401
+ if (name && strncmp(name, "xs:", 3) == 0)
402
+ return xsd_builtin_valid(name, v);
403
+ if (def) return xsd_valid_chain(s, def, v, depth + 1);
404
+ if (name) {
405
+ const XsdSimple* next = xsd_find_simple(s, name);
406
+ if (next) return xsd_valid_chain(s, next, v, depth + 1);
407
+ }
408
+ return 1;
409
+ }
410
+
411
+ static int xsd_deriv_valid(struct leptris_xsd_schema* s,
412
+ const XsdSimple* t, const char* v,
413
+ int depth) {
414
+ if (t->deriv == XSD_SIMPLE_LIST) {
415
+ char* dup = xsd_strdup(v);
416
+ if (!dup) return 0;
417
+ size_t n = 0;
418
+ int ok = 1;
419
+ for (char* p = dup; *p;) {
420
+ while (*p && isspace((unsigned char)*p)) p++;
421
+ if (!*p) break;
422
+ char* end = p;
423
+ while (*end && !isspace((unsigned char)*end)) end++;
424
+ char was = *end;
425
+ *end = 0;
426
+ if (ok && !xsd_item_valid(s, t->item_type, t->item_def, p,
427
+ depth))
428
+ ok = 0;
429
+ *end = was;
430
+ p = end;
431
+ n++;
432
+ }
433
+ free(dup);
434
+ /* length family counts ITEMS for lists (XSD semantics) */
435
+ if (ok && t->length >= 0 && (long)n != t->length) ok = 0;
436
+ if (ok && t->min_length >= 0 && (long)n < t->min_length) ok = 0;
437
+ if (ok && t->max_length >= 0 && (long)n > t->max_length) ok = 0;
438
+ /* pattern applies to the whole lexical */
439
+ if (ok && t->pattern && !rng_regex_matches(t->pattern, v))
440
+ ok = 0;
441
+ return ok;
442
+ }
443
+ if (t->deriv == XSD_SIMPLE_UNION) {
444
+ for (size_t i = 0; i < t->member_count; i++)
445
+ if (xsd_item_valid(s, t->members[i], NULL, v, depth))
446
+ return 1;
447
+ for (size_t i = 0; i < t->member_def_count; i++)
448
+ if (xsd_item_valid(s, NULL, t->member_defs[i], v, depth))
449
+ return 1;
450
+ return 0;
451
+ }
452
+ return xsd_simple_valid(t, v);
453
+ }
454
+
287
455
  static int xsd_valid_chain(struct leptris_xsd_schema* s,
288
456
  const XsdSimple* t, const char* v, int depth) {
289
457
  if (!t || depth > 32) return 0;
@@ -291,6 +459,8 @@ static int xsd_valid_chain(struct leptris_xsd_schema* s,
291
459
  const XsdSimple* next = xsd_find_simple(s, t->base);
292
460
  if (next && !xsd_valid_chain(s, next, v, depth + 1)) return 0;
293
461
  }
462
+ if (t->deriv != XSD_SIMPLE_RESTRICT)
463
+ return xsd_deriv_valid(s, t, v, depth);
294
464
  return xsd_simple_valid(t, v);
295
465
  }
296
466
 
@@ -318,9 +488,82 @@ static int xsd_is_declaration(const char* local, const char* uri) {
318
488
  return 0;
319
489
  }
320
490
 
491
+ static char* xsd_read_file(const char* path, size_t* out_len) {
492
+ *out_len = 0;
493
+ FILE* f = fopen(path, "rb");
494
+ if (!f) return NULL;
495
+ if (fseek(f, 0, SEEK_END) != 0) {
496
+ fclose(f);
497
+ return NULL;
498
+ }
499
+ long size = ftell(f);
500
+ if (size < 0 || fseek(f, 0, SEEK_SET) != 0) {
501
+ fclose(f);
502
+ return NULL;
503
+ }
504
+ char* buf = (char*)malloc((size_t)size + 1);
505
+ if (!buf) {
506
+ fclose(f);
507
+ return NULL;
508
+ }
509
+ size_t got = fread(buf, 1, (size_t)size, f);
510
+ fclose(f);
511
+ buf[got] = 0;
512
+ *out_len = got;
513
+ return buf;
514
+ }
515
+
516
+ static LeptrisXsdSchema xsd_compile_impl(const char* xsd_text,
517
+ size_t len,
518
+ const char* base_dir,
519
+ LeptrisStatus* status);
520
+
321
521
  LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
322
522
  size_t len,
323
523
  LeptrisStatus* status) {
524
+ return xsd_compile_impl(xsd_text, len, NULL, status);
525
+ }
526
+
527
+ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile_file(
528
+ const char* path, LeptrisStatus* status) {
529
+ if (status) *status = LEPTRIS_OK;
530
+ if (!path || !*path) {
531
+ if (status) *status = LEPTRIS_ERROR_PARSE;
532
+ return NULL;
533
+ }
534
+ size_t len = 0;
535
+ char* text = xsd_read_file(path, &len);
536
+ if (!text) {
537
+ if (status) *status = LEPTRIS_ERROR_PARSE;
538
+ return NULL;
539
+ }
540
+ /* relative schemaLocations resolve against the schema's own
541
+ * directory (both separators: Windows paths carry '\') */
542
+ const char* slash = strrchr(path, '/');
543
+ const char* bslash = strrchr(path, '\\');
544
+ if (bslash && (!slash || bslash > slash)) slash = bslash;
545
+ char* base_dir = NULL;
546
+ if (slash) {
547
+ size_t n = (size_t)(slash - path);
548
+ if (n == 0) n = 1; /* "/f.xsd" roots at "/" */
549
+ base_dir = (char*)malloc(n + 1);
550
+ if (base_dir) {
551
+ memcpy(base_dir, path, n);
552
+ base_dir[n] = 0;
553
+ }
554
+ } else {
555
+ base_dir = xsd_strdup(".");
556
+ }
557
+ LeptrisXsdSchema s = xsd_compile_impl(text, len, base_dir, status);
558
+ free(base_dir);
559
+ free(text);
560
+ return s;
561
+ }
562
+
563
+ static LeptrisXsdSchema xsd_compile_impl(const char* xsd_text,
564
+ size_t len,
565
+ const char* base_dir,
566
+ LeptrisStatus* status) {
324
567
  if (status) *status = LEPTRIS_OK;
325
568
  if (!xsd_text || len == 0) {
326
569
  if (status) *status = LEPTRIS_ERROR_PARSE;
@@ -333,6 +576,7 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
333
576
  if (status) *status = LEPTRIS_ERROR_MEMORY;
334
577
  return NULL;
335
578
  }
579
+ s->base_dir = xsd_strdup(base_dir);
336
580
 
337
581
  /* target namespace (xs:any ##other/##targetNamespace filters) */
338
582
  s->target_ns = NULL;
@@ -364,15 +608,79 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
364
608
  goto fail;
365
609
  }
366
610
 
611
+ xsd_capture_root(s, root);
612
+
613
+ leptris_document_free(doc);
614
+ return s;
615
+
616
+ fail:
617
+ if (doc) leptris_document_free(doc);
618
+ if (status) *status = LEPTRIS_ERROR_PARSE;
619
+ /* Failure returns an error-carrying handle (the documented
620
+ * contract): leptris_xsd_error says why, free it the same way. */
621
+ return (LeptrisXsdSchema)s;
622
+ }
623
+
624
+ /* Every top-level declaration under an xs:schema root. xs:include
625
+ * / xs:import / xs:redefine resolve their schemaLocation against
626
+ * the owning schema's base_dir (string-compiled schemas have none
627
+ * and skip them); the referenced root's declarations merge into
628
+ * the same tables. */
629
+ static void xsd_capture_root(struct leptris_xsd_schema* s,
630
+ LeptrisElement root) {
367
631
  for (LeptrisNodeRef n =
368
632
  leptris_node_first_child(leptris_element_as_node(root));
369
633
  n; n = leptris_node_next_sibling(n)) {
370
634
  if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
371
635
  continue;
636
+ const char* local = NULL;
637
+ const char* prefix = NULL;
638
+ const char* uri = NULL;
372
639
  leptris_element_expanded_name((LeptrisElement)n, &local, &prefix,
373
640
  &uri);
374
641
  if (!xsd_is_declaration(local, uri)) continue;
375
642
  s->declaration_count++;
643
+
644
+ if (strcmp(local, "include") == 0 ||
645
+ strcmp(local, "import") == 0 ||
646
+ strcmp(local, "redefine") == 0) {
647
+ const char* loc = leptris_element_attribute(
648
+ (LeptrisElement)n, "schemaLocation");
649
+ if (loc && s->base_dir && s->include_depth < 16) {
650
+ char path[4096];
651
+ snprintf(path, sizeof(path), "%s/%s", s->base_dir,
652
+ loc);
653
+ size_t flen = 0;
654
+ char* text = xsd_read_file(path, &flen);
655
+ if (text) {
656
+ LeptrisDocument sub =
657
+ leptris_parse_string(text, flen, NULL);
658
+ if (sub) {
659
+ LeptrisElement subroot =
660
+ leptris_document_root(sub);
661
+ const char* tns =
662
+ subroot
663
+ ? leptris_element_attribute(
664
+ subroot, "targetNamespace")
665
+ : NULL;
666
+ /* same-namespace merges and no-namespace
667
+ * chameleon includes; foreign-namespace
668
+ * imports stay lax at this tier */
669
+ if (subroot &&
670
+ (!tns || !s->target_ns ||
671
+ strcmp(tns, s->target_ns) == 0)) {
672
+ s->include_depth++;
673
+ xsd_capture_root(s, subroot);
674
+ s->include_depth--;
675
+ }
676
+ leptris_document_free(sub);
677
+ }
678
+ free(text);
679
+ }
680
+ }
681
+ continue;
682
+ }
683
+
376
684
  if (strcmp(local, "element") == 0 && uri && !strcmp(uri, XSD_NS)) {
377
685
  const char* nm = leptris_element_attribute((LeptrisElement)n,
378
686
  "name");
@@ -384,10 +692,23 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
384
692
  if (d) {
385
693
  d->name = xsd_strdup(nm);
386
694
  d->type = xsd_strdup(ty);
695
+ d->fixed = xsd_strdup(
696
+ leptris_element_attribute((LeptrisElement)n,
697
+ "fixed"));
698
+ const char* sg = leptris_element_attribute(
699
+ (LeptrisElement)n, "substitutionGroup");
700
+ if (sg) {
701
+ const char* colon = strchr(sg, ':');
702
+ d->sub_head = xsd_strdup(
703
+ colon ? colon + 1 : sg);
704
+ }
387
705
  d->next = s->elements;
388
706
  s->elements = d;
389
707
  }
708
+ xsd_capture_ics(s, (LeptrisElement)n, nm);
390
709
  } else if (nm) {
710
+ /* slice 6: identity constraints ride the element. */
711
+ xsd_capture_ics(s, (LeptrisElement)n, nm);
391
712
  /* #1592: inline anonymous type — capture under the
392
713
  * synthesized slot and point the declaration at it. */
393
714
  char synth[512];
@@ -405,57 +726,54 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
405
726
  }
406
727
  } else if (strcmp(local, "complexType") == 0 && uri &&
407
728
  !strcmp(uri, XSD_NS)) {
729
+ const char* nm =
730
+ leptris_element_attribute((LeptrisElement)n, "name");
731
+ if (nm)
732
+ xsd_capture_complex_model(s, (LeptrisElement)n, nm);
733
+ } else if (strcmp(local, "attribute") == 0 && uri && !strcmp(uri, XSD_NS)) {
408
734
  const char* nm =
409
735
  leptris_element_attribute((LeptrisElement)n, "name");
410
736
  if (nm) {
411
- XsdCm* root =
412
- xsd_cm_new(XSD_CM_SEQ); /* wrapper keeps the name */
413
- if (root) {
414
- root->name = xsd_strdup(nm);
415
- root->first_child =
416
- xsd_cm_parse(s, (LeptrisElement)n, NULL);
417
- root->next = s->complex_types;
418
- s->complex_types = root;
419
- }
420
- /* slice 4: attribute declarations of the type */
421
- XsdAttrDecl* head = NULL;
422
- XsdAttrDecl* tail = NULL;
423
- for (LeptrisNodeRef ac = leptris_node_first_child(
424
- leptris_element_as_node((LeptrisElement)n));
425
- ac; ac = leptris_node_next_sibling(ac)) {
426
- if (leptris_node_get_type(ac) !=
427
- LEPTRIS_NODE_TYPE_ELEMENT)
428
- continue;
429
- LeptrisElement ae = (LeptrisElement)ac;
430
- const char* al = NULL, *ap = NULL, *au = NULL;
431
- leptris_element_expanded_name(ae, &al, &ap, &au);
432
- if (!al || (au && strcmp(au, XSD_NS) != 0)) continue;
433
- if (strcmp(al, "attribute") != 0) continue;
434
- const char* an = leptris_element_attribute(ae, "name");
435
- if (!an) continue; /* ref forms: slice 5 */
436
- XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
437
- if (!d) continue;
438
- d->name = xsd_strdup(an);
737
+ XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
738
+ if (d) {
739
+ d->name = xsd_strdup(nm);
439
740
  d->type = xsd_strdup(
440
- leptris_element_attribute(ae, "type"));
441
- const char* use =
442
- leptris_element_attribute(ae, "use");
741
+ leptris_element_attribute((LeptrisElement)n,
742
+ "type"));
743
+ d->fixed = xsd_strdup(
744
+ leptris_element_attribute((LeptrisElement)n,
745
+ "fixed"));
746
+ const char* use = leptris_element_attribute(
747
+ (LeptrisElement)n, "use");
443
748
  d->required = (use && strcmp(use, "required") == 0);
444
- if (tail)
445
- tail->next = d;
446
- else
447
- head = d;
448
- tail = d;
749
+ d->next = s->top_attrs;
750
+ s->top_attrs = d;
449
751
  }
450
- if (head) {
451
- XsdTypeAttrs* ta =
452
- (XsdTypeAttrs*)calloc(1, sizeof(*ta));
453
- if (ta) {
454
- ta->type_name = xsd_strdup(nm);
455
- ta->attrs = head;
456
- ta->next = s->type_attrs;
457
- s->type_attrs = ta;
458
- }
752
+ }
753
+ } else if (strcmp(local, "attributeGroup") == 0 && uri && !strcmp(uri, XSD_NS)) {
754
+ const char* nm =
755
+ leptris_element_attribute((LeptrisElement)n, "name");
756
+ if (nm) {
757
+ XsdAttrGroupDef* g =
758
+ (XsdAttrGroupDef*)calloc(1, sizeof(*g));
759
+ if (g) {
760
+ g->name = xsd_strdup(nm);
761
+ g->attrs = xsd_capture_attr_rows(
762
+ s, (LeptrisElement)n, 0);
763
+ g->next = s->attr_groups;
764
+ s->attr_groups = g;
765
+ }
766
+ }
767
+ } else if (strcmp(local, "group") == 0 && uri && !strcmp(uri, XSD_NS)) {
768
+ const char* nm =
769
+ leptris_element_attribute((LeptrisElement)n, "name");
770
+ if (nm) {
771
+ XsdGroupDef* g = (XsdGroupDef*)calloc(1, sizeof(*g));
772
+ if (g) {
773
+ g->name = xsd_strdup(nm);
774
+ g->model = xsd_cm_parse(s, (LeptrisElement)n, NULL);
775
+ g->next = s->groups;
776
+ s->groups = g;
459
777
  }
460
778
  }
461
779
  } else if (strcmp(local, "simpleType") == 0 && uri && !strcmp(uri, XSD_NS)) {
@@ -466,22 +784,57 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
466
784
  }
467
785
  }
468
786
  }
787
+ }
469
788
 
470
- leptris_document_free(doc);
471
- return s;
472
-
473
- fail:
474
- if (doc) leptris_document_free(doc);
475
- if (status) *status = LEPTRIS_ERROR_PARSE;
476
- /* Failure returns an error-carrying handle (the documented
477
- * contract): leptris_xsd_error says why, free it the same way. */
478
- return (LeptrisXsdSchema)s;
789
+ static void xsd_free_simple_one(XsdSimple* t) {
790
+ if (!t) return;
791
+ free(t->name);
792
+ free(t->base);
793
+ free(t->pattern);
794
+ XsdFacetValue* fv = t->enum_values;
795
+ while (fv) {
796
+ XsdFacetValue* fn = fv->next;
797
+ free(fv->text);
798
+ free(fv);
799
+ fv = fn;
800
+ }
801
+ free(t->min_inclusive);
802
+ free(t->min_exclusive);
803
+ free(t->max_inclusive);
804
+ free(t->max_exclusive);
805
+ free(t->whitespace);
806
+ free(t->item_type);
807
+ xsd_free_simple_one(t->item_def);
808
+ for (size_t i = 0; i < t->member_count; i++) free(t->members[i]);
809
+ free(t->members);
810
+ for (size_t i = 0; i < t->member_def_count; i++)
811
+ xsd_free_simple_one(t->member_defs[i]);
812
+ free(t->member_defs);
813
+ free(t);
479
814
  }
480
815
 
481
816
  LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
482
817
  struct leptris_xsd_schema* s = (struct leptris_xsd_schema*)schema;
483
818
  if (!s) return;
484
819
  xsd_cm_free(s->complex_types);
820
+ XsdElementIcs* slot = s->element_ics;
821
+ while (slot) {
822
+ XsdElementIcs* sn = slot->next;
823
+ XsdIc* ic = slot->constraints;
824
+ while (ic) {
825
+ XsdIc* in = ic->next;
826
+ free(ic->name);
827
+ free(ic->selector);
828
+ for (size_t f = 0; f < ic->field_count; f++)
829
+ free(ic->fields[f]);
830
+ free(ic->refer);
831
+ free(ic);
832
+ ic = in;
833
+ }
834
+ free(slot->element_name);
835
+ free(slot);
836
+ slot = sn;
837
+ }
485
838
  XsdTypeAttrs* ta = s->type_attrs;
486
839
  while (ta) {
487
840
  XsdTypeAttrs* tn = ta->next;
@@ -490,6 +843,7 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
490
843
  XsdAttrDecl* an = a->next;
491
844
  free(a->name);
492
845
  free(a->type);
846
+ free(a->fixed);
493
847
  free(a);
494
848
  a = an;
495
849
  }
@@ -502,31 +856,52 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
502
856
  XsdElementDecl* dn = d->next;
503
857
  free(d->name);
504
858
  free(d->type);
859
+ free(d->fixed);
860
+ free(d->sub_head);
505
861
  free(d);
506
862
  d = dn;
507
863
  }
864
+ XsdAttrGroupDef* ag = s->attr_groups;
865
+ while (ag) {
866
+ XsdAttrGroupDef* agn = ag->next;
867
+ free(ag->name);
868
+ XsdAttrDecl* a = ag->attrs;
869
+ while (a) {
870
+ XsdAttrDecl* an2 = a->next;
871
+ free(a->name);
872
+ free(a->type);
873
+ free(a->fixed);
874
+ free(a);
875
+ a = an2;
876
+ }
877
+ free(ag);
878
+ ag = agn;
879
+ }
880
+ XsdAttrDecl* ta_row = s->top_attrs;
881
+ while (ta_row) {
882
+ XsdAttrDecl* ta_next = ta_row->next;
883
+ free(ta_row->name);
884
+ free(ta_row->type);
885
+ free(ta_row->fixed);
886
+ free(ta_row);
887
+ ta_row = ta_next;
888
+ }
889
+ XsdGroupDef* g = s->groups;
890
+ while (g) {
891
+ XsdGroupDef* gn = g->next;
892
+ free(g->name);
893
+ xsd_cm_free(g->model);
894
+ free(g);
895
+ g = gn;
896
+ }
508
897
  free(s->target_ns);
898
+ free(s->base_dir);
509
899
  if (s->validator)
510
900
  xsd_validator_free((struct xsd_validator*)s->validator);
511
901
  XsdSimple* t = s->simple_types;
512
902
  while (t) {
513
903
  XsdSimple* next = t->next;
514
- free(t->name);
515
- free(t->base);
516
- free(t->pattern);
517
- XsdFacetValue* fv = t->enum_values;
518
- while (fv) {
519
- XsdFacetValue* fn = fv->next;
520
- free(fv->text);
521
- free(fv);
522
- fv = fn;
523
- }
524
- free(t->min_inclusive);
525
- free(t->min_exclusive);
526
- free(t->max_inclusive);
527
- free(t->max_exclusive);
528
- free(t->whitespace);
529
- free(t);
904
+ xsd_free_simple_one(t);
530
905
  t = next;
531
906
  }
532
907
  free(s->error);
@@ -535,12 +910,376 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
535
910
 
536
911
  /* ---- slice 2: lexical validation --------------------------------- */
537
912
 
913
+ static XsdCm* xsd_cm_copy_list(const XsdCm* head) {
914
+ XsdCm* out = NULL;
915
+ XsdCm* out_tail = NULL;
916
+ for (const XsdCm* src = head; src; src = src->next) {
917
+ XsdCm* d = (XsdCm*)calloc(1, sizeof(*d));
918
+ if (!d) break;
919
+ d->kind = src->kind;
920
+ d->name = xsd_strdup(src->name);
921
+ d->type = xsd_strdup(src->type);
922
+ d->ns = xsd_strdup(src->ns);
923
+ d->any_ns = xsd_strdup(src->any_ns);
924
+ d->process_skip = src->process_skip;
925
+ d->min = src->min;
926
+ d->max = src->max;
927
+ d->first_child = xsd_cm_copy_list(src->first_child);
928
+ if (out_tail)
929
+ out_tail->next = d;
930
+ else
931
+ out = d;
932
+ out_tail = d;
933
+ }
934
+ return out;
935
+ }
936
+
937
+ static void xsd_register_complex(struct leptris_xsd_schema* s,
938
+ XsdCm* root, XsdAttrDecl* attrs,
939
+ const char* name) {
940
+ if (!root) return;
941
+ root->name = xsd_strdup(name);
942
+ root->next = s->complex_types;
943
+ s->complex_types = root;
944
+ if (attrs) {
945
+ XsdTypeAttrs* ta = (XsdTypeAttrs*)calloc(1, sizeof(*ta));
946
+ if (ta) {
947
+ ta->type_name = xsd_strdup(name);
948
+ ta->attrs = attrs;
949
+ ta->next = s->type_attrs;
950
+ s->type_attrs = ta;
951
+ }
952
+ }
953
+ }
954
+
955
+ /* One complexType capture, shared by top-level declarations and
956
+ * inline anonymous types: content model + derivation
957
+ * (complexContent/simpleContent extension|restriction) + the
958
+ * attribute rows the derivation carries. */
959
+ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
960
+ LeptrisElement ct,
961
+ const char* name) {
962
+ XsdCm* root = xsd_cm_new(XSD_CM_SEQ); /* wrapper keeps the name */
963
+ if (!root) return NULL;
964
+ XsdAttrDecl* attrs = NULL;
965
+
966
+ for (LeptrisNodeRef n =
967
+ leptris_node_first_child(leptris_element_as_node(ct));
968
+ n; n = leptris_node_next_sibling(n)) {
969
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
970
+ continue;
971
+ LeptrisElement e = (LeptrisElement)n;
972
+ const char* local = NULL, *prefix = NULL, *uri = NULL;
973
+ leptris_element_expanded_name(e, &local, &prefix, &uri);
974
+ if (!local || !uri || strcmp(uri, XSD_NS) != 0) continue;
975
+
976
+ if (strcmp(local, "complexContent") == 0) {
977
+ /* extension|restriction child carries base + model */
978
+ for (LeptrisNodeRef q = leptris_node_first_child(
979
+ leptris_element_as_node(e));
980
+ q; q = leptris_node_next_sibling(q)) {
981
+ if (leptris_node_get_type(q) !=
982
+ LEPTRIS_NODE_TYPE_ELEMENT)
983
+ continue;
984
+ LeptrisElement dq = (LeptrisElement)q;
985
+ const char* dl = NULL;
986
+ leptris_element_expanded_name(dq, &dl, NULL, NULL);
987
+ if (!dl || (strcmp(dl, "extension") != 0 &&
988
+ strcmp(dl, "restriction") != 0))
989
+ continue;
990
+ const char* base =
991
+ leptris_element_attribute(dq, "base");
992
+ XsdCm* base_root =
993
+ (base && strncmp(base, "xs:", 3) != 0)
994
+ ? xsd_find_complex(s, base)
995
+ : NULL;
996
+ if (strcmp(dl, "extension") == 0) {
997
+ /* base content first, derived content after */
998
+ root->first_child = xsd_cm_copy_list(
999
+ base_root ? base_root->first_child : NULL);
1000
+ XsdCm* own_tail = root->first_child;
1001
+ while (own_tail && own_tail->next)
1002
+ own_tail = own_tail->next;
1003
+ XsdCm* own = xsd_cm_parse(s, dq, NULL);
1004
+ if (own_tail)
1005
+ own_tail->next = own;
1006
+ else if (!root->first_child)
1007
+ root->first_child = own;
1008
+ if (base_root) {
1009
+ const XsdTypeAttrs* bt = xsd_find_type_attrs(
1010
+ s, base);
1011
+ attrs = xsd_copy_attr_rows(bt ? bt->attrs
1012
+ : NULL);
1013
+ }
1014
+ XsdAttrDecl* own_attrs =
1015
+ xsd_capture_attr_rows(s, dq, 0);
1016
+ if (own_attrs) {
1017
+ XsdAttrDecl* at = attrs;
1018
+ while (at && at->next) at = at->next;
1019
+ if (at)
1020
+ at->next = own_attrs;
1021
+ else
1022
+ attrs = own_attrs;
1023
+ }
1024
+ } else {
1025
+ /* restriction: the derived model REPLACES */
1026
+ root->first_child = xsd_cm_parse(s, dq, NULL);
1027
+ attrs = xsd_capture_attr_rows(s, dq, 0);
1028
+ }
1029
+ }
1030
+ } else if (strcmp(local, "simpleContent") == 0) {
1031
+ for (LeptrisNodeRef q = leptris_node_first_child(
1032
+ leptris_element_as_node(e));
1033
+ q; q = leptris_node_next_sibling(q)) {
1034
+ if (leptris_node_get_type(q) !=
1035
+ LEPTRIS_NODE_TYPE_ELEMENT)
1036
+ continue;
1037
+ LeptrisElement dq = (LeptrisElement)q;
1038
+ const char* dl = NULL;
1039
+ leptris_element_expanded_name(dq, &dl, NULL, NULL);
1040
+ if (!dl || (strcmp(dl, "extension") != 0 &&
1041
+ strcmp(dl, "restriction") != 0))
1042
+ continue;
1043
+ const char* base =
1044
+ leptris_element_attribute(dq, "base");
1045
+ if (base) {
1046
+ if (strncmp(base, "xs:", 3) == 0) {
1047
+ root->text_type = xsd_strdup(base);
1048
+ } else {
1049
+ /* restriction facets under a local base
1050
+ * ride a synthesized "complex:NAME" slot */
1051
+ int has_facets = 0;
1052
+ for (LeptrisNodeRef f =
1053
+ leptris_node_first_child(
1054
+ leptris_element_as_node(dq));
1055
+ f; f = leptris_node_next_sibling(f)) {
1056
+ if (leptris_node_get_type(f) !=
1057
+ LEPTRIS_NODE_TYPE_ELEMENT)
1058
+ continue;
1059
+ const char* fl = NULL;
1060
+ leptris_element_expanded_name(
1061
+ (LeptrisElement)f, &fl, NULL, NULL);
1062
+ if (fl && strcmp(fl, "facet") != 0 &&
1063
+ strcmp(fl, "simpleType") != 0)
1064
+ has_facets = 1;
1065
+ }
1066
+ XsdSimple* captured =
1067
+ has_facets
1068
+ ? xsd_capture_simple(dq)
1069
+ : NULL;
1070
+ if (captured) {
1071
+ char synth[512];
1072
+ snprintf(synth, sizeof(synth),
1073
+ "complex:%s", name);
1074
+ free(captured->name);
1075
+ captured->name = xsd_strdup(synth);
1076
+ captured->next = s->simple_types;
1077
+ s->simple_types = captured;
1078
+ root->text_type = xsd_strdup(synth);
1079
+ } else {
1080
+ root->text_type = xsd_strdup(base);
1081
+ }
1082
+ }
1083
+ }
1084
+ attrs = xsd_capture_attr_rows(s, dq, 0);
1085
+ }
1086
+ }
1087
+ }
1088
+ if (!root->first_child && !root->text_type) {
1089
+ /* no derivation: the direct-content spelling. A derivation
1090
+ * that captured attributes but produced no element model
1091
+ * (e.g. an attributes-only restriction) already set them —
1092
+ * re-capturing would orphan those rows. */
1093
+ root->first_child = xsd_cm_parse(s, ct, NULL);
1094
+ if (!attrs) attrs = xsd_capture_attr_rows(s, ct, 0);
1095
+ }
1096
+ /* the NFA and model walkers consume ONE root particle —
1097
+ * a multi-particle model (derivation splices) normalizes
1098
+ * under a sequence */
1099
+ if (root->first_child && root->first_child->next) {
1100
+ XsdCm* seq = xsd_cm_new(XSD_CM_SEQ);
1101
+ if (seq) {
1102
+ seq->first_child = root->first_child;
1103
+ root->first_child = seq;
1104
+ }
1105
+ }
1106
+ xsd_register_complex(s, root, attrs, name);
1107
+ return root;
1108
+ }
1109
+
1110
+ static const XsdElementDecl* xsd_find_element_decl(
1111
+ struct leptris_xsd_schema* s, const char* name) {
1112
+ for (XsdElementDecl* d = s->elements; d; d = d->next)
1113
+ if (strcmp(d->name, name) == 0) return d;
1114
+ return NULL;
1115
+ }
1116
+
1117
+ int xsd_is_substitute(struct leptris_xsd_schema* s,
1118
+ const char* name, const char* head) {
1119
+ const XsdElementDecl* d = xsd_find_element_decl(s, name);
1120
+ for (int hops = 0; d && d->sub_head && hops < 16; hops++) {
1121
+ if (strcmp(d->sub_head, head) == 0) return 1;
1122
+ d = xsd_find_element_decl(s, d->sub_head);
1123
+ }
1124
+ return 0;
1125
+ }
1126
+
1127
+ XsdGroupDef* xsd_find_group(struct leptris_xsd_schema* s,
1128
+ const char* name) {
1129
+ if (!s || !name) return NULL;
1130
+ for (XsdGroupDef* g = s->groups; g; g = g->next)
1131
+ if (strcmp(g->name, name) == 0) return g;
1132
+ return NULL;
1133
+ }
1134
+
1135
+ static const XsdAttrDecl* xsd_find_top_attr(
1136
+ struct leptris_xsd_schema* s, const char* name) {
1137
+ if (!s || !name) return NULL;
1138
+ for (XsdAttrDecl* a = s->top_attrs; a; a = a->next)
1139
+ if (strcmp(a->name, name) == 0) return a;
1140
+ return NULL;
1141
+ }
1142
+
1143
+ /* Deep-copy a captured row chain (attributeGroup refs splice
1144
+ * copies — every referencing type owns its rows). */
1145
+ static XsdAttrDecl* xsd_copy_attr_rows(const XsdAttrDecl* src) {
1146
+ XsdAttrDecl* head = NULL;
1147
+ XsdAttrDecl* tail = NULL;
1148
+ for (; src; src = src->next) {
1149
+ XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
1150
+ if (!d) break;
1151
+ d->name = xsd_strdup(src->name);
1152
+ d->type = xsd_strdup(src->type);
1153
+ d->fixed = xsd_strdup(src->fixed);
1154
+ d->required = src->required;
1155
+ if (tail)
1156
+ tail->next = d;
1157
+ else
1158
+ head = d;
1159
+ tail = d;
1160
+ }
1161
+ return head;
1162
+ }
1163
+
1164
+ /* Capture the attribute rows of a complexType or attributeGroup:
1165
+ * named rows, inline anonymous simpleTypes (synthesized slots),
1166
+ * refs to top-level attributes, and attributeGroup refs. */
1167
+ /* Named row with an inline anonymous simpleType rides a
1168
+ * synthesized "attribute:NAME" slot, the element-slot pattern. */
1169
+ static const char* xsd_inline_attr_type(
1170
+ struct leptris_xsd_schema* s, LeptrisElement ae,
1171
+ const char* attr_name) {
1172
+ for (LeptrisNodeRef q = leptris_node_first_child(
1173
+ leptris_element_as_node(ae));
1174
+ q; q = leptris_node_next_sibling(q)) {
1175
+ if (leptris_node_get_type(q) != LEPTRIS_NODE_TYPE_ELEMENT)
1176
+ continue;
1177
+ LeptrisElement qe = (LeptrisElement)q;
1178
+ const char* ql = NULL;
1179
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
1180
+ if (!ql || strcmp(ql, "simpleType") != 0) continue;
1181
+ char synth[512];
1182
+ snprintf(synth, sizeof(synth), "attribute:%s", attr_name);
1183
+ XsdSimple* captured = xsd_capture_simple(qe);
1184
+ if (!captured) return NULL;
1185
+ free(captured->name);
1186
+ captured->name = xsd_strdup(synth);
1187
+ captured->next = s->simple_types;
1188
+ s->simple_types = captured;
1189
+ return captured->name;
1190
+ }
1191
+ return NULL;
1192
+ }
1193
+
1194
+ static XsdAttrDecl* xsd_capture_attr_rows(struct leptris_xsd_schema* s,
1195
+ LeptrisElement container,
1196
+ int depth) {
1197
+ XsdAttrDecl* head = NULL;
1198
+ XsdAttrDecl* tail = NULL;
1199
+ if (depth > 8) return NULL;
1200
+ for (LeptrisNodeRef ac = leptris_node_first_child(
1201
+ leptris_element_as_node(container));
1202
+ ac; ac = leptris_node_next_sibling(ac)) {
1203
+ if (leptris_node_get_type(ac) != LEPTRIS_NODE_TYPE_ELEMENT)
1204
+ continue;
1205
+ LeptrisElement ae = (LeptrisElement)ac;
1206
+ const char* al = NULL, *ap = NULL, *au = NULL;
1207
+ leptris_element_expanded_name(ae, &al, &ap, &au);
1208
+ if (!al || (au && strcmp(au, XSD_NS) != 0)) continue;
1209
+
1210
+ if (strcmp(al, "attribute") == 0) {
1211
+ const char* an = leptris_element_attribute(ae, "name");
1212
+ const char* use = leptris_element_attribute(ae, "use");
1213
+ XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
1214
+ if (!d) continue;
1215
+ if (an) {
1216
+ d->name = xsd_strdup(an);
1217
+ d->type = xsd_strdup(
1218
+ leptris_element_attribute(ae, "type"));
1219
+ if (!d->type)
1220
+ d->type = xsd_strdup(
1221
+ xsd_inline_attr_type(s, ae, an));
1222
+ d->fixed = xsd_strdup(
1223
+ leptris_element_attribute(ae, "fixed"));
1224
+ d->required = (use && strcmp(use, "required") == 0);
1225
+ } else {
1226
+ /* ref to a top-level attribute declaration */
1227
+ const char* ref =
1228
+ leptris_element_attribute(ae, "ref");
1229
+ const XsdAttrDecl* src =
1230
+ ref ? xsd_find_top_attr(s, ref) : NULL;
1231
+ if (!src) {
1232
+ free(d);
1233
+ continue;
1234
+ }
1235
+ d->name = xsd_strdup(src->name);
1236
+ d->type = xsd_strdup(src->type);
1237
+ d->fixed = xsd_strdup(src->fixed);
1238
+ d->required = use
1239
+ ? (strcmp(use, "required") == 0)
1240
+ : src->required;
1241
+ }
1242
+ if (tail)
1243
+ tail->next = d;
1244
+ else
1245
+ head = d;
1246
+ tail = d;
1247
+ } else if (strcmp(al, "attributeGroup") == 0) {
1248
+ const char* ref =
1249
+ leptris_element_attribute(ae, "ref");
1250
+ if (!ref) continue;
1251
+ XsdAttrGroupDef* g = s->attr_groups;
1252
+ for (; g; g = g->next)
1253
+ if (strcmp(g->name, ref) == 0) break;
1254
+ if (!g || !g->attrs) continue;
1255
+ XsdAttrDecl* copied = xsd_copy_attr_rows(g->attrs);
1256
+ if (!copied) continue;
1257
+ if (!head)
1258
+ head = copied;
1259
+ else
1260
+ tail->next = copied;
1261
+ tail = copied;
1262
+ while (tail->next) tail = tail->next;
1263
+ }
1264
+ }
1265
+ return head;
1266
+ }
1267
+
538
1268
  LEPTRIS_API int leptris_xsd_builtin_valid(const char* builtin,
539
1269
  const char* lexical) {
540
1270
  extern int xsd_builtin_valid(const char* type, const char* v);
541
1271
  return xsd_builtin_valid(builtin, lexical);
542
1272
  }
543
1273
 
1274
+ /* The instance validator routes every user simpleType through the
1275
+ * chain walker so list/union derivations dispatch with the schema
1276
+ * in hand (the bare xsd_simple_valid cannot resolve item/member
1277
+ * names). */
1278
+ int xsd_simple_valid_chain(struct leptris_xsd_schema* s,
1279
+ const XsdSimple* t, const char* v) {
1280
+ return xsd_valid_chain(s, t, v, 0);
1281
+ }
1282
+
544
1283
  LEPTRIS_API int leptris_xsd_simple_valid(LeptrisXsdSchema schema,
545
1284
  const char* type_name,
546
1285
  const char* lexical) {
@@ -551,6 +1290,146 @@ LEPTRIS_API int leptris_xsd_simple_valid(LeptrisXsdSchema schema,
551
1290
  return xsd_valid_chain(s, t, lexical, 0);
552
1291
  }
553
1292
 
1293
+ /* ---- slice 6: identity-constraint capture ------------------------ */
1294
+
1295
+ /* The xs:selector child's @xpath (a child element, not an
1296
+ * attribute of the constraint element itself). */
1297
+ static char* xsd_ic_selector_xpath(LeptrisElement constraint) {
1298
+ for (LeptrisNodeRef n =
1299
+ leptris_node_first_child(leptris_element_as_node(constraint));
1300
+ n; n = leptris_node_next_sibling(n)) {
1301
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1302
+ continue;
1303
+ LeptrisElement e = (LeptrisElement)n;
1304
+ const char* local = NULL, *prefix = NULL, *uri = NULL;
1305
+ leptris_element_expanded_name(e, &local, &prefix, &uri);
1306
+ if (!local || !uri || strcmp(uri, XSD_NS) != 0) continue;
1307
+ if (strcmp(local, "selector") == 0)
1308
+ return xsd_strdup(leptris_element_attribute(e, "xpath"));
1309
+ }
1310
+ return NULL;
1311
+ }
1312
+
1313
+ void xsd_capture_ics(struct leptris_xsd_schema* s,
1314
+ LeptrisElement elem, const char* element_name) {
1315
+ XsdIc* head = NULL;
1316
+ XsdIc* tail = NULL;
1317
+ /* ICs are direct children of xs:element, or children of its
1318
+ * inline complexType — one hop covers the second spelling. */
1319
+ for (int hop = 0; hop < 2; hop++) {
1320
+ for (LeptrisNodeRef n =
1321
+ leptris_node_first_child(leptris_element_as_node(elem));
1322
+ n; n = leptris_node_next_sibling(n)) {
1323
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1324
+ continue;
1325
+ LeptrisElement e = (LeptrisElement)n;
1326
+ const char* local = NULL, *prefix = NULL, *uri = NULL;
1327
+ leptris_element_expanded_name(e, &local, &prefix, &uri);
1328
+ if (!local || !uri || strcmp(uri, XSD_NS) != 0) continue;
1329
+ if (hop == 0 &&
1330
+ (strcmp(local, "key") == 0 ||
1331
+ strcmp(local, "unique") == 0 ||
1332
+ strcmp(local, "keyref") == 0)) {
1333
+ XsdIc* ic = (XsdIc*)calloc(1, sizeof(*ic));
1334
+ if (!ic) continue;
1335
+ ic->kind = strcmp(local, "key") == 0 ? 0
1336
+ : strcmp(local, "unique") == 0 ? 1
1337
+ : 2;
1338
+ ic->name = xsd_strdup(
1339
+ leptris_element_attribute(e, "name"));
1340
+ ic->selector = xsd_ic_selector_xpath(e);
1341
+ ic->refer = xsd_strdup(
1342
+ leptris_element_attribute(e, "refer"));
1343
+ for (LeptrisNodeRef fc = leptris_node_first_child(
1344
+ leptris_element_as_node(e));
1345
+ fc && ic->field_count < 8;
1346
+ fc = leptris_node_next_sibling(fc)) {
1347
+ if (leptris_node_get_type(fc) !=
1348
+ LEPTRIS_NODE_TYPE_ELEMENT)
1349
+ continue;
1350
+ LeptrisElement fe = (LeptrisElement)fc;
1351
+ const char* fl = NULL, *fp = NULL, *fu = NULL;
1352
+ leptris_element_expanded_name(fe, &fl, &fp, &fu);
1353
+ if (!fl || !fu || strcmp(fu, XSD_NS) != 0) continue;
1354
+ if (strcmp(fl, "field") != 0) continue;
1355
+ ic->fields[ic->field_count++] = xsd_strdup(
1356
+ leptris_element_attribute(fe, "xpath"));
1357
+ }
1358
+ if (tail)
1359
+ tail->next = ic;
1360
+ else
1361
+ head = ic;
1362
+ tail = ic;
1363
+ } else if (hop == 0 && strcmp(local, "complexType") == 0) {
1364
+ /* descend into the inline type's ICs */
1365
+ for (LeptrisNodeRef ic_n = leptris_node_first_child(
1366
+ leptris_element_as_node(e));
1367
+ ic_n; ic_n = leptris_node_next_sibling(ic_n)) {
1368
+ if (leptris_node_get_type(ic_n) !=
1369
+ LEPTRIS_NODE_TYPE_ELEMENT)
1370
+ continue;
1371
+ LeptrisElement ie = (LeptrisElement)ic_n;
1372
+ const char* il = NULL, *ip = NULL, *iu = NULL;
1373
+ leptris_element_expanded_name(ie, &il, &ip, &iu);
1374
+ if (!il || !iu || strcmp(iu, XSD_NS) != 0) continue;
1375
+ if (strcmp(il, "key") != 0 &&
1376
+ strcmp(il, "unique") != 0 &&
1377
+ strcmp(il, "keyref") != 0)
1378
+ continue;
1379
+ XsdIc* ic = (XsdIc*)calloc(1, sizeof(*ic));
1380
+ if (!ic) continue;
1381
+ ic->kind = strcmp(il, "key") == 0 ? 0
1382
+ : strcmp(il, "unique") == 0 ? 1
1383
+ : 2;
1384
+ ic->name = xsd_strdup(
1385
+ leptris_element_attribute(ie, "name"));
1386
+ ic->selector = xsd_ic_selector_xpath(ie);
1387
+ ic->refer = xsd_strdup(
1388
+ leptris_element_attribute(ie, "refer"));
1389
+ for (LeptrisNodeRef fc =
1390
+ leptris_node_first_child(
1391
+ leptris_element_as_node(ie));
1392
+ fc && ic->field_count < 8;
1393
+ fc = leptris_node_next_sibling(fc)) {
1394
+ if (leptris_node_get_type(fc) !=
1395
+ LEPTRIS_NODE_TYPE_ELEMENT)
1396
+ continue;
1397
+ LeptrisElement fe = (LeptrisElement)fc;
1398
+ const char* fl = NULL, *fp = NULL, *fu = NULL;
1399
+ leptris_element_expanded_name(fe, &fl, &fp,
1400
+ &fu);
1401
+ if (!fl || !fu || strcmp(fu, XSD_NS) != 0)
1402
+ continue;
1403
+ if (strcmp(fl, "field") != 0) continue;
1404
+ ic->fields[ic->field_count++] = xsd_strdup(
1405
+ leptris_element_attribute(fe, "xpath"));
1406
+ }
1407
+ if (tail)
1408
+ tail->next = ic;
1409
+ else
1410
+ head = ic;
1411
+ tail = ic;
1412
+ }
1413
+ }
1414
+ }
1415
+ }
1416
+ if (!head) return;
1417
+ XsdElementIcs* slot = (XsdElementIcs*)calloc(1, sizeof(*slot));
1418
+ if (!slot) return;
1419
+ slot->element_name = xsd_strdup(element_name);
1420
+ slot->constraints = head;
1421
+ slot->next = s->element_ics;
1422
+ s->element_ics = slot;
1423
+ }
1424
+
1425
+ const XsdElementIcs* xsd_find_element_ics(struct leptris_xsd_schema* s,
1426
+ const char* element_name) {
1427
+ if (!s || !element_name) return NULL;
1428
+ for (XsdElementIcs* t = s->element_ics; t; t = t->next)
1429
+ if (strcmp(t->element_name, element_name) == 0) return t;
1430
+ return NULL;
1431
+ }
1432
+
554
1433
  /* ---- validator accessors (validate.c) --------------------------- */
555
1434
 
556
1435
  const XsdElementDecl* xsd_find_element(struct leptris_xsd_schema* s,