Class: Canon::Xml::DataModel

Inherits:
DataModel
  • Object
show all
Defined in:
lib/canon/xml/data_model.rb

Class Method Summary collapse

Class Method Details

.assign_moxml_namespace_scopes(element, inherited, own_namespaces) ⇒ Object

Expand each element's own declarations to in-scope bindings: the element's declarations shadow inherited ones; xml is prebound. Scope flows down the canon tree — no engine calls.



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/canon/xml/data_model.rb', line 405

def self.assign_moxml_namespace_scopes(element, inherited, own_namespaces)
  scope = inherited.dup
  own_namespaces[element].to_a.each do |prefix, uri|
    scope[prefix || ""] = uri
  end

  scope.each do |prefix, uri|
    element.add_namespace(Nodes::NamespaceNode.new(
                            prefix: prefix,
                            uri: uri,
                          ))
  end

  element.children.each do |child|
    assign_moxml_namespace_scopes(child, scope, own_namespaces) if child.is_a?(Nodes::ElementNode)
  end
end

.build_attribute_nodes(nokogiri_element, element) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/canon/xml/data_model.rb', line 213

def self.build_attribute_nodes(nokogiri_element, element)
  # attribute_nodes, not the attributes hash: the hash is keyed by
  # local name and collapses e.g. attr / a:attr / b:attr into one
  # entry, losing namespaced attributes (W3C C14N ex 3.3).
  nokogiri_element.attribute_nodes.each do |attr|
    attr_node = Nodes::AttributeNode.new(
      name: attr.name,
      value: attr.value,
      namespace_uri: attr.namespace&.href,
      prefix: attr.namespace&.prefix,
    )
    element.add_attribute(attr_node)
  end
end

.build_comment_node(nokogiri_comment) ⇒ Object



239
240
241
# File 'lib/canon/xml/data_model.rb', line 239

def self.build_comment_node(nokogiri_comment)
  Nodes::CommentNode.new(value: nokogiri_comment.content)
end

.build_element_node(nokogiri_element, preserve_whitespace: false, inherited_namespaces: nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/canon/xml/data_model.rb', line 173

def self.build_element_node(nokogiri_element, preserve_whitespace: false,
inherited_namespaces: nil)
  element = Nodes::ElementNode.new(
    name: nokogiri_element.name,
    namespace_uri: nokogiri_element.namespace&.href,
    prefix: nokogiri_element.namespace&.prefix,
  )

  scope = nokogiri_namespace_scope(nokogiri_element, inherited_namespaces)
  scope.each do |prefix, uri|
    element.add_namespace(Nodes::NamespaceNode.new(
                            prefix: prefix,
                            uri: uri,
                          ))
  end

  build_attribute_nodes(nokogiri_element, element)

  nokogiri_element.children.each do |child|
    node = build_node_from_nokogiri(child,
                                    preserve_whitespace: preserve_whitespace,
                                    inherited_namespaces: scope)
    element.add_child(node) if node
  end

  element
end

.build_from_moxml(moxml_doc, preserve_whitespace: false) ⇒ Object



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
# File 'lib/canon/xml/data_model.rb', line 281

def self.build_from_moxml(moxml_doc, preserve_whitespace: false)
  root = Nodes::RootNode.new

  if moxml_doc.is_a?(Moxml::Document) && moxml_doc.root
    element = build_moxml_subtree_from_records(moxml_doc.root,
                                               preserve_whitespace: preserve_whitespace)
    root.add_child(element) if element
    moxml_doc.children.each do |child|
      next if child.equal?(moxml_doc.root)
      next if child.is_a?(Moxml::Doctype)

      node = build_moxml_node(child,
                              preserve_whitespace: preserve_whitespace)
      root.add_child(node) if node
    end
  else
    moxml_doc.children.each do |child|
      next if child.is_a?(Moxml::Doctype)

      node = build_moxml_node(child,
                              preserve_whitespace: preserve_whitespace)
      root.add_child(node) if node
    end
  end

  root
end

.build_from_nokogiri(nokogiri_doc, preserve_whitespace: false) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/canon/xml/data_model.rb', line 129

def self.build_from_nokogiri(nokogiri_doc, preserve_whitespace: false)
  root = Nodes::RootNode.new

  if nokogiri_doc.is_a?(Nokogiri::XML::Document) && nokogiri_doc.root
    root.add_child(build_element_node(nokogiri_doc.root,
                                      preserve_whitespace: preserve_whitespace))
    nokogiri_doc.children.each do |child|
      next if child == nokogiri_doc.root
      next if child.is_a?(Nokogiri::XML::DTD)

      node = build_node_from_nokogiri(child,
                                      preserve_whitespace: preserve_whitespace)
      root.add_child(node) if node
    end
  else
    nokogiri_doc.children.each do |child|
      next if child.is_a?(Nokogiri::XML::DTD)

      node = build_node_from_nokogiri(child,
                                      preserve_whitespace: preserve_whitespace)
      root.add_child(node) if node
    end
  end

  root
end

.build_moxml_attribute_nodes(moxml_element, element) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/canon/xml/data_model.rb', line 482

def self.build_moxml_attribute_nodes(moxml_element, element)
  seen = {}
  moxml_element.attributes.each do |attr|
    # Duplicate attribute names are invalid XML; engines expose
    # them differently (libxml2 lists repeats, libleptris dedups
    # last-wins). Canon follows the Nokogiri presentation contract:
    # first occurrence wins, keyed by name + namespace.
    key = [attr.name, attr.namespace&.uri]
    next if seen.key?(key)

    seen[key] = true
    element.add_attribute(Nodes::AttributeNode.new(
                            name: attr.name,
                            value: attr.value,
                            namespace_uri: attr.namespace&.uri,
                            prefix: attr.namespace&.prefix,
                          ))
  end
end

.build_moxml_comment_node(moxml_comment) ⇒ Object



512
513
514
# File 'lib/canon/xml/data_model.rb', line 512

def self.build_moxml_comment_node(moxml_comment)
  Nodes::CommentNode.new(value: moxml_comment.content)
end

.build_moxml_element_from_record(record, frames, own_namespaces) ⇒ Object



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
# File 'lib/canon/xml/data_model.rb', line 361

def self.build_moxml_element_from_record(record, frames, own_namespaces)
  element = Nodes::ElementNode.new(
    name: record[:qname],
    namespace_uri: record[:namespace_uri],
    prefix: record[:prefix],
  )

  seen = {}
  record[:attributes].each do |name, value, namespace_uri, prefix|
    key = [name, namespace_uri]
    next if seen.key?(key)

    seen[key] = true
    element.add_attribute(Nodes::AttributeNode.new(
                            name: name,
                            value: value,
                            namespace_uri: namespace_uri,
                            prefix: prefix,
                          ))
  end

  # Adopt completed children: they pop in reverse order; reversing
  # once is O(n) (unshift per child would be O(n^2) on wide trees).
  children = []
  children << frames.pop[1] while frames.any? && frames.last[0] > record[:depth]
  children.reverse_each { |child| element.add_child(child) }

  own_namespaces[element] = record[:namespaces]
  element
end

.build_moxml_element_node(moxml_element, preserve_whitespace: false, inherited_namespaces: nil) ⇒ Object



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
# File 'lib/canon/xml/data_model.rb', line 439

def self.build_moxml_element_node(moxml_element,
preserve_whitespace: false,
inherited_namespaces: nil)
  ns = moxml_element.namespace
  element = Nodes::ElementNode.new(
    name: moxml_element.name,
    namespace_uri: ns&.uri,
    prefix: ns&.prefix,
  )

  scope = moxml_namespace_scope(moxml_element, inherited_namespaces)
  scope.each do |prefix, uri|
    element.add_namespace(Nodes::NamespaceNode.new(
                            prefix: prefix,
                            uri: uri,
                          ))
  end

  build_moxml_attribute_nodes(moxml_element, element)

  moxml_element.children.each do |child|
    node = build_moxml_node(child,
                            preserve_whitespace: preserve_whitespace,
                            inherited_namespaces: scope)
    element.add_child(node) if node
  end

  element
end

.build_moxml_node(node, preserve_whitespace: false, inherited_namespaces: nil) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/canon/xml/data_model.rb', line 423

def self.build_moxml_node(node, preserve_whitespace: false,
inherited_namespaces: nil)
  case node
  when Moxml::Element
    build_moxml_element_node(node,
                             preserve_whitespace: preserve_whitespace,
                             inherited_namespaces: inherited_namespaces)
  when Moxml::Text, Moxml::Cdata
    build_moxml_text_node(node, preserve_whitespace: preserve_whitespace)
  when Moxml::Comment
    build_moxml_comment_node(node)
  when Moxml::ProcessingInstruction
    build_moxml_pi_node(node)
  end
end

.build_moxml_pi_node(moxml_pi) ⇒ Object



516
517
518
519
520
521
# File 'lib/canon/xml/data_model.rb', line 516

def self.build_moxml_pi_node(moxml_pi)
  Nodes::ProcessingInstructionNode.new(
    target: moxml_pi.target,
    data: moxml_pi.content,
  )
end

.build_moxml_subtree_from_records(moxml_element, preserve_whitespace: false) ⇒ Object

Document path: build the root subtree from materialize records — one flattened record stream (moxml#132/#138) instead of a wrapper walk, so conversion costs no Moxml::Node allocation per node.

Records arrive post-order (children before parents): completed nodes stack up with their depth, and an element record adopts every completed node deeper than itself. Each element's OWN namespace declarations ride an identity-keyed stash; afterwards assign_moxml_namespace_scopes expands them to in-scope scopes with the same first-wins/xml-prebound semantics as the Nokogiri collector.



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
# File 'lib/canon/xml/data_model.rb', line 320

def self.build_moxml_subtree_from_records(moxml_element,
preserve_whitespace: false)
  frames = []
  own_namespaces = {}.compare_by_identity

  moxml_element.materialize do |record|
    # The record contract is root-subtree-only since moxml 0.5.11
    # (moxml#140). Older 0.5.x releases — still allowed by canon's
    # gemspec floor — leaked epilog document-level records at depth
    # 0, which the document-children enumeration below also covers;
    # one integer compare per record keeps those working.
    next if record[:depth].zero? && record[:kind] != :element

    node = case record[:kind]
           when :element
             build_moxml_element_from_record(record, frames,
                                             own_namespaces)
           when :text, :cdata
             build_moxml_text_from_record(record, preserve_whitespace)
           when :comment
             Nodes::CommentNode.new(value: record[:text])
           when :processing_instruction
             Nodes::ProcessingInstructionNode.new(
               target: record[:qname],
               data: record[:text] || "",
             )
           end

    frames.push([record[:depth], node]) if node
  end

  top = frames.last
  return nil unless top

  element = top[1]
  assign_moxml_namespace_scopes(element,
                                { "xml" => "http://www.w3.org/XML/1998/namespace" },
                                own_namespaces)
  element
end

.build_moxml_text_from_record(record, preserve_whitespace) ⇒ Object

Text records inside a document subtree always have an element parent (only the document element sits at depth 0), so the whitespace-only skip rule matches the wrapper path exactly.



395
396
397
398
399
400
# File 'lib/canon/xml/data_model.rb', line 395

def self.build_moxml_text_from_record(record, preserve_whitespace)
  content = record[:text].to_s
  return nil if !preserve_whitespace && content.strip.empty?

  Nodes::TextNode.new(value: content, original: content)
end

.build_moxml_text_node(moxml_text, preserve_whitespace: false) ⇒ Object



502
503
504
505
506
507
508
509
510
# File 'lib/canon/xml/data_model.rb', line 502

def self.build_moxml_text_node(moxml_text, preserve_whitespace: false)
  content = moxml_text.content

  if !preserve_whitespace && content.strip.empty? && moxml_text.parent.is_a?(Moxml::Element)
    return nil
  end

  Nodes::TextNode.new(value: content, original: content)
end

.build_node_from_nokogiri(nokogiri_node, preserve_whitespace: false, inherited_namespaces: nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/canon/xml/data_model.rb', line 156

def self.build_node_from_nokogiri(nokogiri_node,
preserve_whitespace: false, inherited_namespaces: nil)
  case nokogiri_node
  when Nokogiri::XML::Element
    build_element_node(nokogiri_node,
                       preserve_whitespace: preserve_whitespace,
                       inherited_namespaces: inherited_namespaces)
  when Nokogiri::XML::Text, Nokogiri::XML::CDATA
    build_text_node(nokogiri_node,
                    preserve_whitespace: preserve_whitespace)
  when Nokogiri::XML::Comment
    build_comment_node(nokogiri_node)
  when Nokogiri::XML::ProcessingInstruction
    build_pi_node(nokogiri_node)
  end
end

.build_pi_node(nokogiri_pi) ⇒ Object



243
244
245
246
247
248
# File 'lib/canon/xml/data_model.rb', line 243

def self.build_pi_node(nokogiri_pi)
  Nodes::ProcessingInstructionNode.new(
    target: nokogiri_pi.name,
    data: nokogiri_pi.content,
  )
end

.build_text_node(nokogiri_text, preserve_whitespace: false) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/canon/xml/data_model.rb', line 228

def self.build_text_node(nokogiri_text, preserve_whitespace: false)
  content = nokogiri_text.content

  if !preserve_whitespace && content.strip.empty? && nokogiri_text.parent.is_a?(Nokogiri::XML::Element)
    return nil
  end

  original = nokogiri_text.to_xml
  Nodes::TextNode.new(value: content, original: original)
end

.check_for_relative_namespace_uris(doc) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/canon/xml/data_model.rb', line 115

def self.check_for_relative_namespace_uris(doc)
  doc.traverse do |node|
    next unless node.is_a?(Nokogiri::XML::Element)

    node.namespace_definitions.each do |ns|
      next if ns.href.nil? || ns.href.empty?
      if relative_uri?(ns.href)
        raise Canon::Error,
              "Relative namespace URI not allowed: #{ns.href}"
      end
    end
  end
end

.check_moxml_relative_namespace_uris(node) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/canon/xml/data_model.rb', line 264

def self.check_moxml_relative_namespace_uris(node)
  case node
  when Moxml::Element
    node.namespace_definitions.each do |ns|
      href = ns.uri
      next if href.nil? || href.empty?
      if relative_uri?(href)
        raise Canon::Error,
              "Relative namespace URI not allowed: #{href}"
      end
    end
    node.children.each { |child| check_moxml_relative_namespace_uris(child) }
  when Moxml::Document
    node.children.each { |child| check_moxml_relative_namespace_uris(child) }
  end
end

.extract_xml_encoding(xml_string) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/canon/xml/data_model.rb', line 82

def self.extract_xml_encoding(xml_string)
  binary_string = xml_string.dup.force_encoding("BINARY")
  if binary_string =~ /\A\s*<\?xml[^>]*\bencoding\s*=\s*["']([^"']+)["'][^>]*\?>/i
    return Regexp.last_match(1)
  end

  nil
end

.from_moxml_xml(xml_string, preserve_whitespace:) ⇒ Object

--- Moxml path ---



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/canon/xml/data_model.rb', line 252

def self.from_moxml_xml(xml_string, preserve_whitespace:)
  doc = Canon::XmlParsing.moxml_context.parse(xml_string, readonly: true)
  check_moxml_relative_namespace_uris(doc)
  result = build_from_moxml(doc, preserve_whitespace: preserve_whitespace)
  # Canon owns this document's full lifecycle: the canon tree holds
  # no engine references once built, so release the native tree now
  # instead of waiting for the GC finalizer (moxml#134; no-op on
  # GC-managed adapters).
  doc.free
  result
end

.from_nokogiri_xml(xml_string, preserve_whitespace:) ⇒ Object

--- Nokogiri path ---



105
106
107
108
109
110
111
112
113
# File 'lib/canon/xml/data_model.rb', line 105

def self.from_nokogiri_xml(xml_string, preserve_whitespace:)
  doc = Nokogiri::XML(xml_string, &:nonet)
  check_for_relative_namespace_uris(doc)
  result = build_from_nokogiri(doc,
                               preserve_whitespace: preserve_whitespace)
  errors = Array(doc.errors).map(&:to_s)
  result.parse_errors = errors if errors.any?
  result
end

.from_xml(xml_string, preserve_whitespace: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/canon/xml/data_model.rb', line 9

def self.from_xml(xml_string, preserve_whitespace: false)
  normalized_xml = normalize_encoding(xml_string)

  if Canon::XmlBackend.nokogiri?
    from_nokogiri_xml(normalized_xml,
                      preserve_whitespace: preserve_whitespace)
  else
    from_moxml_xml(normalized_xml,
                   preserve_whitespace: preserve_whitespace)
  end
end

.moxml_namespace_scope(moxml_element, inherited) ⇒ Object

In-scope namespace bindings: the element's own declarations shadow inherited ones, xml is prebound. The scope is passed down the recursion so each element pays one declaration fetch instead of an ancestor walk through the adapter layer (the Nokogiri collector walks ancestors because those calls are cheap there).



474
475
476
477
478
479
480
# File 'lib/canon/xml/data_model.rb', line 474

def self.moxml_namespace_scope(moxml_element, inherited)
  scope = inherited ? inherited.dup : { "xml" => "http://www.w3.org/XML/1998/namespace" }
  moxml_element.namespace_definitions.each do |decl|
    scope[decl.prefix || ""] = decl.uri
  end
  scope
end

.nokogiri_namespace_scope(nokogiri_element, inherited) ⇒ Object

In-scope namespace bindings: the element's own declarations shadow inherited ones, xml is prebound. The scope is passed down the recursion — one definition fetch per element instead of an ancestor walk per element (O(n) total, not O(n * depth)).



205
206
207
208
209
210
211
# File 'lib/canon/xml/data_model.rb', line 205

def self.nokogiri_namespace_scope(nokogiri_element, inherited)
  scope = inherited ? inherited.dup : { "xml" => "http://www.w3.org/XML/1998/namespace" }
  nokogiri_element.namespace_definitions.each do |ns|
    scope[ns.prefix || ""] = ns.href
  end
  scope
end

.normalize_encoding(xml_string) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/canon/xml/data_model.rb', line 21

def self.normalize_encoding(xml_string)
  return xml_string unless xml_string.is_a?(String)

  declared_encoding = extract_xml_encoding(xml_string)

  if declared_encoding
    if declared_encoding.upcase != "UTF-8"
      utf8_reinterpreted = try_utf8_reinterpretation(xml_string)
      if utf8_reinterpreted
        return update_xml_declaration(xml_string,
                                      "UTF-8")
      end

      return transcode_to_utf8(xml_string, declared_encoding)
    end
  elsif xml_string.encoding.name != "UTF-8"
    reinterpreted = try_utf8_reinterpretation(xml_string)
    return reinterpreted if reinterpreted

    return transcode_to_utf8(xml_string, xml_string.encoding.name)
  end

  xml_string
end

.parse(xml_string) ⇒ Object



91
92
93
# File 'lib/canon/xml/data_model.rb', line 91

def self.parse(xml_string)
  from_xml(xml_string)
end

.relative_uri?(uri) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/canon/xml/data_model.rb', line 99

def self.relative_uri?(uri)
  uri !~ %r{^[a-zA-Z][a-zA-Z0-9+.-]*:}
end

.serialize(node) ⇒ Object



95
96
97
# File 'lib/canon/xml/data_model.rb', line 95

def self.serialize(node)
  node.to_s
end

.transcode_to_utf8(xml_string, source_encoding) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/canon/xml/data_model.rb', line 52

def self.transcode_to_utf8(xml_string, source_encoding)
  if source_encoding != "UTF-8"
    forced = xml_string.dup.force_encoding(source_encoding)
    if forced.valid_encoding?
      utf8_check = xml_string.dup.force_encoding("UTF-8")
      if utf8_check.valid_encoding?
        return xml_string.dup.force_encoding("UTF-8")
      end

      return forced.encode("UTF-8", source_encoding,
                           invalid: :replace,
                           undef: :replace,
                           replace: "?")
    end
  end

  xml_string.dup.force_encoding("UTF-8")
rescue EncodingError
  xml_string
end

.try_utf8_reinterpretation(xml_string) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/canon/xml/data_model.rb', line 73

def self.try_utf8_reinterpretation(xml_string)
  return xml_string if xml_string.encoding.name == "UTF-8"

  forced = xml_string.dup.force_encoding("UTF-8")
  return forced if forced.valid_encoding?

  nil
end

.update_xml_declaration(xml_string, new_encoding) ⇒ Object



46
47
48
49
50
# File 'lib/canon/xml/data_model.rb', line 46

def self.update_xml_declaration(xml_string, new_encoding)
  xml_string.sub(/\bencoding\s*=\s*["'][^"']+["']/i) do |_match|
    %(encoding="#{new_encoding}")
  end
end