Class: Moxml::Adapter::Rexml
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Rexml
show all
- Defined in:
- lib/moxml/adapter/rexml.rb
Constant Summary
Constants included
from XmlUtils
XmlUtils::VALID_ELEMENT_NAMES
Class Method Summary
collapse
-
.add_child(element, child) ⇒ Object
-
.add_next_sibling(node, sibling) ⇒ Object
-
.add_previous_sibling(node, sibling) ⇒ Object
-
.append_child_sequence(element, type) ⇒ Object
-
.at_xpath(node, expression, namespaces = {}) ⇒ Object
-
.attachments ⇒ Object
-
.attribute_element(attribute) ⇒ Object
-
.attributes(element) ⇒ Object
-
.bare_set_qname_safe? ⇒ Boolean
-
.cdata_content(node) ⇒ Object
-
.children(node, **_entity_bearing) ⇒ Object
-
.comment_content(node) ⇒ Object
-
.create_document(_native_doc = nil) ⇒ Object
-
.create_native_cdata(content, _owner_doc = nil) ⇒ Object
-
.create_native_comment(content, _owner_doc = nil) ⇒ Object
-
.create_native_declaration(version, encoding, standalone) ⇒ Object
-
.create_native_doctype(name, external_id, system_id) ⇒ Object
-
.create_native_element(name, _owner_doc = nil) ⇒ Object
-
.create_native_entity_reference(name, _owner_doc = nil) ⇒ Object
-
.create_native_namespace(element, prefix, uri) ⇒ Object
add a namespace definition, keep the element name unchanged.
-
.create_native_processing_instruction(target, content) ⇒ Object
-
.create_native_text(content, _owner_doc = nil) ⇒ Object
-
.declaration_attribute(node, name) ⇒ Object
-
.doctype_external_id(native) ⇒ Object
-
.doctype_name(native) ⇒ Object
Doctype accessor methods.
-
.doctype_system_id(native) ⇒ Object
-
.document(node) ⇒ Object
-
.duplicate_node(node) ⇒ Object
-
.entity_reference_name(node) ⇒ Object
-
.extract_encoding_from_xml(xml) ⇒ Object
-
.extract_text_recursively(element) ⇒ Object
-
.get_attribute(element, name) ⇒ Object
-
.get_attribute_value(element, name) ⇒ Object
-
.has_declaration?(native_doc, wrapper) ⇒ Boolean
-
.in_scope_namespaces(element) ⇒ Object
-
.inner_text(node) ⇒ Object
-
.namespace(node) ⇒ Object
-
.namespace_definitions(node) ⇒ Object
-
.namespace_prefix(node) ⇒ Object
-
.namespace_uri(node) ⇒ Object
-
.native_identity_stable? ⇒ Boolean
-
.next_sibling(node) ⇒ Object
-
.node_name(node) ⇒ Object
-
.node_type(node) ⇒ Object
-
.parent(node) ⇒ Object
-
.parse(xml, options = {}, _context = nil) ⇒ Object
-
.parse_fragment(xml, _context = nil) ⇒ Object
-
.prepare_xpath_namespaces(node) ⇒ Object
not used at the moment but may be useful when the xpath is upgraded to work with namespaces.
-
.previous_sibling(node) ⇒ Object
-
.processing_instruction_content(node) ⇒ Object
-
.processing_instruction_target(node) ⇒ Object
-
.remove(node) ⇒ Object
-
.remove_attribute(element, name) ⇒ Object
-
.remove_attribute_native(attribute) ⇒ Object
-
.remove_declaration(native_doc) ⇒ Object
-
.replace(node, new_node) ⇒ Object
-
.replace_children(element, children) ⇒ Object
-
.root(document) ⇒ Object
-
.sax_parse(xml, handler) ⇒ void
SAX parsing implementation for REXML.
-
.serialize(node, options = {}) ⇒ Object
-
.set_attribute(element, name, value) ⇒ Object
-
.set_attribute_name(attribute, name) ⇒ Object
-
.set_attribute_value(attribute, value) ⇒ Object
-
.set_cdata_content(node, content) ⇒ Object
-
.set_comment_content(node, content) ⇒ Object
-
.set_declaration_attribute(node, name, value) ⇒ Object
-
.set_namespace(element, ns) ⇒ Object
add a namespace prefix to the element name AND a namespace definition.
-
.set_node_name(node, name) ⇒ Object
-
.set_processing_instruction_content(node, content) ⇒ Object
-
.set_root(doc, element) ⇒ Object
-
.set_text_content(node, content) ⇒ Object
-
.text_content(node) ⇒ Object
-
.xpath(node, expression, namespaces = {}) ⇒ Object
Methods inherited from Base
actual_native, attribute_name, attribute_pairs, bare_get_qname_safe?, bulk_materialize?, children_accepts_entity_flag?, create_cdata, create_comment, create_declaration, create_doctype, create_element, create_element_with_attrs, create_entity_reference, create_namespace, create_processing_instruction, create_text, decode_entities, digest, entity_bearing?, expanded_attr_reads?, expanded_attr_value, free_document, iterparse, iterparse_file, line_number, materialize_fields, native_inclusive10, parse_diagnostics, parse_errors, parse_html, patch_node, patches_children?, plan_structs, preprocess_entities, restore_entities, same_node?, sax_supported?, serialize_generation, source_position, walk_descendants, wrap_native, wrappers_recyclable?
Methods included from XmlUtils
#encode_entities, #normalize_xml_value, #validate_comment_content, #validate_declaration_encoding, #validate_declaration_standalone, #validate_declaration_version, #validate_element_name, #validate_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri
Class Method Details
.add_child(element, child) ⇒ Object
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# File 'lib/moxml/adapter/rexml.rb', line 331
def add_child(element, child)
if element.is_a?(::REXML::Document) && child.is_a?(::REXML::XMLDecl)
attachments.set(element, :xml_declaration, child)
end
case child
when String
element.add_text(child)
append_child_sequence(element, :native)
when ::Moxml::Adapter::CustomizedRexml::EntityReference
refs = attachments.get(element, :entity_refs) || []
refs << child
attachments.set(element, :entity_refs, refs)
append_child_sequence(element, :eref)
else
element.add(child)
append_child_sequence(element, :native)
end
end
|
.add_next_sibling(node, sibling) ⇒ Object
371
372
373
374
|
# File 'lib/moxml/adapter/rexml.rb', line 371
def add_next_sibling(node, sibling)
parent = node.parent
parent.insert_after(node, sibling)
end
|
.add_previous_sibling(node, sibling) ⇒ Object
361
362
363
364
365
366
367
368
369
|
# File 'lib/moxml/adapter/rexml.rb', line 361
def add_previous_sibling(node, sibling)
parent = node.parent
parent.insert_before(node, sibling)
end
|
.append_child_sequence(element, type) ⇒ Object
355
356
357
358
359
|
# File 'lib/moxml/adapter/rexml.rb', line 355
def append_child_sequence(element, type)
seq = attachments.get(element, :child_sequence) || []
seq << type
attachments.set(element, :child_sequence, seq)
end
|
.at_xpath(node, expression, namespaces = {}) ⇒ Object
621
622
623
624
|
# File 'lib/moxml/adapter/rexml.rb', line 621
def at_xpath(node, expression, namespaces = {})
results = xpath(node, expression, namespaces)
results.first
end
|
.attribute_element(attribute) ⇒ Object
290
291
292
|
# File 'lib/moxml/adapter/rexml.rb', line 290
def attribute_element(attribute)
attribute.element
end
|
.attributes(element) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/moxml/adapter/rexml.rb', line 271
def attributes(element)
return [] unless element.is_a?(::REXML::Element)
result = []
element.attributes.each_attribute do |attr|
next if attr.prefix.to_s.start_with?("xmlns")
next if attr.name == "xmlns" && attr.prefix.to_s.empty?
result << attr
end
result
end
|
.bare_set_qname_safe? ⇒ Boolean
17
18
19
|
# File 'lib/moxml/adapter/rexml.rb', line 17
def bare_set_qname_safe?
true
end
|
.cdata_content(node) ⇒ Object
425
426
427
|
# File 'lib/moxml/adapter/rexml.rb', line 425
def cdata_content(node)
node.value
end
|
.children(node, **_entity_bearing) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/moxml/adapter/rexml.rb', line 192
def children(node, **_entity_bearing)
return [] unless node.is_a?(::REXML::Parent)
result = node.children.dup
entity_refs = attachments.get(node, :entity_refs)
if entity_refs
result.concat(entity_refs).uniq!(&:object_id) || result
else
result
end
end
|
417
418
419
|
# File 'lib/moxml/adapter/rexml.rb', line 417
def (node)
node.string
end
|
.create_document(_native_doc = nil) ⇒ Object
94
95
96
|
# File 'lib/moxml/adapter/rexml.rb', line 94
def create_document(_native_doc = nil)
::REXML::Document.new
end
|
.create_native_cdata(content, _owner_doc = nil) ⇒ Object
114
115
116
|
# File 'lib/moxml/adapter/rexml.rb', line 114
def create_native_cdata(content, _owner_doc = nil)
::REXML::CData.new(content.to_s)
end
|
118
119
120
|
# File 'lib/moxml/adapter/rexml.rb', line 118
def (content, _owner_doc = nil)
::REXML::Comment.new(content.to_s)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
127
128
129
|
# File 'lib/moxml/adapter/rexml.rb', line 127
def create_native_declaration(version, encoding, standalone)
::REXML::XMLDecl.new(version, encoding&.downcase, standalone)
end
|
.create_native_doctype(name, external_id, system_id) ⇒ Object
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/moxml/adapter/rexml.rb', line 131
def create_native_doctype(name, external_id, system_id)
return nil unless name
external = if external_id
system_id ? %(PUBLIC "#{external_id}" "#{system_id}") : %(PUBLIC "#{external_id}")
elsif system_id
%(SYSTEM "#{system_id}")
end
external ? ::REXML::DocType.new(name.to_s, external) : ::REXML::DocType.new(name.to_s)
end
|
.create_native_element(name, _owner_doc = nil) ⇒ Object
98
99
100
|
# File 'lib/moxml/adapter/rexml.rb', line 98
def create_native_element(name, _owner_doc = nil)
::REXML::Element.new(name.to_s)
end
|
.create_native_entity_reference(name, _owner_doc = nil) ⇒ Object
.create_native_namespace(element, prefix, uri) ⇒ Object
add a namespace definition, keep the element name unchanged
498
499
500
501
|
# File 'lib/moxml/adapter/rexml.rb', line 498
def create_native_namespace(element, prefix, uri)
element.add_namespace(prefix.to_s, uri)
::REXML::Attribute.new(prefix.to_s, uri, element)
end
|
.create_native_processing_instruction(target, content) ⇒ Object
122
123
124
125
|
# File 'lib/moxml/adapter/rexml.rb', line 122
def create_native_processing_instruction(target, content)
::REXML::Instruction.new(target.to_s.dup, content.to_s.dup)
end
|
.create_native_text(content, _owner_doc = nil) ⇒ Object
102
103
104
|
# File 'lib/moxml/adapter/rexml.rb', line 102
def create_native_text(content, _owner_doc = nil)
::REXML::Text.new(content.to_s, true, nil)
end
|
.declaration_attribute(node, name) ⇒ Object
395
396
397
398
399
400
401
402
403
404
|
# File 'lib/moxml/adapter/rexml.rb', line 395
def declaration_attribute(node, name)
case name
when "version"
node.version
when "encoding"
node.encoding
when "standalone"
node.standalone
end
end
|
.doctype_external_id(native) ⇒ Object
572
573
574
575
576
577
|
# File 'lib/moxml/adapter/rexml.rb', line 572
def doctype_external_id(native)
native.public ||
native.external_id&.match(/PUBLIC\s+"([^"]*)"/)&.[](1)
end
|
.doctype_name(native) ⇒ Object
568
569
570
|
# File 'lib/moxml/adapter/rexml.rb', line 568
def doctype_name(native)
native.name
end
|
.doctype_system_id(native) ⇒ Object
579
580
581
582
583
584
|
# File 'lib/moxml/adapter/rexml.rb', line 579
def doctype_system_id(native)
return native.system if native.system
quoted = native.external_id&.scan(/"([^"]*)"/)
quoted&.last&.first
end
|
.document(node) ⇒ Object
258
259
260
|
# File 'lib/moxml/adapter/rexml.rb', line 258
def document(node)
node.document
end
|
.duplicate_node(node) ⇒ Object
184
185
186
187
188
189
190
|
# File 'lib/moxml/adapter/rexml.rb', line 184
def duplicate_node(node)
if node.is_a?(::REXML::Parent)
node.deep_clone
else
Marshal.load(Marshal.dump(node))
end
end
|
.entity_reference_name(node) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/moxml/adapter/rexml.rb', line 57
def (xml)
return "UTF-8" unless xml.start_with?("<?xml")
decl_end = xml.index("?>")
return "UTF-8" unless decl_end
decl = xml[0...decl_end]
match = decl.match(/encoding\s*=\s*["']([^"']+)["']/i)
match ? match[1] : "UTF-8"
end
|
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
# File 'lib/moxml/adapter/rexml.rb', line 457
def (element)
return "" unless element
text = ""
element.children.each do |child|
case child
when ::REXML::Text
text += child.value
when ::REXML::Element
child_text = (child)
text += child_text
end
end
text
end
|
.get_attribute(element, name) ⇒ Object
319
320
321
|
# File 'lib/moxml/adapter/rexml.rb', line 319
def get_attribute(element, name)
element.attributes.get_attribute(name)
end
|
.get_attribute_value(element, name) ⇒ Object
323
324
325
|
# File 'lib/moxml/adapter/rexml.rb', line 323
def get_attribute_value(element, name)
element.attributes[name]
end
|
.has_declaration?(native_doc, wrapper) ⇒ Boolean
675
676
677
678
679
680
681
682
683
684
685
686
|
# File 'lib/moxml/adapter/rexml.rb', line 675
def has_declaration?(native_doc, wrapper)
xml_decl = attachments.get(native_doc, :xml_declaration)
if xml_decl.nil?
if attachments.key?(native_doc, :xml_declaration)
false
else
wrapper.has_xml_declaration
end
else
true
end
end
|
.in_scope_namespaces(element) ⇒ Object
557
558
559
560
561
562
563
564
565
|
# File 'lib/moxml/adapter/rexml.rb', line 557
def in_scope_namespaces(element)
namespaces = {}
element.namespaces.each do |prefix, uri|
key = prefix.to_s.empty? ? "xmlns" : prefix.to_s
ns = ::REXML::Attribute.new(key, uri, element)
namespaces[prefix] = ns
end
namespaces.values
end
|
.inner_text(node) ⇒ Object
477
478
479
480
481
482
483
|
# File 'lib/moxml/adapter/rexml.rb', line 477
def inner_text(node)
text_children = node.children
.grep(::REXML::Text)
.uniq(&:object_id)
text_children.map(&:value).join
end
|
.namespace(node) ⇒ Object
536
537
538
539
540
541
542
543
|
# File 'lib/moxml/adapter/rexml.rb', line 536
def namespace(node)
prefix = node.prefix
uri = node.namespace(prefix)
return if prefix.to_s.empty? && uri.to_s.empty?
owner = node.is_a?(::REXML::Attribute) ? node.element : node
::REXML::Attribute.new(prefix, uri, owner)
end
|
.namespace_definitions(node) ⇒ Object
545
546
547
548
549
550
551
552
553
554
555
|
# File 'lib/moxml/adapter/rexml.rb', line 545
def namespace_definitions(node)
return [] unless node.is_a?(::REXML::Element)
result = []
node.attributes.each_attribute do |attr|
next unless attr.prefix == "xmlns" || (attr.name == "xmlns" && attr.prefix.to_s.empty?)
result << attr
end
result
end
|
.namespace_prefix(node) ⇒ Object
528
529
530
|
# File 'lib/moxml/adapter/rexml.rb', line 528
def namespace_prefix(node)
node.name unless node.name == "xmlns"
end
|
.namespace_uri(node) ⇒ Object
532
533
534
|
# File 'lib/moxml/adapter/rexml.rb', line 532
def namespace_uri(node)
node.value
end
|
.native_identity_stable? ⇒ Boolean
21
22
23
|
# File 'lib/moxml/adapter/rexml.rb', line 21
def native_identity_stable?
true
end
|
.next_sibling(node) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/moxml/adapter/rexml.rb', line 214
def next_sibling(node)
current = node.next_sibling
seen = {}
while current
if current.is_a?(::REXML::Text) && current.to_s.strip.empty?
current = current.next_sibling
next
end
if seen[current.object_id]
current = current.next_sibling
next
end
seen[current.object_id] = true
break
end
current
end
|
.node_name(node) ⇒ Object
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/moxml/adapter/rexml.rb', line 173
def node_name(node)
case node
when ::REXML::Element, ::REXML::DocType
node.name
when ::REXML::XMLDecl
"xml"
when ::REXML::Instruction
node.target
end
end
|
.node_type(node) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/moxml/adapter/rexml.rb', line 147
def node_type(node)
case node
when ::REXML::Document then :document
when ::REXML::Element then :element
when ::REXML::CData then :cdata
when ::REXML::Text then :text
when ::REXML::Comment then :comment
when ::REXML::Attribute then :attribute when ::REXML::Namespace then :namespace when ::REXML::Instruction then :processing_instruction
when ::REXML::DocType then :doctype
when ::REXML::XMLDecl then :declaration
when ::Moxml::Adapter::CustomizedRexml::EntityReference then :entity_reference
else :unknown
end
end
|
.parent(node) ⇒ Object
210
211
212
|
# File 'lib/moxml/adapter/rexml.rb', line 210
def parent(node)
node.parent
end
|
.parse(xml, options = {}, _context = nil) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/moxml/adapter/rexml.rb', line 25
def parse(xml, options = {}, _context = nil)
xml = "" if xml.nil?
processed_xml = xml.encode("UTF-8")
processed_xml = preprocess_entities(processed_xml)
native_doc = begin
::REXML::Document.new(processed_xml)
rescue ::REXML::ParseException => e
if options[:strict]
raise Moxml::ParseError.new(
e.message,
line: e.line,
source: xml.is_a?(String) ? xml[0..100] : nil,
)
end
create_document
end
ctx = _context || Context.new(:rexml)
doc = Wrappers::Document.new(native_doc, ctx)
Entity::Restorer.new(doc).run if ctx.config.restore_entities
doc
end
|
.parse_fragment(xml, _context = nil) ⇒ Object
262
263
264
265
|
# File 'lib/moxml/adapter/rexml.rb', line 262
def parse_fragment(xml, _context = nil)
doc = parse("<m>#{xml}</m>").native
children(root(doc))
end
|
.prepare_xpath_namespaces(node) ⇒ Object
not used at the moment
but may be useful when the xpath is upgraded to work with namespaces
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
# File 'lib/moxml/adapter/rexml.rb', line 588
def prepare_xpath_namespaces(node)
ns = {}
all_ns = namespace_definitions(node)
all_ns.each do |prefix, uri|
if prefix.to_s.empty?
ns["xmlns"] = uri
else
ns[prefix] = uri
end
end
ns
end
|
.previous_sibling(node) ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/moxml/adapter/rexml.rb', line 236
def previous_sibling(node)
current = node.previous_sibling
seen = {}
while current
if current.is_a?(::REXML::Text) && current.to_s.strip.empty?
current = current.previous_sibling
next
end
if seen[current.object_id]
current = current.previous_sibling
next
end
seen[current.object_id] = true
break
end
current
end
|
.processing_instruction_content(node) ⇒ Object
437
438
439
|
# File 'lib/moxml/adapter/rexml.rb', line 437
def processing_instruction_content(node)
node.content
end
|
.processing_instruction_target(node) ⇒ Object
433
434
435
|
# File 'lib/moxml/adapter/rexml.rb', line 433
def processing_instruction_target(node)
node.target
end
|
.remove(node) ⇒ Object
376
377
378
379
380
381
382
383
384
|
# File 'lib/moxml/adapter/rexml.rb', line 376
def remove(node)
if node.is_a?(::REXML::XMLDecl) && node.parent.is_a?(::REXML::Document)
attachments.set(node.parent, :xml_declaration, nil)
end
node.remove
end
|
.remove_attribute(element, name) ⇒ Object
327
328
329
|
# File 'lib/moxml/adapter/rexml.rb', line 327
def remove_attribute(element, name)
element.delete_attribute(name.to_s)
end
|
.remove_attribute_native(attribute) ⇒ Object
314
315
316
317
|
# File 'lib/moxml/adapter/rexml.rb', line 314
def remove_attribute_native(attribute)
attribute.element.attributes.delete(attribute.expanded_name)
attribute
end
|
.remove_declaration(native_doc) ⇒ Object
688
689
690
|
# File 'lib/moxml/adapter/rexml.rb', line 688
def remove_declaration(native_doc)
attachments.set(native_doc, :xml_declaration, nil)
end
|
.replace(node, new_node) ⇒ Object
386
387
388
|
# File 'lib/moxml/adapter/rexml.rb', line 386
def replace(node, new_node)
node.replace_with(new_node)
end
|
.replace_children(element, children) ⇒ Object
390
391
392
393
|
# File 'lib/moxml/adapter/rexml.rb', line 390
def replace_children(element, children)
element.children.each(&:remove)
children.each { |child| element.add(child) }
end
|
.root(document) ⇒ Object
267
268
269
|
# File 'lib/moxml/adapter/rexml.rb', line 267
def root(document)
document.root
end
|
.sax_parse(xml, handler) ⇒ void
This method returns an undefined value.
SAX parsing implementation for REXML
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/moxml/adapter/rexml.rb', line 73
def sax_parse(xml, handler)
require "rexml/parsers/streamparser"
require "rexml/source"
bridge = REXMLStreamBridge.new(handler)
xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s
handler.on_start_document
::REXML::Parsers::StreamParser.new(xml_string, bridge).parse
handler.on_end_document
rescue ::REXML::ParseException => e
error = Moxml::ParseError.new(e.message, line: e.line)
handler.on_error(error)
end
|
.serialize(node, options = {}) ⇒ Object
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
# File 'lib/moxml/adapter/rexml.rb', line 626
def serialize(node, options = {})
output = StringIO.new("") if RUBY_ENGINE == "opal"
output ||= +""
if node.is_a?(::REXML::Document)
should_include_decl = if options.key?(:no_declaration)
!options[:no_declaration]
else
!node.xml_decl.nil?
end
if should_include_decl && node.xml_decl
decl = node.xml_decl
decl.encoding = options[:encoding] if options[:encoding]
output << "<?xml"
output << %( version="#{decl.version}") if decl.version
output << %( encoding="#{decl.encoding}") if decl.encoding
output << %( standalone="#{decl.standalone}") if decl.standalone
output << "?>"
end
node.doctype&.write(output)
node.children.each do |child|
next unless [::REXML::Instruction, ::REXML::CData,
::REXML::Comment, ::REXML::Text].include?(child.class)
write_with_formatter(child, output, options[:indent] || 2)
end
if node.root
write_with_formatter(node.root, output,
options[:indent] || 2)
end
else
write_with_formatter(node, output, options[:indent] || 2)
end
result = output.is_a?(StringIO) ? output.string : output
result.strip
end
|
.set_attribute(element, name, value) ⇒ Object
294
295
296
297
|
# File 'lib/moxml/adapter/rexml.rb', line 294
def set_attribute(element, name, value)
element.attributes[name&.to_s] = value&.to_s
element.attributes.get_attribute(name&.to_s)
end
|
.set_attribute_name(attribute, name) ⇒ Object
299
300
301
302
303
304
305
306
307
|
# File 'lib/moxml/adapter/rexml.rb', line 299
def set_attribute_name(attribute, name)
old_name = attribute.expanded_name
attribute.name = name
element = attribute.element
element.attributes.delete(old_name)
element.attributes << attribute
attribute
end
|
.set_attribute_value(attribute, value) ⇒ Object
309
310
311
312
|
# File 'lib/moxml/adapter/rexml.rb', line 309
def set_attribute_value(attribute, value)
attribute.normalized = value
attribute
end
|
.set_cdata_content(node, content) ⇒ Object
429
430
431
|
# File 'lib/moxml/adapter/rexml.rb', line 429
def set_cdata_content(node, content)
node.value = content.to_s
end
|
421
422
423
|
# File 'lib/moxml/adapter/rexml.rb', line 421
def (node, content)
node.string = content.to_s
end
|
.set_declaration_attribute(node, name, value) ⇒ Object
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/moxml/adapter/rexml.rb', line 406
def set_declaration_attribute(node, name, value)
case name
when "version"
node.version = value
when "encoding"
node.encoding = value
when "standalone"
node.standalone = value
end
end
|
.set_namespace(element, ns) ⇒ Object
add a namespace prefix to the element name AND a namespace definition
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
# File 'lib/moxml/adapter/rexml.rb', line 504
def set_namespace(element, ns)
if ns.nil?
element.add_namespace(nil, "") if element.is_a?(::REXML::Element)
element.name = element.name.to_s.sub(/\A(?:\w+):/, "")
return element
end
prefix = ns.name.to_s.empty? ? "xmlns" : ns.name.to_s
if element.is_a?(::REXML::Element)
element.add_namespace(prefix,
ns.value)
end
local = element.name.to_s.split(":", 2)[-1]
element.name = "#{prefix}:#{local}"
element
end
|
.set_node_name(node, name) ⇒ Object
164
165
166
167
168
169
170
171
|
# File 'lib/moxml/adapter/rexml.rb', line 164
def set_node_name(node, name)
case node
when ::REXML::Element
node.name = name.to_s
when ::REXML::Instruction
node.target = name.to_s
end
end
|
.set_processing_instruction_content(node, content) ⇒ Object
441
442
443
|
# File 'lib/moxml/adapter/rexml.rb', line 441
def set_processing_instruction_content(node, content)
node.content = content.to_s
end
|
.set_root(doc, element) ⇒ Object
142
143
144
145
|
# File 'lib/moxml/adapter/rexml.rb', line 142
def set_root(doc, element)
doc.delete_element(doc.root) if doc.root
doc.add_element(element)
end
|
.set_text_content(node, content) ⇒ Object
485
486
487
488
489
490
491
492
493
494
495
|
# File 'lib/moxml/adapter/rexml.rb', line 485
def set_text_content(node, content)
case node
when ::REXML::Text, ::REXML::CData
node.value = content.to_s
when ::REXML::Element
node.texts.each(&:remove)
node.add_text(content.to_s)
end
end
|
.text_content(node) ⇒ Object
.xpath(node, expression, namespaces = {}) ⇒ Object
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
# File 'lib/moxml/adapter/rexml.rb', line 606
def xpath(node, expression, namespaces = {})
if namespaces && !namespaces.empty?
::REXML::XPath.match(node, expression, namespaces)
else
node.get_elements(expression).to_a
end
rescue ::REXML::ParseException => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "REXML",
node: node,
)
end
|