Class: Lutaml::Model::Xml::Document
- Inherits:
-
Object
- Object
- Lutaml::Model::Xml::Document
- Defined in:
- lib/lutaml/model/xml/document.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#register ⇒ Object
readonly
Returns the value of attribute register.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
- .encoding(xml, options) ⇒ Object
- .name_of(element) ⇒ Object
- .namespaced_name_of(element) ⇒ Object
- .order_of(element) ⇒ Object
- .parse(xml, _options = {}) ⇒ Object
- .text_of(element) ⇒ Object
- .type ⇒ Object
Instance Method Summary collapse
- #add_to_xml(xml, element, prefix, value, options = {}) ⇒ Object
- #add_value(xml, value, attribute, cdata: false) ⇒ Object
- #attribute_definition_for(element, rule, mapper_class: nil) ⇒ Object
- #attribute_value_for(element, rule) ⇒ Object
- #attributes ⇒ Object
- #attributes_hash(element) ⇒ Object
- #build_attributes(element, xml_mapping, options = {}) ⇒ Object
- #build_element(xml, element, options = {}) ⇒ Object
- #build_namespace_attributes(klass, processed = {}, options = {}) ⇒ Object
- #build_options_for_nested_elements(options = {}) ⇒ Object
- #build_unordered_element(xml, element, options = {}) ⇒ Object
- #cdata ⇒ Object
- #children ⇒ Object
- #declaration(options) ⇒ Object
- #handle_nested_elements(builder, value, options = {}) ⇒ Object
-
#initialize(root, encoding = nil, register: nil) ⇒ Document
constructor
A new instance of Document.
- #namespace_attributes(xml_mapping) ⇒ Object
- #order ⇒ Object
- #ordered?(element, options = {}) ⇒ Boolean
- #parse_element(element, klass = nil, format = nil) ⇒ Object
- #process_content_mapping(element, content_rule, xml, mapper_class) ⇒ Object
- #render_default?(rule, element) ⇒ Boolean
- #render_element?(rule, element, value) ⇒ Boolean
- #set_namespace?(rule) ⇒ Boolean
- #text ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(root, encoding = nil, register: nil) ⇒ Document
13 14 15 16 17 |
# File 'lib/lutaml/model/xml/document.rb', line 13 def initialize(root, encoding = nil, register: nil) @root = root @encoding = encoding @register = setup_register(register) end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
11 12 13 |
# File 'lib/lutaml/model/xml/document.rb', line 11 def encoding @encoding end |
#register ⇒ Object (readonly)
Returns the value of attribute register.
11 12 13 |
# File 'lib/lutaml/model/xml/document.rb', line 11 def register @register end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
11 12 13 |
# File 'lib/lutaml/model/xml/document.rb', line 11 def root @root end |
Class Method Details
.encoding(xml, options) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/lutaml/model/xml/document.rb', line 31 def self.encoding(xml, ) if .key?(:encoding) [:encoding] else xml.encoding.to_s end end |
.name_of(element) ⇒ Object
417 418 419 |
# File 'lib/lutaml/model/xml/document.rb', line 417 def self.name_of(element) element.name end |
.namespaced_name_of(element) ⇒ Object
425 426 427 |
# File 'lib/lutaml/model/xml/document.rb', line 425 def self.namespaced_name_of(element) element.namespaced_name end |
.order_of(element) ⇒ Object
413 414 415 |
# File 'lib/lutaml/model/xml/document.rb', line 413 def self.order_of(element) element.order end |
.parse(xml, _options = {}) ⇒ Object
19 20 21 |
# File 'lib/lutaml/model/xml/document.rb', line 19 def self.parse(xml, = {}) raise NotImplementedError, "Subclasses must implement `parse`." end |
.text_of(element) ⇒ Object
421 422 423 |
# File 'lib/lutaml/model/xml/document.rb', line 421 def self.text_of(element) element.text end |
.type ⇒ Object
409 410 411 |
# File 'lib/lutaml/model/xml/document.rb', line 409 def self.type Utils.snake_case(self).split("/").last.split("_").first end |
Instance Method Details
#add_to_xml(xml, element, prefix, value, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/lutaml/model/xml/document.rb', line 147 def add_to_xml(xml, element, prefix, value, = {}) attribute = [:attribute] rule = [:rule] if rule.custom_methods[:to] [:mapper_class].new.send(rule.custom_methods[:to], element, xml.parent, xml) return end # Only transform when recursion is not called if !attribute.collection? || attribute.collection_instance?(value) value = ExportTransformer.call(value, rule, attribute) end if attribute.collection_instance?(value) && !Utils.empty_collection?(value) value.each do |item| add_to_xml(xml, element, prefix, item, ) end return end return if !render_element?(rule, element, value) value = rule.render_value_for(value) if value && (attribute&.type(register)&.<= Lutaml::Model::Serialize) handle_nested_elements( xml, value, .merge({ rule: rule, attribute: attribute }), ) elsif value.nil? xml.create_and_add_element(rule.name, attributes: { "xsi:nil" => true }) elsif Utils.empty?(value) xml.create_and_add_element(rule.name) elsif rule.raw_mapping? xml.add_xml_fragment(xml, value) elsif rule.prefix_set? xml.create_and_add_element(rule.name, prefix: prefix) do add_value(xml, value, attribute, cdata: rule.cdata) end else xml.create_and_add_element(rule.name) do add_value(xml, value, attribute, cdata: rule.cdata) end end end |
#add_value(xml, value, attribute, cdata: false) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/lutaml/model/xml/document.rb', line 196 def add_value(xml, value, attribute, cdata: false) if !value.nil? serialized_value = attribute.serialize(value, :xml, register) if attribute.raw? xml.add_xml_fragment(xml, value) elsif attribute.type(register) == Lutaml::Model::Type::Hash serialized_value.each do |key, val| xml.create_and_add_element(key) do |element| element.text(val) end end else xml.add_text(xml, serialized_value, cdata: cdata) end end end |
#attribute_definition_for(element, rule, mapper_class: nil) ⇒ Object
389 390 391 392 393 394 |
# File 'lib/lutaml/model/xml/document.rb', line 389 def attribute_definition_for(element, rule, mapper_class: nil) klass = mapper_class || element.class return klass.attributes[rule.to] unless rule.delegate element.send(rule.delegate).class.attributes[rule.to] end |
#attribute_value_for(element, rule) ⇒ Object
396 397 398 399 400 |
# File 'lib/lutaml/model/xml/document.rb', line 396 def attribute_value_for(element, rule) return element.send(rule.to) unless rule.delegate element.send(rule.delegate).send(rule.to) end |
#attributes ⇒ Object
27 28 29 |
# File 'lib/lutaml/model/xml/document.rb', line 27 def attributes root.attributes end |
#attributes_hash(element) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/lutaml/model/xml/document.rb', line 121 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_attributes(element, xml_mapping, options = {}) ⇒ Object
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 |
# File 'lib/lutaml/model/xml/document.rb', line 348 def build_attributes(element, xml_mapping, = {}) attrs = if .fetch(:set_namespace, true) namespace_attributes(xml_mapping) else {} end if element.respond_to?(:schema_location) && element.schema_location.is_a?(Lutaml::Model::SchemaLocation) && ![:except]&.include?(:schema_location) attrs.merge!(element.schema_location.to_xml_attributes) end xml_mapping.attributes.each_with_object(attrs) do |mapping_rule, hash| next if [:except]&.include?(mapping_rule.to) next if mapping_rule.custom_methods[:to] mapping_rule_name = mapping_rule.multiple_mappings? ? mapping_rule.name.first : mapping_rule.name if mapping_rule.namespace && mapping_rule.prefix && mapping_rule_name != "lang" hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace end value = mapping_rule.to_value_for(element) attr = attribute_definition_for(element, mapping_rule, mapper_class: [:mapper_class]) value = attr.serialize(value, :xml, register) if attr value = ExportTransformer.call(value, mapping_rule, attr) if render_element?(mapping_rule, element, value) hash[mapping_rule.prefixed_name] = value ? value.to_s : value end end xml_mapping.elements.each_with_object(attrs) do |mapping_rule, hash| next if [:except]&.include?(mapping_rule.to) if mapping_rule.namespace && mapping_rule.prefix hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace end end end |
#build_element(xml, element, options = {}) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/lutaml/model/xml/document.rb', line 139 def build_element(xml, element, = {}) if ordered?(element, ) build_ordered_element(xml, element, ) else build_unordered_element(xml, element, ) end end |
#build_namespace_attributes(klass, processed = {}, options = {}) ⇒ Object
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 |
# File 'lib/lutaml/model/xml/document.rb', line 304 def build_namespace_attributes(klass, processed = {}, = {}) xml_mappings = klass.mappings_for(:xml) attributes = klass.attributes attrs = {} if xml_mappings.namespace_uri && set_namespace?([:caller_rule]) prefixed_name = [ "xmlns", xml_mappings.namespace_prefix, ].compact.join(":") attrs[prefixed_name] = xml_mappings.namespace_uri end xml_mappings.mappings.each do |mapping_rule| processed[klass] ||= {} next if processed[klass][mapping_rule.name] processed[klass][mapping_rule.name] = true type = if mapping_rule.delegate attributes[mapping_rule.delegate].type(register) .attributes[mapping_rule.to].type(register) else attributes[mapping_rule.to]&.type(register) end next unless type if type <= Lutaml::Model::Serialize attrs = attrs.merge(build_namespace_attributes(type, processed, { caller_rule: mapping_rule })) end if mapping_rule.namespace && mapping_rule.prefix && mapping_rule.name != "lang" attrs["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace end end attrs end |
#build_options_for_nested_elements(options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lutaml/model/xml/document.rb', line 71 def ( = {}) attribute = .delete(:attribute) rule = .delete(:rule) return {} unless rule # options = {} [:namespace_prefix] = rule.prefix if rule&.namespace_set? [:mixed_content] = rule.mixed_content [:tag_name] = rule.name [:mapper_class] = attribute&.type(register) if attribute [:set_namespace] = set_namespace?(rule) end |
#build_unordered_element(xml, element, options = {}) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/lutaml/model/xml/document.rb', line 213 def build_unordered_element(xml, element, = {}) mapper_class = determine_mapper_class(element, ) xml_mapping = mapper_class.mappings_for(:xml) return xml unless xml_mapping attributes = build_element_attributes(element, xml_mapping, ) prefix = determine_namespace_prefix(, xml_mapping) prefixed_xml = xml.add_namespace_prefix(prefix) tag_name = [:tag_name] || xml_mapping.root_element prefixed_xml.create_and_add_element(tag_name, prefix: prefix, attributes: attributes) do if .key?(:namespace_prefix) && ![:namespace_prefix] prefixed_xml.add_namespace_prefix(nil) end xml_mapping.attributes.each do |attribute_rule| attribute_rule.serialize_attribute(element, prefixed_xml.parent, xml) end mappings = xml_mapping.elements + [xml_mapping.raw_mapping].compact mappings.each do |element_rule| attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class) if attribute_def value = attribute_value_for(element, element_rule) next if !element_rule.render?(value, element) value = attribute_def.build_collection(value) if attribute_def.collection? && !attribute_def.collection_instance?(value) end add_to_xml( prefixed_xml, element, element_rule.prefix, value, .merge({ attribute: attribute_def, rule: element_rule, mapper_class: mapper_class }), ) end process_content_mapping(element, xml_mapping.content_mapping, prefixed_xml, mapper_class) end end |
#cdata ⇒ Object
435 436 437 |
# File 'lib/lutaml/model/xml/document.rb', line 435 def cdata @root.cdata end |
#children ⇒ Object
23 24 25 |
# File 'lib/lutaml/model/xml/document.rb', line 23 def children @root.children end |
#declaration(options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lutaml/model/xml/document.rb', line 39 def declaration() version = "1.0" version = [:declaration] if [:declaration].is_a?(String) encoding = [:encoding] ? "UTF-8" : nil encoding = [:encoding] if [:encoding].is_a?(String) declaration = "<?xml version=\"#{version}\"" declaration += " encoding=\"#{encoding}\"" if encoding declaration += "?>\n" declaration end |
#handle_nested_elements(builder, value, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lutaml/model/xml/document.rb', line 60 def handle_nested_elements(builder, value, = {}) = () case value when Array value.each { |val| build_element(builder, val, ) } else build_element(builder, value, ) end end |
#namespace_attributes(xml_mapping) ⇒ Object
402 403 404 405 406 407 |
# File 'lib/lutaml/model/xml/document.rb', line 402 def namespace_attributes(xml_mapping) return {} unless xml_mapping.namespace_uri key = ["xmlns", xml_mapping.namespace_prefix].compact.join(":") { key => xml_mapping.namespace_uri } end |
#order ⇒ Object
56 57 58 |
# File 'lib/lutaml/model/xml/document.rb', line 56 def order @root.order end |
#ordered?(element, options = {}) ⇒ Boolean
281 282 283 284 285 286 287 288 |
# File 'lib/lutaml/model/xml/document.rb', line 281 def ordered?(element, = {}) return false unless element.respond_to?(:element_order) return element.ordered? if element.respond_to?(:ordered?) return [:mixed_content] if .key?(:mixed_content) mapper_class = [:mapper_class] mapper_class ? mapper_class.mappings_for(:xml).mixed_content? : false end |
#parse_element(element, klass = nil, format = nil) ⇒ Object
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 |
# File 'lib/lutaml/model/xml/document.rb', line 89 def parse_element(element, klass = nil, format = nil) result = Lutaml::Model::MappingHash.new result.node = element result.item_order = self.class.order_of(element) element.children.each do |child| if klass&.<= Serialize attr = klass.attribute_for_child(self.class.name_of(child), format) end if child.respond_to?(:text?) && child.text? result.assign_or_append_value( self.class.name_of(child), self.class.text_of(child), ) next end result["elements"] ||= Lutaml::Model::MappingHash.new result["elements"].assign_or_append_value( self.class.namespaced_name_of(child), parse_element(child, attr&.type(register) || klass, format), ) end result["attributes"] = attributes_hash(element) if element.attributes&.any? result.merge(attributes_hash(element)) result end |
#process_content_mapping(element, content_rule, xml, mapper_class) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/lutaml/model/xml/document.rb', line 263 def process_content_mapping(element, content_rule, xml, mapper_class) return unless content_rule if content_rule.custom_methods[:to] mapper_class.new.send( content_rule.custom_methods[:to], element, xml.parent, xml, ) else text = content_rule.serialize(element) text = text.join if text.is_a?(Array) xml.add_text(xml, text, cdata: content_rule.cdata) end end |
#render_default?(rule, element) ⇒ Boolean
298 299 300 301 302 |
# File 'lib/lutaml/model/xml/document.rb', line 298 def render_default?(rule, element) !element.respond_to?(:using_default?) || rule.render_default? || !element.using_default?(rule.to) end |
#render_element?(rule, element, value) ⇒ Boolean
294 295 296 |
# File 'lib/lutaml/model/xml/document.rb', line 294 def render_element?(rule, element, value) rule.render?(value, element) end |
#set_namespace?(rule) ⇒ Boolean
290 291 292 |
# File 'lib/lutaml/model/xml/document.rb', line 290 def set_namespace?(rule) rule.nil? || !rule.namespace_set? end |
#text ⇒ Object
429 430 431 432 433 |
# File 'lib/lutaml/model/xml/document.rb', line 429 def text return @root.text_children.map(&:text) if @root.children.count > 1 @root.text end |
#to_h ⇒ Object
52 53 54 |
# File 'lib/lutaml/model/xml/document.rb', line 52 def to_h parse_element(@root) end |