lutaml 0.11.7 → 0.11.8

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 (881) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -0
  3. data/.rubocop_todo.yml +0 -1
  4. data/Gemfile +32 -5
  5. data/README.adoc +3 -3
  6. data/README.md +6 -8
  7. data/Rakefile +41 -1
  8. data/bin/console +0 -1
  9. data/exe/lutaml-lml +6 -0
  10. data/exe/lutaml-lml-lsp +11 -0
  11. data/lib/compat/opal/generate_boot.rb +132 -0
  12. data/lib/compat/opal/js_bundle_entry.rb +42 -0
  13. data/lib/compat/opal/lutaml_model_boot.rb +540 -0
  14. data/lib/compat/opal/monitor.rb +31 -0
  15. data/lib/compat/opal/moxml/adapter/customized_rexml/entity_reference.rb +9 -0
  16. data/lib/compat/opal/moxml/adapter/customized_rexml/formatter.rb +225 -0
  17. data/lib/compat/opal/moxml/adapter/customized_rexml.rb +11 -0
  18. data/lib/compat/opal/moxml_boot.rb +68 -0
  19. data/lib/compat/opal/objectspace_shim.rb +23 -0
  20. data/lib/compat/opal/rexml/namespace.rb +59 -0
  21. data/lib/compat/opal/rexml/parsers/baseparser.rb +1016 -0
  22. data/lib/compat/opal/rexml/source.rb +214 -0
  23. data/lib/compat/opal/rexml/text.rb +426 -0
  24. data/lib/compat/opal/rexml/xmltokens.rb +45 -0
  25. data/lib/compat/opal/rexml_compat.rb +85 -0
  26. data/lib/compat/opal/yaml_compat.rb +32 -0
  27. data/lib/lutaml/hash_format/adapter/document.rb +10 -0
  28. data/lib/lutaml/hash_format/adapter/mapping.rb +17 -0
  29. data/lib/lutaml/hash_format/adapter/mapping_rule.rb +9 -0
  30. data/lib/lutaml/hash_format/adapter/standard_adapter.rb +17 -0
  31. data/lib/lutaml/hash_format/adapter/transform.rb +9 -0
  32. data/lib/lutaml/hash_format/adapter.rb +13 -0
  33. data/lib/lutaml/hash_format/format.rb +29 -0
  34. data/lib/lutaml/hash_format/type/serializers.rb +24 -0
  35. data/lib/lutaml/hash_format.rb +7 -0
  36. data/lib/lutaml/json/adapter/document.rb +13 -0
  37. data/lib/lutaml/json/adapter/mapping.rb +17 -0
  38. data/lib/lutaml/json/adapter/mapping_rule.rb +9 -0
  39. data/lib/lutaml/json/adapter/multi_json_adapter.rb +45 -0
  40. data/lib/lutaml/json/adapter/oj_adapter.rb +41 -0
  41. data/lib/lutaml/json/adapter/standard_adapter.rb +43 -0
  42. data/lib/lutaml/json/adapter/transform.rb +9 -0
  43. data/lib/lutaml/json/adapter/yeptris_adapter.rb +74 -0
  44. data/lib/lutaml/json/adapter.rb +21 -0
  45. data/lib/lutaml/json/format.rb +61 -0
  46. data/lib/lutaml/json/generator_options.rb +62 -0
  47. data/lib/lutaml/json/schema/json_schema.rb +62 -0
  48. data/lib/lutaml/json/schema.rb +9 -0
  49. data/lib/lutaml/json/type/serializers.rb +72 -0
  50. data/lib/lutaml/json.rb +7 -0
  51. data/lib/lutaml/jsonl/adapter/document.rb +14 -0
  52. data/lib/lutaml/jsonl/adapter/mapping.rb +17 -0
  53. data/lib/lutaml/jsonl/adapter/mapping_rule.rb +9 -0
  54. data/lib/lutaml/jsonl/adapter/standard_adapter.rb +34 -0
  55. data/lib/lutaml/jsonl/adapter/transform.rb +26 -0
  56. data/lib/lutaml/jsonl/adapter.rb +13 -0
  57. data/lib/lutaml/jsonl/format.rb +22 -0
  58. data/lib/lutaml/jsonl.rb +7 -0
  59. data/lib/lutaml/jsonld/adapter.rb +31 -0
  60. data/lib/lutaml/jsonld.rb +20 -0
  61. data/lib/lutaml/key_value/adapter/hash/document.rb +13 -0
  62. data/lib/lutaml/key_value/adapter/hash/mapping.rb +17 -0
  63. data/lib/lutaml/key_value/adapter/hash/mapping_rule.rb +9 -0
  64. data/lib/lutaml/key_value/adapter/hash/standard_adapter.rb +19 -0
  65. data/lib/lutaml/key_value/adapter/hash/transform.rb +10 -0
  66. data/lib/lutaml/key_value/adapter/hash.rb +15 -0
  67. data/lib/lutaml/key_value/adapter/json/document.rb +16 -0
  68. data/lib/lutaml/key_value/adapter/json/mapping.rb +22 -0
  69. data/lib/lutaml/key_value/adapter/json/mapping_rule.rb +14 -0
  70. data/lib/lutaml/key_value/adapter/json/multi_json_adapter.rb +54 -0
  71. data/lib/lutaml/key_value/adapter/json/oj_adapter.rb +46 -0
  72. data/lib/lutaml/key_value/adapter/json/standard_adapter.rb +47 -0
  73. data/lib/lutaml/key_value/adapter/json/transform.rb +14 -0
  74. data/lib/lutaml/key_value/adapter/json/yeptris_adapter.rb +15 -0
  75. data/lib/lutaml/key_value/adapter/json.rb +24 -0
  76. data/lib/lutaml/key_value/adapter/jsonl/document.rb +16 -0
  77. data/lib/lutaml/key_value/adapter/jsonl/mapping.rb +17 -0
  78. data/lib/lutaml/key_value/adapter/jsonl/mapping_rule.rb +9 -0
  79. data/lib/lutaml/key_value/adapter/jsonl/standard_adapter.rb +36 -0
  80. data/lib/lutaml/key_value/adapter/jsonl/transform.rb +26 -0
  81. data/lib/lutaml/key_value/adapter/jsonl.rb +17 -0
  82. data/lib/lutaml/key_value/adapter/toml/document.rb +13 -0
  83. data/lib/lutaml/key_value/adapter/toml/mapping.rb +26 -0
  84. data/lib/lutaml/key_value/adapter/toml/mapping_rule.rb +9 -0
  85. data/lib/lutaml/key_value/adapter/toml/teptris_adapter.rb +36 -0
  86. data/lib/lutaml/key_value/adapter/toml/toml_rb_adapter.rb +34 -0
  87. data/lib/lutaml/key_value/adapter/toml/tomlib_adapter.rb +40 -0
  88. data/lib/lutaml/key_value/adapter/toml/transform.rb +10 -0
  89. data/lib/lutaml/key_value/adapter/toml.rb +22 -0
  90. data/lib/lutaml/key_value/adapter/yaml/document.rb +12 -0
  91. data/lib/lutaml/key_value/adapter/yaml/mapping.rb +17 -0
  92. data/lib/lutaml/key_value/adapter/yaml/mapping_rule.rb +9 -0
  93. data/lib/lutaml/key_value/adapter/yaml/standard_adapter.rb +35 -0
  94. data/lib/lutaml/key_value/adapter/yaml/transform.rb +10 -0
  95. data/lib/lutaml/key_value/adapter/yaml/yeptris_adapter.rb +15 -0
  96. data/lib/lutaml/key_value/adapter/yaml.rb +17 -0
  97. data/lib/lutaml/key_value/adapter/yamls/document.rb +16 -0
  98. data/lib/lutaml/key_value/adapter/yamls/mapping.rb +17 -0
  99. data/lib/lutaml/key_value/adapter/yamls/mapping_rule.rb +9 -0
  100. data/lib/lutaml/key_value/adapter/yamls/standard_adapter.rb +37 -0
  101. data/lib/lutaml/key_value/adapter/yamls/transform.rb +26 -0
  102. data/lib/lutaml/key_value/adapter/yamls.rb +17 -0
  103. data/lib/lutaml/key_value/adapter.rb +14 -0
  104. data/lib/lutaml/key_value/data_model/element.rb +225 -0
  105. data/lib/lutaml/key_value/data_model.rb +14 -0
  106. data/lib/lutaml/key_value/document.rb +28 -0
  107. data/lib/lutaml/key_value/format.rb +26 -0
  108. data/lib/lutaml/key_value/mapping.rb +393 -0
  109. data/lib/lutaml/key_value/mapping_rule.rb +95 -0
  110. data/lib/lutaml/key_value/transform.rb +1223 -0
  111. data/lib/lutaml/key_value/transformation/collection_serializer.rb +572 -0
  112. data/lib/lutaml/key_value/transformation/rule_compiler.rb +299 -0
  113. data/lib/lutaml/key_value/transformation/value_serializer.rb +399 -0
  114. data/lib/lutaml/key_value/transformation.rb +1276 -0
  115. data/lib/lutaml/key_value/transformation_builder.rb +30 -0
  116. data/lib/lutaml/key_value.rb +7 -0
  117. data/lib/lutaml/lml/association_label_resolver.rb +52 -0
  118. data/lib/lutaml/lml/cli.rb +506 -0
  119. data/lib/lutaml/lml/data_processor/attribute_processing.rb +133 -0
  120. data/lib/lutaml/lml/data_processor/collection_processing.rb +37 -0
  121. data/lib/lutaml/lml/data_processor/instance_processing.rb +77 -0
  122. data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +70 -0
  123. data/lib/lutaml/lml/data_processor/value_processing.rb +103 -0
  124. data/lib/lutaml/lml/data_processor/view_processing.rb +26 -0
  125. data/lib/lutaml/lml/data_processor.rb +59 -0
  126. data/lib/lutaml/lml/diagnostic.rb +149 -0
  127. data/lib/lutaml/lml/document_builder.rb +481 -0
  128. data/lib/lutaml/lml/entity_types.rb +30 -0
  129. data/lib/lutaml/lml/executor/adapter_helpers.rb +45 -0
  130. data/lib/lutaml/lml/executor/condition_evaluator.rb +169 -0
  131. data/lib/lutaml/lml/executor/csv_adapter.rb +88 -0
  132. data/lib/lutaml/lml/executor/format_adapter.rb +54 -0
  133. data/lib/lutaml/lml/executor/xml_adapter.rb +102 -0
  134. data/lib/lutaml/lml/executor.rb +96 -0
  135. data/lib/lutaml/lml/format/adapter/document.rb +11 -0
  136. data/lib/lutaml/lml/format/adapter/mapping.rb +31 -0
  137. data/lib/lutaml/lml/format/adapter/standard_adapter.rb +162 -0
  138. data/lib/lutaml/lml/format/adapter/transform.rb +11 -0
  139. data/lib/lutaml/lml/format.rb +29 -0
  140. data/lib/lutaml/lml/formatter/base.rb +79 -0
  141. data/lib/lutaml/lml/formatter/canonical.rb +736 -0
  142. data/lib/lutaml/lml/formatter/elk.rb +45 -0
  143. data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +134 -0
  144. data/lib/lutaml/lml/formatter/svg_pdf.rb +285 -0
  145. data/lib/lutaml/lml/formatter.rb +11 -0
  146. data/lib/lutaml/lml/grammar/lml.artifact.json +14993 -0
  147. data/lib/lutaml/lml/grammar/lml.parg +389 -0
  148. data/lib/lutaml/lml/grammar.rb +60 -0
  149. data/lib/lutaml/lml/has_attributes.rb +14 -0
  150. data/lib/lutaml/lml/import_resolver.rb +96 -0
  151. data/lib/lutaml/lml/layout/elk_engine.rb +15 -0
  152. data/lib/lutaml/lml/layout/elk_graph_builder.rb +129 -0
  153. data/lib/lutaml/lml/layout/elk_svg_markers.rb +45 -0
  154. data/lib/lutaml/lml/layout/elk_svg_renderer.rb +119 -0
  155. data/lib/lutaml/lml/layout/engine.rb +17 -0
  156. data/lib/lutaml/lml/layout.rb +12 -0
  157. data/lib/lutaml/lml/literal_value.rb +32 -0
  158. data/lib/lutaml/lml/lsp/conversions.rb +78 -0
  159. data/lib/lutaml/lml/lsp/keywords.rb +43 -0
  160. data/lib/lutaml/lml/lsp/server.rb +378 -0
  161. data/lib/lutaml/lml/lsp/store.rb +68 -0
  162. data/lib/lutaml/lml/lsp/transport.rb +53 -0
  163. data/lib/lutaml/lml/lsp.rb +16 -0
  164. data/lib/lutaml/lml/model_compiler/enums.rb +172 -0
  165. data/lib/lutaml/lml/model_compiler/hydration.rb +123 -0
  166. data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +188 -0
  167. data/lib/lutaml/lml/model_compiler/string_formats.rb +42 -0
  168. data/lib/lutaml/lml/model_compiler/validation.rb +65 -0
  169. data/lib/lutaml/lml/model_compiler.rb +323 -0
  170. data/lib/lutaml/lml/models/action.rb +10 -0
  171. data/lib/lutaml/lml/models/association.rb +29 -0
  172. data/lib/lutaml/lml/models/cardinality.rb +10 -0
  173. data/lib/lutaml/lml/models/collection.rb +11 -0
  174. data/lib/lutaml/lml/models/constraint.rb +23 -0
  175. data/lib/lutaml/lml/models/data_type.rb +41 -0
  176. data/lib/lutaml/lml/models/derived_field.rb +14 -0
  177. data/lib/lutaml/lml/models/diagram.rb +20 -0
  178. data/lib/lutaml/lml/models/document.rb +84 -0
  179. data/lib/lutaml/lml/models/enum.rb +38 -0
  180. data/lib/lutaml/lml/models/fidelity.rb +10 -0
  181. data/lib/lutaml/lml/models/group.rb +11 -0
  182. data/lib/lutaml/lml/models/instance.rb +26 -0
  183. data/lib/lutaml/lml/models/instance_collection.rb +16 -0
  184. data/lib/lutaml/lml/models/instances_export.rb +10 -0
  185. data/lib/lutaml/lml/models/instances_import.rb +11 -0
  186. data/lib/lutaml/lml/models/mapping.rb +15 -0
  187. data/lib/lutaml/lml/models/mapping_rule.rb +16 -0
  188. data/lib/lutaml/lml/models/member_field.rb +14 -0
  189. data/lib/lutaml/lml/models/namespace.rb +16 -0
  190. data/lib/lutaml/lml/models/operation.rb +26 -0
  191. data/lib/lutaml/lml/models/operation_parameter.rb +12 -0
  192. data/lib/lutaml/lml/models/package.rb +26 -0
  193. data/lib/lutaml/lml/models/primitive_type.rb +33 -0
  194. data/lib/lutaml/lml/models/reference.rb +23 -0
  195. data/lib/lutaml/lml/models/serialization_mapping.rb +19 -0
  196. data/lib/lutaml/lml/models/string_format.rb +16 -0
  197. data/lib/lutaml/lml/models/top_element_attribute.rb +116 -0
  198. data/lib/lutaml/lml/models/uml_class.rb +85 -0
  199. data/lib/lutaml/lml/models/value.rb +15 -0
  200. data/lib/lutaml/lml/models/view_filter.rb +11 -0
  201. data/lib/lutaml/lml/models/view_import.rb +11 -0
  202. data/lib/lutaml/lml/parser.rb +19 -0
  203. data/lib/lutaml/lml/pipeline.rb +73 -0
  204. data/lib/lutaml/lml/positioned.rb +13 -0
  205. data/lib/lutaml/lml/preprocessor.rb +130 -0
  206. data/lib/lutaml/lml/quoting.rb +60 -0
  207. data/lib/lutaml/lml/source.rb +49 -0
  208. data/lib/lutaml/lml/source_position.rb +20 -0
  209. data/lib/lutaml/lml/symbol_index.rb +200 -0
  210. data/lib/lutaml/lml/transform.rb +119 -0
  211. data/lib/lutaml/lml/types/text_type.rb +28 -0
  212. data/lib/lutaml/lml/types.rb +10 -0
  213. data/lib/lutaml/lml/validator.rb +458 -0
  214. data/lib/lutaml/lml/version.rb +10 -0
  215. data/lib/lutaml/lml/view_resolution.rb +32 -0
  216. data/lib/lutaml/lml/view_resolver.rb +48 -0
  217. data/lib/lutaml/lml/yaml_parser.rb +17 -0
  218. data/lib/lutaml/lml.rb +130 -0
  219. data/lib/lutaml/model/adapter_resolver.rb +487 -0
  220. data/lib/lutaml/model/adapter_scope.rb +64 -0
  221. data/lib/lutaml/model/attribute.rb +1511 -0
  222. data/lib/lutaml/model/attribute_validator.rb +290 -0
  223. data/lib/lutaml/model/cached_type_resolver/concurrent_map_cache.rb +48 -0
  224. data/lib/lutaml/model/cached_type_resolver/mutex_hash_cache.rb +53 -0
  225. data/lib/lutaml/model/cached_type_resolver.rb +145 -0
  226. data/lib/lutaml/model/choice.rb +343 -0
  227. data/lib/lutaml/model/cli.rb +85 -0
  228. data/lib/lutaml/model/collection.rb +740 -0
  229. data/lib/lutaml/model/collection_handler.rb +157 -0
  230. data/lib/lutaml/model/comparable_model.rb +710 -0
  231. data/lib/lutaml/model/comparable_nil.rb +9 -0
  232. data/lib/lutaml/model/comparison.rb +15 -0
  233. data/lib/lutaml/model/compiled_rule.rb +242 -0
  234. data/lib/lutaml/model/config.rb +169 -0
  235. data/lib/lutaml/model/configuration.rb +138 -0
  236. data/lib/lutaml/model/consolidation/attribute_grouper.rb +44 -0
  237. data/lib/lutaml/model/consolidation/dispatcher.rb +45 -0
  238. data/lib/lutaml/model/consolidation/engine.rb +33 -0
  239. data/lib/lutaml/model/consolidation/pattern_chunker.rb +87 -0
  240. data/lib/lutaml/model/consolidation_map.rb +68 -0
  241. data/lib/lutaml/model/consolidation_rule/dispatch_block.rb +34 -0
  242. data/lib/lutaml/model/consolidation_rule/gather_rule.rb +17 -0
  243. data/lib/lutaml/model/consolidation_rule/pattern_content_rule.rb +15 -0
  244. data/lib/lutaml/model/consolidation_rule/pattern_element_rule.rb +17 -0
  245. data/lib/lutaml/model/consolidation_rule.rb +27 -0
  246. data/lib/lutaml/model/constants.rb +11 -0
  247. data/lib/lutaml/model/context_registry.rb +208 -0
  248. data/lib/lutaml/model/deep_dupable.rb +16 -0
  249. data/lib/lutaml/model/error/choice_lower_bound_error.rb +9 -0
  250. data/lib/lutaml/model/error/choice_upper_bound_error.rb +9 -0
  251. data/lib/lutaml/model/error/collection_count_out_of_range_error.rb +29 -0
  252. data/lib/lutaml/model/error/collection_true_missing_error.rb +16 -0
  253. data/lib/lutaml/model/error/element_count_out_of_range_error.rb +35 -0
  254. data/lib/lutaml/model/error/format_adapter_not_specified_error.rb +25 -0
  255. data/lib/lutaml/model/error/fraction_digits_error.rb +20 -0
  256. data/lib/lutaml/model/error/import_model_with_root_error.rb +9 -0
  257. data/lib/lutaml/model/error/incorrect_mapping_argument_error.rb +6 -0
  258. data/lib/lutaml/model/error/incorrect_sequence_error.rb +9 -0
  259. data/lib/lutaml/model/error/invalid_attribute_name_error.rb +15 -0
  260. data/lib/lutaml/model/error/invalid_attribute_options_error.rb +16 -0
  261. data/lib/lutaml/model/error/invalid_attribute_type_error.rb +98 -0
  262. data/lib/lutaml/model/error/invalid_choice_range_error.rb +18 -0
  263. data/lib/lutaml/model/error/invalid_format_error.rb +11 -0
  264. data/lib/lutaml/model/error/invalid_value_error.rb +18 -0
  265. data/lib/lutaml/model/error/length_error.rb +19 -0
  266. data/lib/lutaml/model/error/liquid_class_not_found_error.rb +9 -0
  267. data/lib/lutaml/model/error/liquid_drop_already_registered_error.rb +11 -0
  268. data/lib/lutaml/model/error/liquid_not_enabled_error.rb +9 -0
  269. data/lib/lutaml/model/error/mapping_already_exists_error.rb +6 -0
  270. data/lib/lutaml/model/error/mapping_attribute_missing_error.rb +6 -0
  271. data/lib/lutaml/model/error/mapping_attribute_type_error.rb +6 -0
  272. data/lib/lutaml/model/error/max_exclusive_error.rb +19 -0
  273. data/lib/lutaml/model/error/max_inclusive_error.rb +20 -0
  274. data/lib/lutaml/model/error/max_length_error.rb +20 -0
  275. data/lib/lutaml/model/error/min_exclusive_error.rb +19 -0
  276. data/lib/lutaml/model/error/min_inclusive_error.rb +20 -0
  277. data/lib/lutaml/model/error/min_length_error.rb +20 -0
  278. data/lib/lutaml/model/error/mixed_content_collection_error.rb +17 -0
  279. data/lib/lutaml/model/error/multiple_mappings_error.rb +6 -0
  280. data/lib/lutaml/model/error/no_attributes_defined_liquid_error.rb +9 -0
  281. data/lib/lutaml/model/error/no_mapping_found_error.rb +9 -0
  282. data/lib/lutaml/model/error/no_root_mapping_error.rb +10 -0
  283. data/lib/lutaml/model/error/no_root_namespace_error.rb +10 -0
  284. data/lib/lutaml/model/error/ordered_content_mapping_error.rb +17 -0
  285. data/lib/lutaml/model/error/pattern_not_matched_error.rb +17 -0
  286. data/lib/lutaml/model/error/polymorphic_error.rb +14 -0
  287. data/lib/lutaml/model/error/register/not_registrable_class_error.rb +11 -0
  288. data/lib/lutaml/model/error/required_attribute_missing_error.rb +9 -0
  289. data/lib/lutaml/model/error/restriction_error.rb +8 -0
  290. data/lib/lutaml/model/error/reverse_transform_block_not_defined_error.rb +6 -0
  291. data/lib/lutaml/model/error/reverse_transformation_declaration_error.rb +6 -0
  292. data/lib/lutaml/model/error/sorting_configuration_conflict_error.rb +9 -0
  293. data/lib/lutaml/model/error/total_digits_error.rb +20 -0
  294. data/lib/lutaml/model/error/transform_block_not_defined_error.rb +6 -0
  295. data/lib/lutaml/model/error/type/invalid_value_error.rb +21 -0
  296. data/lib/lutaml/model/error/type/max_bound_error.rb +20 -0
  297. data/lib/lutaml/model/error/type/max_exclusive_error.rb +20 -0
  298. data/lib/lutaml/model/error/type/max_length_error.rb +20 -0
  299. data/lib/lutaml/model/error/type/min_bound_error.rb +20 -0
  300. data/lib/lutaml/model/error/type/min_exclusive_error.rb +20 -0
  301. data/lib/lutaml/model/error/type/min_length_error.rb +20 -0
  302. data/lib/lutaml/model/error/type/pattern_not_matched_error.rb +18 -0
  303. data/lib/lutaml/model/error/type.rb +19 -0
  304. data/lib/lutaml/model/error/type_error.rb +9 -0
  305. data/lib/lutaml/model/error/type_not_enabled_error.rb +9 -0
  306. data/lib/lutaml/model/error/type_only_mapping_error.rb +13 -0
  307. data/lib/lutaml/model/error/type_only_namespace_error.rb +12 -0
  308. data/lib/lutaml/model/error/undefined_attribute_error.rb +9 -0
  309. data/lib/lutaml/model/error/union_schema_unsupported_error.rb +11 -0
  310. data/lib/lutaml/model/error/unknown_adapter_type_error.rb +95 -0
  311. data/lib/lutaml/model/error/unknown_discriminator_error.rb +11 -0
  312. data/lib/lutaml/model/error/unknown_mapping_error.rb +18 -0
  313. data/lib/lutaml/model/error/unknown_sequence_mapping_error.rb +9 -0
  314. data/lib/lutaml/model/error/unknown_type_error.rb +32 -0
  315. data/lib/lutaml/model/error/unresolvable_type_error.rb +27 -0
  316. data/lib/lutaml/model/error/unsupported_version_error.rb +14 -0
  317. data/lib/lutaml/model/error/validation_error.rb +20 -0
  318. data/lib/lutaml/model/error/validation_failed_error.rb +9 -0
  319. data/lib/lutaml/model/error.rb +8 -0
  320. data/lib/lutaml/model/errors.rb +38 -0
  321. data/lib/lutaml/model/evolution_steps.rb +85 -0
  322. data/lib/lutaml/model/finalization_cache.rb +51 -0
  323. data/lib/lutaml/model/format_registry.rb +218 -0
  324. data/lib/lutaml/model/global_context.rb +476 -0
  325. data/lib/lutaml/model/global_register.rb +142 -0
  326. data/lib/lutaml/model/hash.rb +15 -0
  327. data/lib/lutaml/model/import_registry.rb +250 -0
  328. data/lib/lutaml/model/instrumentation.rb +304 -0
  329. data/lib/lutaml/model/json.rb +25 -0
  330. data/lib/lutaml/model/json_adapter.rb +8 -0
  331. data/lib/lutaml/model/jsonl.rb +15 -0
  332. data/lib/lutaml/model/liquefiable.rb +198 -0
  333. data/lib/lutaml/model/liquid/indexed_access.rb +33 -0
  334. data/lib/lutaml/model/liquid/mapping.rb +30 -0
  335. data/lib/lutaml/model/liquid.rb +10 -0
  336. data/lib/lutaml/model/lml/document.rb +109 -0
  337. data/lib/lutaml/model/lml/exporter.rb +328 -0
  338. data/lib/lutaml/model/lml/rejected_error.rb +32 -0
  339. data/lib/lutaml/model/lml/round_trip.rb +49 -0
  340. data/lib/lutaml/model/lml.rb +40 -0
  341. data/lib/lutaml/model/mapping/listener.rb +126 -0
  342. data/lib/lutaml/model/mapping/mapping.rb +209 -0
  343. data/lib/lutaml/model/mapping/mapping_rule.rb +673 -0
  344. data/lib/lutaml/model/mapping/model_mapping.rb +133 -0
  345. data/lib/lutaml/model/mapping/model_mapping_rule.rb +57 -0
  346. data/lib/lutaml/model/mapping_hash.rb +80 -0
  347. data/lib/lutaml/model/model_transformer.rb +101 -0
  348. data/lib/lutaml/model/one_entry_cache.rb +57 -0
  349. data/lib/lutaml/model/organization.rb +29 -0
  350. data/lib/lutaml/model/register/model_tree_importer.rb +88 -0
  351. data/lib/lutaml/model/register/namespace_binding.rb +92 -0
  352. data/lib/lutaml/model/register.rb +589 -0
  353. data/lib/lutaml/model/registrable.rb +8 -0
  354. data/lib/lutaml/model/render_policy.rb +166 -0
  355. data/lib/lutaml/model/restriction_validation.rb +437 -0
  356. data/lib/lutaml/model/runtime_compatibility.rb +144 -0
  357. data/lib/lutaml/model/schema/base_schema.rb +77 -0
  358. data/lib/lutaml/model/schema/class_loader.rb +59 -0
  359. data/lib/lutaml/model/schema/compiled_output.rb +83 -0
  360. data/lib/lutaml/model/schema/decorators/attribute.rb +116 -0
  361. data/lib/lutaml/model/schema/decorators/choices.rb +29 -0
  362. data/lib/lutaml/model/schema/decorators/class_definition.rb +84 -0
  363. data/lib/lutaml/model/schema/decorators/definition_collection.rb +97 -0
  364. data/lib/lutaml/model/schema/decorators.rb +15 -0
  365. data/lib/lutaml/model/schema/definitions/attribute.rb +32 -0
  366. data/lib/lutaml/model/schema/definitions/choice.rb +20 -0
  367. data/lib/lutaml/model/schema/definitions/facet.rb +40 -0
  368. data/lib/lutaml/model/schema/definitions/group_import.rb +22 -0
  369. data/lib/lutaml/model/schema/definitions/member_walk.rb +33 -0
  370. data/lib/lutaml/model/schema/definitions/model.rb +40 -0
  371. data/lib/lutaml/model/schema/definitions/namespace.rb +24 -0
  372. data/lib/lutaml/model/schema/definitions/restricted_type.rb +32 -0
  373. data/lib/lutaml/model/schema/definitions/sequence.rb +19 -0
  374. data/lib/lutaml/model/schema/definitions/simple_content.rb +22 -0
  375. data/lib/lutaml/model/schema/definitions/transform_facet.rb +21 -0
  376. data/lib/lutaml/model/schema/definitions/type_ref.rb +20 -0
  377. data/lib/lutaml/model/schema/definitions/union_type.rb +28 -0
  378. data/lib/lutaml/model/schema/definitions/xml_root.rb +20 -0
  379. data/lib/lutaml/model/schema/definitions.rb +24 -0
  380. data/lib/lutaml/model/schema/file_writer.rb +73 -0
  381. data/lib/lutaml/model/schema/generator/definition.rb +98 -0
  382. data/lib/lutaml/model/schema/generator/definitions_collection.rb +117 -0
  383. data/lib/lutaml/model/schema/generator/properties_collection.rb +65 -0
  384. data/lib/lutaml/model/schema/generator/property.rb +182 -0
  385. data/lib/lutaml/model/schema/generator/ref.rb +24 -0
  386. data/lib/lutaml/model/schema/generator.rb +15 -0
  387. data/lib/lutaml/model/schema/helpers/template_helper.rb +49 -0
  388. data/lib/lutaml/model/schema/helpers.rb +11 -0
  389. data/lib/lutaml/model/schema/json_schema.rb +17 -0
  390. data/lib/lutaml/model/schema/json_schema_parser.rb +91 -0
  391. data/lib/lutaml/model/schema/module_nesting.rb +31 -0
  392. data/lib/lutaml/model/schema/namespace_naming.rb +46 -0
  393. data/lib/lutaml/model/schema/registry_generator.rb +115 -0
  394. data/lib/lutaml/model/schema/renderer.rb +38 -0
  395. data/lib/lutaml/model/schema/renderers/base.rb +41 -0
  396. data/lib/lutaml/model/schema/renderers/mappings.rb +78 -0
  397. data/lib/lutaml/model/schema/renderers/member_decls.rb +121 -0
  398. data/lib/lutaml/model/schema/renderers/model.rb +133 -0
  399. data/lib/lutaml/model/schema/renderers/namespace.rb +30 -0
  400. data/lib/lutaml/model/schema/renderers/registration.rb +64 -0
  401. data/lib/lutaml/model/schema/renderers/required_files_calculator.rb +94 -0
  402. data/lib/lutaml/model/schema/renderers/restricted_type.rb +218 -0
  403. data/lib/lutaml/model/schema/renderers/union.rb +83 -0
  404. data/lib/lutaml/model/schema/renderers.rb +20 -0
  405. data/lib/lutaml/model/schema/rnc_compiler/source_resolver.rb +51 -0
  406. data/lib/lutaml/model/schema/rnc_compiler.rb +64 -0
  407. data/lib/lutaml/model/schema/rng_compiler/define_classifier.rb +94 -0
  408. data/lib/lutaml/model/schema/rng_compiler/element_visitor.rb +412 -0
  409. data/lib/lutaml/model/schema/rng_compiler/member_collector.rb +30 -0
  410. data/lib/lutaml/model/schema/rng_compiler/rng_helpers.rb +160 -0
  411. data/lib/lutaml/model/schema/rng_compiler/value_type_resolver.rb +129 -0
  412. data/lib/lutaml/model/schema/rng_compiler.rb +225 -0
  413. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +41 -0
  414. data/lib/lutaml/model/schema/schema_builder/oga.rb +92 -0
  415. data/lib/lutaml/model/schema/shared_methods.rb +24 -0
  416. data/lib/lutaml/model/schema/templates/model.erb +9 -0
  417. data/lib/lutaml/model/schema/templates.rb +164 -0
  418. data/lib/lutaml/model/schema/xml_compiler/element_order.rb +42 -0
  419. data/lib/lutaml/model/schema/xml_compiler/registry_generator.rb +51 -0
  420. data/lib/lutaml/model/schema/xml_compiler/spec_builder/complex_types.rb +198 -0
  421. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +249 -0
  422. data/lib/lutaml/model/schema/xml_compiler/spec_builder/simple_types.rb +185 -0
  423. data/lib/lutaml/model/schema/xml_compiler/spec_builder.rb +186 -0
  424. data/lib/lutaml/model/schema/xml_compiler/supported_data_types.rb +100 -0
  425. data/lib/lutaml/model/schema/xml_compiler.rb +109 -0
  426. data/lib/lutaml/model/schema/yaml_schema.rb +17 -0
  427. data/lib/lutaml/model/schema.rb +88 -0
  428. data/lib/lutaml/model/schema_location.rb +18 -0
  429. data/lib/lutaml/model/sequence.rb +220 -0
  430. data/lib/lutaml/model/serializable.rb +46 -0
  431. data/lib/lutaml/model/serialization_adapter.rb +24 -0
  432. data/lib/lutaml/model/serialize/attribute_definition.rb +897 -0
  433. data/lib/lutaml/model/serialize/builder.rb +258 -0
  434. data/lib/lutaml/model/serialize/conversion_caching.rb +129 -0
  435. data/lib/lutaml/model/serialize/deserialization_context.rb +51 -0
  436. data/lib/lutaml/model/serialize/enum_handling.rb +171 -0
  437. data/lib/lutaml/model/serialize/format_conversion.rb +624 -0
  438. data/lib/lutaml/model/serialize/initialization.rb +825 -0
  439. data/lib/lutaml/model/serialize/model_import.rb +425 -0
  440. data/lib/lutaml/model/serialize/transformation_builder.rb +128 -0
  441. data/lib/lutaml/model/serialize/value_mapping.rb +173 -0
  442. data/lib/lutaml/model/serialize.rb +533 -0
  443. data/lib/lutaml/model/services/base.rb +13 -0
  444. data/lib/lutaml/model/services/logger.rb +57 -0
  445. data/lib/lutaml/model/services/rule_value_extractor.rb +95 -0
  446. data/lib/lutaml/model/services/transformer.rb +143 -0
  447. data/lib/lutaml/model/services/type/validator/number.rb +25 -0
  448. data/lib/lutaml/model/services/type/validator/string.rb +53 -0
  449. data/lib/lutaml/model/services/type/validator/symbol.rb +30 -0
  450. data/lib/lutaml/model/services/type/validator.rb +69 -0
  451. data/lib/lutaml/model/services/type.rb +9 -0
  452. data/lib/lutaml/model/services/validator.rb +143 -0
  453. data/lib/lutaml/model/services.rb +10 -0
  454. data/lib/lutaml/model/store.rb +239 -0
  455. data/lib/lutaml/model/toml.rb +40 -0
  456. data/lib/lutaml/model/transform/xml_transform.rb +5 -0
  457. data/lib/lutaml/model/transform.rb +229 -0
  458. data/lib/lutaml/model/transformation.rb +89 -0
  459. data/lib/lutaml/model/transformation_builder.rb +55 -0
  460. data/lib/lutaml/model/transformation_registry.rb +327 -0
  461. data/lib/lutaml/model/type/base64_binary.rb +58 -0
  462. data/lib/lutaml/model/type/boolean.rb +35 -0
  463. data/lib/lutaml/model/type/date.rb +75 -0
  464. data/lib/lutaml/model/type/date_time.rb +60 -0
  465. data/lib/lutaml/model/type/date_time_with_timezone.rb +28 -0
  466. data/lib/lutaml/model/type/date_with_timezone.rb +28 -0
  467. data/lib/lutaml/model/type/day_time_duration.rb +35 -0
  468. data/lib/lutaml/model/type/decimal.rb +64 -0
  469. data/lib/lutaml/model/type/duration.rb +82 -0
  470. data/lib/lutaml/model/type/email_address.rb +28 -0
  471. data/lib/lutaml/model/type/float.rb +33 -0
  472. data/lib/lutaml/model/type/hash.rb +74 -0
  473. data/lib/lutaml/model/type/hex_binary.rb +56 -0
  474. data/lib/lutaml/model/type/hostname.rb +30 -0
  475. data/lib/lutaml/model/type/integer.rb +62 -0
  476. data/lib/lutaml/model/type/ip_v4_address.rb +27 -0
  477. data/lib/lutaml/model/type/ip_v6_address.rb +47 -0
  478. data/lib/lutaml/model/type/metaschema_pattern.rb +34 -0
  479. data/lib/lutaml/model/type/non_negative_integer.rb +25 -0
  480. data/lib/lutaml/model/type/positive_integer.rb +25 -0
  481. data/lib/lutaml/model/type/qname.rb +85 -0
  482. data/lib/lutaml/model/type/reference.rb +76 -0
  483. data/lib/lutaml/model/type/string.rb +62 -0
  484. data/lib/lutaml/model/type/symbol.rb +67 -0
  485. data/lib/lutaml/model/type/time.rb +52 -0
  486. data/lib/lutaml/model/type/time_without_date.rb +50 -0
  487. data/lib/lutaml/model/type/token.rb +25 -0
  488. data/lib/lutaml/model/type/uninitialized_class_guard.rb +36 -0
  489. data/lib/lutaml/model/type/uri.rb +51 -0
  490. data/lib/lutaml/model/type/uri_reference.rb +29 -0
  491. data/lib/lutaml/model/type/uuid.rb +25 -0
  492. data/lib/lutaml/model/type/value.rb +364 -0
  493. data/lib/lutaml/model/type/year_month_duration.rb +29 -0
  494. data/lib/lutaml/model/type.rb +199 -0
  495. data/lib/lutaml/model/type_context.rb +294 -0
  496. data/lib/lutaml/model/type_registry.rb +165 -0
  497. data/lib/lutaml/model/type_resolver.rb +187 -0
  498. data/lib/lutaml/model/type_substitution.rb +117 -0
  499. data/lib/lutaml/model/uninitialized_class.rb +70 -0
  500. data/lib/lutaml/model/union.rb +363 -0
  501. data/lib/lutaml/model/utils.rb +352 -0
  502. data/lib/lutaml/model/validation/concerns/has_issues.rb +27 -0
  503. data/lib/lutaml/model/validation/context.rb +36 -0
  504. data/lib/lutaml/model/validation/issue.rb +62 -0
  505. data/lib/lutaml/model/validation/layer_result.rb +34 -0
  506. data/lib/lutaml/model/validation/profile.rb +66 -0
  507. data/lib/lutaml/model/validation/registry.rb +60 -0
  508. data/lib/lutaml/model/validation/remediation.rb +33 -0
  509. data/lib/lutaml/model/validation/remediation_result.rb +20 -0
  510. data/lib/lutaml/model/validation/report.rb +39 -0
  511. data/lib/lutaml/model/validation/rule.rb +59 -0
  512. data/lib/lutaml/model/validation.rb +137 -0
  513. data/lib/lutaml/model/validation_framework.rb +77 -0
  514. data/lib/lutaml/model/value_policy.rb +27 -0
  515. data/lib/lutaml/model/value_transformer.rb +25 -0
  516. data/lib/lutaml/model/version.rb +7 -0
  517. data/lib/lutaml/model/yaml.rb +15 -0
  518. data/lib/lutaml/model/yamls.rb +15 -0
  519. data/lib/lutaml/model.rb +309 -0
  520. data/lib/lutaml/rdf/context.rb +69 -0
  521. data/lib/lutaml/rdf/error.rb +7 -0
  522. data/lib/lutaml/rdf/iri.rb +44 -0
  523. data/lib/lutaml/rdf/language_tagged.rb +11 -0
  524. data/lib/lutaml/rdf/linked_data_transform.rb +249 -0
  525. data/lib/lutaml/rdf/literal.rb +62 -0
  526. data/lib/lutaml/rdf/mapping.rb +86 -0
  527. data/lib/lutaml/rdf/mapping_rule.rb +52 -0
  528. data/lib/lutaml/rdf/member_rule.rb +55 -0
  529. data/lib/lutaml/rdf/namespace.rb +58 -0
  530. data/lib/lutaml/rdf/namespace_set.rb +69 -0
  531. data/lib/lutaml/rdf/namespaces/dcterms_namespace.rb +12 -0
  532. data/lib/lutaml/rdf/namespaces/owl_namespace.rb +12 -0
  533. data/lib/lutaml/rdf/namespaces/rdf_namespace.rb +14 -0
  534. data/lib/lutaml/rdf/namespaces/rdfs_namespace.rb +12 -0
  535. data/lib/lutaml/rdf/namespaces/skos_namespace.rb +12 -0
  536. data/lib/lutaml/rdf/namespaces/xsd_namespace.rb +12 -0
  537. data/lib/lutaml/rdf/namespaces.rb +14 -0
  538. data/lib/lutaml/rdf/term_definition.rb +39 -0
  539. data/lib/lutaml/rdf/transform.rb +45 -0
  540. data/lib/lutaml/rdf.rb +22 -0
  541. data/lib/lutaml/toml/adapter/document.rb +10 -0
  542. data/lib/lutaml/toml/adapter/mapping.rb +26 -0
  543. data/lib/lutaml/toml/adapter/mapping_rule.rb +9 -0
  544. data/lib/lutaml/toml/adapter/teptris_adapter.rb +14 -0
  545. data/lib/lutaml/toml/adapter/toml_rb_adapter.rb +32 -0
  546. data/lib/lutaml/toml/adapter/tomlib_adapter.rb +48 -0
  547. data/lib/lutaml/toml/adapter/transform.rb +9 -0
  548. data/lib/lutaml/toml/adapter.rb +20 -0
  549. data/lib/lutaml/toml/format.rb +48 -0
  550. data/lib/lutaml/toml/type/serializers.rb +71 -0
  551. data/lib/lutaml/toml.rb +7 -0
  552. data/lib/lutaml/turtle/adapter.rb +35 -0
  553. data/lib/lutaml/turtle/mapping.rb +7 -0
  554. data/lib/lutaml/turtle/transform.rb +250 -0
  555. data/lib/lutaml/turtle.rb +22 -0
  556. data/lib/lutaml/version.rb +1 -1
  557. data/lib/lutaml/xml/adapter/adapter_helpers.rb +108 -0
  558. data/lib/lutaml/xml/adapter/base_adapter.rb +249 -0
  559. data/lib/lutaml/xml/adapter/leptris_adapter.rb +16 -0
  560. data/lib/lutaml/xml/adapter/namespace_data.rb +56 -0
  561. data/lib/lutaml/xml/adapter/namespace_uri_collector.rb +71 -0
  562. data/lib/lutaml/xml/adapter/nokogiri_adapter.rb +16 -0
  563. data/lib/lutaml/xml/adapter/oga_adapter.rb +20 -0
  564. data/lib/lutaml/xml/adapter/ox_adapter.rb +16 -0
  565. data/lib/lutaml/xml/adapter/plan_based_builder.rb +944 -0
  566. data/lib/lutaml/xml/adapter/rexml_adapter.rb +25 -0
  567. data/lib/lutaml/xml/adapter/xml_parser.rb +89 -0
  568. data/lib/lutaml/xml/adapter/xml_serializer.rb +334 -0
  569. data/lib/lutaml/xml/adapter.rb +24 -0
  570. data/lib/lutaml/xml/adapter_element.rb +339 -0
  571. data/lib/lutaml/xml/adapter_loader.rb +56 -0
  572. data/lib/lutaml/xml/attribute_namespace_resolver.rb +139 -0
  573. data/lib/lutaml/xml/blank_namespace.rb +51 -0
  574. data/lib/lutaml/xml/blank_namespace_handler.rb +50 -0
  575. data/lib/lutaml/xml/builder/base.rb +258 -0
  576. data/lib/lutaml/xml/builder/leptris.rb +13 -0
  577. data/lib/lutaml/xml/builder/nokogiri.rb +13 -0
  578. data/lib/lutaml/xml/builder/oga.rb +13 -0
  579. data/lib/lutaml/xml/builder/ox.rb +13 -0
  580. data/lib/lutaml/xml/builder/rexml.rb +13 -0
  581. data/lib/lutaml/xml/builder.rb +18 -0
  582. data/lib/lutaml/xml/configurable.rb +215 -0
  583. data/lib/lutaml/xml/data_model.rb +295 -0
  584. data/lib/lutaml/xml/decisions/decision.rb +120 -0
  585. data/lib/lutaml/xml/decisions/decision_context.rb +225 -0
  586. data/lib/lutaml/xml/decisions/decision_engine.rb +67 -0
  587. data/lib/lutaml/xml/decisions/decision_rule.rb +54 -0
  588. data/lib/lutaml/xml/decisions/element_prefix_resolver.rb +85 -0
  589. data/lib/lutaml/xml/decisions/rules/attribute_usage_rule.rb +37 -0
  590. data/lib/lutaml/xml/decisions/rules/default_preference_rule.rb +62 -0
  591. data/lib/lutaml/xml/decisions/rules/element_form_default_rule.rb +66 -0
  592. data/lib/lutaml/xml/decisions/rules/element_form_default_unqualified_rule.rb +73 -0
  593. data/lib/lutaml/xml/decisions/rules/element_form_option_rule.rb +83 -0
  594. data/lib/lutaml/xml/decisions/rules/explicit_option_rule.rb +57 -0
  595. data/lib/lutaml/xml/decisions/rules/format_preservation_rule.rb +74 -0
  596. data/lib/lutaml/xml/decisions/rules/hoisted_on_parent_rule.rb +60 -0
  597. data/lib/lutaml/xml/decisions/rules/inherit_from_parent_rule.rb +52 -0
  598. data/lib/lutaml/xml/decisions/rules/inherit_parent_prefix_rule.rb +69 -0
  599. data/lib/lutaml/xml/decisions/rules/namespace_scope_rule.rb +54 -0
  600. data/lib/lutaml/xml/decisions/rules/reuse_parent_prefix_rule.rb +78 -0
  601. data/lib/lutaml/xml/decisions/rules/used_prefix_rule.rb +70 -0
  602. data/lib/lutaml/xml/decisions/rules.rb +39 -0
  603. data/lib/lutaml/xml/decisions.rb +15 -0
  604. data/lib/lutaml/xml/declaration_handler.rb +115 -0
  605. data/lib/lutaml/xml/declaration_plan/attribute_node.rb +81 -0
  606. data/lib/lutaml/xml/declaration_plan/element_node.rb +101 -0
  607. data/lib/lutaml/xml/declaration_plan.rb +480 -0
  608. data/lib/lutaml/xml/declaration_plan_query.rb +105 -0
  609. data/lib/lutaml/xml/declaration_planner.rb +1914 -0
  610. data/lib/lutaml/xml/doctype_extractor.rb +51 -0
  611. data/lib/lutaml/xml/document.rb +385 -0
  612. data/lib/lutaml/xml/element.rb +108 -0
  613. data/lib/lutaml/xml/element_prefix_resolver.rb +50 -0
  614. data/lib/lutaml/xml/encoding_normalizer.rb +54 -0
  615. data/lib/lutaml/xml/error/invalid_namespace_error.rb +46 -0
  616. data/lib/lutaml/xml/error/invalid_xsd_type_error.rb +43 -0
  617. data/lib/lutaml/xml/error/namespace_mismatch_error.rb +53 -0
  618. data/lib/lutaml/xml/error/schema_validation_error.rb +25 -0
  619. data/lib/lutaml/xml/error/xml_configuration_error.rb +17 -0
  620. data/lib/lutaml/xml/error/xml_error.rb +21 -0
  621. data/lib/lutaml/xml/format.rb +364 -0
  622. data/lib/lutaml/xml/format_chooser.rb +214 -0
  623. data/lib/lutaml/xml/hoisting_algorithm.rb +234 -0
  624. data/lib/lutaml/xml/html_entities.rb +290 -0
  625. data/lib/lutaml/xml/leptris/element.rb +34 -0
  626. data/lib/lutaml/xml/leptris/native_emission.rb +295 -0
  627. data/lib/lutaml/xml/leptris/plan_compiler.rb +652 -0
  628. data/lib/lutaml/xml/leptris/plan_hydrator.rb +827 -0
  629. data/lib/lutaml/xml/leptris/plan_order.rb +72 -0
  630. data/lib/lutaml/xml/leptris/plan_serializer.rb +711 -0
  631. data/lib/lutaml/xml/leptris/plan_walk.rb +36 -0
  632. data/lib/lutaml/xml/leptris.rb +48 -0
  633. data/lib/lutaml/xml/listener.rb +33 -0
  634. data/lib/lutaml/xml/mapping.rb +1746 -0
  635. data/lib/lutaml/xml/mapping_rule.rb +688 -0
  636. data/lib/lutaml/xml/model_transform.rb +1824 -0
  637. data/lib/lutaml/xml/namespace.rb +445 -0
  638. data/lib/lutaml/xml/namespace_class_registry.rb +144 -0
  639. data/lib/lutaml/xml/namespace_collector.rb +649 -0
  640. data/lib/lutaml/xml/namespace_declaration.rb +202 -0
  641. data/lib/lutaml/xml/namespace_declaration_builder.rb +67 -0
  642. data/lib/lutaml/xml/namespace_declaration_data.rb +179 -0
  643. data/lib/lutaml/xml/namespace_inheritance_resolver.rb +142 -0
  644. data/lib/lutaml/xml/namespace_inheritance_strategy.rb +26 -0
  645. data/lib/lutaml/xml/namespace_needs.rb +255 -0
  646. data/lib/lutaml/xml/namespace_resolution_strategy.rb +230 -0
  647. data/lib/lutaml/xml/namespace_resolver.rb +219 -0
  648. data/lib/lutaml/xml/namespace_scope_config.rb +110 -0
  649. data/lib/lutaml/xml/namespace_type_resolver.rb +135 -0
  650. data/lib/lutaml/xml/namespace_usage.rb +121 -0
  651. data/lib/lutaml/xml/nested_collection_attribute.rb +95 -0
  652. data/lib/lutaml/xml/nokogiri/element.rb +34 -0
  653. data/lib/lutaml/xml/nokogiri.rb +10 -0
  654. data/lib/lutaml/xml/oga/element.rb +15 -0
  655. data/lib/lutaml/xml/oga.rb +10 -0
  656. data/lib/lutaml/xml/ox/element.rb +15 -0
  657. data/lib/lutaml/xml/ox.rb +10 -0
  658. data/lib/lutaml/xml/parse_session.rb +139 -0
  659. data/lib/lutaml/xml/parsed_namespace_declaration.rb +129 -0
  660. data/lib/lutaml/xml/parsed_namespace_set.rb +149 -0
  661. data/lib/lutaml/xml/polymorphic_value_handler.rb +41 -0
  662. data/lib/lutaml/xml/qualified_inheritance_strategy.rb +27 -0
  663. data/lib/lutaml/xml/rexml/element.rb +15 -0
  664. data/lib/lutaml/xml/rexml.rb +10 -0
  665. data/lib/lutaml/xml/schema/builder/nokogiri.rb +41 -0
  666. data/lib/lutaml/xml/schema/builder/oga.rb +92 -0
  667. data/lib/lutaml/xml/schema/builder.rb +59 -0
  668. data/lib/lutaml/xml/schema/builtin_types.rb +112 -0
  669. data/lib/lutaml/xml/schema/relaxng_schema.rb +83 -0
  670. data/lib/lutaml/xml/schema/xsd/all.rb +31 -0
  671. data/lib/lutaml/xml/schema/xsd/annotation.rb +31 -0
  672. data/lib/lutaml/xml/schema/xsd/any.rb +33 -0
  673. data/lib/lutaml/xml/schema/xsd/any_attribute.rb +29 -0
  674. data/lib/lutaml/xml/schema/xsd/appinfo.rb +25 -0
  675. data/lib/lutaml/xml/schema/xsd/attribute.rb +78 -0
  676. data/lib/lutaml/xml/schema/xsd/attribute_group.rb +89 -0
  677. data/lib/lutaml/xml/schema/xsd/base.rb +270 -0
  678. data/lib/lutaml/xml/schema/xsd/choice.rb +68 -0
  679. data/lib/lutaml/xml/schema/xsd/complex_content.rb +39 -0
  680. data/lib/lutaml/xml/schema/xsd/complex_type.rb +159 -0
  681. data/lib/lutaml/xml/schema/xsd/documentation.rb +26 -0
  682. data/lib/lutaml/xml/schema/xsd/element.rb +117 -0
  683. data/lib/lutaml/xml/schema/xsd/enumeration.rb +25 -0
  684. data/lib/lutaml/xml/schema/xsd/errors/enhanced_error.rb +142 -0
  685. data/lib/lutaml/xml/schema/xsd/errors/error_context.rb +67 -0
  686. data/lib/lutaml/xml/schema/xsd/errors/message_builder.rb +103 -0
  687. data/lib/lutaml/xml/schema/xsd/errors/suggestion.rb +67 -0
  688. data/lib/lutaml/xml/schema/xsd/errors/troubleshooters/namespace_troubleshooter.rb +96 -0
  689. data/lib/lutaml/xml/schema/xsd/errors/troubleshooters/troubleshooting_handler.rb +70 -0
  690. data/lib/lutaml/xml/schema/xsd/errors.rb +148 -0
  691. data/lib/lutaml/xml/schema/xsd/extension_complex_content.rb +63 -0
  692. data/lib/lutaml/xml/schema/xsd/extension_simple_content.rb +48 -0
  693. data/lib/lutaml/xml/schema/xsd/field.rb +25 -0
  694. data/lib/lutaml/xml/schema/xsd/file_validation_result.rb +55 -0
  695. data/lib/lutaml/xml/schema/xsd/fraction_digits.rb +29 -0
  696. data/lib/lutaml/xml/schema/xsd/glob.rb +105 -0
  697. data/lib/lutaml/xml/schema/xsd/group.rb +79 -0
  698. data/lib/lutaml/xml/schema/xsd/import.rb +33 -0
  699. data/lib/lutaml/xml/schema/xsd/include.rb +31 -0
  700. data/lib/lutaml/xml/schema/xsd/key.rb +32 -0
  701. data/lib/lutaml/xml/schema/xsd/keyref.rb +34 -0
  702. data/lib/lutaml/xml/schema/xsd/length.rb +27 -0
  703. data/lib/lutaml/xml/schema/xsd/list.rb +29 -0
  704. data/lib/lutaml/xml/schema/xsd/max_exclusive.rb +25 -0
  705. data/lib/lutaml/xml/schema/xsd/max_inclusive.rb +23 -0
  706. data/lib/lutaml/xml/schema/xsd/max_length.rb +25 -0
  707. data/lib/lutaml/xml/schema/xsd/min_exclusive.rb +25 -0
  708. data/lib/lutaml/xml/schema/xsd/min_inclusive.rb +23 -0
  709. data/lib/lutaml/xml/schema/xsd/min_length.rb +25 -0
  710. data/lib/lutaml/xml/schema/xsd/namespace_mapping.rb +37 -0
  711. data/lib/lutaml/xml/schema/xsd/namespace_uri_remapping.rb +79 -0
  712. data/lib/lutaml/xml/schema/xsd/notation.rb +31 -0
  713. data/lib/lutaml/xml/schema/xsd/pattern.rb +25 -0
  714. data/lib/lutaml/xml/schema/xsd/redefine.rb +35 -0
  715. data/lib/lutaml/xml/schema/xsd/restriction_complex_content.rb +56 -0
  716. data/lib/lutaml/xml/schema/xsd/restriction_simple_content.rb +84 -0
  717. data/lib/lutaml/xml/schema/xsd/restriction_simple_type.rb +64 -0
  718. data/lib/lutaml/xml/schema/xsd/schema.rb +379 -0
  719. data/lib/lutaml/xml/schema/xsd/schema_file_validation_results.rb +55 -0
  720. data/lib/lutaml/xml/schema/xsd/schema_location_mapping.rb +74 -0
  721. data/lib/lutaml/xml/schema/xsd/schema_name_resolver.rb +32 -0
  722. data/lib/lutaml/xml/schema/xsd/schema_path.rb +207 -0
  723. data/lib/lutaml/xml/schema/xsd/schema_validator.rb +259 -0
  724. data/lib/lutaml/xml/schema/xsd/selector.rb +27 -0
  725. data/lib/lutaml/xml/schema/xsd/sequence.rb +68 -0
  726. data/lib/lutaml/xml/schema/xsd/serialized_schema.rb +185 -0
  727. data/lib/lutaml/xml/schema/xsd/simple_content.rb +61 -0
  728. data/lib/lutaml/xml/schema/xsd/simple_type.rb +37 -0
  729. data/lib/lutaml/xml/schema/xsd/total_digits.rb +29 -0
  730. data/lib/lutaml/xml/schema/xsd/type_index_entry.rb +40 -0
  731. data/lib/lutaml/xml/schema/xsd/type_resolution_result.rb +112 -0
  732. data/lib/lutaml/xml/schema/xsd/union.rb +30 -0
  733. data/lib/lutaml/xml/schema/xsd/unique.rb +31 -0
  734. data/lib/lutaml/xml/schema/xsd/validation_error.rb +48 -0
  735. data/lib/lutaml/xml/schema/xsd/validation_result.rb +86 -0
  736. data/lib/lutaml/xml/schema/xsd/version.rb +11 -0
  737. data/lib/lutaml/xml/schema/xsd/white_space.rb +27 -0
  738. data/lib/lutaml/xml/schema/xsd.rb +164 -0
  739. data/lib/lutaml/xml/schema/xsd_namespace.rb +29 -0
  740. data/lib/lutaml/xml/schema/xsd_schema.rb +1002 -0
  741. data/lib/lutaml/xml/schema.rb +16 -0
  742. data/lib/lutaml/xml/schema_location.rb +155 -0
  743. data/lib/lutaml/xml/serialization/collection_ext.rb +61 -0
  744. data/lib/lutaml/xml/serialization/format_conversion.rb +433 -0
  745. data/lib/lutaml/xml/serialization/instance_methods.rb +323 -0
  746. data/lib/lutaml/xml/serialization/model_import_ext.rb +40 -0
  747. data/lib/lutaml/xml/serialization.rb +15 -0
  748. data/lib/lutaml/xml/shared_dsl.rb +119 -0
  749. data/lib/lutaml/xml/transform.rb +6 -0
  750. data/lib/lutaml/xml/transformation/custom_method_wrapper.rb +258 -0
  751. data/lib/lutaml/xml/transformation/element_builder.rb +697 -0
  752. data/lib/lutaml/xml/transformation/order_reconciler.rb +282 -0
  753. data/lib/lutaml/xml/transformation/ordered_applier.rb +529 -0
  754. data/lib/lutaml/xml/transformation/rule_applier.rb +403 -0
  755. data/lib/lutaml/xml/transformation/rule_compiler.rb +556 -0
  756. data/lib/lutaml/xml/transformation/skip_logic.rb +70 -0
  757. data/lib/lutaml/xml/transformation/value_serializer.rb +137 -0
  758. data/lib/lutaml/xml/transformation.rb +454 -0
  759. data/lib/lutaml/xml/transformation_builder.rb +28 -0
  760. data/lib/lutaml/xml/transformation_support.rb +15 -0
  761. data/lib/lutaml/xml/type/configurable.rb +153 -0
  762. data/lib/lutaml/xml/type/serializers.rb +105 -0
  763. data/lib/lutaml/xml/type/value_xml_mapping.rb +94 -0
  764. data/lib/lutaml/xml/type_namespace/collector.rb +101 -0
  765. data/lib/lutaml/xml/type_namespace/declaration.rb +61 -0
  766. data/lib/lutaml/xml/type_namespace/planner.rb +121 -0
  767. data/lib/lutaml/xml/type_namespace/reference.rb +71 -0
  768. data/lib/lutaml/xml/type_namespace/resolver.rb +43 -0
  769. data/lib/lutaml/xml/type_namespace.rb +13 -0
  770. data/lib/lutaml/xml/type_namespace_resolver.rb +76 -0
  771. data/lib/lutaml/xml/unqualified_inheritance_strategy.rb +34 -0
  772. data/lib/lutaml/xml/w3c/registration.rb +66 -0
  773. data/lib/lutaml/xml/w3c.rb +398 -0
  774. data/lib/lutaml/xml/xml_attribute.rb +58 -0
  775. data/lib/lutaml/xml/xml_element.rb +498 -0
  776. data/lib/lutaml/xml/xml_namespace.rb +72 -0
  777. data/lib/lutaml/xml/xml_orderable.rb +17 -0
  778. data/lib/lutaml/xml/xsd_validator.rb +66 -0
  779. data/lib/lutaml/xml.rb +7 -0
  780. data/lib/lutaml/yaml/adapter/document.rb +12 -0
  781. data/lib/lutaml/yaml/adapter/mapping.rb +17 -0
  782. data/lib/lutaml/yaml/adapter/mapping_rule.rb +9 -0
  783. data/lib/lutaml/yaml/adapter/standard_adapter.rb +35 -0
  784. data/lib/lutaml/yaml/adapter/transform.rb +9 -0
  785. data/lib/lutaml/yaml/adapter/yeptris_adapter.rb +107 -0
  786. data/lib/lutaml/yaml/adapter.rb +18 -0
  787. data/lib/lutaml/yaml/format.rb +31 -0
  788. data/lib/lutaml/yaml/schema/yaml_schema.rb +65 -0
  789. data/lib/lutaml/yaml/schema.rb +9 -0
  790. data/lib/lutaml/yaml/type/serializers.rb +60 -0
  791. data/lib/lutaml/yaml.rb +7 -0
  792. data/lib/lutaml/yamlld/adapter.rb +20 -0
  793. data/lib/lutaml/yamlld.rb +25 -0
  794. data/lib/lutaml/yamls/adapter/document.rb +14 -0
  795. data/lib/lutaml/yamls/adapter/mapping.rb +30 -0
  796. data/lib/lutaml/yamls/adapter/mapping_rule.rb +9 -0
  797. data/lib/lutaml/yamls/adapter/standard_adapter.rb +56 -0
  798. data/lib/lutaml/yamls/adapter/transform.rb +124 -0
  799. data/lib/lutaml/yamls/adapter/yamls_sequence.rb +20 -0
  800. data/lib/lutaml/yamls/adapter/yamls_sequence_rule.rb +48 -0
  801. data/lib/lutaml/yamls/adapter.rb +15 -0
  802. data/lib/lutaml/yamls/format.rb +22 -0
  803. data/lib/lutaml/yamls.rb +7 -0
  804. data/lib/lutaml.rb +11 -5
  805. data/lib/tasks/benchmark_runner.rb +161 -0
  806. data/lib/tasks/memory_profile.rb +176 -0
  807. data/lib/tasks/performance.rake +80 -0
  808. data/lib/tasks/performance_benchmark.rb +499 -0
  809. data/lib/tasks/performance_comparator.rb +130 -0
  810. data/lib/tasks/performance_helpers.rb +149 -0
  811. data/lib/tasks/performance_probe.rake +33 -0
  812. data/lib/tasks/performance_probe.rb +106 -0
  813. data/lib/tasks/performance_probe_plan_walk.rb +58 -0
  814. data/lib/tasks/performance_probe_xml.rb +54 -0
  815. data/lib/tasks/xml_compiler_benchmark.rb +226 -0
  816. data/lib/tasks/xml_optimization_benchmark.rb +178 -0
  817. data/lutaml.gemspec +16 -8
  818. metadata +902 -83
  819. data/.github/workflows/docs.yml +0 -86
  820. data/.github/workflows/links.yml +0 -113
  821. data/.github/workflows/rake.yml +0 -23
  822. data/.github/workflows/release.yml +0 -37
  823. data/.gitignore +0 -29
  824. data/CLAUDE.md +0 -62
  825. data/docs/.github/workflows/docs.yml +0 -66
  826. data/docs/.github/workflows/links.yml +0 -78
  827. data/docs/CONTINUATION_PLAN.md +0 -168
  828. data/docs/Gemfile +0 -13
  829. data/docs/MIGRATION_GUIDE.md +0 -202
  830. data/docs/README.md +0 -252
  831. data/docs/REFACTOR_PLAN.md +0 -214
  832. data/docs/_config.yml +0 -144
  833. data/docs/_guides/cli-usage.adoc +0 -43
  834. data/docs/_guides/diagram-generation.adoc +0 -316
  835. data/docs/_guides/index.adoc +0 -136
  836. data/docs/_guides/parsing/express-parsing.adoc +0 -42
  837. data/docs/_guides/parsing/index.adoc +0 -78
  838. data/docs/_guides/parsing/qea-parsing.adoc +0 -47
  839. data/docs/_guides/parsing/xmi-parsing.adoc +0 -40
  840. data/docs/_guides/repository-management.adoc +0 -44
  841. data/docs/_guides/sysml-modeling.adoc +0 -37
  842. data/docs/_guides/uml-modeling.adoc +0 -115
  843. data/docs/_pages/concepts/diagram-generation.adoc +0 -337
  844. data/docs/_pages/concepts/lur-format.adoc +0 -711
  845. data/docs/_pages/concepts/qea-parsing.adoc +0 -418
  846. data/docs/_pages/concepts/uml-models.adoc +0 -43
  847. data/docs/_pages/core/architecture.adoc +0 -329
  848. data/docs/_pages/core/installation.adoc +0 -321
  849. data/docs/_pages/core/overview.adoc +0 -197
  850. data/docs/_pages/index.adoc +0 -49
  851. data/docs/_pages/uml-syntax.adoc +0 -1011
  852. data/docs/_references/api/parsers.adoc +0 -61
  853. data/docs/_references/api/presenters.adoc +0 -43
  854. data/docs/_references/api/repository.adoc +0 -67
  855. data/docs/_references/cli/commands.adoc +0 -72
  856. data/docs/_references/configuration/diagram-styles.adoc +0 -716
  857. data/docs/_references/configuration/index.adoc +0 -53
  858. data/docs/_references/configuration/package-metadata.adoc +0 -416
  859. data/docs/_references/formats/index.adoc +0 -43
  860. data/docs/_references/formats/lur-format.adoc +0 -39
  861. data/docs/_references/formats/lutaml-syntax.adoc +0 -379
  862. data/docs/_references/formats/qea-structure.adoc +0 -1364
  863. data/docs/_references/index.adoc +0 -198
  864. data/docs/_tutorials/documentation-site.adoc +0 -47
  865. data/docs/_tutorials/first-lur-package.adoc +0 -45
  866. data/docs/_tutorials/generating-diagrams.adoc +0 -42
  867. data/docs/_tutorials/getting-started.adoc +0 -45
  868. data/docs/_tutorials/index.adoc +0 -100
  869. data/docs/_tutorials/querying-models.adoc +0 -44
  870. data/docs/examples/build_package.rb +0 -12
  871. data/docs/examples/build_site.rb +0 -12
  872. data/docs/examples/city_model.rb +0 -21
  873. data/docs/examples/present_class.rb +0 -10
  874. data/docs/examples/query_model.rb +0 -12
  875. data/docs/examples/render_diagram.rb +0 -11
  876. data/docs/index.adoc +0 -64
  877. data/docs/lychee.toml +0 -50
  878. data/lib/lutaml/express/parsers/exp.rb +0 -28
  879. data/lib/lutaml/express.rb +0 -10
  880. data/scripts/check-site-urls.rb +0 -54
  881. data/scripts/check-vendor-link.py +0 -64
@@ -0,0 +1,1746 @@
1
+ module Lutaml
2
+ module Xml
3
+ class Mapping < ::Lutaml::Model::Mapping
4
+ TYPES = {
5
+ attribute: :map_attribute,
6
+ element: :map_element,
7
+ content: :map_content,
8
+ all_content: :map_all,
9
+ processing_instruction: :map_processing_instruction,
10
+ }.freeze
11
+
12
+ # Class-level XML DSL for reusable mapping classes.
13
+ #
14
+ # When a subclass of Lutaml::Xml::Mapping uses `xml do...end` in its
15
+ # class body, this method creates an instance and evaluates the block.
16
+ #
17
+ # @param block [Proc] DSL block with map_element, namespace_scope, etc.
18
+ # @return [Lutaml::Xml::Mapping] the mapping instance
19
+ #
20
+ # @example
21
+ # class BaseMapping < Lutaml::Xml::Mapping
22
+ # xml do
23
+ # namespace_scope [MyNamespace]
24
+ # map_element "Foo", to: :foo
25
+ # end
26
+ # end
27
+ def self.xml(&block)
28
+ @xml_instance ||= new
29
+ @xml_instance.instance_eval(&block) if block
30
+ @xml_instance
31
+ end
32
+
33
+ # Get the shared XML mapping instance for this mapping class.
34
+ # Created lazily via the xml() class method.
35
+ #
36
+ # @return [Lutaml::Xml::Mapping, nil]
37
+ def self.xml_mapping_instance
38
+ @xml_instance
39
+ end
40
+
41
+ attr_reader :root_element, :namespace_uri, :namespace_prefix, :element_sequence, :mappings_imported, :namespace_class, :namespace_param, :element_name, :documentation_text, :type_name_value, :namespace_scope, :mapper_class, :xml_space, :consolidation_maps, :processing_instruction_mappings, :any_element_rule, :all_attributes_rule
42
+
43
+ def initialize
44
+ super
45
+
46
+ @elements = {}
47
+ @attributes = {}
48
+ @any_element_rule = nil
49
+ @element_sequence = []
50
+ @content_mapping = nil
51
+ @raw_mapping = nil
52
+ @all_attributes_rule = nil
53
+ @mixed_content = false
54
+ @xml_space = nil
55
+ @format = :xml
56
+ @register_elements = ::Hash.new { |h, k| h[k] = {} }
57
+ @register_attributes = ::Hash.new { |h, k| h[k] = {} }
58
+ @register_element_sequences = ::Hash.new { |h, k| h[k] = [] }
59
+ @consolidation_maps = []
60
+ @element_name = nil
61
+ @namespace_class = nil
62
+ @documentation_text = nil
63
+ @type_name_value = nil
64
+ @namespace_scope = []
65
+ @namespace_scope_config = []
66
+ @mapper_class = nil
67
+ @importing_mappings = false
68
+ @attributes_with_methods_defined = Set.new
69
+ @processing_instruction_mappings = []
70
+
71
+ # Performance: Caches for finalized mapping queries
72
+ @cached_elements = {}
73
+ @cached_attributes = {}
74
+ @cached_mappings = {}
75
+ @cached_attribute_name_counts = {}
76
+ @finalized = false
77
+ end
78
+
79
+ def finalize(mapper_class)
80
+ # Store mapper class for later use in deferred imports
81
+ @mapper_class = mapper_class
82
+
83
+ # DO NOT auto-generate root element
84
+ # Models should explicitly declare root in their xml block if needed
85
+ # Type-only models (used as nested types) don't need a root element
86
+
87
+ # Resolve any deferred mapping imports before finalizing
88
+ ensure_mappings_imported!
89
+
90
+ # lutaml-model#687: nested sequence choices tag their target
91
+ # model attributes now that the mapper class is known.
92
+ @element_sequence.each do |seq|
93
+ seq.bind_choice_compositors!(mapper_class)
94
+ end
95
+
96
+ # lutaml-model#296: a mapped rule whose attribute type can never
97
+ # accept the serialization shape fails here, with the attribute
98
+ # and type named, instead of surfacing mid-parse as a cast oddity.
99
+ validate_mapped_attribute_types!(mapper_class)
100
+
101
+ # Validate mixed content requires collection attribute for content mapping
102
+ validate_mixed_content_collection!(mapper_class)
103
+
104
+ # Validate element-only content models do not use map_content
105
+ validate_ordered_content_mapping!(mapper_class)
106
+
107
+ # Performance: Clear caches and mark finalized
108
+ @cached_elements.clear
109
+ @cached_attributes.clear
110
+ @cached_mappings.clear
111
+ @cached_attribute_name_counts.clear
112
+ @rule_records_version = (@rule_records_version || 0) + 1
113
+ @finalized = true
114
+ end
115
+
116
+ # Bumped at finalize; ModelTransform's compiled rule records are
117
+ # keyed on it so late mapping changes rebuild instead of going stale.
118
+ def rule_records_version
119
+ @rule_records_version ||= 0
120
+ end
121
+
122
+ def finalized?
123
+ @finalized
124
+ end
125
+
126
+ # Validate that mixed content models have a collection attribute for
127
+ # their content mapping. When mixed_content is enabled, the XML element
128
+ # can contain interleaved text and child elements, resulting in multiple
129
+ # text nodes. The content attribute must be a string collection to hold
130
+ # all of them; otherwise only the first text node is preserved.
131
+ def validate_mixed_content_collection!(mapper_class)
132
+ return unless @mixed_content && @content_mapping
133
+
134
+ attr_name = @content_mapping.to
135
+ return unless attr_name
136
+
137
+ # Follow delegate chain to find the actual attribute definition
138
+ delegate = @content_mapping.delegate
139
+ attr_def = if delegate
140
+ delegate_obj = mapper_class.attributes[delegate]
141
+ delegate_obj&.type&.attributes&.dig(attr_name) if delegate_obj&.type.is_a?(Class) && delegate_obj&.type.include?(Lutaml::Model::Serialize)
142
+ else
143
+ mapper_class.attributes[attr_name]
144
+ end
145
+ return unless attr_def
146
+ return if attr_def.collection?
147
+
148
+ raise Lutaml::Model::MixedContentCollectionError.new(attr_name,
149
+ mapper_class)
150
+ end
151
+
152
+ def validate_ordered_content_mapping!(mapper_class)
153
+ return unless @ordered && !@mixed_content && @content_mapping
154
+
155
+ raise Lutaml::Model::OrderedContentMappingError.new(mapper_class)
156
+ end
157
+
158
+ # Enable mixed content for this element
159
+ #
160
+ # Mixed content means the element can contain both text nodes
161
+ # and child elements interspersed.
162
+ #
163
+ # When using mixed_content with map_content, the mapped attribute
164
+ # MUST be a string collection, e.g.:
165
+ #
166
+ # attribute :content, :string, collection: true
167
+ #
168
+ # @return [Boolean] true
169
+ def mixed_content
170
+ @mixed_content = true
171
+ @ordered = true
172
+ end
173
+
174
+ # Check if mixed content is enabled
175
+ # @return [Boolean] true if mixed content is enabled
176
+ def mixed_content?
177
+ @mixed_content || false
178
+ end
179
+
180
+ # Return whether this mapping uses ordered content
181
+ # @return [Boolean, nil] true if ordered, nil if not set
182
+ def ordered?
183
+ @ordered
184
+ end
185
+
186
+ # Set the XML element name for this mapping
187
+ #
188
+ # This is the primary method for declaring the element name.
189
+ # Use this for the new clean API.
190
+ #
191
+ # @param name [String] the element name
192
+ # @return [String] the element name
193
+ def element(name)
194
+ @element_name = name
195
+ @root_element = name # Maintain backward compatibility
196
+ end
197
+
198
+ # Set the root element name with optional configuration
199
+ #
200
+ # This is kept as an alias to element() for backward compatibility,
201
+ # but also supports the mixed: and ordered: options.
202
+ #
203
+ # @param name [String] the root element name
204
+ # @param mixed [Boolean] whether content is mixed (text + elements)
205
+ # @param ordered [Boolean] whether to preserve element order
206
+ # @return [String] the root element name
207
+ def root(name, mixed: false, ordered: false)
208
+ element(name)
209
+ @mixed_content = mixed
210
+ @ordered = ordered || mixed # mixed content is always ordered
211
+ end
212
+
213
+ # Enable mixed content for this element
214
+ #
215
+ # Mixed content means the element can contain both text nodes
216
+ # and child elements interspersed.
217
+ #
218
+ # @return [Boolean] true
219
+ def enable_mixed_content
220
+ @mixed_content = true
221
+ @ordered = true # mixed content implies ordered
222
+ end
223
+
224
+ # Alias for enable_mixed_content for DSL usage
225
+ # @return [Boolean] true
226
+ alias mixed_content enable_mixed_content
227
+
228
+ # Control the xml:space attribute for this element
229
+ #
230
+ # When set to :preserve, automatically adds `xml:space="preserve"`
231
+ # during serialization. When set to :default, adds `xml:space="default"`.
232
+ # When false or nil, no xml:space attribute is added.
233
+ #
234
+ # @param value [Symbol, Boolean, nil] :preserve, :default, false, or nil
235
+ # @return [Symbol, nil] the xml_space value
236
+ #
237
+ # @example Add xml:space="preserve"
238
+ # xml do
239
+ # element "text"
240
+ # mixed_content
241
+ # xml_space :preserve # Adds xml:space="preserve"
242
+ # end
243
+ #
244
+ # @example Add xml:space="default"
245
+ # xml do
246
+ # element "text"
247
+ # xml_space :default
248
+ # end
249
+ def xml_space(value = nil)
250
+ @xml_space = value if value
251
+ @xml_space
252
+ end
253
+
254
+ # Whether this mapping should auto-add xml:space="preserve"
255
+ #
256
+ # @return [Boolean]
257
+ def preserve_whitespace?
258
+ @xml_space == :preserve
259
+ end
260
+
261
+ # Convenience method for standard W3C XML attributes
262
+ #
263
+ # Automatically creates attribute mappings for attributes that use
264
+ # W3C types. The attribute must already be declared with a W3C type.
265
+ #
266
+ # Available options: :lang, :space, :base, :id
267
+ #
268
+ # @param attrs [Array<Symbol>] List of W3C attribute names to auto-map
269
+ # @example
270
+ # class Paragraph < Lutaml::Model::Serializable
271
+ # attribute :lang, Lutaml::Xml::W3c::XmlLangType
272
+ # attribute :space, Lutaml::Xml::W3c::XmlSpaceType
273
+ # attribute :content, :string
274
+ #
275
+ # xml do
276
+ # element "p"
277
+ # w3c_attributes :lang, :space # Maps to xml:lang and xml:space
278
+ # map_content to: :content
279
+ # end
280
+ # end
281
+ def w3c_attributes(*attrs)
282
+ attrs.each do |attr|
283
+ map_attribute attr.to_s, to: attr
284
+ end
285
+ end
286
+
287
+ # Enable ordered content for this element
288
+ #
289
+ # Ordered content means element order is preserved during
290
+ # round-trip serialization without validation.
291
+ # This is different from `sequence` which enforces and validates order.
292
+ #
293
+ # Use this when:
294
+ # - Element order matters for your application
295
+ # - You need to preserve input order exactly
296
+ # - You DON'T want to validate/enforce specific order
297
+ #
298
+ # Use `sequence` when you need strict order validation.
299
+ #
300
+ # @return [Boolean] true
301
+ def ordered
302
+ @ordered = true
303
+ end
304
+
305
+ def root?
306
+ !!root_element
307
+ end
308
+
309
+ # Mark this model as having no root element (type-only)
310
+ #
311
+ # @deprecated Use absence of element() declaration and type_name() instead
312
+ def no_root
313
+ warn "[Lutaml::Model] DEPRECATED: no_root is deprecated. " \
314
+ "Simply omit the element declaration for type-only models. " \
315
+ "Use type_name() to set the XSD type name."
316
+ @no_root = true
317
+ end
318
+
319
+ # Check if this mapping has no root element
320
+ #
321
+ # Returns true if:
322
+ # - The deprecated @no_root flag is explicitly set, OR
323
+ # - No element is declared (modern pattern - just omit element() call)
324
+ #
325
+ # @return [Boolean] true if no root element
326
+ def no_root?
327
+ !!@no_root || no_element?
328
+ end
329
+
330
+ # Check if this mapping has no element declaration
331
+ #
332
+ # @return [Boolean] true if no element is declared
333
+ def no_element?
334
+ !element_name
335
+ end
336
+
337
+ def prefixed_root
338
+ if namespace_uri && namespace_prefix
339
+ "#{namespace_prefix}:#{root_element}"
340
+ else
341
+ root_element
342
+ end
343
+ end
344
+
345
+ # Set the XML namespace for this mapping
346
+ #
347
+ # @param ns_class_or_symbol [Class, Symbol] XmlNamespace class or :blank/:inherit
348
+ # @param _deprecated_prefix [String, nil] DEPRECATED - no longer used
349
+ # @return [void]
350
+ #
351
+ # @example Using XmlNamespace class (REQUIRED)
352
+ # namespace ContactNamespace
353
+ #
354
+ # @example Using :inherit to inherit parent namespace
355
+ # namespace :inherit
356
+ #
357
+ # @example Using :blank for explicit no namespace
358
+ # namespace :blank
359
+ #
360
+ # @raise [ArgumentError] if invalid arguments provided
361
+ # @raise [Lutaml::Model::TypeOnlyNamespaceError] if explicitly marked as no_root
362
+ def namespace(ns_class_or_symbol, _deprecated_prefix = nil)
363
+ # Only raise error for explicitly marked no_root (using deprecated method)
364
+ # Type-only models (no element declared) CAN have namespaces
365
+ raise Lutaml::Model::TypeOnlyNamespaceError if @no_root
366
+
367
+ # Warn if prefix parameter is provided
368
+ if _deprecated_prefix
369
+ warn "[DEPRECATED] The prefix parameter on namespace() is deprecated. " \
370
+ "Define prefix_default in your XmlNamespace class instead. " \
371
+ "Prefix '#{_deprecated_prefix}' will be ignored."
372
+ end
373
+
374
+ # Handle :blank symbol - explicit blank namespace
375
+ if ns_class_or_symbol == :blank
376
+ @namespace_class = nil
377
+ @namespace_uri = nil
378
+ @namespace_prefix = nil
379
+ @namespace_set = true # Mark as explicitly set
380
+ @namespace_param = :blank # Store original value
381
+ return
382
+ end
383
+
384
+ # Handle :inherit symbol
385
+ if ns_class_or_symbol == :inherit
386
+ @namespace_set = true
387
+ @namespace_param = :inherit
388
+ return
389
+ end
390
+
391
+ # nil means "not set" - DON'T set @namespace_set
392
+ if ns_class_or_symbol.nil?
393
+ @namespace_class = nil
394
+ @namespace_uri = nil
395
+ @namespace_prefix = nil
396
+ @namespace_set = false # Explicitly NOT set
397
+ @namespace_param = nil
398
+ return
399
+ end
400
+
401
+ # Accept both Lutaml::Xml::Namespace and Lutaml::Model::Xml::Namespace for compatibility
402
+ is_namespace_class = ns_class_or_symbol.is_a?(Class) && (
403
+ (defined?(::Lutaml::Xml::Namespace) && ns_class_or_symbol < ::Lutaml::Xml::Namespace) ||
404
+ (defined?(::Lutaml::Model::Xml::Namespace) && ns_class_or_symbol < ::Lutaml::Model::Xml::Namespace)
405
+ )
406
+
407
+ if is_namespace_class
408
+ # XmlNamespace class passed - register and use
409
+ @namespace_class = NamespaceClassRegistry.instance.register_named(ns_class_or_symbol)
410
+ @namespace_uri = ns_class_or_symbol.uri
411
+ @namespace_prefix = ns_class_or_symbol.prefix_default
412
+ elsif ns_class_or_symbol.is_a?(String)
413
+ # String URI - NOT SUPPORTED
414
+ raise Lutaml::Xml::Error::InvalidNamespaceError.new(
415
+ expected: "XmlNamespace class",
416
+ got: ns_class_or_symbol,
417
+ message: "String namespace URIs are not supported. " \
418
+ "Define an XmlNamespace class instead. " \
419
+ "See docs/_guides/xml-namespaces.adoc for migration guide.",
420
+ )
421
+ else
422
+ raise Lutaml::Xml::Error::InvalidNamespaceError.new(
423
+ expected: "XmlNamespace class, :inherit, or :blank",
424
+ got: ns_class_or_symbol,
425
+ )
426
+ end
427
+ end
428
+
429
+ # Set the namespace scope for this mapping
430
+ #
431
+ # Controls which namespaces are declared at the root element level
432
+ # versus being declared locally on each element that uses them.
433
+ #
434
+ # Namespaces listed in namespace_scope will be declared once on the
435
+ # root element. Namespaces not listed will be declared locally on
436
+ # elements where they are used.
437
+ #
438
+ # @param namespaces [Array<Class>, Array<Hash>] array of XmlNamespace classes
439
+ # or hashes with :namespace and :declare keys
440
+ # @return [Array<Class>] the current namespace scope
441
+ #
442
+ # @example Simple array of namespace classes (all default to :auto)
443
+ # namespace_scope [VcardNamespace, DctermsNamespace, DcElementsNamespace]
444
+ #
445
+ # @example Per-namespace declaration control
446
+ # namespace_scope [
447
+ # { namespace: VcardNamespace, declare: :always },
448
+ # { namespace: DctermsNamespace, declare: :auto },
449
+ # XsiNamespace # Can mix hash and class entries
450
+ # ]
451
+ def namespace_scope(namespaces = nil)
452
+ if namespaces
453
+ validate_namespace_scope!(namespaces)
454
+
455
+ # Convert to normalized format
456
+ @namespace_scope_config = normalize_namespace_scope(namespaces)
457
+ @namespace_scope = @namespace_scope_config.map do |cfg|
458
+ cfg[:namespace]
459
+ end
460
+ end
461
+ @namespace_scope
462
+ end
463
+
464
+ # Set documentation text for this mapping
465
+ #
466
+ # Used for XSD annotation generation.
467
+ #
468
+ # @param text [String] the documentation text
469
+ # @return [String] the documentation text
470
+ def documentation(text)
471
+ @documentation_text = text
472
+ end
473
+
474
+ # Set explicit type name for XSD generation
475
+ #
476
+ # By default, type name is inferred as "ClassNameType".
477
+ # Use this to override.
478
+ #
479
+ # @param name [String, nil] the type name
480
+ # @return [String, nil] the type name
481
+ def type_name(name = nil)
482
+ @type_name_value = name if name
483
+ @type_name_value
484
+ end
485
+
486
+ # Alias for type_name - both methods are equivalent
487
+ #
488
+ # xsd_type and type_name set the XSD complexType/simpleType name
489
+ # for schema generation. Use type_name for clarity (recommended).
490
+ #
491
+ # @param name [String, nil] the type name
492
+ # @return [String, nil] the type name
493
+ alias xsd_type type_name
494
+
495
+ # Catch-all element mapping: every child element with no
496
+ # explicit rule routes to one collection attribute. For
497
+ # parse-side consumption of dynamic vocabularies (semantic__
498
+ # trees); element_order preserves the original tag names.
499
+ # Serialization of a catch-all attribute emits the attribute's
500
+ # declared element name (not per-item originals).
501
+ def map_any_element(to:, render_nil: false, render_default: false)
502
+ validate!(nil, to, {}, render_nil, false, type: TYPES[:element])
503
+ @any_element_rule = MappingRule.new(
504
+ nil,
505
+ to: to,
506
+ render_nil: render_nil,
507
+ render_default: render_default,
508
+ )
509
+ @element_rules_index = nil if instance_variable_defined?(:@element_rules_index)
510
+ @any_element_rule
511
+ end
512
+
513
+ def map_instances(to:, polymorphic: {})
514
+ map_element(to, to: to, polymorphic: polymorphic)
515
+ end
516
+
517
+ def map_element(
518
+ name,
519
+ to: nil,
520
+ render_nil: false,
521
+ render_default: false,
522
+ render_empty: false,
523
+ treat_nil: :nil,
524
+ treat_empty: :empty,
525
+ treat_omitted: :nil,
526
+ with: {},
527
+ delegate: nil,
528
+ cdata: false,
529
+ polymorphic: {},
530
+ namespace: (namespace_set = false
531
+ nil),
532
+ prefix: (prefix_provided = false
533
+ nil),
534
+ transform: {},
535
+ value_map: {},
536
+ when_attribute: {},
537
+ unmatched: :drop,
538
+ form: nil,
539
+ documentation: nil,
540
+ xsd_type: (xsd_type_provided = false
541
+ nil),
542
+ raw: nil
543
+ )
544
+ validate!(
545
+ name, to, with, render_nil, render_empty, type: TYPES[:element]
546
+ )
547
+
548
+ # lutaml-model#88: grouped form — `when_attribute: "type"` plus
549
+ # `to: { value => attribute }` expands to one rule per value at
550
+ # DSL time, so the compiled rules are identical to the per-rule
551
+ # form and nothing downstream changes.
552
+ if when_attribute.is_a?(::String) || when_attribute.is_a?(::Symbol)
553
+ # These locals are `false` when their kwarg default ran and
554
+ # nil when the caller passed the kwarg explicitly (planned
555
+ # locals read as nil, so defined? is useless here).
556
+ if namespace_set.nil? || prefix_provided.nil? ||
557
+ xsd_type_provided.nil?
558
+ raise Lutaml::Model::IncorrectMappingArgumentsError,
559
+ "namespace/prefix/xsd_type are not supported in the " \
560
+ "grouped when_attribute form; declare rules individually"
561
+ end
562
+
563
+ when_attribute_group(when_attribute, to).each do |value, target|
564
+ map_element(
565
+ name,
566
+ to: target,
567
+ render_nil: render_nil,
568
+ render_default: render_default,
569
+ render_empty: render_empty,
570
+ treat_nil: treat_nil,
571
+ treat_empty: treat_empty,
572
+ treat_omitted: treat_omitted,
573
+ with: with,
574
+ delegate: delegate,
575
+ cdata: cdata,
576
+ polymorphic: polymorphic,
577
+ transform: transform,
578
+ value_map: value_map,
579
+ when_attribute: { when_attribute.to_s => value.to_s },
580
+ unmatched: unmatched,
581
+ form: form,
582
+ documentation: documentation,
583
+ raw: raw,
584
+ )
585
+ end
586
+ return
587
+ end
588
+ reject_ambiguous_when_attribute!(when_attribute, to)
589
+
590
+ validate_when_attribute!(when_attribute) unless when_attribute.empty?
591
+ validate_unmatched!(unmatched, when_attribute)
592
+
593
+ # Warn if prefix parameter is provided
594
+ if prefix_provided != false
595
+ warn "[DEPRECATED] The prefix parameter on map_element is deprecated. " \
596
+ "Define prefix_default in your XmlNamespace class instead. " \
597
+ "Prefix '#{prefix}' will be ignored."
598
+ end
599
+
600
+ # Raise error if xsd_type parameter is provided
601
+ if xsd_type_provided != false
602
+ raise Lutaml::Model::IncorrectMappingArgumentsError,
603
+ "xsd_type is not allowed at mapping level. " \
604
+ "XSD type must be declared in Type::Value classes using the xsd_type directive. " \
605
+ "See docs/migration-guides/xsd-type-migration.adoc"
606
+ end
607
+
608
+ # Raise error if namespace parameter is a non-nil value at element level
609
+ # This is NOT allowed - namespaces should be declared at model level
610
+ # However, namespace: nil is allowed to explicitly opt out of namespace
611
+ # Note: namespace_set is false when default is used, nil when explicit arg passed
612
+ if namespace_set != false && !namespace.nil?
613
+ raise Lutaml::Model::IncorrectMappingArgumentsError,
614
+ "namespace is not allowed at element mapping level. " \
615
+ "Namespaces must be declared on the MODEL CLASS itself using 'namespace' at the xml block level. " \
616
+ "Each model class should declare its own namespace, not individual elements."
617
+ end
618
+
619
+ rule = MappingRule.new(
620
+ name,
621
+ to: to,
622
+ render_nil: render_nil,
623
+ render_default: render_default,
624
+ render_empty: render_empty,
625
+ treat_nil: treat_nil,
626
+ treat_empty: treat_empty,
627
+ treat_omitted: treat_omitted,
628
+ with: with,
629
+ delegate: delegate,
630
+ cdata: cdata,
631
+ namespace: namespace,
632
+ default_namespace: namespace_uri,
633
+ polymorphic: polymorphic,
634
+ namespace_set: namespace_set != false || namespace == :inherit,
635
+ transform: transform,
636
+ value_map: value_map,
637
+ form: form,
638
+ documentation: documentation,
639
+ raw: raw,
640
+ when_attribute: when_attribute,
641
+ unmatched: unmatched,
642
+ )
643
+ # Store rules with the same element name in an array to support
644
+ # multiple mapping rules for the same element name with different target types
645
+ # Use eql? to detect and skip exact duplicates (prevents accumulation)
646
+ key = rule.namespaced_name
647
+ existing = @elements[key]
648
+
649
+ if existing.nil?
650
+ # New mapping - store directly
651
+ @elements[key] = rule
652
+ elsif existing.is_a?(Array)
653
+ # Array exists - check for duplicate or add
654
+ duplicate_index = existing.find_index { |r| r.eql?(rule) }
655
+ # Only add if not a duplicate
656
+ existing << rule unless duplicate_index
657
+ elsif existing.eql?(rule)
658
+ # Exact duplicate - already stored, no action needed
659
+ else
660
+ # Different mapping (polymorphic) - convert to array
661
+ @elements[key] = [existing, rule]
662
+ end
663
+ end
664
+
665
+ def map_attribute(
666
+ name,
667
+ to: nil,
668
+ render_nil: false,
669
+ render_default: false,
670
+ render_empty: false,
671
+ with: {},
672
+ delegate: nil,
673
+ polymorphic_map: {},
674
+ namespace: (namespace_set = false
675
+ nil),
676
+ prefix: (prefix_provided = false
677
+ nil),
678
+ transform: {},
679
+ value_map: {},
680
+ as_list: nil,
681
+ delimiter: nil,
682
+ form: nil,
683
+ documentation: nil,
684
+ xsd_type: (xsd_type_provided = false
685
+ nil)
686
+ )
687
+ validate!(
688
+ name, to, with, render_nil, render_empty, type: TYPES[:attribute]
689
+ )
690
+
691
+ # Warn if prefix parameter is provided
692
+ if prefix_provided != false
693
+ warn "[DEPRECATED] The prefix parameter on map_attribute is deprecated. " \
694
+ "Define prefix_default in your XmlNamespace class instead. " \
695
+ "Prefix '#{prefix}' will be ignored."
696
+ end
697
+
698
+ # Raise error if xsd_type parameter is provided
699
+ if xsd_type_provided != false
700
+ raise Lutaml::Model::IncorrectMappingArgumentsError,
701
+ "xsd_type is not allowed at mapping level. " \
702
+ "XSD type must be declared in Type::Value classes using the xsd_type directive. " \
703
+ "See docs/migration-guides/xsd-type-migration.adoc"
704
+ end
705
+
706
+ # Raise error if namespace parameter is a non-nil value at attribute level
707
+ # This is NOT allowed - namespaces should be declared at model level
708
+ # However, namespace: nil is allowed to explicitly opt out of namespace
709
+ # Note: namespace_set is false when default is used, nil when explicit arg passed
710
+ if namespace_set != false && !namespace.nil?
711
+ raise Lutaml::Model::IncorrectMappingArgumentsError,
712
+ "namespace is not allowed at attribute mapping level. " \
713
+ "Namespaces should be declared on the MODEL CLASS itself using 'namespace' at the xml block level."
714
+ end
715
+
716
+ if name == "schemaLocation"
717
+ location = caller_locations(1, 1)[0]
718
+ caller_file = if defined?(File)
719
+ File.basename(location.path)
720
+ else
721
+ location.path.to_s.split("/").last
722
+ end
723
+ ::Lutaml::Model::Logger.warn_auto_handling(
724
+ name: name,
725
+ caller_file: caller_file,
726
+ caller_line: location.lineno,
727
+ )
728
+ end
729
+
730
+ rule = MappingRule.new(
731
+ name,
732
+ to: to,
733
+ render_nil: render_nil,
734
+ render_default: render_default,
735
+ render_empty: render_empty,
736
+ with: with,
737
+ delegate: delegate,
738
+ namespace: namespace,
739
+ attribute: true,
740
+ polymorphic_map: polymorphic_map,
741
+ default_namespace: namespace_uri,
742
+ namespace_set: namespace_set != false,
743
+ transform: transform,
744
+ value_map: value_map,
745
+ as_list: as_list,
746
+ delimiter: delimiter,
747
+ form: form,
748
+ documentation: documentation,
749
+ )
750
+ # Use eql? to detect and skip exact duplicates (prevents accumulation)
751
+ key = rule.namespaced_name
752
+ existing = @attributes[key]
753
+
754
+ if existing.nil?
755
+ # New mapping - store directly
756
+ @attributes[key] = rule
757
+ elsif existing.is_a?(Array)
758
+ # Array exists - check for duplicate or add
759
+ duplicate_index = existing.find_index { |r| r.eql?(rule) }
760
+ # Only add if not a duplicate
761
+ existing << rule unless duplicate_index
762
+ elsif existing.eql?(rule)
763
+ # Exact duplicate - already stored, no action needed
764
+ else
765
+ # Different mapping (polymorphic) - convert to array
766
+ @attributes[key] = [existing, rule]
767
+ end
768
+ end
769
+
770
+ def map_content(
771
+ to: nil,
772
+ render_nil: false,
773
+ render_default: false,
774
+ render_empty: false,
775
+ with: {},
776
+ delegate: nil,
777
+ mixed: false,
778
+ cdata: false,
779
+ transform: {},
780
+ value_map: {}
781
+ )
782
+ validate!(
783
+ "content", to, with, render_nil, render_empty, type: TYPES[:content]
784
+ )
785
+
786
+ @content_mapping = MappingRule.new(
787
+ nil,
788
+ to: to,
789
+ render_nil: render_nil,
790
+ render_default: render_default,
791
+ render_empty: render_empty,
792
+ with: with,
793
+ delegate: delegate,
794
+ mixed_content: mixed,
795
+ cdata: cdata,
796
+ transform: transform,
797
+ value_map: value_map,
798
+ )
799
+ end
800
+
801
+ def map_all(
802
+ to:,
803
+ render_nil: false,
804
+ render_default: false,
805
+ delegate: nil,
806
+ with: {},
807
+ namespace: (namespace_set = false
808
+ nil),
809
+ prefix: (prefix_provided = false
810
+ nil),
811
+ render_empty: false
812
+ )
813
+ validate!(
814
+ ::Lutaml::Model::Constants::RAW_MAPPING_KEY,
815
+ to,
816
+ with,
817
+ render_nil,
818
+ render_empty,
819
+ type: TYPES[:all_content],
820
+ )
821
+
822
+ # Warn if prefix parameter is provided
823
+ if prefix_provided != false
824
+ warn "[DEPRECATED] The prefix parameter on map_all is deprecated. " \
825
+ "Define prefix_default in your XmlNamespace class instead. " \
826
+ "Prefix '#{prefix}' will be ignored."
827
+ end
828
+
829
+ # Raise error if namespace parameter is provided
830
+ if namespace_set != false
831
+ raise Lutaml::Model::IncorrectMappingArgumentsError,
832
+ "namespace is not allowed at map_all level. " \
833
+ "Namespaces must be declared on the MODEL CLASS itself using 'namespace' at the xml block level."
834
+ end
835
+
836
+ rule = MappingRule.new(
837
+ ::Lutaml::Model::Constants::RAW_MAPPING_KEY,
838
+ to: to,
839
+ render_nil: render_nil,
840
+ render_default: render_default,
841
+ with: with,
842
+ delegate: delegate,
843
+ namespace: namespace,
844
+ default_namespace: namespace_uri,
845
+ namespace_set: namespace_set != false,
846
+ )
847
+
848
+ @raw_mapping = rule
849
+ end
850
+
851
+ alias map_all_content map_all
852
+
853
+ # Map XML processing instructions with a given target to a model attribute.
854
+ #
855
+ # During serialization, the attribute value (Hash) is expanded into
856
+ # individual PIs: each key-value pair generates `<?target key="value"?>`.
857
+ # During deserialization, PIs with matching target are parsed into the hash.
858
+ #
859
+ # @param target [String] the PI target name (e.g., "rfc")
860
+ # @param to [Symbol] the model attribute name
861
+ #
862
+ # @example
863
+ # xml do
864
+ # element "rfc"
865
+ # map_processing_instruction "rfc", to: :pi_settings
866
+ # end
867
+ #
868
+ # # With pi_settings = { "strict" => "yes", "compact" => "yes" }
869
+ # # generates:
870
+ # # <?rfc strict="yes"?>
871
+ # # <?rfc compact="yes"?>
872
+ # # <rfc>...</rfc>
873
+ def map_processing_instruction(target, to:)
874
+ @processing_instruction_mappings << ProcessingInstructionMapping.new(
875
+ target.to_s, to
876
+ )
877
+ end
878
+
879
+ # Capture every attribute not claimed by an explicit map_attribute
880
+ # (and matching an optional pattern) into a Hash attribute, and emit
881
+ # it back as attributes after the mapped ones on serialization —
882
+ # the remainder semantics of map_all, for attribute families
883
+ # (lutaml-model#1031): HTML data-*, ARIA aria-*, on* handlers.
884
+ #
885
+ # @param to [Symbol] the Hash attribute name
886
+ # @param matching [Regexp, nil] optional name filter; nil captures
887
+ # every unclaimed attribute
888
+ #
889
+ # @example
890
+ # xml do
891
+ # element "mn"
892
+ # map_attribute "name", to: :name
893
+ # map_all_attributes to: :other_attributes, matching: /\A(data-|aria-|on)/
894
+ # end
895
+ def map_all_attributes(to:, matching: nil)
896
+ @all_attributes_rule = AllAttributesMapping.new(to, matching)
897
+ end
898
+
899
+ def sequence(&block)
900
+ @element_sequence << ::Lutaml::Model::Sequence.new(self,
901
+ format: :xml).tap do |s|
902
+ s.instance_eval(&block)
903
+ end
904
+ end
905
+
906
+ # Declare consolidation rules for this mapping.
907
+ #
908
+ # Creates a ConsolidationMap that describes how sibling elements
909
+ # are grouped into structured model instances.
910
+ #
911
+ # @param by [Symbol] grouping criterion (:attr_name or :pattern)
912
+ # @param to [Symbol] target attribute name on the Collection
913
+ # @param group_class [Class] the GroupClass to instantiate (optional, resolved from Organization)
914
+ # @yield Builder block with gather/dispatch_by or map_element/map_content
915
+ # @return [ConsolidationMap]
916
+ def consolidate_map(by:, to:, group_class: nil, &block)
917
+ builder = ::Lutaml::Model::ConsolidationMap::Builder.new(by, to,
918
+ group_class)
919
+ builder.instance_eval(&block)
920
+ @consolidation_maps << builder.build
921
+ end
922
+
923
+ # Import mappings from another model.
924
+ # For default register: imports BOTH attributes (into @mapper_class) AND mappings.
925
+ # For non-default register: stores mappings in register-specific storage
926
+ # (@register_elements, @register_attributes).
927
+ #
928
+ # @param model [Class] The model to import from
929
+ # @param register_id [Symbol, nil] The register context
930
+ def import_model_mappings(model, register_id = nil)
931
+ register_id ||= Lutaml::Model::Config.default_register
932
+ reg_id = register_id
933
+ return import_mappings_later(model, reg_id) if model_importable?(model)
934
+ raise Lutaml::Model::ImportModelWithRootError.new(model) if model.root?(reg_id)
935
+
936
+ if register_id != :default
937
+ register_only_import_model_mappings(model, register_id)
938
+ return
939
+ end
940
+
941
+ # CRITICAL: Access raw mapping structure directly without triggering import resolution
942
+ # Calling model.mappings_for() can trigger ensure_imports! on that model
943
+ # which creates circular chains: A imports B, B's mappings_for imports C, C imports A
944
+ # Architecture: Access data structure directly, don't call methods that trigger resolutions
945
+ mappings = model.instance_variable_get(:@mappings)&.dig(:xml)
946
+ return unless mappings # Skip if no XML mappings defined
947
+
948
+ # ATOMIC IMPORT: Both object model (attributes) AND serialization mappings
949
+ # This mimics XSD complexType composition where importing a type means
950
+ # getting both its structure (attributes) and serialization rules (mappings)
951
+
952
+ # 1. Import object model (attributes with accessors)
953
+ # This defines the data structure of the model
954
+ # CRITICAL: Access attributes directly to avoid triggering ensure_imports!
955
+ imported_attributes = ::Lutaml::Model::Utils.deep_dup(model.instance_variable_get(:@attributes)&.values || [])
956
+ if @mapper_class
957
+ imported_attributes.each do |attr|
958
+ # CRITICAL: Check LOCAL set to avoid calling @mapper_class.attributes()
959
+ # which can trigger ensure_imports! and create circular calls
960
+ unless @attributes_with_methods_defined.include?(attr.name)
961
+ # Define accessor methods on the model class
962
+ @mapper_class.define_attribute_methods(attr, reg_id)
963
+ @attributes_with_methods_defined.add(attr.name)
964
+ end
965
+ end
966
+ # Merge attributes data - use direct access to avoid triggering imports
967
+ attrs_hash = imported_attributes.to_h { |attr| [attr.name, attr] }
968
+ existing_attrs = @mapper_class.instance_variable_get(:@attributes) || {}
969
+ attrs_hash.each do |name, attr|
970
+ if existing_attrs.key?(name)
971
+ existing_attrs[name].options.merge!(attr.options)
972
+ else
973
+ existing_attrs[name] = attr
974
+ end
975
+ end
976
+ @mapper_class.instance_variable_set(:@attributes, existing_attrs)
977
+ end
978
+
979
+ # 2. Import serialization mappings (XML element/attribute names → model attributes)
980
+ # This defines how the data structure maps to/from XML
981
+ # CRITICAL: Deep-copy mapping rules to prevent shared state
982
+ # When multiple classes import the same model, each must have independent MappingRule instances
983
+ # Otherwise, any state mutation during serialization affects ALL importing classes
984
+ @elements.merge!(dup_mappings(mappings.instance_variable_get(:@elements)))
985
+ @attributes.merge!(dup_mappings(mappings.instance_variable_get(:@attributes)))
986
+ # CRITICAL: Deep-copy sequences to prevent shared state
987
+ # Each importing class must have its own Sequence objects
988
+ imported_sequences = mappings.element_sequence.map do |seq|
989
+ seq.deep_dup(self)
990
+ end
991
+ (@element_sequence << imported_sequences).flatten!
992
+ end
993
+
994
+ # Register-specific import: stores mappings in register-specific storage
995
+ # (@register_elements, @register_attributes) without modifying class-level mappings.
996
+ #
997
+ # @param model [Class] The model to import mappings from
998
+ # @param register_id [Symbol] The register context (must be non-default)
999
+ def register_only_import_model_mappings(model, register_id)
1000
+ mappings = model.instance_variable_get(:@mappings)&.dig(:xml)
1001
+ return unless mappings
1002
+
1003
+ @register_attributes[register_id].merge!(mappings.mapping_attributes_hash(register_id))
1004
+ @register_elements[register_id].merge!(mappings.mapping_elements_hash(register_id))
1005
+ mappings.element_sequence.each do |sequence|
1006
+ @register_element_sequences[register_id] << sequence_dup(sequence)
1007
+ end
1008
+ end
1009
+ private :register_only_import_model_mappings
1010
+
1011
+ def set_mappings_imported(value)
1012
+ if @mappings_imported.is_a?(Hash)
1013
+ @mappings_imported.each_key { |k| @mappings_imported[k] = value }
1014
+ else
1015
+ @mappings_imported = value
1016
+ end
1017
+ end
1018
+
1019
+ def validate!(key, to, with, render_nil, render_empty, type: nil)
1020
+ validate_raw_mappings!(type)
1021
+ validate_to_and_with_arguments!(key, to, with)
1022
+
1023
+ if render_nil == :as_empty || render_empty == :as_empty
1024
+ raise ::Lutaml::Model::IncorrectMappingArgumentsError.new(
1025
+ ":as_empty is not supported for XML mappings. " \
1026
+ "Use :as_blank instead to create blank XML elements.",
1027
+ )
1028
+ end
1029
+ end
1030
+
1031
+ def validate_to_and_with_arguments!(key, to, with)
1032
+ if to.nil? && with.empty?
1033
+ raise ::Lutaml::Model::IncorrectMappingArgumentsError.new(
1034
+ ":to or :with argument is required for mapping '#{key}'",
1035
+ )
1036
+ end
1037
+
1038
+ validate_with_options!(key, to, with)
1039
+ end
1040
+
1041
+ def validate_with_options!(key, to, with)
1042
+ return true if to
1043
+
1044
+ if !with.empty? && (with[:from].nil? || with[:to].nil?)
1045
+ raise ::Lutaml::Model::IncorrectMappingArgumentsError.new(
1046
+ ":with argument for mapping '#{key}' requires :to and :from keys",
1047
+ )
1048
+ end
1049
+ end
1050
+
1051
+ def validate_raw_mappings!(type)
1052
+ if !@raw_mapping.nil? && type != TYPES[:attribute]
1053
+ raise StandardError, "#{type} is not allowed, only #{TYPES[:attribute]} " \
1054
+ "is allowed with #{TYPES[:all_content]}"
1055
+ end
1056
+
1057
+ if type == TYPES[:all_content] &&
1058
+ !(elements.empty? && content_mapping.nil?)
1059
+ # lutaml-model#223: map_all may coexist with mapped siblings.
1060
+ # The raw capture receives only the UNCLAIMED content — the
1061
+ # verbatim subtree minus children matched by element rules.
1062
+ # The historical exclusivity raised here; the coexistence
1063
+ # contract is enforced by scoped capture on parse and
1064
+ # remainder-after-mapped emission on serialize.
1065
+ end
1066
+ end
1067
+
1068
+ # Validate namespace prefix parameter
1069
+ #
1070
+ # Raises ArgumentError if prefix is Hash or Array, which indicates
1071
+ # the common mistake of passing options as prefix.
1072
+ #
1073
+ # @param prefix [Object] the prefix parameter to validate
1074
+ # @raise [ArgumentError] if prefix is Hash or Array
1075
+ # @return [void]
1076
+ def validate_namespace_prefix!(prefix)
1077
+ if prefix.is_a?(Hash) || prefix.is_a?(Array)
1078
+ raise ArgumentError,
1079
+ "namespace prefix must be a String or Symbol, not #{prefix.class}. " \
1080
+ "Did you mean to use 'root' with mixed: true?"
1081
+ end
1082
+ end
1083
+
1084
+ # Validate namespace_scope parameter
1085
+ #
1086
+ # Ensures all items are XmlNamespace classes or valid Hash entries
1087
+ #
1088
+ # @param namespaces [Array] the namespaces to validate
1089
+ # @raise [ArgumentError] if invalid namespace classes provided
1090
+ # @return [void]
1091
+ def validate_namespace_scope!(namespaces)
1092
+ unless namespaces.is_a?(Array)
1093
+ raise ArgumentError,
1094
+ "namespace_scope must be an Array of XmlNamespace classes, " \
1095
+ "got #{namespaces.class}"
1096
+ end
1097
+
1098
+ namespaces.each do |ns|
1099
+ if ns.is_a?(Class)
1100
+ unless ns < Lutaml::Xml::Namespace
1101
+ raise ArgumentError,
1102
+ "namespace_scope must contain only XmlNamespace classes, " \
1103
+ "got #{ns}"
1104
+ end
1105
+ elsif ns.is_a?(::Hash)
1106
+ ns_class = ns[:namespace]
1107
+ unless ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace
1108
+ raise ArgumentError,
1109
+ "namespace_scope Hash entry must have :namespace key " \
1110
+ "with XmlNamespace class, got #{ns_class.class}"
1111
+ end
1112
+
1113
+ # Validate :declare option if present
1114
+ if ns.key?(:declare)
1115
+ declare_value = ns[:declare]
1116
+ valid_modes = %i[auto always never]
1117
+ unless valid_modes.include?(declare_value)
1118
+ raise ArgumentError,
1119
+ "namespace_scope Hash entry :declare must be one of " \
1120
+ "#{valid_modes.inspect}, got #{declare_value.inspect}"
1121
+ end
1122
+ end
1123
+ else
1124
+ raise ArgumentError,
1125
+ "namespace_scope must contain only XmlNamespace classes or Hashes, " \
1126
+ "got #{ns.class}"
1127
+ end
1128
+ end
1129
+ end
1130
+
1131
+ # Fire InvalidAttributeTypeError at definition time for mapped
1132
+ # rules whose target attribute type is neither a Type::Value nor
1133
+ # Serializable (lutaml-model#296). Undeclared types are left to
1134
+ # deferred-import resolution.
1135
+ def validate_mapped_attribute_types!(mapper_class)
1136
+ (elements + attributes).each do |rule|
1137
+ target = mapper_class.attributes[rule.to]
1138
+ next unless target
1139
+
1140
+ begin
1141
+ resolved = target.loaded_type(Lutaml::Model::Config.default_register)
1142
+ rescue Lutaml::Model::UnknownTypeError
1143
+ next
1144
+ end
1145
+ next unless resolved.is_a?(Class)
1146
+
1147
+ target.validate_attr_type!(resolved)
1148
+ end
1149
+ end
1150
+
1151
+ def elements(register_id = nil)
1152
+ reg_key = register_id || :default
1153
+ cached = @cached_elements[reg_key]
1154
+ return cached if cached
1155
+
1156
+ result = mapping_elements_hash(register_id).values.flat_map do |v|
1157
+ v.is_a?(Array) ? v : [v]
1158
+ end
1159
+ @cached_elements[reg_key] = result.freeze if @finalized
1160
+ result
1161
+ end
1162
+
1163
+ def attributes(register_id = nil)
1164
+ reg_key = register_id || :default
1165
+ cached = @cached_attributes[reg_key]
1166
+ return cached if cached
1167
+
1168
+ result = mapping_attributes_hash(register_id).values.flat_map do |v|
1169
+ v.is_a?(Array) ? v : [v]
1170
+ end
1171
+ @cached_attributes[reg_key] = result.freeze if @finalized
1172
+ result
1173
+ end
1174
+
1175
+ def content_mapping
1176
+ @content_mapping
1177
+ end
1178
+
1179
+ def raw_mapping
1180
+ @raw_mapping
1181
+ end
1182
+
1183
+ # lutaml-model#790/#791: whether `rule` is the only attribute rule
1184
+ # claiming its exact name spelling. The answer is a property of the
1185
+ # mapping, not the document, so it is counted once per register
1186
+ # (rules sharing a spelling make each other non-sole).
1187
+ def sole_local_attribute_claimant?(rule, register_id = nil)
1188
+ reg_key = register_id || :default
1189
+ counts = @cached_attribute_name_counts[reg_key]
1190
+ unless counts
1191
+ counts = {}
1192
+ attributes(register_id).each do |r|
1193
+ key = r.name_strings
1194
+ counts[key] = (counts[key] || 0) + 1
1195
+ end
1196
+ counts.freeze if @finalized
1197
+ @cached_attribute_name_counts[reg_key] = counts
1198
+ end
1199
+
1200
+ (counts[rule.name_strings] || 1) == 1
1201
+ end
1202
+
1203
+ def mappings(register_id = nil)
1204
+ reg_key = register_id || :default
1205
+ cached = @cached_mappings[reg_key]
1206
+ return cached if cached
1207
+
1208
+ result = elements(register_id) + attributes(register_id) + [content_mapping,
1209
+ raw_mapping].compact
1210
+ @cached_mappings[reg_key] = result.freeze if @finalized
1211
+ result
1212
+ end
1213
+
1214
+ # lutaml-model#765: plain element rules grouped by target attribute
1215
+ # name, for spelling-tolerance mapping (several element names feeding
1216
+ # one attribute). Cached per register so the parse hot path only pays
1217
+ # a hash lookup. Grouping uses rule-level properties only — the
1218
+ # transform applies the collection-attribute filter.
1219
+ #
1220
+ # @return [Hash{Symbol, String => Array<MappingRule>}]
1221
+ # The when_attribute siblings-vs-plain-names partition of the
1222
+ # element rules. A ParseSession used to rebuild this per parsed
1223
+ # node (sessions are per-element), paying an O(rules) pass on
1224
+ # every node of large documents; it is a pure function of the
1225
+ # rules, so it memoizes here — same lifetime as
1226
+ # plain_element_rules_by_attr.
1227
+ def when_attribute_partition
1228
+ @when_attribute_partition ||= begin
1229
+ siblings = {}
1230
+ plain_names = {}
1231
+ mappings.each do |rule|
1232
+ next if rule.attribute? || rule.content_mapping? ||
1233
+ rule.raw_mapping? || rule.cdata
1234
+
1235
+ pairs = rule.when_attribute
1236
+ if pairs && !pairs.empty?
1237
+ (siblings[rule.name.to_s] ||= []) << rule
1238
+ else
1239
+ plain_names[rule.name.to_s] = true
1240
+ end
1241
+ end
1242
+ [siblings, plain_names]
1243
+ end
1244
+ end
1245
+
1246
+ def plain_element_rules_by_attr(register_id = nil)
1247
+ reg_key = register_id || :default
1248
+ @plain_element_rules_by_attr ||= {}
1249
+ @plain_element_rules_by_attr[reg_key] ||= begin
1250
+ grouped = {}
1251
+ mappings(reg_key).each do |r|
1252
+ next if r.attribute? || r.raw_mapping? || r.content_mapping? ||
1253
+ r.cdata || r.when_attribute? ||
1254
+ r.has_custom_method_for_deserialization? ||
1255
+ (r.transform.is_a?(Hash) && !r.transform.empty?) ||
1256
+ r.transform.is_a?(Class)
1257
+
1258
+ (grouped[r.to] ||= []) << r
1259
+ end
1260
+ grouped
1261
+ end
1262
+ end
1263
+
1264
+ def importable_mappings
1265
+ @importable_mappings ||= []
1266
+ end
1267
+
1268
+ def ensure_mappings_imported!(register_id = nil)
1269
+ # CRITICAL: Return immediately if already imported to prevent redundant processing
1270
+ # This prevents the exponential explosion of recursive ensure_imports! calls
1271
+ # in complex schemas with hundreds of interdependent classes
1272
+ register_id ||= Lutaml::Model::Config.default_register
1273
+
1274
+ # Use per-register tracking
1275
+ imported_flag = register_id || :default
1276
+ return if @mappings_imported[imported_flag]
1277
+
1278
+ # CRITICAL: Prevent re-entrant calls during import processing
1279
+ # This prevents infinite loops when importing models that themselves have imports
1280
+ # Architecture: Import resolution should be atomic and non-recursive
1281
+ return if @importing_mappings
1282
+
1283
+ # Check if there's any work to do - either regular imports OR sequence imports
1284
+ return if importable_mappings.empty? && sequence_importable_mappings.empty?
1285
+
1286
+ # Mark as currently importing to prevent re-entrance
1287
+ @importing_mappings = true
1288
+
1289
+ # Track if all imports were successfully resolved
1290
+ all_resolved = true
1291
+
1292
+ # Process each deferred mapping import
1293
+ importable_mappings.dup.each do |model_sym|
1294
+ begin
1295
+ model_class = Lutaml::Model::GlobalContext.resolve_type(model_sym,
1296
+ register_id)
1297
+ rescue Lutaml::Model::UnknownTypeError
1298
+ # Model not registered yet - skip for now, will retry later
1299
+ all_resolved = false
1300
+ next
1301
+ end
1302
+
1303
+ next if model_class.nil? # Skip if not registered yet
1304
+
1305
+ # Recursively ensure the imported model's imports are resolved
1306
+ # Use the child class's own register if it has one
1307
+ if model_class.is_a?(Class) && model_class.include?(Lutaml::Model::Serialize)
1308
+ child_register = Lutaml::Model::Register.resolve_for_child(
1309
+ model_class, register_id
1310
+ )
1311
+ model_class.ensure_imports!(child_register)
1312
+ end
1313
+
1314
+ # Now import the mappings
1315
+ import_model_mappings(model_class, register_id)
1316
+ end
1317
+
1318
+ # CRITICAL FIX: Process sequence importable mappings
1319
+ # Sequence blocks can have their own deferred imports via import_model_mappings
1320
+ # These need to be resolved separately because they add attributes to sequences
1321
+ unless sequence_importable_mappings.empty?
1322
+ sequence_importable_mappings.each do |sequence, model_syms|
1323
+ model_syms.dup.each do |model_sym|
1324
+ begin
1325
+ model_class = Lutaml::Model::GlobalContext.resolve_type(
1326
+ model_sym, register_id
1327
+ )
1328
+ rescue Lutaml::Model::UnknownTypeError
1329
+ # Model not registered yet - skip for now
1330
+ all_resolved = false
1331
+ next
1332
+ end
1333
+
1334
+ next if model_class.nil?
1335
+
1336
+ # Recursively ensure the imported model's imports are resolved
1337
+ # Use the child class's own register if it has one
1338
+ if model_class.is_a?(Class) && model_class.include?(Lutaml::Model::Serialize)
1339
+ child_register = Lutaml::Model::Register.resolve_for_child(
1340
+ model_class, register_id
1341
+ )
1342
+ model_class.ensure_imports!(child_register)
1343
+ end
1344
+
1345
+ # Now import into the sequence
1346
+ # This will call Sequence#import_model_mappings which adds to sequence.attributes
1347
+ # and also calls @model.import_model_mappings to add to main mapping
1348
+ sequence.import_model_mappings(model_class, register_id)
1349
+
1350
+ # Remove from queue after successful import
1351
+ model_syms.delete(model_sym)
1352
+ end
1353
+ end
1354
+
1355
+ # Clean up empty sequence entries
1356
+ sequence_importable_mappings.reject! { |_, models| models.empty? }
1357
+ end
1358
+
1359
+ # Mark as fully imported only if both queues are empty
1360
+ @mappings_imported[imported_flag] =
1361
+ all_resolved && importable_mappings.empty? && sequence_importable_mappings.empty?
1362
+ ensure
1363
+ # CRITICAL: Always reset the importing flag to prevent deadlock
1364
+ # Even if an exception occurs, we must allow future import attempts
1365
+ @importing_mappings = false
1366
+ end
1367
+
1368
+ def sequence_importable_mappings
1369
+ @sequence_importable_mappings ||= ::Hash.new { |h, k| h[k] = [] }
1370
+ end
1371
+
1372
+ # Find element mapping rule by attribute name
1373
+ #
1374
+ # @param name [Symbol, String] the attribute name
1375
+ # @return [MappingRule, nil] the matching element rule
1376
+ def find_element(name, register_id = nil)
1377
+ elements(register_id).detect { |rule| name == rule.to }
1378
+ end
1379
+
1380
+ # Find attribute mapping rule by attribute name
1381
+ #
1382
+ # @param name [Symbol, String] the attribute name
1383
+ # @return [MappingRule, nil] the matching attribute rule
1384
+ def find_attribute(name)
1385
+ attributes.detect { |rule| name == rule.to }
1386
+ end
1387
+
1388
+ def find_by_name(name, type: "Text", node_type: nil, namespace_uri: nil)
1389
+ # If node_type is provided, use it for type detection (preferred)
1390
+ if node_type && %i[text cdata].include?(node_type)
1391
+ content_mapping
1392
+ # Backward compatibility: still check name for old code that doesn't pass node_type
1393
+ elsif ["text", "#cdata-section"].include?(name.to_s) && type == "Text"
1394
+ content_mapping
1395
+ else
1396
+ candidates = element_rules_index[name.to_s]
1397
+ return @any_element_rule if candidates.nil? && @any_element_rule
1398
+ return nil if candidates.nil?
1399
+ return candidates.first if namespace_uri.nil? || candidates.one?
1400
+
1401
+ candidates.find do |r|
1402
+ r.namespace_class&.uri == namespace_uri
1403
+ end || candidates.first
1404
+ end
1405
+ end
1406
+
1407
+ # O(1) name -> rules lookup. Parsing consults this once per
1408
+ # child element; the linear scan over every rule made large
1409
+ # documents quadratic (155s on a 7MB vocabulary tree). Keyed on
1410
+ # the stable source hashes (@elements/@attributes mutate only at
1411
+ # DSL time), never on the per-call mappings array.
1412
+ def element_rules_index
1413
+ cache_key = [@elements.object_id, @attributes.object_id]
1414
+ cached = (@element_rules_index ||= {})
1415
+ idx = cached[cache_key]
1416
+ return idx if idx
1417
+
1418
+ idx = {}
1419
+ ([content_mapping, raw_mapping].compact + @elements.values +
1420
+ @attributes.values).flatten.each do |rule|
1421
+ next if rule.is_a?(::Hash)
1422
+
1423
+ (idx[rule.name.to_s] ||= []) << rule
1424
+ end
1425
+ cached[cache_key] = idx
1426
+ idx
1427
+ end
1428
+ private :element_rules_index
1429
+
1430
+ def find_by_to(to)
1431
+ mappings.detect { |rule| rule.to.to_s == to.to_s }
1432
+ end
1433
+
1434
+ def find_by_to!(to)
1435
+ mapping = find_by_to(to)
1436
+
1437
+ return mapping if !!mapping
1438
+
1439
+ raise Lutaml::Model::NoMappingFoundError.new(to.to_s)
1440
+ end
1441
+
1442
+ def mapping_attributes_hash(register_id = nil)
1443
+ if register_id.nil? || register_id == :default
1444
+ @attributes
1445
+ else
1446
+ @attributes.merge(@register_attributes[register_id])
1447
+ end
1448
+ end
1449
+
1450
+ def mapping_elements_hash(register_id = nil)
1451
+ if register_id.nil? || register_id == :default
1452
+ @elements
1453
+ else
1454
+ @elements.merge(@register_elements[register_id])
1455
+ end
1456
+ end
1457
+
1458
+ # Whether the model maps the engine-reserved schemaLocation name
1459
+ # itself (the XSD schema objects map it as a plain XSD attribute).
1460
+ # Raw xsi:schemaLocation capture must stay out of the way, or the
1461
+ # writer re-emits the value beside the mapped attribute (#1042).
1462
+ def schema_location_mapped?(register_id = nil)
1463
+ # Memoized per register against the mapping's rule-records
1464
+ # version: the walk re-asks per parse, and the un-memoized form
1465
+ # allocated a merged attributes hash plus a name string per rule
1466
+ # per ask.
1467
+ version = rule_records_version.to_i
1468
+ cache = (@schema_location_mapped ||= {})
1469
+ cached = cache[register_id]
1470
+ return cached[:value] if cached && cached[:version] == version
1471
+
1472
+ value = mapping_attributes_hash(register_id).each_value.any? do |rule_or_array|
1473
+ Array(rule_or_array).any? do |rule|
1474
+ rule.name.to_s == "schemaLocation"
1475
+ end
1476
+ end
1477
+ cache[register_id] = { version: version, value: value }
1478
+ value
1479
+ end
1480
+
1481
+ # Raw hash access for inheritance and deep_dup operations.
1482
+ # Callers may mutate the returned hash (e.g., setting keys).
1483
+ def elements_hash
1484
+ @elements
1485
+ end
1486
+
1487
+ def attributes_hash
1488
+ @attributes
1489
+ end
1490
+
1491
+ # Whether namespace_class was explicitly set (not nil)
1492
+ def namespace_class?
1493
+ !@namespace_class.nil?
1494
+ end
1495
+
1496
+ # lutaml-model#154: opt-in decoding of HTML named/numeric entities
1497
+ # (e.g. &copy;, &#8212;) in mapped text and attribute values. XML
1498
+ # documents are otherwise kept byte-faithful: HTML entities are not
1499
+ # defined in XML without a DTD.
1500
+ def html_entities(enabled: true)
1501
+ @decode_html_entities = enabled
1502
+ end
1503
+
1504
+ def decode_html_entities?
1505
+ !!@decode_html_entities
1506
+ end
1507
+
1508
+ # Whether namespace was explicitly set via DSL
1509
+ def namespace_set?
1510
+ !!@namespace_set
1511
+ end
1512
+
1513
+ attr_accessor :namespace_scope_config
1514
+
1515
+ def sequence_dup(sequence)
1516
+ Lutaml::Model::Sequence.new(self, format: :xml).tap do |instance|
1517
+ sequence.attributes.each do |attr|
1518
+ instance.attributes << attr.deep_dup
1519
+ end
1520
+ end
1521
+ end
1522
+
1523
+ def merge_elements_sequence(mapping)
1524
+ mapping.element_sequence.each do |sequence|
1525
+ element_sequence << sequence_dup(sequence)
1526
+ end
1527
+ end
1528
+
1529
+ def deep_dup
1530
+ self.class.new.tap do |xml_mapping|
1531
+ if @root_element
1532
+ xml_mapping.root(@root_element.dup, mixed: @mixed_content,
1533
+ ordered: @ordered)
1534
+ end
1535
+ if @namespace_class
1536
+ xml_mapping.namespace(@namespace_class)
1537
+ elsif @namespace_param == :inherit
1538
+ xml_mapping.namespace(:inherit)
1539
+ elsif @namespace_param == :blank
1540
+ xml_mapping.namespace(:blank)
1541
+ end
1542
+
1543
+ attributes_to_dup.each do |var_name|
1544
+ value = instance_variable_get(var_name)
1545
+ # Skip nil values (e.g., @register_elements not yet initialized on fresh copies)
1546
+ # Accessor methods will lazily initialize when needed
1547
+ next if value.nil?
1548
+
1549
+ xml_mapping.instance_variable_set(var_name, ::Lutaml::Model::Utils.deep_dup(value))
1550
+ end
1551
+ xml_mapping.instance_variable_set(:@mappings_imported,
1552
+ ::Hash.new { |h, k| h[k] = false })
1553
+ xml_mapping.instance_variable_set(:@register_elements,
1554
+ ::Hash.new { |h, k| h[k] = {} })
1555
+ xml_mapping.instance_variable_set(:@register_attributes,
1556
+ ::Hash.new { |h, k| h[k] = {} })
1557
+ xml_mapping.instance_variable_set(:@register_element_sequences,
1558
+ ::Hash.new { |h, k| h[k] = [] })
1559
+ xml_mapping.instance_variable_set(:@cached_elements, {})
1560
+ xml_mapping.instance_variable_set(:@cached_attributes, {})
1561
+ xml_mapping.instance_variable_set(:@cached_mappings, {})
1562
+ xml_mapping.instance_variable_set(:@finalized, true)
1563
+ # CRITICAL: Do NOT copy @mapper_class to the duplicate
1564
+ # The duplicate may be used in a different class context
1565
+ # and should not carry over the original's mapper_class reference
1566
+ xml_mapping.instance_variable_set(:@mapper_class, nil)
1567
+ end
1568
+ end
1569
+
1570
+ def polymorphic_mapping
1571
+ mappings.find(&:polymorphic_mapping?)
1572
+ end
1573
+
1574
+ def attributes_to_dup
1575
+ @attributes_to_dup ||= %i[
1576
+ @content_mapping
1577
+ @raw_mapping
1578
+ @element_sequence
1579
+ @attributes
1580
+ @elements
1581
+ @processing_instruction_mappings
1582
+ @all_attributes_rule
1583
+ @register_elements
1584
+ @register_attributes
1585
+ @register_element_sequences
1586
+ @mappings_imported
1587
+ @sequence_importable_mappings
1588
+ @consolidation_maps
1589
+ ]
1590
+ end
1591
+
1592
+ def dup_mappings(mappings)
1593
+ new_mappings = {}
1594
+
1595
+ mappings.each do |key, mapping_rule|
1596
+ new_mappings[key] = mapping_rule.deep_dup
1597
+ end
1598
+
1599
+ new_mappings
1600
+ end
1601
+
1602
+ # Add a complex listener for an XML element with a custom handler block.
1603
+ #
1604
+ # Unlike map_element which creates a simple listener (framework handles
1605
+ # deserialization), on_element allows custom deserialization logic.
1606
+ #
1607
+ # Multiple listeners for the same element are allowed — all matching
1608
+ # listeners are invoked during parsing.
1609
+ #
1610
+ # @param name [String] XML element name
1611
+ # @param id [Symbol, String, nil] Unique identifier for override/omit.
1612
+ # If omitted, listener cannot be targeted by omit_listener.
1613
+ # @param block [Proc] Custom handler receiving (element, context)
1614
+ # @return [void]
1615
+ #
1616
+ # @example Custom deserialization
1617
+ # class MyMapping < Lutaml::Xml::Mapping
1618
+ # on_element "CustomElement", id: :custom_parse do |element, context|
1619
+ # context[:custom] = CustomParser.parse(element)
1620
+ # end
1621
+ # end
1622
+ #
1623
+ # @example Multiple listeners for same element
1624
+ # class MyMapping < Lutaml::Xml::Mapping
1625
+ # on_element "Documentation", id: :parse_docs do |element, context|
1626
+ # context[:documentation] = Documentation.from_xml(element)
1627
+ # end
1628
+ #
1629
+ # on_element "Documentation", id: :log_docs do |element, context|
1630
+ # logger.info("Parsing docs: #{element.text}")
1631
+ # end
1632
+ # end
1633
+ def on_element(name, id: nil, &block)
1634
+ add_listener(Lutaml::Xml::Listener.new(
1635
+ target: name,
1636
+ id: id,
1637
+ handler: block,
1638
+ ))
1639
+ end
1640
+
1641
+ # Add a complex listener for an XML attribute with a custom handler block.
1642
+ #
1643
+ # @param name [String] XML attribute name
1644
+ # @param id [Symbol, String, nil] Unique identifier for override/omit.
1645
+ # @param block [Proc] Custom handler receiving (element, context)
1646
+ # @return [void]
1647
+ #
1648
+ # @example
1649
+ # class MyMapping < Lutaml::Xml::Mapping
1650
+ # on_attribute "xmlAttr", id: :parse_attr do |element, context|
1651
+ # context[:custom] = element["xmlAttr"]
1652
+ # end
1653
+ # end
1654
+ def on_attribute(name, id: nil, &block)
1655
+ add_listener(Lutaml::Xml::Listener.new(
1656
+ target: name,
1657
+ id: id,
1658
+ handler: block,
1659
+ ))
1660
+ end
1661
+
1662
+ # Remove ALL listeners for a given XML element name.
1663
+ #
1664
+ # @param name [String] XML element name
1665
+ # @return [void]
1666
+ #
1667
+ # @example Remove all listeners for "UnusedElement"
1668
+ # class MyMapping < ParentMapping
1669
+ # omit_element "UnusedElement"
1670
+ # end
1671
+
1672
+ # Remove a specific listener by XML element name and ID.
1673
+ #
1674
+ # @param name [String] XML element name
1675
+ # @param id [Symbol, String] The listener ID to remove
1676
+ # @return [void]
1677
+ #
1678
+ # @example Remove parent's :validate_tags listener
1679
+ # class MyMapping < ParentMapping
1680
+ # omit_listener "TaggedValue", id: :validate_tags
1681
+ # end
1682
+
1683
+ private
1684
+
1685
+ # Normalize namespace_scope input to unified format
1686
+ #
1687
+ # Converts various input formats into a normalized array of hashes
1688
+ # with :namespace and :declare keys.
1689
+ #
1690
+ # @param namespaces [Array] array of namespace classes or hashes
1691
+ # @return [Array<Hash>] normalized config array
1692
+ def normalize_namespace_scope(namespaces)
1693
+ namespaces.map do |ns_entry|
1694
+ if ns_entry.is_a?(::Hash) # Use ::Hash explicitly
1695
+ # Hash format already has namespace and optional declare
1696
+ # Don't double-nest!
1697
+ {
1698
+ namespace: ns_entry[:namespace],
1699
+ declare: ns_entry[:declare] || :auto, # Default to :auto
1700
+ }
1701
+ else
1702
+ # Simple namespace class - wrap it
1703
+ {
1704
+ namespace: ns_entry,
1705
+ declare: :auto,
1706
+ }
1707
+ end
1708
+ end
1709
+ end
1710
+ end
1711
+
1712
+ # Represents a processing instruction mapping in the XML DSL.
1713
+ #
1714
+ # Maps a PI target (e.g., "rfc") to a model attribute. The attribute
1715
+ # should be a Hash where keys are PI parameter names and values are
1716
+ # their string values.
1717
+ class ProcessingInstructionMapping
1718
+ attr_reader :target, :to
1719
+
1720
+ def initialize(target, to)
1721
+ @target = target
1722
+ @to = to
1723
+ end
1724
+
1725
+ def dup
1726
+ self.class.new(@target.dup, @to)
1727
+ end
1728
+ end
1729
+
1730
+ # Wildcard attribute-family mapping (#1031): the target Hash collects
1731
+ # every attribute no explicit map_attribute claims and that matches
1732
+ # the optional pattern; serialization emits each pair as an attribute.
1733
+ class AllAttributesMapping
1734
+ attr_reader :to, :matching
1735
+
1736
+ def initialize(to, matching)
1737
+ @to = to
1738
+ @matching = matching
1739
+ end
1740
+
1741
+ def dup
1742
+ self.class.new(@to, @matching)
1743
+ end
1744
+ end
1745
+ end
1746
+ end