-
Notifications
You must be signed in to change notification settings - Fork 7
/
rexml.rb
283 lines (244 loc) · 7.25 KB
/
rexml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require 'htmlentities'
module RDF::RDFXML
class Reader < RDF::Reader
##
# REXML implementation of an XML parser.
#
# @see http://www.germane-software.com/software/rexml/
module REXML
##
# Returns the name of the underlying XML library.
#
# @return [Symbol]
def self.library
:rexml
end
# For Attribute namespaces
Namespace = Struct.new(:href, :prefix) do
def to_s; href; end
end
# Proxy class to implement uniform element accessors
class NodeProxy
attr_reader :node
attr_reader :parent
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
# Create a new element child of an existing node
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
##
# Element language
#
# @return [String]
def language
@node.attribute("lang", RDF::XML.to_s)
end
##
# Return xml:base on element, if defined
#
# @return [String]
def base
if @base.nil?
@base = attributes['xml:base'] ||
(parent && parent.element? && parent.base) ||
false
end
@base == false ? nil : @base
end
def attribute_with_ns(name, namespace)
@node.attribute(name, namespace)
end
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
# URI of namespace + name
def uri
ns = namespace || RDF::XML.to_s
ns = ns.href if ns.respond_to?(:href)
RDF::URI.intern(ns + @node.name)
end
##
# Return true of all child elements are text
#
# @return [Array<:text, :element, :attribute>]
def text_content?
@node.children.all? {|c| c.is_a?(::REXML::Text)}
end
##
# Retrieve XMLNS definitions for this element
#
# @return [Hash{String => String}]
def namespaces
ns_decls = {}
@node.attributes.each do |name, attr|
next unless name =~ /^xmlns(?:\:(.+))?/
ns_decls[$1] = attr
end
ns_decls
end
def namespace
Namespace.new(@node.namespace, @node.prefix) unless @node.namespace.to_s.empty?
end
def add_namespace(prefix, uri)
prefix ? !@node.add_namespace(prefix, uri) : @node.add_namespace(uri)
end
##
# Children of this node
#
# @return [NodeSetProxy]
def children
NodeSetProxy.new(@node.children, self)
end
# Ancestors of this element, in order
def ancestors
@ancestors ||= parent ? parent.ancestors + [parent] : []
end
##
# Inner text of an element
#
# @see http://apidock.com/ruby/REXML/Element/get_text#743-Get-all-inner-texts
# @return [String]
def inner_text
coder = HTMLEntities.new
::REXML::XPath.match(@node,'.//text()').map { |e|
coder.decode(e)
}.join
end
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
def remove_attribute(key)
@node.delete_attribute(key)
end
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
def at_xpath(*args)
xpath(*args).first
end
##
# Node type accessors
#
# @return [Boolean]
def text?
@node.is_a?(::REXML::Text)
end
def element?
@node.is_a?(::REXML::Element)
end
def blank?
@node.is_a?(::REXML::Text) && @node.empty?
end
def to_s; @node.to_s; end
##
# Proxy for everything else to @node
def method_missing(method, *args)
@node.send(method, *args)
end
end
##
# NodeSet proxy
class NodeSetProxy
attr_reader :node_set
attr_reader :parent
def initialize(node_set, parent)
@node_set = node_set
@parent = parent
end
##
# Return a proxy for each child
#
# @yield child
# @yieldparam [NodeProxy]
def each
@node_set.to_a.each do |c|
yield NodeProxy.new(c, parent)
end
end
##
# Return selected NodeProxies based on selection
#
# @yield child
# @yieldparam [NodeProxy]
# @return [Array[NodeProxy]]
def select
@node_set.to_a.map {|n| NodeProxy.new(n, parent)}.select do |c|
yield c
end
end
##
# Proxy for everything else to @node_set
def method_missing(method, *args)
@node_set.send(method, *args)
end
end
##
# Initializes the underlying XML library.
#
# @param [Hash{Symbol => Object}] options
# @return [void]
def initialize_xml(input, **options)
require 'rexml/document' unless defined?(::REXML)
@doc = case input
when ::REXML::Document
input
else
# Try to detect charset from input
options[:encoding] ||= input.charset if input.respond_to?(:charset)
# Otherwise, default is utf-8
options[:encoding] ||= 'utf-8'
# Set xml:base for the document element, if defined
@base_uri = base_uri ? base_uri.to_s : nil
# Only parse as XML, no HTML mode
::REXML::Document.new(input.respond_to?(:read) ? input.read : input.to_s)
end
end
# Accessor methods to mask native elements & attributes
##
# Return proxy for document root
def root
@root ||= NodeProxy.new(@doc.root) if @doc && @doc.root
end
##
# Document errors
def doc_errors
[]
end
end
end
end