Class: Lutaml::Xml::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xml/document.rb

Direct Known Subclasses

Adapter::BaseAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, **options)
  @root = root
  @encoding = encoding
  @doctype = doctype
  @parsed_doc = parsed_doc
  @xml_declaration = xml_declaration
  @register = setup_register(register)
  @options = options # NEW: Store options
end

Instance Attribute Details

#doctypeObject (readonly)

Returns the value of attribute doctype.



4
5
6
# File 'lib/lutaml/xml/document.rb', line 4

def doctype
  @doctype
end

#encodingObject (readonly)

Returns the value of attribute encoding.



4
5
6
# File 'lib/lutaml/xml/document.rb', line 4

def encoding
  @encoding
end

#parsed_docObject (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

#registerObject (readonly)

Returns the value of attribute register.



4
5
6
# File 'lib/lutaml/xml/document.rb', line 4

def register
  @register
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/lutaml/xml/document.rb', line 4

def root
  @root
end

#xml_declarationObject (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, options)
  if options.key?(:encoding)
    options[: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

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/lutaml/xml/document.rb', line 18

def self.parse(xml, _options = {})
  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

.typeObject



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

#attributesObject



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

#cdataObject



259
260
261
# File 'lib/lutaml/xml/document.rb', line 259

def cdata
  @root.cdata
end

#childrenObject



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(options)
  version = "1.0"
  version = options[:declaration] if options[:declaration].is_a?(String)

  encoding = options[:encoding] ? "UTF-8" : nil
  encoding = options[:encoding] if options[:encoding].is_a?(String)

  declaration = "<?xml version=\"#{version}\""
  declaration += " encoding=\"#{encoding}\"" if encoding
  declaration += "?>\n"
  declaration
end

#doctype_declarationString?

Generate DOCTYPE declaration string

Uses native Nokogiri DOCTYPE if available, otherwise generates from hash

Returns:

  • (String, nil)

    the DOCTYPE declaration or nil if no DOCTYPE



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_childrenObject



26
27
28
# File 'lib/lutaml/xml/document.rb', line 26

def element_children
  @root.element_children
end

#element_children_indexObject



30
31
32
# File 'lib/lutaml/xml/document.rb', line 30

def element_children_index
  @root.element_children_index
end

#orderObject



89
90
91
# File 'lib/lutaml/xml/document.rb', line 89

def order
  @root.order
end

#ordered?(element, options = {}) ⇒ Boolean

Returns:

  • (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, options = {})
  return false unless element.respond_to?(:element_order)

  mapper_class = options[: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 options[:mixed_content] if options.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

Returns:

  • (Boolean)


161
162
163
# File 'lib/lutaml/xml/document.rb', line 161

def render_element?(rule, element, value)
  rule.render?(value, element)
end

#textObject



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_hObject



85
86
87
# File 'lib/lutaml/xml/document.rb', line 85

def to_h
  parse_element(@root)
end