class Lutaml::Model::XmlAdapter::OxAdapter
def self.parse(xml)
def self.parse(xml) parsed = Ox.parse(xml) root = OxElement.new(parsed) new(root) end
def add_to_xml(xml, value, attribute, rule)
def add_to_xml(xml, value, attribute, rule) if rule.custom_methods[:to] value = @root.send(rule.custom_methods[:to], @root, value) end if value && (attribute&.type&.<= Lutaml::Model::Serialize) handle_nested_elements( xml, value, rule: rule, attribute: attribute, ) else xml.element(rule.name) do |el| if !value.nil? serialized_value = attribute.type.serialize(value) if attribute.type == Lutaml::Model::Type::Hash serialized_value.each do |key, val| el.element(key) { |child_el| child_el.text val } end else el.text(serialized_value) end end end end end
def build_ordered_element(builder, element, options = {})
def build_ordered_element(builder, element, options = {}) mapper_class = options[:mapper_class] || element.class xml_mapping = mapper_class.mappings_for(:xml) return xml unless xml_mapping attributes = build_attributes(element, xml_mapping).compact tag_name = options[:tag_name] || xml_mapping.root_element builder.element(tag_name, attributes) do |el| index_hash = {} element.element_order.each do |name| index_hash[name] ||= -1 curr_index = index_hash[name] += 1 element_rule = xml_mapping.find_by_name(name) attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class) value = attribute_value_for(element, element_rule) if element_rule == xml_mapping.content_mapping text = element.send(xml_mapping.content_mapping.to) text = text[curr_index] if text.is_a?(Array) el.text text elsif attribute_def.collection? add_to_xml(el, value[curr_index], attribute_def, element_rule) elsif !value.nil? || element_rule.render_nil? add_to_xml(el, value, attribute_def, element_rule) end end end end
def build_unordered_element(builder, element, options = {})
def build_unordered_element(builder, element, options = {}) mapper_class = options[:mapper_class] || element.class xml_mapping = mapper_class.mappings_for(:xml) return xml unless xml_mapping attributes = build_attributes(element, xml_mapping).compact tag_name = options[:tag_name] || xml_mapping.root_element prefixed_name = if options.key?(:namespace_prefix) [options[:namespace_prefix], tag_name].compact.join(":") elsif xml_mapping.namespace_prefix "#{xml_mapping.namespace_prefix}:#{tag_name}" else tag_name end builder.element(prefixed_name, attributes) do |el| xml_mapping.elements.each do |element_rule| attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class) value = attribute_value_for(element, element_rule) val = if attribute_def.collection? value elsif value || element_rule.render_nil? [value] else [] end val.each do |v| if attribute_def&.type&.<= Lutaml::Model::Serialize handle_nested_elements(el, v, rule: element_rule, attribute: attribute_def) else builder.element(element_rule.prefixed_name) do |el| el.text(attribute_def.type.serialize(v)) if v end end end end if (content_rule = xml_mapping.content_mapping) text = element.send(xml_mapping.content_mapping.to) text = text.join if text.is_a?(Array) if content_rule.custom_methods[:to] text = @root.send(content_rule.custom_methods[:to], @root, text) end el.text text end end end
def to_xml(options = {})
def to_xml(options = {}) builder = Ox::Builder.new if @root.is_a?(Lutaml::Model::XmlAdapter::OxElement) @root.to_xml(builder) elsif ordered?(@root, options) build_ordered_element(builder, @root, options) else build_element(builder, @root, options) end # xml_data = Ox.dump(builder) xml_data = builder.to_s options[:declaration] ? declaration(options) + xml_data : xml_data end