Class: Moxml::Adapter::Base

Inherits:
Object
  • Object
show all
Extended by:
XmlUtils
Defined in:
lib/moxml/adapter/base.rb

Direct Known Subclasses

Libxml, Nokogiri, Oga, Ox, Rexml

Class Method Summary collapse

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_pi_target, validate_prefix, validate_uri

Class Method Details

.create_cdata(content) ⇒ Object



74
75
76
# File 'lib/moxml/adapter/base.rb', line 74

def create_cdata(content)
  create_native_cdata(normalize_xml_value(content))
end

.create_comment(content) ⇒ Object



78
79
80
81
# File 'lib/moxml/adapter/base.rb', line 78

def create_comment(content)
  validate_comment_content(content)
  create_native_comment(normalize_xml_value(content))
end

.create_declaration(version = "1.0", encoding = "UTF-8", standalone = nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/moxml/adapter/base.rb', line 93

def create_declaration(version = "1.0", encoding = "UTF-8",
                       standalone = nil)
  validate_declaration_version(version)
  validate_declaration_encoding(encoding)
  validate_declaration_standalone(standalone)
  create_native_declaration(version, encoding, standalone)
end

.create_doctype(name, external_id, system_id) ⇒ Object



83
84
85
# File 'lib/moxml/adapter/base.rb', line 83

def create_doctype(name, external_id, system_id)
  create_native_doctype(name, external_id, system_id)
end

.create_document(_native_doc = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/moxml/adapter/base.rb', line 56

def create_document(_native_doc = nil)
  raise Moxml::NotImplementedError.new(
    "create_document not implemented",
    feature: "create_document",
    adapter: name,
  )
end

.create_element(name) ⇒ Object



64
65
66
67
# File 'lib/moxml/adapter/base.rb', line 64

def create_element(name)
  validate_element_name(name)
  create_native_element(name)
end

.create_namespace(element, prefix, uri) ⇒ Object



101
102
103
104
105
# File 'lib/moxml/adapter/base.rb', line 101

def create_namespace(element, prefix, uri)
  validate_prefix(prefix) if prefix
  validate_uri(uri)
  create_native_namespace(element, prefix, uri)
end

.create_processing_instruction(target, content) ⇒ Object



87
88
89
90
91
# File 'lib/moxml/adapter/base.rb', line 87

def create_processing_instruction(target, content)
  validate_pi_target(target)
  create_native_processing_instruction(target,
                                       normalize_xml_value(content))
end

.create_text(content) ⇒ Object



69
70
71
72
# File 'lib/moxml/adapter/base.rb', line 69

def create_text(content)
  # Ox freezes the content, so we need to dup it
  create_native_text(normalize_xml_value(content).dup)
end

.duplicate_node(node) ⇒ Object



115
116
117
# File 'lib/moxml/adapter/base.rb', line 115

def duplicate_node(node)
  node.dup
end

.parse(_xml, _options = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/moxml/adapter/base.rb', line 22

def parse(_xml, _options = {})
  raise Moxml::NotImplementedError.new(
    "parse not implemented",
    feature: "parse",
    adapter: name,
  )
end

.patch_node(node, _parent = nil) ⇒ Object



119
120
121
122
# File 'lib/moxml/adapter/base.rb', line 119

def patch_node(node, _parent = nil)
  # monkey-patch the native node if necessary
  node
end

.prepare_for_new_document(node, _target_doc) ⇒ Object



124
125
126
127
128
129
# File 'lib/moxml/adapter/base.rb', line 124

def prepare_for_new_document(node, _target_doc)
  # Hook for adapters that need special handling when moving nodes
  # between documents (e.g., LibXML's document.import)
  # Default: no-op for backward compatibility
  node
end

.sax_parse(_xml, _handler) ⇒ void

This method returns an undefined value.

Parse XML using SAX (event-driven) parsing

SAX parsing provides a memory-efficient way to process XML by triggering events as the document is parsed, rather than building a complete DOM tree.

Parameters:

  • xml (String, IO)

    XML string or IO object to parse

  • handler (Moxml::SAX::Handler)

    Handler object receiving events

Raises:



40
41
42
43
44
45
46
# File 'lib/moxml/adapter/base.rb', line 40

def sax_parse(_xml, _handler)
  raise Moxml::NotImplementedError.new(
    "sax_parse not implemented",
    feature: "sax_parse",
    adapter: name,
  )
end

.sax_supported?Boolean

Check if this adapter supports SAX parsing

Returns:

  • (Boolean)

    true if SAX parsing is supported



51
52
53
54
# File 'lib/moxml/adapter/base.rb', line 51

def sax_supported?
  respond_to?(:sax_parse) &&
    method(:sax_parse).owner != Moxml::Adapter::Base.singleton_class
end

.set_attribute_name(attribute, name) ⇒ Object



107
108
109
# File 'lib/moxml/adapter/base.rb', line 107

def set_attribute_name(attribute, name)
  attribute.name = name
end

.set_attribute_value(attribute, value) ⇒ Object



111
112
113
# File 'lib/moxml/adapter/base.rb', line 111

def set_attribute_value(attribute, value)
  attribute.value = value
end

.set_root(_doc, _element) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/moxml/adapter/base.rb', line 14

def set_root(_doc, _element)
  raise Moxml::NotImplementedError.new(
    "set_root not implemented",
    feature: "set_root",
    adapter: name,
  )
end