Class: Lutaml::Xml::Adapter::OxAdapter
- Inherits:
-
BaseAdapter
- Object
- Document
- BaseAdapter
- Lutaml::Xml::Adapter::OxAdapter
- Extended by:
- AdapterHelpers, DocTypeExtractor
- Defined in:
- lib/lutaml/xml/adapter/ox_adapter.rb
Constant Summary collapse
- TEXT_CLASSES =
[Moxml::Text, Moxml::Cdata].freeze
Instance Attribute Summary
Attributes inherited from Document
#doctype, #encoding, #parsed_doc, #register, #root, #xml_declaration
Class Method Summary collapse
- .order_of(element) ⇒ Object
- .parse(xml, options = {}) ⇒ Object
-
.text_of(element) ⇒ Object
NOTE: name_of, prefixed_name_of, namespaced_attr_name, namespaced_name_of are provided by AdapterHelpers module via extend.
Instance Method Summary collapse
-
#add_simple_value(xml, rule, value, attribute, plan: nil, mapping: nil, options: {}) ⇒ Object
Add simple (non-model) values to XML.
- #attributes_hash(element) ⇒ Object
-
#build_element_with_plan(xml, element, plan, options = {}) ⇒ Object
Build element using prepared namespace declaration plan.
-
#build_xml_element(xml, element, parent_uses_default_ns: false, parent_element_form_default: nil, parent_namespace_class: nil, plan: nil, xml_mapping: nil) ⇒ Object
Build XML from XmlDataModel::XmlElement structure.
-
#build_xml_element_with_plan(builder, xml_element, plan, _options = {}) ⇒ Object
Build XML from XmlDataModel::XmlElement using DeclarationPlan tree (PARALLEL TRAVERSAL).
-
#handle_nested_elements_with_plan(xml, value, rule, attribute, plan, options, parent_plan: nil) ⇒ Object
NOTE: build_unordered_children_with_plan and build_ordered_element_with_plan are inherited from BaseAdapter - no need to override.
- #to_xml(options = {}) ⇒ Object
Methods included from DocTypeExtractor
Methods included from AdapterHelpers
build_element_from_child, name_of, namespace_uri_hoisted?, namespaced_attr_name, namespaced_name_of, node_type_of, prefix_for_namespace_uri, prefixed_name_of, text_node?
Methods inherited from BaseAdapter
#add_value, #attribute_definition_for, #attribute_value_for, #build_ordered_element_with_plan, #build_unordered_children_with_plan, #child_plan_for, #determine_encoding, extract_document_processing_instructions, fpi?, fpi_to_urn, name_of, namespaced_attr_name, namespaced_name, namespaced_name_of, #ordered?, prefixed_name_of, #process_content_mapping, #render_element?
Methods included from PolymorphicValueHandler
Methods included from DeclarationHandler
extract_attribute, extract_xml_declaration, #generate_declaration, #generate_doctype_declaration, #should_include_declaration?
Methods inherited from Document
#add_value, #attribute_definition_for, #attribute_value_for, #attributes, #cdata, #children, #declaration, #doctype_declaration, #element_children, #element_children_index, encoding, #initialize, name_of, namespaced_name_of, #order, #ordered?, #parse_element, #process_content_mapping, #render_element?, #text, #to_h, type
Constructor Details
This class inherits a constructor from Lutaml::Xml::Document
Class Method Details
.order_of(element) ⇒ Object
885 886 887 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 885 def self.order_of(element) element.order end |
.parse(xml, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 12 def self.parse(xml, = {}) enc = encoding(xml, ) parsed = begin Moxml::Adapter::Ox.parse(xml, encoding: enc) rescue Moxml::ParseError => e raise Lutaml::Model::InvalidFormatError.new( :xml, e., ) end root_element = parsed.root # Validate that we have a root element if root_element.nil? raise Lutaml::Model::InvalidFormatError.new( :xml, "Document has no root element. " \ "The XML may be empty, contain only whitespace, " \ "or consist only of an XML declaration.", ) end # Extract DOCTYPE information if present # Moxml/Ox doesn't directly expose DOCTYPE, extract from raw XML doctype_info = extract_doctype_from_xml(xml) @root = Ox::Element.new(root_element) @root.processing_instructions = extract_document_processing_instructions(parsed) new(@root, enc, doctype: doctype_info) end |
.text_of(element) ⇒ Object
NOTE: name_of, prefixed_name_of, namespaced_attr_name, namespaced_name_of are provided by AdapterHelpers module via extend
881 882 883 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 881 def self.text_of(element) element.text end |
Instance Method Details
#add_simple_value(xml, rule, value, attribute, plan: nil, mapping: nil, options: {}) ⇒ Object
Add simple (non-model) values to XML
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 694 def add_simple_value(xml, rule, value, attribute, plan: nil, mapping: nil, options: {}) if value.is_a?(Array) value.each do |val| add_simple_value(xml, rule, val, attribute, plan: plan, mapping: mapping, options: ) end return end resolver = NamespaceResolver.new(register) # Extract parent_uses_default_ns from options or calculate it parent_uses_default_ns = [:parent_uses_default_ns] if parent_uses_default_ns.nil? parent_uses_default_ns = if mapping&.namespace_class && plan DeclarationPlanQuery.declared_at_root_default_format?(plan, mapping.namespace_class) else false end end # Resolve namespace using the resolver ns_result = resolver.resolve_for_element(rule, attribute, mapping, plan, ) resolved_prefix = ns_result[:prefix] type_ns_info = ns_result[:ns_info] # CRITICAL FIX: Type namespace format inheritance for namespace_scope # When a type has namespace_class and that namespace is in the stored plan, # inherit the format from the stored plan (preserves input format) type_ns_class = if attribute && !rule.namespace_set? type_class = attribute.type(register) type_class.namespace_class if type_class.is_a?(Class) && type_class <= Lutaml::Model::Type::Value end format_from_stored_plan = false false # Will be set below if needed if type_ns_class # Check BOTH the current plan (programmatic) and stored plan (round-trip) check_plan = plan || [:stored_xml_declaration_plan] if check_plan stored_ns_decl = check_plan.namespaces.values.find do |decl| decl.uri == type_ns_class.uri end if stored_ns_decl # Namespace in plan - inherit its format # CRITICAL: local_on_use namespaces MUST use prefix format # (can't use default format - parent already using default) resolved_prefix = if stored_ns_decl.local_on_use? || stored_ns_decl.prefix_format? stored_ns_decl.prefix else nil # Use default format end format_from_stored_plan = true # Don't let subsequent logic override this false # Using plan namespace format, no xmlns="" needed end end end # BUG FIX #49: Check if child element is in same namespace as parent # If yes, inherit parent's format (default vs prefix) # Get parent's namespace URI parent_ns_class = [:parent_namespace_class] parent_ns_decl = [:parent_ns_decl] parent_ns_uri = parent_ns_class&.uri # Get child's resolved namespace URI child_ns_uri = ns_result[:uri] # Initialize resolved_prefix from namespace resolution resolved_prefix = ns_result[:prefix] # CRITICAL FIX FOR NATIVE TYPE NAMESPACE INHERITANCE: # Elements without explicit namespace declaration should NOT inherit # parent's prefix format. They should be in blank namespace. # # BUT: Skip this logic if we already determined format from stored plan unless format_from_stored_plan # Check if this is a native type without explicit namespace: # 1. No namespace directive on the mapping rule # 2. Attribute type doesn't have namespace_class (native type like :string) element_has_no_explicit_ns = !rule.namespace_set? type_class = attribute&.type(register) type_has_no_ns = !(type_class.is_a?(Class) && type_class <= Lutaml::Model::Type::Value) || !type_class&.namespace_class # If native type with no explicit namespace, DON'T inherit parent's prefix if element_has_no_explicit_ns && type_has_no_ns # Native type - force blank namespace (no prefix) resolved_prefix = nil # Check if parent uses default format - if so, need xmlns="" to opt out parent_ns_decl&.default_format? elsif parent_ns_class && parent_ns_decl && child_ns_uri && parent_ns_uri && child_ns_uri == parent_ns_uri # Same namespace URI - inherit parent's format resolved_prefix = if parent_ns_decl.prefix_format? parent_ns_decl.prefix else # Parent uses default format, child should too (no prefix) nil end # No blank xmlns needed when inheriting false else # Different namespace or no parent context - use standard resolution resolved_prefix = ns_result[:prefix] ns_result[:blank_xmlns] end end # Prepare attributes for element creation attributes = {} # W3C COMPLIANCE: Use resolver to determine xmlns="" requirement if resolver.xmlns_blank_required?(ns_result, parent_uses_default_ns) attributes["xmlns"] = "" end # Check if this namespace needs local declaration (out of scope) if resolved_prefix && plan&.namespaces ns_entry = plan.namespaces.values.find do |ns_decl| ns_decl.ns_object.prefix_default == resolved_prefix || (type_ns_info && type_ns_info[:uri] && ns_decl.ns_object.uri == type_ns_info[:uri]) end if ns_entry&.local_on_use? xmlns_attr = resolved_prefix ? "xmlns:#{resolved_prefix}" : "xmlns" attributes[xmlns_attr] = ns_entry.ns_object.uri end end if value.nil? xml.create_and_add_element(rule.name, attributes: attributes.merge({ "xsi:nil" => true }), prefix: resolved_prefix) elsif ::Lutaml::Model::Utils.empty?(value) xml.create_and_add_element(rule.name, attributes: attributes.empty? ? nil : attributes, prefix: resolved_prefix) elsif rule.raw_mapping? xml.add_xml_fragment(xml, value) elsif value.is_a?(::Hash) && attribute&.type(register) == Lutaml::Model::Type::Hash # Check if value is Hash type that needs wrapper xml.create_and_add_element(rule.name, attributes: attributes.empty? ? nil : attributes, prefix: resolved_prefix) do value.each do |key, val| xml.create_and_add_element(key.to_s) do xml.add_text(xml, val.to_s) end end end else xml.create_and_add_element(rule.name, attributes: attributes.empty? ? nil : attributes, prefix: resolved_prefix) do add_value(xml, value, attribute, cdata: rule.cdata) end end end |
#attributes_hash(element) ⇒ Object
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 860 def attributes_hash(element) result = Lutaml::Model::MappingHash.new element.attributes.each_value do |attr| if attr.unprefixed_name == "schemaLocation" result["__schema_location"] = { namespace: attr.namespace, prefix: attr.namespace_prefix, schema_location: attr.value, } else result[attr.namespaced_name] = attr.value end end result end |
#build_element_with_plan(xml, element, plan, options = {}) ⇒ Object
Build element using prepared namespace declaration plan
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 244 def build_element_with_plan(xml, element, plan, = {}) mapper_class = [:mapper_class] || element.class xml_mapping = mapper_class.mappings_for(:xml) return xml unless xml_mapping # TYPE-ONLY MODELS: No element wrapper, serialize children directly # BUT if we have a tag_name in options, that means parent wants a wrapper plan ||= DeclarationPlan.empty if xml_mapping.no_element? # If parent provided a tag_name, create that wrapper first if [:tag_name] xml.create_and_add_element([:tag_name]) do |inner_xml| # Serialize type-only model's children inside parent's wrapper xml_mapping.elements.each do |element_rule| next if [:except]&.include?(element_rule.to) attribute_def = mapper_class.attributes[element_rule.to] next unless attribute_def value = element.send(element_rule.to) next unless element_rule.render?(value, element) # For type-only models, children plans may not be available # Serialize children directly if value && attribute_def.type(register)&.<=(Lutaml::Model::Serialize) # Nested model - recursively build it child_plan = plan.child_plan(element_rule.to) || DeclarationPlan.empty build_element_with_plan( inner_xml, value, child_plan, { mapper_class: attribute_def.type(register), tag_name: element_rule.name }, ) else # Simple value - create element directly inner_xml.create_and_add_element(element_rule.name) do add_value(inner_xml, value, attribute_def, cdata: element_rule.cdata) end end end end else # No wrapper at all - serialize children directly (for root-level type-only) xml_mapping.elements.each do |element_rule| next if [:except]&.include?(element_rule.to) attribute_def = mapper_class.attributes[element_rule.to] next unless attribute_def value = element.send(element_rule.to) next unless element_rule.render?(value, element) child_plan = plan.child_plan(element_rule.to) if value && attribute_def.type(register)&.<=(Lutaml::Model::Serialize) handle_nested_elements_with_plan( xml, value, element_rule, attribute_def, child_plan, , ) else add_simple_value(xml, element_rule, value, attribute_def, plan: plan, mapping: xml_mapping, options: ) end end end return xml end # Use xmlns declarations from plan attributes = {} # Apply namespace declarations from plan using extracted module attributes.merge!(NamespaceDeclarationBuilder.build_xmlns_attributes(plan)) # Collect attribute custom methods to call after element creation attribute_custom_methods = [] # Add regular attributes (non-xmlns) xml_mapping.attributes.each do |attribute_rule| next if [:except]&.include?(attribute_rule.to) # Collect custom methods for later execution (after element is created) if attribute_rule.custom_methods[:to] attribute_custom_methods << attribute_rule next end mapping_rule_name = if attribute_rule.multiple_mappings? attribute_rule.name.first else attribute_rule.name end attr = attribute_definition_for(element, attribute_rule, mapper_class: mapper_class) value = attribute_rule.to_value_for(element) # Handle as_list and delimiter BEFORE serialization for array values # These features convert arrays to delimited strings before serialization if value.is_a?(Array) if attribute_rule.as_list && attribute_rule.as_list[:export] value = attribute_rule.as_list[:export].call(value) elsif attribute_rule.delimiter value = value.join(attribute_rule.delimiter) end end value = attr.serialize(value, :xml, register) if attr value = ExportTransformer.call(value, attribute_rule, attr, format: :xml) if render_element?(attribute_rule, element, value) # Resolve attribute namespace using extracted module ns_info = AttributeNamespaceResolver.resolve( rule: attribute_rule, attribute: attr, plan: plan, mapper_class: mapper_class, register: register, ) # Build qualified attribute name based on W3C semantics attr_name = AttributeNamespaceResolver.build_qualified_name( ns_info, mapping_rule_name, attribute_rule, ) attributes[attr_name] = value ? value.to_s : value # Add local xmlns declaration if needed if ns_info[:needs_local_declaration] attributes[ns_info[:local_xmlns_attr]] = ns_info[:local_xmlns_uri] end end end # Add schema_location attribute from ElementNode if present # This is for the plan-based path where schema_location_attr is computed during planning attributes.merge!(plan.root_node.schema_location_attr) if plan&.root_node&.schema_location_attr # Determine prefix from plan using extracted module prefix_info = ElementPrefixResolver.resolve(mapping: xml_mapping, plan: plan) prefix = prefix_info[:prefix] tag_name = [:tag_name] || xml_mapping.root_element return if [:except]&.include?(tag_name) xml.create_and_add_element(tag_name, prefix: prefix, attributes: attributes.compact) do |el| # Call attribute custom methods now that element is created attribute_custom_methods.each do |attribute_rule| mapper_class.new.send(attribute_rule.custom_methods[:to], element, el.parent, el) end if ordered?(element, .merge(mapper_class: mapper_class)) build_ordered_element_with_plan(el, element, plan, .merge( mapper_class: mapper_class, parent_ns_decl: prefix_info[:ns_decl], )) else build_unordered_children_with_plan(el, element, plan, .merge( mapper_class: mapper_class, parent_ns_decl: prefix_info[:ns_decl], )) end end end |
#build_xml_element(xml, element, parent_uses_default_ns: false, parent_element_form_default: nil, parent_namespace_class: nil, plan: nil, xml_mapping: nil) ⇒ Object
Build XML from XmlDataModel::XmlElement structure
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 433 def build_xml_element(xml, element, parent_uses_default_ns: false, parent_element_form_default: nil, parent_namespace_class: nil, plan: nil, xml_mapping: nil) # Prepare attributes hash attributes = {} # Get element's namespace class element_ns_class = element.namespace_class attribute_form_default = element_ns_class&.attribute_form_default || :unqualified element_prefix = element_ns_class&.prefix_default # Get element_form_default for children this_element_form_default = element_ns_class&.element_form_default || :unqualified # Add regular attributes element.attributes.each do |attr| # Determine attribute name with namespace consideration attr_name = if attr.namespace_class # Check if attribute is in SAME namespace as element if attr.namespace_class == element_ns_class && attribute_form_default == :unqualified # Same namespace + unqualified → NO prefix (W3C rule) attr.name else # Different namespace OR qualified → use prefix attr_prefix = attr.namespace_class.prefix_default attr_prefix ? "#{attr_prefix}:#{attr.name}" : attr.name end elsif attribute_form_default == :qualified && element_prefix # Attribute inherits element's namespace when qualified "#{element_prefix}:#{attr.name}" else # Unqualified attribute attr.name end # Ensure attribute value is a string for Ox attributes[attr_name] = attr.value.to_s end # Determine element name with namespace prefix tag_name = element.name # Priority 2.5: Child namespace different from parent's default namespace # MUST use prefix format to distinguish from parent child_needs_prefix = if element_ns_class && parent_namespace_class && element_ns_class != parent_namespace_class && parent_uses_default_ns element_prefix # Use child's prefix end # FIX: Read prefix from plan if available, otherwise use fallback logic prefix = if child_needs_prefix # Priority 2.5 takes precedence child_needs_prefix elsif plan && element_ns_class # Read format decision from DeclarationPlan ns_info = ElementPrefixResolver.resolve( mapping: xml_mapping, plan: plan, ) ns_info[:prefix] elsif element_ns_class && element_prefix # Fallback: Element has explicit prefix_default - use prefix format element_prefix else # Fallback: No prefix (default format or no namespace) # CRITICAL: Child with NO namespace should NEVER get parent's prefix nil end # Track if THIS element uses default namespace format for children this_element_uses_default_ns = false # Add namespace declaration if element has namespace if element.namespace_class ns_uri = element.namespace_class.uri # Check if namespace is already declared by parent (hoisting optimization) # This works for BOTH default and prefix format parents ns_already_declared = parent_namespace_class && parent_namespace_class.uri == ns_uri if prefix && !ns_already_declared attributes["xmlns:#{prefix}"] = ns_uri # W3C Compliance: xmlns="" only needed for blank namespace children # Prefixed children are already in different namespace from parent's default elsif !prefix && !ns_already_declared attributes["xmlns"] = ns_uri this_element_uses_default_ns = true end elsif plan && DeclarationPlanQuery.element_needs_xmlns_blank?(plan, element) # W3C Compliance: Element has no namespace (blank namespace) # Check if DeclarationPlan says this element needs xmlns="" # The planner already determined this based on W3C semantics during planning phase attributes["xmlns"] = "" elsif !plan # Fallback logic when no plan is available # Check if should inherit parent's namespace based on element_form_default if parent_uses_default_ns # Parent uses default namespace format if parent_element_form_default == :qualified # Child should INHERIT parent's namespace - no xmlns="" needed # The child is in same namespace as parent (qualified) else # Parent's element_form_default is :unqualified - child in blank namespace # Add xmlns="" to explicitly opt out of parent's default namespace attributes["xmlns"] = "" end end end # Check if element was created from nil value with render_nil option # Add xsi:nil="true" attribute for W3C compliance if element.respond_to?(:xsi_nil) && element.xsi_nil attributes["xsi:nil"] = true end # Create element xml.create_and_add_element(tag_name, attributes: attributes, prefix: prefix) do |inner_xml| # Handle raw content (map_all directive) has_raw_content = false if element.respond_to?(:raw_content) raw_content = element.raw_content if raw_content && !raw_content.to_s.empty? inner_xml.add_xml_fragment(inner_xml, raw_content.to_s) has_raw_content = true end end # Skip text content and children if we have raw content unless has_raw_content # Add text content if present if element.text_content if element.cdata inner_xml.cdata(element.text_content.to_s) else inner_xml.text(element.text_content.to_s) end end # Recursively build child elements, passing namespace context and plan element.children.each do |child| if child.is_a?(Lutaml::Xml::DataModel::XmlElement) build_xml_element(inner_xml, child, parent_uses_default_ns: this_element_uses_default_ns, parent_element_form_default: this_element_form_default, parent_namespace_class: element_ns_class, plan: plan, xml_mapping: xml_mapping) elsif child.is_a?(String) inner_xml.text(child) end end end end end |
#build_xml_element_with_plan(builder, xml_element, plan, _options = {}) ⇒ Object
Build XML from XmlDataModel::XmlElement using DeclarationPlan tree (PARALLEL TRAVERSAL)
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 137 def build_xml_element_with_plan(builder, xml_element, plan, = {}) # Add processing instructions before root element xml_element.processing_instructions.each do |pi| builder.add_processing_instruction(pi.target, pi.content) end build_ox_node(builder, xml_element, plan.root_node, plan.global_prefix_registry, plan: plan) end |
#handle_nested_elements_with_plan(xml, value, rule, attribute, plan, options, parent_plan: nil) ⇒ Object
NOTE: build_unordered_children_with_plan and build_ordered_element_with_plan are inherited from BaseAdapter - no need to override
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 591 def handle_nested_elements_with_plan(xml, value, rule, attribute, plan, , parent_plan: nil) = .merge( rule: rule, attribute: attribute, tag_name: rule.name, mapper_class: attribute.type(register), # Override with child's type ) # Handle Collection instances if value.is_a?(Lutaml::Model::Collection) items = value.collection attr_type = attribute.type(register) if attr_type <= Lutaml::Model::Type::Value # Simple types - use add_simple_value for each item items.each do |val| xml_mapping = [:mapper_class]&.mappings_for(:xml) add_simple_value(xml, rule, val, attribute, plan: parent_plan, mapping: xml_mapping, options: ) end else # Model types - build elements with plans items.each do |val| # For polymorphic collections, use each item's actual class item_mapper_class = if polymorphic_value?(attribute, val) val.class else attribute.type(register) end # CRITICAL: Transform model to XmlElement, then collect and plan item_mapping = item_mapper_class.mappings_for(:xml) if item_mapping # Transform model to XmlElement tree transformation = item_mapper_class.transformation_for(:xml, register) xml_element = transformation.transform(val, ) # Collect namespace needs from XmlElement tree collector = NamespaceCollector.new(register) item_needs = collector.collect(xml_element, item_mapping, mapper_class: item_mapper_class) # Plan with XmlElement tree (not model instance) planner = DeclarationPlanner.new(register) item_plan = planner.plan(xml_element, item_mapping, item_needs, parent_plan: parent_plan, options: ) else item_plan = plan end = .merge(mapper_class: item_mapper_class) build_element_with_plan(xml, val, item_plan, ) end end return end case value when Array value.each do |val| # For polymorphic arrays, use each item's actual class item_mapper_class = if polymorphic_value?(attribute, val) val.class else attribute.type(register) end # CRITICAL: Transform model to XmlElement, then collect and plan item_mapping = item_mapper_class.mappings_for(:xml) if item_mapping # Transform model to XmlElement tree transformation = item_mapper_class.transformation_for(:xml, register) xml_element = transformation.transform(val, ) # Collect namespace needs from XmlElement tree collector = NamespaceCollector.new(register) item_needs = collector.collect(xml_element, item_mapping, mapper_class: item_mapper_class) # Plan with XmlElement tree (not model instance) planner = DeclarationPlanner.new(register) item_plan = planner.plan(xml_element, item_mapping, item_needs, parent_plan: parent_plan, options: ) else item_plan = plan end = .merge(mapper_class: item_mapper_class) if item_plan build_element_with_plan(xml, val, item_plan, ) else build_element(xml, val, ) end end else build_element_with_plan(xml, value, plan, ) end end |
#to_xml(options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/lutaml/xml/adapter/ox_adapter.rb', line 44 def to_xml( = {}) # Accept xml_declaration from options if present (for model serialization) @xml_declaration = [:xml_declaration] if [:xml_declaration] = {} encoding = determine_encoding() [:encoding] = encoding if encoding builder = Builder::Ox.build() do |xml| if @root.is_a?(Ox::Element) @root.build_xml(xml) else original_model = nil xml_element = if @root.is_a?(Lutaml::Xml::DataModel::XmlElement) @root else mapper_class = [:mapper_class] || @root.class xml_mapping = mapper_class.mappings_for(:xml) has_custom_map_all = xml_mapping.raw_mapping&.custom_methods && xml_mapping.raw_mapping.custom_methods[:to] if has_custom_map_all nil else original_model = @root transformation = mapper_class.transformation_for( :xml, register ) transformation.transform(@root, ) end end if xml_element mapper_class = [:mapper_class] || xml_element.class mapping = mapper_class.mappings_for(:xml) collector = NamespaceCollector.new(register) needs = collector.collect(xml_element, mapping, mapper_class: mapper_class) planner = DeclarationPlanner.new(register) plan = planner.plan(xml_element, mapping, needs, options: ) = .merge(is_root_element: true) if original_model [:original_model] = original_model end build_xml_element_with_plan(xml, xml_element, plan, ) else mapper_class = [:mapper_class] || @root.class xml_mapping = mapper_class.mappings_for(:xml) collector = NamespaceCollector.new(register) needs = collector.collect(@root, xml_mapping) planner = DeclarationPlanner.new(register) plan = planner.plan(@root, xml_mapping, needs, options: ) build_element_with_plan(xml, @root, plan, ) end end end xml_data = builder.to_xml result = "" # Use DeclarationHandler methods instead of Document#declaration # Include declaration when encoding is specified OR when declaration is requested if ([:encoding] && ![:encoding].nil?) || [:declaration] result += generate_declaration() end # Add DOCTYPE if present - use DeclarationHandler method doctype_to_use = [:doctype] || @doctype if doctype_to_use && ![:omit_doctype] result += generate_doctype_declaration(doctype_to_use) end result += xml_data result end |