Class: RDF::RDFXML::Reader::Nokogiri::NodeProxy

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb

Overview

Proxy class to implement uniform element accessors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, parent = nil) ⇒ NodeProxy

Returns a new instance of NodeProxy.



21
22
23
24
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 21

def initialize(node, parent = nil)
  @node = node
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Proxy for everything else to @node



179
180
181
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 179

def method_missing(method, *args)
  @node.send(method, *args)
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



18
19
20
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 18

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



19
20
21
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 19

def parent
  @parent
end

Instance Method Details

#add_namespace(prefix, href) ⇒ Object



150
151
152
153
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 150

def add_namespace(prefix, href)
  @def_namespace = href if prefix.nil?
  @node.add_namespace(prefix, href)
end

#ancestorsObject

Ancestors of this element, in order



117
118
119
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 117

def ancestors
  @ancestors ||= parent ? parent.ancestors + [parent] : []
end

#at_xpath(*args) ⇒ Object



144
145
146
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 144

def at_xpath(*args)
  xpath(*args).first
end

#attribute_nodesString

Inner text of an element. Decode Entities

def inner_text coder = HTMLEntities.new coder.decode(@node.inner_text) end

Returns:



130
131
132
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 130

def attribute_nodes
  @attribute_nodes ||= NodeSetProxy.new(@node.attribute_nodes, self)
end

#attribute_with_ns(name, namespace) ⇒ Nokogiri::XML::Attr

Monkey patch attribute_with_ns, to insure nil is returned for #null?

Get the attribute node with name and namespace

Parameters:

Returns:

  • (Nokogiri::XML::Attr)


65
66
67
68
69
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 65

def attribute_with_ns(name, namespace)
  a = @node.attribute_with_ns(name, namespace)

  (a.respond_to?(:null?) && a.null?) ? nil : a # to ensure FFI Pointer compatibility
end

#baseString

Get any xml:base in effect for this element

Returns:



47
48
49
50
51
52
53
54
55
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 47

def base
  if @base.nil?
    @base = attribute_with_ns("base", RDF::XML.to_s) ||
    (parent && parent.element? && parent.base) ||
    false
  end

  @base == false ? nil : @base
end

#childrenNodeSetProxy

Children of this node

Returns:



112
113
114
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 112

def children
  @children ||= NodeSetProxy.new(@node.children, self)
end

#create_node(name, children) ⇒ Object

Create a new element child of an existing node



27
28
29
30
31
32
33
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 27

def create_node(name, children)
  native = ::Nokogiri::XML::Element.new(name, @node.document)
  children.each do |c|
    native.add_child(c.node)
  end
  NodeProxy.new(native, self)
end

#display_pathObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 71

def display_path
  @display_path ||= begin
    path = []
    path << parent.display_path if parent
    path << @node.name
    case @node
    when ::Nokogiri::XML::Element then path.join("/")
    when ::Nokogiri::XML::Attr    then path.join("@")
    else path.join("?")
    end
  end
end

#languageString

Element language

Returns:



39
40
41
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 39

def language
  attribute_with_ns("lang", RDF::XML.to_s)
end

#namespaceObject



155
156
157
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 155

def namespace
  @def_namespace || @node.namespace
end

#namespacesHash{String => String}

Retrieve XMLNS definitions for this element

Returns:



104
105
106
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 104

def namespaces
  @namespaces ||= @node.namespace_definitions.inject({}) {|memo, ns| memo[ns.prefix] = ns.href.to_s; memo }
end

#text?Array<:text, :element, :attribute>

Return true of this is a text node

Returns:

  • (Array<:text, :element, :attribute>)


88
89
90
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 88

def text?
  (@node.text? || @node.cdata?) && !@node.blank?
end

#text_content?Array<:text, :element, :attribute>

Return true of all child elements are text

Returns:

  • (Array<:text, :element, :attribute>)


96
97
98
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 96

def text_content?
  @text_content ||= @node.children.all? {|c| c.text?}
end

#to_sObject



175
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 175

def to_s; @node.to_s; end

#uriObject

URI of namespace + node_name



169
170
171
172
173
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 169

def uri
  ns = namespace || RDF::XML.to_s
  ns = ns.href if ns.respond_to?(:href)
  RDF::URI.intern(ns + self.node_name)
end

#xpath(*args) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/nokogiri.rb', line 134

def xpath(*args)
  @node.xpath(*args).map do |n|
    # Get node ancestors
    parent = n.ancestors.reverse.inject(nil) do |p,node|
      NodeProxy.new(node, p)
    end
    NodeProxy.new(n, parent)
  end
end