leptris 1.9.324.0-arm-linux → 1.9.333.0-arm-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.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +42 -0
  3. data/Rakefile +1 -1
  4. data/lib/leptris/version.rb +1 -1
  5. data/lib/leptris/xml/cdata.rb +4 -0
  6. data/lib/leptris/xml/comment.rb +4 -0
  7. data/lib/leptris/xml/entity_reference.rb +4 -0
  8. data/lib/leptris/xml/ffi.rb +5 -0
  9. data/lib/leptris/xml/node.rb +1 -0
  10. data/lib/leptris/xml/processing_instruction.rb +4 -0
  11. data/lib/leptris/xml/text.rb +4 -0
  12. data/lib/leptris/xml/xsd.rb +18 -0
  13. data/lib/libleptris.so +0 -0
  14. data/vendor-src/README.md +1 -1
  15. data/vendor-src/libleptris/CHANGELOG.md +181 -0
  16. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  17. data/vendor-src/libleptris/cli/commands/validate.c +5 -7
  18. data/vendor-src/libleptris/src/include/leptris.h +8 -0
  19. data/vendor-src/libleptris/src/leptris/dom/node_public.c +1 -1
  20. data/vendor-src/libleptris/src/leptris/dom/text.c +4 -2
  21. data/vendor-src/libleptris/src/leptris/dom/text.h +7 -2
  22. data/vendor-src/libleptris/src/leptris/flat/direct_parse.c +32 -4
  23. data/vendor-src/libleptris/src/leptris/html/html_parse.c +2 -1
  24. data/vendor-src/libleptris/src/leptris/leptris.c +7 -2
  25. data/vendor-src/libleptris/src/leptris/xsd/compile.c +1000 -76
  26. data/vendor-src/libleptris/src/leptris/xsd/content_model.c +35 -2
  27. data/vendor-src/libleptris/src/leptris/xsd/validate.c +154 -23
  28. data/vendor-src/libleptris/src/leptris/xsd/xsd_internal.h +95 -3
  29. data/vendor-src/libleptris/src/leptris/xslt/xslt_exec.c +1 -1
  30. data/vendor-src/libleptris/test/CMakeLists.txt +5 -0
  31. data/vendor-src/libleptris/test/parser/test_parser.cpp +116 -0
  32. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +676 -4
  33. data/vendor-src/libleptris/vcpkg.json +1 -1
  34. metadata +2 -2
@@ -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>
@@ -34,9 +35,17 @@ struct leptris_xsd_schema {
34
35
  XsdSimple* simple_types;
35
36
  XsdElementDecl* elements;
36
37
  XsdCm* complex_types; /* name-carrying model roots */
38
+ XsdGroupDef* groups; /* top-level xs:group definitions */
39
+ XsdCm* default_open; /* xs:defaultOpenContent wildcard */
40
+ int default_oc_empty; /* appliesToEmpty="true" */
41
+ XsdAttrGroupDef* attr_groups;
42
+ XsdAttrDecl* top_attrs; /* top-level xs:attribute declarations */
37
43
  XsdTypeAttrs* type_attrs;
38
44
  XsdElementIcs* element_ics;
39
45
  char* target_ns;
46
+ int version_11; /* xs:schema @version = "1.1" */
47
+ char* base_dir; /* schemaLocation resolution (file compile) */
48
+ int include_depth; /* include/import recursion guard */
40
49
  void* validator; /* slice-4 parked error list */
41
50
  char* error;
42
51
  };
@@ -56,6 +65,30 @@ static XsdCm* xsd_cm_new(XsdCmKind kind);
56
65
  static void xsd_cm_occurrence(XsdCm* cm, LeptrisElement e);
57
66
  static XsdCm* xsd_cm_parse(struct leptris_xsd_schema* s,
58
67
  LeptrisElement group, XsdCm** tail_out);
68
+ static XsdAttrDecl* xsd_capture_attr_rows(struct leptris_xsd_schema* s,
69
+ LeptrisElement container,
70
+ int depth);
71
+ extern XsdCm* xsd_find_complex(struct leptris_xsd_schema* s,
72
+ const char* type_name);
73
+ extern const XsdTypeAttrs* xsd_find_type_attrs(
74
+ struct leptris_xsd_schema* s, const char* type_name);
75
+ static XsdCm* xsd_cm_copy_list(const XsdCm* head);
76
+ static void xsd_capture_root(struct leptris_xsd_schema* s,
77
+ LeptrisElement root);
78
+ static char* xsd_read_file(const char* path, size_t* out_len);
79
+ static XsdAttrDecl* xsd_copy_attr_rows(const XsdAttrDecl* src);
80
+ static void xsd_register_complex(struct leptris_xsd_schema* s,
81
+ XsdCm* root, XsdAttrDecl* attrs,
82
+ const char* name);
83
+ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
84
+ LeptrisElement ct,
85
+ const char* name);
86
+ static XsdAssert* xsd_capture_assert(struct leptris_xsd_schema* s,
87
+ LeptrisElement e);
88
+ static XsdCm* xsd_capture_any(LeptrisElement any_child);
89
+ static void xsd_capture_alternatives(struct leptris_xsd_schema* s,
90
+ LeptrisElement decl_elem,
91
+ XsdElementDecl* d);
59
92
  static XsdSimple* xsd_capture_simple(LeptrisElement st);
60
93
 
61
94
 
@@ -71,11 +104,21 @@ static XsdCm* xsd_cm_new(XsdCmKind kind) {
71
104
  static void xsd_cm_free(XsdCm* cm) {
72
105
  while (cm) {
73
106
  XsdCm* next = cm->next;
107
+ XsdAssert* a = cm->asserts;
108
+ while (a) {
109
+ XsdAssert* an = a->next;
110
+ if (a->compiled) leptris_xquery_free(a->compiled);
111
+ free(a->test);
112
+ free(a);
113
+ a = an;
114
+ }
115
+ if (cm->oc_owned && cm->open_any) xsd_cm_free(cm->open_any);
74
116
  xsd_cm_free(cm->first_child);
75
117
  free(cm->name);
76
118
  free(cm->type);
77
119
  free(cm->ns);
78
120
  free(cm->any_ns);
121
+ free(cm->text_type);
79
122
  free(cm);
80
123
  cm = next;
81
124
  }
@@ -107,13 +150,7 @@ static int xsd_capture_inline(struct leptris_xsd_schema* s,
107
150
  leptris_element_expanded_name(e, &local, &prefix, &uri);
108
151
  if (!local || !uri || strcmp(uri, XSD_NS) != 0) continue;
109
152
  if (strcmp(local, "complexType") == 0) {
110
- XsdCm* root = xsd_cm_new(XSD_CM_SEQ);
111
- if (!root) return 0;
112
- root->name = xsd_strdup(synth);
113
- root->first_child = xsd_cm_parse(s, e, NULL);
114
- root->next = s->complex_types;
115
- s->complex_types = root;
116
- return 1;
153
+ return xsd_capture_complex_model(s, e, synth) ? 1 : 0;
117
154
  }
118
155
  if (strcmp(local, "simpleType") == 0) {
119
156
  XsdSimple* captured = xsd_capture_simple(e);
@@ -168,6 +205,13 @@ static XsdCm* xsd_cm_parse(struct leptris_xsd_schema* s,
168
205
  cm->type = xsd_strdup(synth);
169
206
  }
170
207
  }
208
+ } else if (strcmp(local, "group") == 0) {
209
+ /* <xs:group ref="g"/> — expanded at NFA build so
210
+ * forward references need no capture order */
211
+ cm = xsd_cm_new(XSD_CM_GROUP_REF);
212
+ if (cm)
213
+ cm->name = xsd_strdup(
214
+ leptris_element_attribute(e, "ref"));
171
215
  } else if (strcmp(local, "any") == 0) {
172
216
  cm = xsd_cm_new(XSD_CM_ANY);
173
217
  if (cm) {
@@ -225,7 +269,81 @@ static XsdSimple* xsd_capture_simple(LeptrisElement st) {
225
269
  leptris_element_expanded_name(e, &local, &prefix, &uri);
226
270
  if (uri && strcmp(uri, XSD_NS) != 0) continue;
227
271
  if (!local) continue;
228
- if (strcmp(local, "restriction") == 0) {
272
+ if (strcmp(local, "list") == 0) {
273
+ simple->deriv = XSD_SIMPLE_LIST;
274
+ const char* it = leptris_element_attribute(e, "itemType");
275
+ if (it && *it) {
276
+ simple->item_type = xsd_strdup(it);
277
+ } else {
278
+ /* inline anonymous item type rides this capture */
279
+ for (LeptrisNodeRef q = leptris_node_first_child(
280
+ leptris_element_as_node(e));
281
+ q && !simple->item_def;
282
+ q = leptris_node_next_sibling(q)) {
283
+ if (leptris_node_get_type(q) !=
284
+ LEPTRIS_NODE_TYPE_ELEMENT)
285
+ continue;
286
+ LeptrisElement qe = (LeptrisElement)q;
287
+ const char* ql = NULL;
288
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
289
+ if (ql && strcmp(ql, "simpleType") == 0)
290
+ simple->item_def = xsd_capture_simple(qe);
291
+ }
292
+ }
293
+ } else if (strcmp(local, "union") == 0) {
294
+ simple->deriv = XSD_SIMPLE_UNION;
295
+ const char* mt =
296
+ leptris_element_attribute(e, "memberTypes");
297
+ if (mt && *mt) {
298
+ size_t n = 0;
299
+ for (const char* p = mt; *p;) {
300
+ while (*p && isspace((unsigned char)*p)) p++;
301
+ if (!*p) break;
302
+ while (*p && !isspace((unsigned char)*p)) p++;
303
+ n++;
304
+ }
305
+ if (n) {
306
+ simple->members = (char**)calloc(n, sizeof(char*));
307
+ if (simple->members) {
308
+ size_t i = 0;
309
+ const char* p = mt;
310
+ while (*p && i < n) {
311
+ while (*p && isspace((unsigned char)*p)) p++;
312
+ if (!*p) break;
313
+ const char* start = p;
314
+ while (*p && !isspace((unsigned char)*p)) p++;
315
+ size_t len = (size_t)(p - start);
316
+ char* tok = (char*)malloc(len + 1);
317
+ if (!tok) break;
318
+ memcpy(tok, start, len);
319
+ tok[len] = 0;
320
+ simple->members[i++] = tok;
321
+ }
322
+ simple->member_count = i;
323
+ }
324
+ }
325
+ }
326
+ /* inline anonymous members ride this capture */
327
+ for (LeptrisNodeRef q = leptris_node_first_child(
328
+ leptris_element_as_node(e));
329
+ q; q = leptris_node_next_sibling(q)) {
330
+ if (leptris_node_get_type(q) != LEPTRIS_NODE_TYPE_ELEMENT)
331
+ continue;
332
+ LeptrisElement qe = (LeptrisElement)q;
333
+ const char* ql = NULL;
334
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
335
+ if (!ql || strcmp(ql, "simpleType") != 0) continue;
336
+ XsdSimple* def = xsd_capture_simple(qe);
337
+ if (!def) continue;
338
+ XsdSimple** grown = (XsdSimple**)realloc(
339
+ simple->member_defs,
340
+ (simple->member_def_count + 1) *
341
+ sizeof(XsdSimple*));
342
+ if (!grown) break;
343
+ simple->member_defs = grown;
344
+ simple->member_defs[simple->member_def_count++] = def;
345
+ }
346
+ } else if (strcmp(local, "restriction") == 0) {
229
347
  simple->base =
230
348
  xsd_strdup(leptris_element_attribute(e, "base"));
231
349
  for (LeptrisNodeRef f = leptris_node_first_child(
@@ -270,6 +388,9 @@ static XsdSimple* xsd_capture_simple(LeptrisElement st) {
270
388
  simple->fraction_digits = val ? strtol(val, NULL, 10) : -1;
271
389
  else if (strcmp(fl, "whiteSpace") == 0)
272
390
  simple->whitespace = xsd_strdup(val);
391
+ else if (strcmp(fl, "assertion") == 0)
392
+ simple->assertion = xsd_strdup(
393
+ leptris_element_attribute(fe, "test"));
273
394
  }
274
395
  }
275
396
  }
@@ -287,6 +408,71 @@ static const XsdSimple* xsd_find_simple(struct leptris_xsd_schema* s,
287
408
  return NULL;
288
409
  }
289
410
 
411
+ extern int xsd_builtin_valid(const char* type, const char* v);
412
+ extern int rng_regex_matches(const char* pat, const char* text);
413
+ static int xsd_valid_chain(struct leptris_xsd_schema* s,
414
+ const XsdSimple* t, const char* v, int depth);
415
+
416
+ /* One list item or union member: a builtin reference, a local
417
+ * schema type (chain-walked), or an inline capture. Unresolved
418
+ * names accept (the compile-time leniency the corpus gate pins). */
419
+ static int xsd_item_valid(struct leptris_xsd_schema* s,
420
+ const char* name, const XsdSimple* def,
421
+ const char* v, int depth) {
422
+ if (name && strncmp(name, "xs:", 3) == 0)
423
+ return xsd_builtin_valid(name, v);
424
+ if (def) return xsd_valid_chain(s, def, v, depth + 1);
425
+ if (name) {
426
+ const XsdSimple* next = xsd_find_simple(s, name);
427
+ if (next) return xsd_valid_chain(s, next, v, depth + 1);
428
+ }
429
+ return 1;
430
+ }
431
+
432
+ static int xsd_deriv_valid(struct leptris_xsd_schema* s,
433
+ const XsdSimple* t, const char* v,
434
+ int depth) {
435
+ if (t->deriv == XSD_SIMPLE_LIST) {
436
+ char* dup = xsd_strdup(v);
437
+ if (!dup) return 0;
438
+ size_t n = 0;
439
+ int ok = 1;
440
+ for (char* p = dup; *p;) {
441
+ while (*p && isspace((unsigned char)*p)) p++;
442
+ if (!*p) break;
443
+ char* end = p;
444
+ while (*end && !isspace((unsigned char)*end)) end++;
445
+ char was = *end;
446
+ *end = 0;
447
+ if (ok && !xsd_item_valid(s, t->item_type, t->item_def, p,
448
+ depth))
449
+ ok = 0;
450
+ *end = was;
451
+ p = end;
452
+ n++;
453
+ }
454
+ free(dup);
455
+ /* length family counts ITEMS for lists (XSD semantics) */
456
+ if (ok && t->length >= 0 && (long)n != t->length) ok = 0;
457
+ if (ok && t->min_length >= 0 && (long)n < t->min_length) ok = 0;
458
+ if (ok && t->max_length >= 0 && (long)n > t->max_length) ok = 0;
459
+ /* pattern applies to the whole lexical */
460
+ if (ok && t->pattern && !rng_regex_matches(t->pattern, v))
461
+ ok = 0;
462
+ return ok;
463
+ }
464
+ if (t->deriv == XSD_SIMPLE_UNION) {
465
+ for (size_t i = 0; i < t->member_count; i++)
466
+ if (xsd_item_valid(s, t->members[i], NULL, v, depth))
467
+ return 1;
468
+ for (size_t i = 0; i < t->member_def_count; i++)
469
+ if (xsd_item_valid(s, NULL, t->member_defs[i], v, depth))
470
+ return 1;
471
+ return 0;
472
+ }
473
+ return xsd_simple_valid(t, v);
474
+ }
475
+
290
476
  static int xsd_valid_chain(struct leptris_xsd_schema* s,
291
477
  const XsdSimple* t, const char* v, int depth) {
292
478
  if (!t || depth > 32) return 0;
@@ -294,6 +480,8 @@ static int xsd_valid_chain(struct leptris_xsd_schema* s,
294
480
  const XsdSimple* next = xsd_find_simple(s, t->base);
295
481
  if (next && !xsd_valid_chain(s, next, v, depth + 1)) return 0;
296
482
  }
483
+ if (t->deriv != XSD_SIMPLE_RESTRICT)
484
+ return xsd_deriv_valid(s, t, v, depth);
297
485
  return xsd_simple_valid(t, v);
298
486
  }
299
487
 
@@ -314,6 +502,7 @@ static int xsd_is_declaration(const char* local, const char* uri) {
314
502
  static const char* names[] = {
315
503
  "element", "attribute", "simpleType", "complexType",
316
504
  "group", "attributeGroup", "notation", "include",
505
+ "defaultOpenContent",
317
506
  "import", "redefine", NULL
318
507
  };
319
508
  for (int i = 0; names[i]; i++)
@@ -321,9 +510,82 @@ static int xsd_is_declaration(const char* local, const char* uri) {
321
510
  return 0;
322
511
  }
323
512
 
513
+ static char* xsd_read_file(const char* path, size_t* out_len) {
514
+ *out_len = 0;
515
+ FILE* f = fopen(path, "rb");
516
+ if (!f) return NULL;
517
+ if (fseek(f, 0, SEEK_END) != 0) {
518
+ fclose(f);
519
+ return NULL;
520
+ }
521
+ long size = ftell(f);
522
+ if (size < 0 || fseek(f, 0, SEEK_SET) != 0) {
523
+ fclose(f);
524
+ return NULL;
525
+ }
526
+ char* buf = (char*)malloc((size_t)size + 1);
527
+ if (!buf) {
528
+ fclose(f);
529
+ return NULL;
530
+ }
531
+ size_t got = fread(buf, 1, (size_t)size, f);
532
+ fclose(f);
533
+ buf[got] = 0;
534
+ *out_len = got;
535
+ return buf;
536
+ }
537
+
538
+ static LeptrisXsdSchema xsd_compile_impl(const char* xsd_text,
539
+ size_t len,
540
+ const char* base_dir,
541
+ LeptrisStatus* status);
542
+
324
543
  LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
325
544
  size_t len,
326
545
  LeptrisStatus* status) {
546
+ return xsd_compile_impl(xsd_text, len, NULL, status);
547
+ }
548
+
549
+ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile_file(
550
+ const char* path, LeptrisStatus* status) {
551
+ if (status) *status = LEPTRIS_OK;
552
+ if (!path || !*path) {
553
+ if (status) *status = LEPTRIS_ERROR_PARSE;
554
+ return NULL;
555
+ }
556
+ size_t len = 0;
557
+ char* text = xsd_read_file(path, &len);
558
+ if (!text) {
559
+ if (status) *status = LEPTRIS_ERROR_PARSE;
560
+ return NULL;
561
+ }
562
+ /* relative schemaLocations resolve against the schema's own
563
+ * directory (both separators: Windows paths carry '\') */
564
+ const char* slash = strrchr(path, '/');
565
+ const char* bslash = strrchr(path, '\\');
566
+ if (bslash && (!slash || bslash > slash)) slash = bslash;
567
+ char* base_dir = NULL;
568
+ if (slash) {
569
+ size_t n = (size_t)(slash - path);
570
+ if (n == 0) n = 1; /* "/f.xsd" roots at "/" */
571
+ base_dir = (char*)malloc(n + 1);
572
+ if (base_dir) {
573
+ memcpy(base_dir, path, n);
574
+ base_dir[n] = 0;
575
+ }
576
+ } else {
577
+ base_dir = xsd_strdup(".");
578
+ }
579
+ LeptrisXsdSchema s = xsd_compile_impl(text, len, base_dir, status);
580
+ free(base_dir);
581
+ free(text);
582
+ return s;
583
+ }
584
+
585
+ static LeptrisXsdSchema xsd_compile_impl(const char* xsd_text,
586
+ size_t len,
587
+ const char* base_dir,
588
+ LeptrisStatus* status) {
327
589
  if (status) *status = LEPTRIS_OK;
328
590
  if (!xsd_text || len == 0) {
329
591
  if (status) *status = LEPTRIS_ERROR_PARSE;
@@ -336,6 +598,7 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
336
598
  if (status) *status = LEPTRIS_ERROR_MEMORY;
337
599
  return NULL;
338
600
  }
601
+ s->base_dir = xsd_strdup(base_dir);
339
602
 
340
603
  /* target namespace (xs:any ##other/##targetNamespace filters) */
341
604
  s->target_ns = NULL;
@@ -353,6 +616,10 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
353
616
  }
354
617
  s->target_ns = xsd_strdup(
355
618
  leptris_element_attribute(root, "targetNamespace"));
619
+ {
620
+ const char* ver = leptris_element_attribute(root, "version");
621
+ s->version_11 = (ver && strcmp(ver, "1.1") == 0);
622
+ }
356
623
 
357
624
  const char* local = NULL;
358
625
  const char* prefix = NULL;
@@ -367,15 +634,79 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
367
634
  goto fail;
368
635
  }
369
636
 
637
+ xsd_capture_root(s, root);
638
+
639
+ leptris_document_free(doc);
640
+ return s;
641
+
642
+ fail:
643
+ if (doc) leptris_document_free(doc);
644
+ if (status) *status = LEPTRIS_ERROR_PARSE;
645
+ /* Failure returns an error-carrying handle (the documented
646
+ * contract): leptris_xsd_error says why, free it the same way. */
647
+ return (LeptrisXsdSchema)s;
648
+ }
649
+
650
+ /* Every top-level declaration under an xs:schema root. xs:include
651
+ * / xs:import / xs:redefine resolve their schemaLocation against
652
+ * the owning schema's base_dir (string-compiled schemas have none
653
+ * and skip them); the referenced root's declarations merge into
654
+ * the same tables. */
655
+ static void xsd_capture_root(struct leptris_xsd_schema* s,
656
+ LeptrisElement root) {
370
657
  for (LeptrisNodeRef n =
371
658
  leptris_node_first_child(leptris_element_as_node(root));
372
659
  n; n = leptris_node_next_sibling(n)) {
373
660
  if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
374
661
  continue;
662
+ const char* local = NULL;
663
+ const char* prefix = NULL;
664
+ const char* uri = NULL;
375
665
  leptris_element_expanded_name((LeptrisElement)n, &local, &prefix,
376
666
  &uri);
377
667
  if (!xsd_is_declaration(local, uri)) continue;
378
668
  s->declaration_count++;
669
+
670
+ if (strcmp(local, "include") == 0 ||
671
+ strcmp(local, "import") == 0 ||
672
+ strcmp(local, "redefine") == 0) {
673
+ const char* loc = leptris_element_attribute(
674
+ (LeptrisElement)n, "schemaLocation");
675
+ if (loc && s->base_dir && s->include_depth < 16) {
676
+ char path[4096];
677
+ snprintf(path, sizeof(path), "%s/%s", s->base_dir,
678
+ loc);
679
+ size_t flen = 0;
680
+ char* text = xsd_read_file(path, &flen);
681
+ if (text) {
682
+ LeptrisDocument sub =
683
+ leptris_parse_string(text, flen, NULL);
684
+ if (sub) {
685
+ LeptrisElement subroot =
686
+ leptris_document_root(sub);
687
+ const char* tns =
688
+ subroot
689
+ ? leptris_element_attribute(
690
+ subroot, "targetNamespace")
691
+ : NULL;
692
+ /* same-namespace merges and no-namespace
693
+ * chameleon includes; foreign-namespace
694
+ * imports stay lax at this tier */
695
+ if (subroot &&
696
+ (!tns || !s->target_ns ||
697
+ strcmp(tns, s->target_ns) == 0)) {
698
+ s->include_depth++;
699
+ xsd_capture_root(s, subroot);
700
+ s->include_depth--;
701
+ }
702
+ leptris_document_free(sub);
703
+ }
704
+ free(text);
705
+ }
706
+ }
707
+ continue;
708
+ }
709
+
379
710
  if (strcmp(local, "element") == 0 && uri && !strcmp(uri, XSD_NS)) {
380
711
  const char* nm = leptris_element_attribute((LeptrisElement)n,
381
712
  "name");
@@ -387,6 +718,17 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
387
718
  if (d) {
388
719
  d->name = xsd_strdup(nm);
389
720
  d->type = xsd_strdup(ty);
721
+ xsd_capture_alternatives(s, (LeptrisElement)n, d);
722
+ d->fixed = xsd_strdup(
723
+ leptris_element_attribute((LeptrisElement)n,
724
+ "fixed"));
725
+ const char* sg = leptris_element_attribute(
726
+ (LeptrisElement)n, "substitutionGroup");
727
+ if (sg) {
728
+ const char* colon = strchr(sg, ':');
729
+ d->sub_head = xsd_strdup(
730
+ colon ? colon + 1 : sg);
731
+ }
390
732
  d->next = s->elements;
391
733
  s->elements = d;
392
734
  }
@@ -413,55 +755,75 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
413
755
  !strcmp(uri, XSD_NS)) {
414
756
  const char* nm =
415
757
  leptris_element_attribute((LeptrisElement)n, "name");
416
- if (nm) {
417
- XsdCm* root =
418
- xsd_cm_new(XSD_CM_SEQ); /* wrapper keeps the name */
419
- if (root) {
420
- root->name = xsd_strdup(nm);
421
- root->first_child =
422
- xsd_cm_parse(s, (LeptrisElement)n, NULL);
423
- root->next = s->complex_types;
424
- s->complex_types = root;
425
- }
426
- /* slice 4: attribute declarations of the type */
427
- XsdAttrDecl* head = NULL;
428
- XsdAttrDecl* tail = NULL;
429
- for (LeptrisNodeRef ac = leptris_node_first_child(
758
+ if (nm)
759
+ xsd_capture_complex_model(s, (LeptrisElement)n, nm);
760
+ } else if (strcmp(local, "defaultOpenContent") == 0 && uri &&
761
+ !strcmp(uri, XSD_NS)) {
762
+ if (s->version_11) {
763
+ const char* ape = leptris_element_attribute(
764
+ (LeptrisElement)n, "appliesToEmpty");
765
+ for (LeptrisNodeRef q = leptris_node_first_child(
430
766
  leptris_element_as_node((LeptrisElement)n));
431
- ac; ac = leptris_node_next_sibling(ac)) {
432
- if (leptris_node_get_type(ac) !=
767
+ q && !s->default_open;
768
+ q = leptris_node_next_sibling(q)) {
769
+ if (leptris_node_get_type(q) !=
433
770
  LEPTRIS_NODE_TYPE_ELEMENT)
434
771
  continue;
435
- LeptrisElement ae = (LeptrisElement)ac;
436
- const char* al = NULL, *ap = NULL, *au = NULL;
437
- leptris_element_expanded_name(ae, &al, &ap, &au);
438
- if (!al || (au && strcmp(au, XSD_NS) != 0)) continue;
439
- if (strcmp(al, "attribute") != 0) continue;
440
- const char* an = leptris_element_attribute(ae, "name");
441
- if (!an) continue; /* ref forms: slice 5 */
442
- XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
443
- if (!d) continue;
444
- d->name = xsd_strdup(an);
772
+ LeptrisElement qe = (LeptrisElement)q;
773
+ const char* ql = NULL;
774
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
775
+ if (ql && strcmp(ql, "any") == 0)
776
+ s->default_open = xsd_capture_any(qe);
777
+ }
778
+ if (s->default_open)
779
+ s->default_oc_empty =
780
+ (ape && strcmp(ape, "true") == 0);
781
+ }
782
+ continue;
783
+ } else if (strcmp(local, "attribute") == 0 && uri && !strcmp(uri, XSD_NS)) {
784
+ const char* nm =
785
+ leptris_element_attribute((LeptrisElement)n, "name");
786
+ if (nm) {
787
+ XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
788
+ if (d) {
789
+ d->name = xsd_strdup(nm);
445
790
  d->type = xsd_strdup(
446
- leptris_element_attribute(ae, "type"));
447
- const char* use =
448
- leptris_element_attribute(ae, "use");
791
+ leptris_element_attribute((LeptrisElement)n,
792
+ "type"));
793
+ d->fixed = xsd_strdup(
794
+ leptris_element_attribute((LeptrisElement)n,
795
+ "fixed"));
796
+ const char* use = leptris_element_attribute(
797
+ (LeptrisElement)n, "use");
449
798
  d->required = (use && strcmp(use, "required") == 0);
450
- if (tail)
451
- tail->next = d;
452
- else
453
- head = d;
454
- tail = d;
799
+ d->next = s->top_attrs;
800
+ s->top_attrs = d;
455
801
  }
456
- if (head) {
457
- XsdTypeAttrs* ta =
458
- (XsdTypeAttrs*)calloc(1, sizeof(*ta));
459
- if (ta) {
460
- ta->type_name = xsd_strdup(nm);
461
- ta->attrs = head;
462
- ta->next = s->type_attrs;
463
- s->type_attrs = ta;
464
- }
802
+ }
803
+ } else if (strcmp(local, "attributeGroup") == 0 && uri && !strcmp(uri, XSD_NS)) {
804
+ const char* nm =
805
+ leptris_element_attribute((LeptrisElement)n, "name");
806
+ if (nm) {
807
+ XsdAttrGroupDef* g =
808
+ (XsdAttrGroupDef*)calloc(1, sizeof(*g));
809
+ if (g) {
810
+ g->name = xsd_strdup(nm);
811
+ g->attrs = xsd_capture_attr_rows(
812
+ s, (LeptrisElement)n, 0);
813
+ g->next = s->attr_groups;
814
+ s->attr_groups = g;
815
+ }
816
+ }
817
+ } else if (strcmp(local, "group") == 0 && uri && !strcmp(uri, XSD_NS)) {
818
+ const char* nm =
819
+ leptris_element_attribute((LeptrisElement)n, "name");
820
+ if (nm) {
821
+ XsdGroupDef* g = (XsdGroupDef*)calloc(1, sizeof(*g));
822
+ if (g) {
823
+ g->name = xsd_strdup(nm);
824
+ g->model = xsd_cm_parse(s, (LeptrisElement)n, NULL);
825
+ g->next = s->groups;
826
+ s->groups = g;
465
827
  }
466
828
  }
467
829
  } else if (strcmp(local, "simpleType") == 0 && uri && !strcmp(uri, XSD_NS)) {
@@ -472,16 +834,35 @@ LEPTRIS_API LeptrisXsdSchema leptris_xsd_compile(const char* xsd_text,
472
834
  }
473
835
  }
474
836
  }
837
+ }
475
838
 
476
- leptris_document_free(doc);
477
- return s;
478
-
479
- fail:
480
- if (doc) leptris_document_free(doc);
481
- if (status) *status = LEPTRIS_ERROR_PARSE;
482
- /* Failure returns an error-carrying handle (the documented
483
- * contract): leptris_xsd_error says why, free it the same way. */
484
- return (LeptrisXsdSchema)s;
839
+ static void xsd_free_simple_one(XsdSimple* t) {
840
+ if (!t) return;
841
+ free(t->name);
842
+ free(t->base);
843
+ free(t->pattern);
844
+ XsdFacetValue* fv = t->enum_values;
845
+ while (fv) {
846
+ XsdFacetValue* fn = fv->next;
847
+ free(fv->text);
848
+ free(fv);
849
+ fv = fn;
850
+ }
851
+ free(t->min_inclusive);
852
+ free(t->min_exclusive);
853
+ free(t->max_inclusive);
854
+ free(t->max_exclusive);
855
+ free(t->whitespace);
856
+ if (t->assertion_q) leptris_xquery_free(t->assertion_q);
857
+ free(t->assertion);
858
+ free(t->item_type);
859
+ xsd_free_simple_one(t->item_def);
860
+ for (size_t i = 0; i < t->member_count; i++) free(t->members[i]);
861
+ free(t->members);
862
+ for (size_t i = 0; i < t->member_def_count; i++)
863
+ xsd_free_simple_one(t->member_defs[i]);
864
+ free(t->member_defs);
865
+ free(t);
485
866
  }
486
867
 
487
868
  LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
@@ -514,6 +895,7 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
514
895
  XsdAttrDecl* an = a->next;
515
896
  free(a->name);
516
897
  free(a->type);
898
+ free(a->fixed);
517
899
  free(a);
518
900
  a = an;
519
901
  }
@@ -524,33 +906,64 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
524
906
  XsdElementDecl* d = s->elements;
525
907
  while (d) {
526
908
  XsdElementDecl* dn = d->next;
909
+ XsdAlternative* a = d->alternatives;
910
+ while (a) {
911
+ XsdAlternative* an = a->next;
912
+ if (a->compiled) leptris_xquery_free(a->compiled);
913
+ free(a->test);
914
+ free(a->type);
915
+ free(a);
916
+ a = an;
917
+ }
527
918
  free(d->name);
528
919
  free(d->type);
920
+ free(d->fixed);
921
+ free(d->sub_head);
529
922
  free(d);
530
923
  d = dn;
531
924
  }
925
+ XsdAttrGroupDef* ag = s->attr_groups;
926
+ while (ag) {
927
+ XsdAttrGroupDef* agn = ag->next;
928
+ free(ag->name);
929
+ XsdAttrDecl* a = ag->attrs;
930
+ while (a) {
931
+ XsdAttrDecl* an2 = a->next;
932
+ free(a->name);
933
+ free(a->type);
934
+ free(a->fixed);
935
+ free(a);
936
+ a = an2;
937
+ }
938
+ free(ag);
939
+ ag = agn;
940
+ }
941
+ XsdAttrDecl* ta_row = s->top_attrs;
942
+ while (ta_row) {
943
+ XsdAttrDecl* ta_next = ta_row->next;
944
+ free(ta_row->name);
945
+ free(ta_row->type);
946
+ free(ta_row->fixed);
947
+ free(ta_row);
948
+ ta_row = ta_next;
949
+ }
950
+ XsdGroupDef* g = s->groups;
951
+ while (g) {
952
+ XsdGroupDef* gn = g->next;
953
+ free(g->name);
954
+ xsd_cm_free(g->model);
955
+ free(g);
956
+ g = gn;
957
+ }
958
+ if (s->default_open) xsd_cm_free(s->default_open);
532
959
  free(s->target_ns);
960
+ free(s->base_dir);
533
961
  if (s->validator)
534
962
  xsd_validator_free((struct xsd_validator*)s->validator);
535
963
  XsdSimple* t = s->simple_types;
536
964
  while (t) {
537
965
  XsdSimple* next = t->next;
538
- free(t->name);
539
- free(t->base);
540
- free(t->pattern);
541
- XsdFacetValue* fv = t->enum_values;
542
- while (fv) {
543
- XsdFacetValue* fn = fv->next;
544
- free(fv->text);
545
- free(fv);
546
- fv = fn;
547
- }
548
- free(t->min_inclusive);
549
- free(t->min_exclusive);
550
- free(t->max_inclusive);
551
- free(t->max_exclusive);
552
- free(t->whitespace);
553
- free(t);
966
+ xsd_free_simple_one(t);
554
967
  t = next;
555
968
  }
556
969
  free(s->error);
@@ -559,12 +972,523 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
559
972
 
560
973
  /* ---- slice 2: lexical validation --------------------------------- */
561
974
 
975
+ static XsdCm* xsd_cm_copy_list(const XsdCm* head) {
976
+ XsdCm* out = NULL;
977
+ XsdCm* out_tail = NULL;
978
+ for (const XsdCm* src = head; src; src = src->next) {
979
+ XsdCm* d = (XsdCm*)calloc(1, sizeof(*d));
980
+ if (!d) break;
981
+ d->kind = src->kind;
982
+ d->name = xsd_strdup(src->name);
983
+ d->type = xsd_strdup(src->type);
984
+ d->ns = xsd_strdup(src->ns);
985
+ d->any_ns = xsd_strdup(src->any_ns);
986
+ d->process_skip = src->process_skip;
987
+ d->min = src->min;
988
+ d->max = src->max;
989
+ d->first_child = xsd_cm_copy_list(src->first_child);
990
+ if (out_tail)
991
+ out_tail->next = d;
992
+ else
993
+ out = d;
994
+ out_tail = d;
995
+ }
996
+ return out;
997
+ }
998
+
999
+ static void xsd_register_complex(struct leptris_xsd_schema* s,
1000
+ XsdCm* root, XsdAttrDecl* attrs,
1001
+ const char* name) {
1002
+ if (!root) return;
1003
+ root->name = xsd_strdup(name);
1004
+ root->next = s->complex_types;
1005
+ s->complex_types = root;
1006
+ if (attrs) {
1007
+ XsdTypeAttrs* ta = (XsdTypeAttrs*)calloc(1, sizeof(*ta));
1008
+ if (ta) {
1009
+ ta->type_name = xsd_strdup(name);
1010
+ ta->attrs = attrs;
1011
+ ta->next = s->type_attrs;
1012
+ s->type_attrs = ta;
1013
+ }
1014
+ }
1015
+ }
1016
+
1017
+ /* XSD 1.1: xs:alternative children of an element declaration. */
1018
+ static void xsd_capture_alternatives(struct leptris_xsd_schema* s,
1019
+ LeptrisElement decl_elem,
1020
+ XsdElementDecl* d) {
1021
+ if (!s->version_11) return;
1022
+ XsdAlternative* tail = NULL;
1023
+ for (LeptrisNodeRef n =
1024
+ leptris_node_first_child(leptris_element_as_node(decl_elem));
1025
+ n; n = leptris_node_next_sibling(n)) {
1026
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1027
+ continue;
1028
+ LeptrisElement e = (LeptrisElement)n;
1029
+ const char* local = NULL;
1030
+ leptris_element_expanded_name(e, &local, NULL, NULL);
1031
+ if (!local || strcmp(local, "alternative") != 0) continue;
1032
+ const char* test = leptris_element_attribute(e, "test");
1033
+ const char* type = leptris_element_attribute(e, "type");
1034
+ if (!test || !type) continue;
1035
+ XsdAlternative* a = (XsdAlternative*)calloc(1, sizeof(*a));
1036
+ if (!a) continue;
1037
+ a->test = xsd_strdup(test);
1038
+ a->type = xsd_strdup(type);
1039
+ a->compiled = leptris_xquery_parse(test, strlen(test));
1040
+ if (tail)
1041
+ tail->next = a;
1042
+ else
1043
+ d->alternatives = a;
1044
+ tail = a;
1045
+ }
1046
+ }
1047
+
1048
+ static XsdCm* xsd_capture_any(LeptrisElement any_child) {
1049
+ XsdCm* w = xsd_cm_new(XSD_CM_ANY);
1050
+ if (!w) return NULL;
1051
+ w->any_ns = xsd_strdup(
1052
+ leptris_element_attribute(any_child, "namespace"));
1053
+ const char* pc = leptris_element_attribute(any_child,
1054
+ "processContents");
1055
+ w->process_skip =
1056
+ (pc && (strcmp(pc, "skip") == 0 || strcmp(pc, "lax") == 0))
1057
+ ? 1
1058
+ : 0;
1059
+ w->min = 0;
1060
+ w->max = -1;
1061
+ return w;
1062
+ }
1063
+
1064
+ static XsdAssert* xsd_capture_assert(struct leptris_xsd_schema* s,
1065
+ LeptrisElement e) {
1066
+ const char* test = leptris_element_attribute(e, "test");
1067
+ if (!test || !*test) return NULL;
1068
+ XsdAssert* a = (XsdAssert*)calloc(1, sizeof(*a));
1069
+ if (!a) return NULL;
1070
+ a->test = xsd_strdup(test);
1071
+ if (s->version_11) {
1072
+ a->compiled = leptris_xquery_parse(test, strlen(test));
1073
+ /* NULL = syntax error: evaluation reports the assertion
1074
+ * as failed (XSD 1.1: an error is a failure) */
1075
+ }
1076
+ return a;
1077
+ }
1078
+
1079
+ /* Append every xs:assert child under `container` to the type. */
1080
+ static void xsd_capture_assert_children(
1081
+ struct leptris_xsd_schema* s, LeptrisElement container,
1082
+ XsdCm* root) {
1083
+ XsdAssert* tail = root->asserts;
1084
+ while (tail && tail->next) tail = tail->next;
1085
+ for (LeptrisNodeRef n =
1086
+ leptris_node_first_child(leptris_element_as_node(container));
1087
+ n; n = leptris_node_next_sibling(n)) {
1088
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1089
+ continue;
1090
+ LeptrisElement e = (LeptrisElement)n;
1091
+ const char* local = NULL;
1092
+ leptris_element_expanded_name(e, &local, NULL, NULL);
1093
+ if (!local || strcmp(local, "assert") != 0) continue;
1094
+ XsdAssert* a = xsd_capture_assert(s, e);
1095
+ if (!a) continue;
1096
+ if (!root->asserts) root->asserts = a;
1097
+ tail = a;
1098
+ }
1099
+ }
1100
+
1101
+ /* One complexType capture, shared by top-level declarations and
1102
+ * inline anonymous types: content model + derivation
1103
+ * (complexContent/simpleContent extension|restriction) + the
1104
+ * attribute rows the derivation carries. */
1105
+ static XsdCm* xsd_capture_complex_model(struct leptris_xsd_schema* s,
1106
+ LeptrisElement ct,
1107
+ const char* name) {
1108
+ XsdCm* root = xsd_cm_new(XSD_CM_SEQ); /* wrapper keeps the name */
1109
+ if (!root) return NULL;
1110
+ XsdAttrDecl* attrs = NULL;
1111
+
1112
+ for (LeptrisNodeRef n =
1113
+ leptris_node_first_child(leptris_element_as_node(ct));
1114
+ n; n = leptris_node_next_sibling(n)) {
1115
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1116
+ continue;
1117
+ LeptrisElement e = (LeptrisElement)n;
1118
+ const char* local = NULL, *prefix = NULL, *uri = NULL;
1119
+ leptris_element_expanded_name(e, &local, &prefix, &uri);
1120
+ if (!local || !uri || strcmp(uri, XSD_NS) != 0) continue;
1121
+
1122
+ if (strcmp(local, "complexContent") == 0) {
1123
+ /* extension|restriction child carries base + model */
1124
+ for (LeptrisNodeRef q = leptris_node_first_child(
1125
+ leptris_element_as_node(e));
1126
+ q; q = leptris_node_next_sibling(q)) {
1127
+ if (leptris_node_get_type(q) !=
1128
+ LEPTRIS_NODE_TYPE_ELEMENT)
1129
+ continue;
1130
+ LeptrisElement dq = (LeptrisElement)q;
1131
+ const char* dl = NULL;
1132
+ leptris_element_expanded_name(dq, &dl, NULL, NULL);
1133
+ if (!dl || (strcmp(dl, "extension") != 0 &&
1134
+ strcmp(dl, "restriction") != 0))
1135
+ continue;
1136
+ const char* base =
1137
+ leptris_element_attribute(dq, "base");
1138
+ XsdCm* base_root =
1139
+ (base && strncmp(base, "xs:", 3) != 0)
1140
+ ? xsd_find_complex(s, base)
1141
+ : NULL;
1142
+ if (strcmp(dl, "extension") == 0) {
1143
+ /* base content first, derived content after */
1144
+ root->first_child = xsd_cm_copy_list(
1145
+ base_root ? base_root->first_child : NULL);
1146
+ XsdCm* own_tail = root->first_child;
1147
+ while (own_tail && own_tail->next)
1148
+ own_tail = own_tail->next;
1149
+ XsdCm* own = xsd_cm_parse(s, dq, NULL);
1150
+ if (own_tail)
1151
+ own_tail->next = own;
1152
+ else if (!root->first_child)
1153
+ root->first_child = own;
1154
+ if (base_root) {
1155
+ const XsdTypeAttrs* bt = xsd_find_type_attrs(
1156
+ s, base);
1157
+ attrs = xsd_copy_attr_rows(bt ? bt->attrs
1158
+ : NULL);
1159
+ }
1160
+ XsdAttrDecl* own_attrs =
1161
+ xsd_capture_attr_rows(s, dq, 0);
1162
+ if (own_attrs) {
1163
+ XsdAttrDecl* at = attrs;
1164
+ while (at && at->next) at = at->next;
1165
+ if (at)
1166
+ at->next = own_attrs;
1167
+ else
1168
+ attrs = own_attrs;
1169
+ }
1170
+ } else {
1171
+ /* restriction: the derived model REPLACES */
1172
+ root->first_child = xsd_cm_parse(s, dq, NULL);
1173
+ attrs = xsd_capture_attr_rows(s, dq, 0);
1174
+ }
1175
+ /* XSD 1.1: asserts nested under the derivation */
1176
+ if (s->version_11)
1177
+ xsd_capture_assert_children(s, dq, root);
1178
+ }
1179
+ } else if (strcmp(local, "simpleContent") == 0) {
1180
+ for (LeptrisNodeRef q = leptris_node_first_child(
1181
+ leptris_element_as_node(e));
1182
+ q; q = leptris_node_next_sibling(q)) {
1183
+ if (leptris_node_get_type(q) !=
1184
+ LEPTRIS_NODE_TYPE_ELEMENT)
1185
+ continue;
1186
+ LeptrisElement dq = (LeptrisElement)q;
1187
+ const char* dl = NULL;
1188
+ leptris_element_expanded_name(dq, &dl, NULL, NULL);
1189
+ if (!dl || (strcmp(dl, "extension") != 0 &&
1190
+ strcmp(dl, "restriction") != 0))
1191
+ continue;
1192
+ const char* base =
1193
+ leptris_element_attribute(dq, "base");
1194
+ if (base) {
1195
+ if (strncmp(base, "xs:", 3) == 0) {
1196
+ root->text_type = xsd_strdup(base);
1197
+ } else {
1198
+ /* restriction facets under a local base
1199
+ * ride a synthesized "complex:NAME" slot */
1200
+ int has_facets = 0;
1201
+ for (LeptrisNodeRef f =
1202
+ leptris_node_first_child(
1203
+ leptris_element_as_node(dq));
1204
+ f; f = leptris_node_next_sibling(f)) {
1205
+ if (leptris_node_get_type(f) !=
1206
+ LEPTRIS_NODE_TYPE_ELEMENT)
1207
+ continue;
1208
+ const char* fl = NULL;
1209
+ leptris_element_expanded_name(
1210
+ (LeptrisElement)f, &fl, NULL, NULL);
1211
+ if (fl && strcmp(fl, "facet") != 0 &&
1212
+ strcmp(fl, "simpleType") != 0)
1213
+ has_facets = 1;
1214
+ }
1215
+ XsdSimple* captured =
1216
+ has_facets
1217
+ ? xsd_capture_simple(dq)
1218
+ : NULL;
1219
+ if (captured) {
1220
+ char synth[512];
1221
+ snprintf(synth, sizeof(synth),
1222
+ "complex:%s", name);
1223
+ free(captured->name);
1224
+ captured->name = xsd_strdup(synth);
1225
+ captured->next = s->simple_types;
1226
+ s->simple_types = captured;
1227
+ root->text_type = xsd_strdup(synth);
1228
+ } else {
1229
+ root->text_type = xsd_strdup(base);
1230
+ }
1231
+ }
1232
+ }
1233
+ attrs = xsd_capture_attr_rows(s, dq, 0);
1234
+ }
1235
+ }
1236
+ }
1237
+ if (!root->first_child && !root->text_type) {
1238
+ /* no derivation: the direct-content spelling. A derivation
1239
+ * that captured attributes but produced no element model
1240
+ * (e.g. an attributes-only restriction) already set them —
1241
+ * re-capturing would orphan those rows. */
1242
+ root->first_child = xsd_cm_parse(s, ct, NULL);
1243
+ if (!attrs) attrs = xsd_capture_attr_rows(s, ct, 0);
1244
+ }
1245
+ /* XSD 1.1: openContent on the type, else the schema default */
1246
+ if (s->version_11) {
1247
+ for (LeptrisNodeRef n =
1248
+ leptris_node_first_child(leptris_element_as_node(ct));
1249
+ n; n = leptris_node_next_sibling(n)) {
1250
+ if (leptris_node_get_type(n) != LEPTRIS_NODE_TYPE_ELEMENT)
1251
+ continue;
1252
+ LeptrisElement e = (LeptrisElement)n;
1253
+ const char* ocl = NULL;
1254
+ leptris_element_expanded_name(e, &ocl, NULL, NULL);
1255
+ if (!ocl || strcmp(ocl, "openContent") != 0) continue;
1256
+ const char* mode = leptris_element_attribute(e, "mode");
1257
+ for (LeptrisNodeRef q = leptris_node_first_child(
1258
+ leptris_element_as_node(e));
1259
+ q && !root->open_any;
1260
+ q = leptris_node_next_sibling(q)) {
1261
+ if (leptris_node_get_type(q) !=
1262
+ LEPTRIS_NODE_TYPE_ELEMENT)
1263
+ continue;
1264
+ LeptrisElement qe = (LeptrisElement)q;
1265
+ const char* ql = NULL;
1266
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
1267
+ if (ql && strcmp(ql, "any") == 0) {
1268
+ root->open_any = xsd_capture_any(qe);
1269
+ if (root->open_any) root->oc_owned = 1;
1270
+ }
1271
+ }
1272
+ if (mode && strcmp(mode, "suffix") == 0)
1273
+ root->oc_mode = 1;
1274
+ else if (mode && strcmp(mode, "interleave") == 0)
1275
+ root->oc_mode = 2;
1276
+ else
1277
+ root->oc_mode = 0;
1278
+ }
1279
+ }
1280
+ if (!root->oc_mode && s->default_open && root->first_child)
1281
+ root->oc_mode = 2;
1282
+ if (!root->oc_mode && s->default_open && !root->first_child &&
1283
+ s->default_oc_empty)
1284
+ root->oc_mode = 2;
1285
+ if (root->oc_mode == 2 && !root->open_any && s->default_open)
1286
+ root->open_any = s->default_open;
1287
+ /* XSD 1.1: direct xs:assert children of the complexType */
1288
+ if (s->version_11) xsd_capture_assert_children(s, ct, root);
1289
+ /* the NFA and model walkers consume ONE root particle —
1290
+ * a multi-particle model (derivation splices) normalizes
1291
+ * under a sequence */
1292
+ if (root->first_child && root->first_child->next) {
1293
+ XsdCm* seq = xsd_cm_new(XSD_CM_SEQ);
1294
+ if (seq) {
1295
+ seq->first_child = root->first_child;
1296
+ root->first_child = seq;
1297
+ }
1298
+ }
1299
+ /* open-content semantics ride the model root (the NFA builds
1300
+ * from ct->first_child, never the name wrapper) */
1301
+ if (root->first_child) {
1302
+ root->first_child->open_any = root->open_any;
1303
+ root->first_child->oc_mode = root->oc_mode;
1304
+ root->first_child->oc_owned = root->oc_owned;
1305
+ root->open_any = NULL;
1306
+ root->oc_owned = 0;
1307
+ } else if (root->open_any && root->oc_owned) {
1308
+ xsd_cm_free(root->open_any);
1309
+ root->open_any = NULL;
1310
+ }
1311
+ xsd_register_complex(s, root, attrs, name);
1312
+ return root;
1313
+ }
1314
+
1315
+ static const XsdElementDecl* xsd_find_element_decl(
1316
+ struct leptris_xsd_schema* s, const char* name) {
1317
+ for (XsdElementDecl* d = s->elements; d; d = d->next)
1318
+ if (strcmp(d->name, name) == 0) return d;
1319
+ return NULL;
1320
+ }
1321
+
1322
+ int xsd_is_substitute(struct leptris_xsd_schema* s,
1323
+ const char* name, const char* head) {
1324
+ const XsdElementDecl* d = xsd_find_element_decl(s, name);
1325
+ for (int hops = 0; d && d->sub_head && hops < 16; hops++) {
1326
+ if (strcmp(d->sub_head, head) == 0) return 1;
1327
+ d = xsd_find_element_decl(s, d->sub_head);
1328
+ }
1329
+ return 0;
1330
+ }
1331
+
1332
+ int xsd_schema_version_11(struct leptris_xsd_schema* s) {
1333
+ return s ? s->version_11 : 0;
1334
+ }
1335
+
1336
+ XsdGroupDef* xsd_find_group(struct leptris_xsd_schema* s,
1337
+ const char* name) {
1338
+ if (!s || !name) return NULL;
1339
+ for (XsdGroupDef* g = s->groups; g; g = g->next)
1340
+ if (strcmp(g->name, name) == 0) return g;
1341
+ return NULL;
1342
+ }
1343
+
1344
+ static const XsdAttrDecl* xsd_find_top_attr(
1345
+ struct leptris_xsd_schema* s, const char* name) {
1346
+ if (!s || !name) return NULL;
1347
+ for (XsdAttrDecl* a = s->top_attrs; a; a = a->next)
1348
+ if (strcmp(a->name, name) == 0) return a;
1349
+ return NULL;
1350
+ }
1351
+
1352
+ /* Deep-copy a captured row chain (attributeGroup refs splice
1353
+ * copies — every referencing type owns its rows). */
1354
+ static XsdAttrDecl* xsd_copy_attr_rows(const XsdAttrDecl* src) {
1355
+ XsdAttrDecl* head = NULL;
1356
+ XsdAttrDecl* tail = NULL;
1357
+ for (; src; src = src->next) {
1358
+ XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
1359
+ if (!d) break;
1360
+ d->name = xsd_strdup(src->name);
1361
+ d->type = xsd_strdup(src->type);
1362
+ d->fixed = xsd_strdup(src->fixed);
1363
+ d->required = src->required;
1364
+ if (tail)
1365
+ tail->next = d;
1366
+ else
1367
+ head = d;
1368
+ tail = d;
1369
+ }
1370
+ return head;
1371
+ }
1372
+
1373
+ /* Capture the attribute rows of a complexType or attributeGroup:
1374
+ * named rows, inline anonymous simpleTypes (synthesized slots),
1375
+ * refs to top-level attributes, and attributeGroup refs. */
1376
+ /* Named row with an inline anonymous simpleType rides a
1377
+ * synthesized "attribute:NAME" slot, the element-slot pattern. */
1378
+ static const char* xsd_inline_attr_type(
1379
+ struct leptris_xsd_schema* s, LeptrisElement ae,
1380
+ const char* attr_name) {
1381
+ for (LeptrisNodeRef q = leptris_node_first_child(
1382
+ leptris_element_as_node(ae));
1383
+ q; q = leptris_node_next_sibling(q)) {
1384
+ if (leptris_node_get_type(q) != LEPTRIS_NODE_TYPE_ELEMENT)
1385
+ continue;
1386
+ LeptrisElement qe = (LeptrisElement)q;
1387
+ const char* ql = NULL;
1388
+ leptris_element_expanded_name(qe, &ql, NULL, NULL);
1389
+ if (!ql || strcmp(ql, "simpleType") != 0) continue;
1390
+ char synth[512];
1391
+ snprintf(synth, sizeof(synth), "attribute:%s", attr_name);
1392
+ XsdSimple* captured = xsd_capture_simple(qe);
1393
+ if (!captured) return NULL;
1394
+ free(captured->name);
1395
+ captured->name = xsd_strdup(synth);
1396
+ captured->next = s->simple_types;
1397
+ s->simple_types = captured;
1398
+ return captured->name;
1399
+ }
1400
+ return NULL;
1401
+ }
1402
+
1403
+ static XsdAttrDecl* xsd_capture_attr_rows(struct leptris_xsd_schema* s,
1404
+ LeptrisElement container,
1405
+ int depth) {
1406
+ XsdAttrDecl* head = NULL;
1407
+ XsdAttrDecl* tail = NULL;
1408
+ if (depth > 8) return NULL;
1409
+ for (LeptrisNodeRef ac = leptris_node_first_child(
1410
+ leptris_element_as_node(container));
1411
+ ac; ac = leptris_node_next_sibling(ac)) {
1412
+ if (leptris_node_get_type(ac) != LEPTRIS_NODE_TYPE_ELEMENT)
1413
+ continue;
1414
+ LeptrisElement ae = (LeptrisElement)ac;
1415
+ const char* al = NULL, *ap = NULL, *au = NULL;
1416
+ leptris_element_expanded_name(ae, &al, &ap, &au);
1417
+ if (!al || (au && strcmp(au, XSD_NS) != 0)) continue;
1418
+
1419
+ if (strcmp(al, "attribute") == 0) {
1420
+ const char* an = leptris_element_attribute(ae, "name");
1421
+ const char* use = leptris_element_attribute(ae, "use");
1422
+ XsdAttrDecl* d = (XsdAttrDecl*)calloc(1, sizeof(*d));
1423
+ if (!d) continue;
1424
+ if (an) {
1425
+ d->name = xsd_strdup(an);
1426
+ d->type = xsd_strdup(
1427
+ leptris_element_attribute(ae, "type"));
1428
+ if (!d->type)
1429
+ d->type = xsd_strdup(
1430
+ xsd_inline_attr_type(s, ae, an));
1431
+ d->fixed = xsd_strdup(
1432
+ leptris_element_attribute(ae, "fixed"));
1433
+ d->required = (use && strcmp(use, "required") == 0);
1434
+ } else {
1435
+ /* ref to a top-level attribute declaration */
1436
+ const char* ref =
1437
+ leptris_element_attribute(ae, "ref");
1438
+ const XsdAttrDecl* src =
1439
+ ref ? xsd_find_top_attr(s, ref) : NULL;
1440
+ if (!src) {
1441
+ free(d);
1442
+ continue;
1443
+ }
1444
+ d->name = xsd_strdup(src->name);
1445
+ d->type = xsd_strdup(src->type);
1446
+ d->fixed = xsd_strdup(src->fixed);
1447
+ d->required = use
1448
+ ? (strcmp(use, "required") == 0)
1449
+ : src->required;
1450
+ }
1451
+ if (tail)
1452
+ tail->next = d;
1453
+ else
1454
+ head = d;
1455
+ tail = d;
1456
+ } else if (strcmp(al, "attributeGroup") == 0) {
1457
+ const char* ref =
1458
+ leptris_element_attribute(ae, "ref");
1459
+ if (!ref) continue;
1460
+ XsdAttrGroupDef* g = s->attr_groups;
1461
+ for (; g; g = g->next)
1462
+ if (strcmp(g->name, ref) == 0) break;
1463
+ if (!g || !g->attrs) continue;
1464
+ XsdAttrDecl* copied = xsd_copy_attr_rows(g->attrs);
1465
+ if (!copied) continue;
1466
+ if (!head)
1467
+ head = copied;
1468
+ else
1469
+ tail->next = copied;
1470
+ tail = copied;
1471
+ while (tail->next) tail = tail->next;
1472
+ }
1473
+ }
1474
+ return head;
1475
+ }
1476
+
562
1477
  LEPTRIS_API int leptris_xsd_builtin_valid(const char* builtin,
563
1478
  const char* lexical) {
564
1479
  extern int xsd_builtin_valid(const char* type, const char* v);
565
1480
  return xsd_builtin_valid(builtin, lexical);
566
1481
  }
567
1482
 
1483
+ /* The instance validator routes every user simpleType through the
1484
+ * chain walker so list/union derivations dispatch with the schema
1485
+ * in hand (the bare xsd_simple_valid cannot resolve item/member
1486
+ * names). */
1487
+ int xsd_simple_valid_chain(struct leptris_xsd_schema* s,
1488
+ const XsdSimple* t, const char* v) {
1489
+ return xsd_valid_chain(s, t, v, 0);
1490
+ }
1491
+
568
1492
  LEPTRIS_API int leptris_xsd_simple_valid(LeptrisXsdSchema schema,
569
1493
  const char* type_name,
570
1494
  const char* lexical) {