Class: RDF::RDFXML::Reader::REXML::NodeProxy
- 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
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_namespace(prefix, uri) ⇒ Object
-
#ancestors ⇒ Object
Ancestors of this element, in order.
- #at_xpath(*args) ⇒ Object
- #attribute_nodes ⇒ Object
- #attribute_with_ns(name, namespace) ⇒ Object
-
#base ⇒ String
Return xml:base on element, if defined.
- #blank? ⇒ Boolean
-
#children ⇒ NodeSetProxy
Children of this node.
-
#create_node(name, children) ⇒ Object
Create a new element child of an existing node.
- #display_path ⇒ Object
- #element? ⇒ Boolean
-
#initialize(node, parent = nil) ⇒ NodeProxy
constructor
A new instance of NodeProxy.
-
#inner_text ⇒ String
Inner text of an element.
-
#language ⇒ String
Element language.
-
#method_missing(method, *args) ⇒ Object
Proxy for everything else to @node.
- #namespace ⇒ Object
-
#namespaces ⇒ Hash{String => String}
Retrieve XMLNS definitions for this element.
- #remove_attribute(key) ⇒ Object
-
#text? ⇒ Boolean
Node type accessors.
-
#text_content? ⇒ Array<:text, :element, :attribute>
Return true of all child elements are text.
- #to_s ⇒ Object
-
#uri ⇒ Object
URI of namespace + name.
- #xpath(*args) ⇒ Object
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
#node ⇒ Object (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 |
#parent ⇒ Object (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 |
#ancestors ⇒ Object
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_nodes ⇒ Object
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 |
#base ⇒ String
Return xml:base on element, if defined
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
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 |
#children ⇒ NodeSetProxy
Children of this node
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_path ⇒ Object
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
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_text ⇒ String
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 |
#language ⇒ String
Element language
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 |
#namespace ⇒ Object
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 |
#namespaces ⇒ Hash{String => String}
Retrieve XMLNS definitions for this element
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
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
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_s ⇒ Object
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 |
#uri ⇒ Object
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 |