Class: Lutaml::Xml::Document
- Inherits:
-
Object
- Object
- Lutaml::Xml::Document
- Defined in:
- lib/lutaml/xml/document.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#doctype ⇒ Object
readonly
Returns the value of attribute doctype.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#parsed_doc ⇒ Object
readonly
Returns the value of attribute parsed_doc.
-
#register ⇒ Object
readonly
Returns the value of attribute register.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#xml_declaration ⇒ Object
readonly
Returns the value of attribute xml_declaration.
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_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
- #cdata ⇒ Object
- #children ⇒ Object
- #declaration(options) ⇒ Object
-
#doctype_declaration ⇒ String?
Generate DOCTYPE declaration string.
- #element_children ⇒ Object
- #element_children_index ⇒ Object
-
#initialize(root, encoding = nil, register: nil, doctype: nil, parsed_doc: nil, xml_declaration: nil, **options) ⇒ Document
constructor
A new instance of Document.
- #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_element?(rule, element, value) ⇒ Boolean
- #text ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(root, encoding = nil, register: nil, doctype: nil, parsed_doc: nil, xml_declaration: nil, **options) ⇒ Document
Returns a new instance of Document.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/lutaml/xml/document.rb', line 7 def initialize(root, encoding = nil, register: nil, doctype: nil, parsed_doc: nil, xml_declaration: nil, **) @root = root @encoding = encoding @doctype = doctype @parsed_doc = parsed_doc @xml_declaration = xml_declaration @register = setup_register(register) @options = # NEW: Store options end |
Instance Attribute Details
#doctype ⇒ Object (readonly)
Returns the value of attribute doctype.
4 5 6 |
# File 'lib/lutaml/xml/document.rb', line 4 def doctype @doctype end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
4 5 6 |
# File 'lib/lutaml/xml/document.rb', line 4 def encoding @encoding end |
#parsed_doc ⇒ Object (readonly)
Returns the value of attribute parsed_doc.
4 5 6 |
# File 'lib/lutaml/xml/document.rb', line 4 def parsed_doc @parsed_doc end |
#register ⇒ Object (readonly)
Returns the value of attribute register.
4 5 6 |
# File 'lib/lutaml/xml/document.rb', line 4 def register @register end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
4 5 6 |
# File 'lib/lutaml/xml/document.rb', line 4 def root @root end |
#xml_declaration ⇒ Object (readonly)
Returns the value of attribute xml_declaration.
4 5 6 |
# File 'lib/lutaml/xml/document.rb', line 4 def xml_declaration @xml_declaration end |
Class Method Details
.encoding(xml, options) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/lutaml/xml/document.rb', line 38 def self.encoding(xml, ) if .key?(:encoding) [:encoding] else xml.encoding.to_s end end |
.name_of(element) ⇒ Object
235 236 237 |
# File 'lib/lutaml/xml/document.rb', line 235 def self.name_of(element) element.name end |
.namespaced_name_of(element) ⇒ Object
243 244 245 |
# File 'lib/lutaml/xml/document.rb', line 243 def self.namespaced_name_of(element) element.namespaced_name end |
.order_of(element) ⇒ Object
231 232 233 |
# File 'lib/lutaml/xml/document.rb', line 231 def self.order_of(element) element.order end |
.parse(xml, _options = {}) ⇒ Object
18 19 20 |
# File 'lib/lutaml/xml/document.rb', line 18 def self.parse(xml, = {}) raise NotImplementedError, "Subclasses must implement `parse`." end |
.text_of(element) ⇒ Object
239 240 241 |
# File 'lib/lutaml/xml/document.rb', line 239 def self.text_of(element) element.text end |
.type ⇒ Object
227 228 229 |
# File 'lib/lutaml/xml/document.rb', line 227 def self.type ::Lutaml::Model::Utils.snake_case(self).split("/").last.split("_").first end |
Instance Method Details
#add_value(xml, value, attribute, cdata: false) ⇒ Object
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 |
# File 'lib/lutaml/xml/document.rb', line 165 def add_value(xml, value, attribute, cdata: false) if !value.nil? if attribute.nil? # For delegated attributes where attribute is nil, just use the raw value xml.add_text(xml, value.to_s, cdata: cdata) elsif attribute.transform.is_a?(Class) && attribute.transform < Lutaml::Model::ValueTransformer # Check if value has already been transformed by a class-based transformer # If so, use it directly without going through attribute.serialize # Value has already been transformed, use it directly xml.add_text(xml, value.to_s, cdata: cdata) else # Normal serialization through attribute type system serialized_value = attribute.serialize(value, :xml, register) if attribute.raw? xml.add_xml_fragment(xml, value) elsif serialized_value.is_a?(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 end |
#attribute_definition_for(element, rule, mapper_class: nil) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/lutaml/xml/document.rb', line 211 def attribute_definition_for(element, rule, mapper_class: nil) klass = mapper_class || element.class return klass.attributes[rule.to] unless rule.delegate delegated_obj = element.send(rule.delegate) return nil if delegated_obj.nil? delegated_obj.class.attributes[rule.to] end |
#attribute_value_for(element, rule) ⇒ Object
221 222 223 224 225 |
# File 'lib/lutaml/xml/document.rb', line 221 def attribute_value_for(element, rule) return element.send(rule.to) unless rule.delegate element.send(rule.delegate).send(rule.to) end |
#attributes ⇒ Object
34 35 36 |
# File 'lib/lutaml/xml/document.rb', line 34 def attributes root.attributes end |
#attributes_hash(element) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/lutaml/xml/document.rb', line 128 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 |
#cdata ⇒ Object
259 260 261 |
# File 'lib/lutaml/xml/document.rb', line 259 def cdata @root.cdata end |
#children ⇒ Object
22 23 24 |
# File 'lib/lutaml/xml/document.rb', line 22 def children @root.children end |
#declaration(options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lutaml/xml/document.rb', line 46 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 |
#doctype_declaration ⇒ String?
Generate DOCTYPE declaration string
Uses native Nokogiri DOCTYPE if available, otherwise generates from hash
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lutaml/xml/document.rb', line 64 def doctype_declaration # Prefer native Nokogiri DOCTYPE if @parsed_doc.respond_to?(:internal_subset) && @parsed_doc.internal_subset return "#{@parsed_doc.internal_subset}\n" end # Fallback to manual generation for model serialization return nil unless @doctype parts = ["<!DOCTYPE #{@doctype[:name]}"] if @doctype[:public_id] parts << %(PUBLIC "#{@doctype[:public_id]}") parts << %("#{@doctype[:system_id]}") if @doctype[:system_id] elsif @doctype[:system_id] parts << %(SYSTEM "#{@doctype[:system_id]}") end "#{parts.join(' ')}>\n" end |
#element_children ⇒ Object
26 27 28 |
# File 'lib/lutaml/xml/document.rb', line 26 def element_children @root.element_children end |
#element_children_index ⇒ Object
30 31 32 |
# File 'lib/lutaml/xml/document.rb', line 30 def element_children_index @root.element_children_index end |
#order ⇒ Object
89 90 91 |
# File 'lib/lutaml/xml/document.rb', line 89 def order @root.order end |
#ordered?(element, options = {}) ⇒ Boolean
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/lutaml/xml/document.rb', line 146 def ordered?(element, = {}) return false unless element.respond_to?(:element_order) mapper_class = [:mapper_class] xml_mapping = mapper_class&.mappings_for(:xml) # Class mapping is the authoritative source for ordered/mixed. # Instance @ordered/@mixed are stale after class definition changes. return true if xml_mapping&.mixed_content? return true if xml_mapping&.ordered? return [:mixed_content] if .key?(:mixed_content) false end |
#parse_element(element, klass = nil, format = nil) ⇒ Object
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 |
# File 'lib/lutaml/xml/document.rb', line 93 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 if element.attributes&.any? result["attributes"] = attributes_hash(element) end result.merge(attributes_hash(element)) result end |
#process_content_mapping(element, content_rule, xml, mapper_class) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/lutaml/xml/document.rb', line 193 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_element?(rule, element, value) ⇒ Boolean
161 162 163 |
# File 'lib/lutaml/xml/document.rb', line 161 def render_element?(rule, element, value) rule.render?(value, element) end |
#text ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/lutaml/xml/document.rb', line 247 def text # Return Array only if there are actual element children (mixed content). # EntityReference nodes are text-like and should not trigger Array return. # For text + entity without elements, return joined String. has_element_children = @root.children.any? do |child| !child.text? && !entity_reference_node?(child) end return @root.text_children.map(&:text) if has_element_children @root.text_children.map(&:text).join end |
#to_h ⇒ Object
85 86 87 |
# File 'lib/lutaml/xml/document.rb', line 85 def to_h parse_element(@root) end |