Class: RDF::RDFXML::Reader::REXML::NodeProxy

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.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.



28
29
30
31
32
33
34
35
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 28

def initialize(node, parent = nil)
  @node = node
  @parent = parent
  @parent = NodeProxy.new(node.parent) if @parent.nil? &&
                                          node.respond_to?(:parent) &&
                                          node.parent &&
                                          !node.parent.is_a?(::REXML::Document)
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



225
226
227
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 225

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

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



25
26
27
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 25

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



26
27
28
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 26

def parent
  @parent
end

Instance Method Details

#add_namespace(prefix, uri) ⇒ Object



154
155
156
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 154

def add_namespace(prefix, uri)
  prefix ? !@node.add_namespace(prefix, uri) : @node.add_namespace(uri)
end

#ancestorsObject

Ancestors of this element, in order



167
168
169
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 167

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

#at_xpath(*args) ⇒ Object



201
202
203
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 201

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

#attribute_nodesObject



183
184
185
186
187
188
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 183

def attribute_nodes
  attrs = @node.attributes.dup.keep_if do |name, attr|
    !name.start_with?('xmlns')
  end
  @attribute_nodes ||= (attrs.empty? ? attrs : NodeSetProxy.new(attrs, self))
end

#attribute_with_ns(name, namespace) ⇒ Object



101
102
103
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 101

def attribute_with_ns(name, namespace)
  @node.attribute(name, namespace)
end

#baseString

Return xml:base on element, if defined

Returns:



67
68
69
70
71
72
73
74
75
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 67

def base
  if @base.nil?
    @base = attributes['xml:base'] ||
    (parent && parent.element? && parent.base) ||
    false
  end

  @base == false ? nil : @base
end

#blank?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 217

def blank?
  @node.is_a?(::REXML::Text) && @node.empty?
end

#childrenNodeSetProxy

Children of this node

Returns:



162
163
164
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 162

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

#create_node(name, children) ⇒ Object

Create a new element child of an existing node



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 38

def create_node(name, children)
  native = ::REXML::Element.new(name, @node)
  children.each do |c|
    case c.node
    when ::REXML::Text
      native.add_text(c.node)
    when ::REXML::Element
      native.add_element(c.node)
    when ::REXML::Comment
      # Skip comments
    else
      raise "Unexpected child node type: #{c.node.class}"
    end
  end
  NodeProxy.new(native, self)
end

#directionString

Element direction

Returns:



81
82
83
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 81

def direction
  @node.attribute("dir", RDF::ITS.to_s)
end

#display_pathObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 105

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

#element?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 213

def element?
  @node.is_a?(::REXML::Element)
end

#inner_textString

Inner text of an element



176
177
178
179
180
181
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 176

def inner_text
  coder = HTMLEntities.new
  ::REXML::XPath.match(@node,'.//text()').map { |e|
    coder.decode(e)
  }.join
end

#its_versionString

ITS version

Returns:



89
90
91
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 89

def its_version
  @node.attribute("version", RDF::ITS.to_s)
end

#languageString

Element language

Returns:



59
60
61
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 59

def language
  @node.attribute("lang", RDF::XML.to_s)
end

#namespaceObject



146
147
148
149
150
151
152
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 146

def namespace
  if @node.namespace && !@node.namespace.empty?
    Namespace.new(@node.namespace, @node.prefix)
  else
    Namespace.new(RDF::XML.to_s, 'xml')
  end
end

#namespacesHash{String => String}

Retrieve XMLNS definitions for this element

Returns:



137
138
139
140
141
142
143
144
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 137

def namespaces
  ns_decls = {}
  @node.attributes.each do |name, attr|
    next unless name =~ /^xmlns(?:\:(.+))?/
    ns_decls[$1] = attr
  end
  ns_decls
end

#remove_attribute(key) ⇒ Object



190
191
192
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 190

def remove_attribute(key)
  @node.delete_attribute(key)
end

#text?Boolean

Node type accessors

Returns:

  • (Boolean)


209
210
211
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 209

def text?
  @node.is_a?(::REXML::Text)
end

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

Return true of all child elements are text

Returns:

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


129
130
131
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 129

def text_content?
  @node.children.all? {|c| c.is_a?(::REXML::Text)}
end

#to_sObject



221
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 221

def to_s; @node.to_s; end

#uriObject

URI of namespace + name



119
120
121
122
123
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 119

def uri
  ns = namespace
  ns = ns.href if ns.respond_to?(:href)
  RDF::URI.intern(ns + @node.name)
end

#versionString

RDF version

Returns:



97
98
99
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 97

def version
  @node.attribute("version", RDF.to_uri.to_s)
end

#xpath(*args) ⇒ Object



194
195
196
197
198
199
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rdf-rdfxml-14ee5432437b/lib/rdf/rdfxml/reader/rexml.rb', line 194

def xpath(*args)
  #NodeSetProxy.new(::REXML::XPath.match(@node, path, namespaces), self)
  ::REXML::XPath.match(@node, *args).map do |n|
    NodeProxy.new(n, parent)
  end
end