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

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/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.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/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



197
198
199
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 197

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.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/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.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 26

def parent
  @parent
end

Instance Method Details

#add_namespace(prefix, uri) ⇒ Object



126
127
128
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 126

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

#ancestorsObject

Ancestors of this element, in order



139
140
141
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 139

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

#at_xpath(*args) ⇒ Object



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

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

#attribute_nodesObject



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

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



77
78
79
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 77

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.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/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)


189
190
191
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 189

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

#childrenNodeSetProxy

Children of this node

Returns:



134
135
136
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 134

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.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/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

#display_pathObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 81

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)


185
186
187
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 185

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

#inner_textString

Inner text of an element



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

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

#languageString

Element language

Returns:



59
60
61
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 59

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

#namespaceObject



122
123
124
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 122

def namespace
  Namespace.new(@node.namespace, @node.prefix) unless @node.namespace.to_s.empty?
end

#namespacesHash{String => String}

Retrieve XMLNS definitions for this element

Returns:



113
114
115
116
117
118
119
120
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 113

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



162
163
164
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 162

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

#text?Boolean

Node type accessors

Returns:

  • (Boolean)


181
182
183
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 181

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>)


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

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

#to_sObject



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

def to_s; @node.to_s; end

#uriObject

URI of namespace + name



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

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

#xpath(*args) ⇒ Object



166
167
168
169
170
171
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfxml-6b89d4c13d63/lib/rdf/rdfxml/reader/rexml.rb', line 166

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