rexle 0.9.15 → 0.9.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/rexle.rb +42 -38
- metadata +2 -2
data/lib/rexle.rb
CHANGED
@@ -9,8 +9,8 @@ require 'polyrex-parser'
|
|
9
9
|
require 'cgi'
|
10
10
|
include REXML
|
11
11
|
|
12
|
-
|
13
12
|
# modifications:
|
13
|
+
# 14-Jan-2012: Implemented Rexle::Elements#each
|
14
14
|
# 21-Dec-2011: Bug fix: xpath modified to allow querying from the actual
|
15
15
|
# root rather than the 1st child element from the root
|
16
16
|
# 04-Sep-2011: Bug fix: xpath will now only return Rexle:elements when
|
@@ -52,10 +52,10 @@ module XMLhelper
|
|
52
52
|
out << x.value unless x.value.nil? || x.value.empty?
|
53
53
|
out << scan_print(x.children)
|
54
54
|
out << "</%s>" % x.name
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
"<![CDATA[%s]]>" % x.value
|
55
|
+
elsif x.name == '!-' then
|
56
|
+
"<!--%s-->" % x.value
|
57
|
+
else
|
58
|
+
"<![CDATA[%s]]>" % x.value
|
59
59
|
end
|
60
60
|
elsif x.is_a? String then
|
61
61
|
x
|
@@ -108,17 +108,21 @@ class Rexle
|
|
108
108
|
Array: proc {|x| x},
|
109
109
|
:"REXML::Document" => proc {|x| scan_doc x.root}
|
110
110
|
}
|
111
|
-
|
111
|
+
|
112
112
|
doc_node = ['doc','',{}]
|
113
|
+
|
113
114
|
@a = procs[x.class.to_s.to_sym].call(x)
|
114
115
|
@doc = scan_element(*(doc_node << @a))
|
115
|
-
|
116
|
+
|
116
117
|
# fetch the namespaces
|
117
118
|
@prefixes = []
|
118
119
|
if @doc.root.attributes then
|
120
|
+
|
119
121
|
xmlns = @doc.root.attributes.select {|k,v| k[/^xmlns:/]}
|
120
122
|
@prefixes = xmlns.keys.map{|x| x[/\w+$/]}
|
121
123
|
end
|
124
|
+
|
125
|
+
|
122
126
|
end
|
123
127
|
|
124
128
|
end
|
@@ -172,21 +176,20 @@ class Rexle
|
|
172
176
|
|
173
177
|
# Array: proc {|x| x.flatten.compact},
|
174
178
|
if (fn_match and fn_match.captures.first[/^(attribute|@)/]) or fn_match.nil? then
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
179
|
+
procs = {
|
180
|
+
Array: proc {|x| block_given? ? x : x.flatten },
|
181
|
+
String: proc {|x| x},
|
182
|
+
TrueClass: proc{|x| x},
|
183
|
+
FalseClass: proc{|x| x},
|
184
|
+
:"Rexle::Element" => proc {|x| [x]}
|
185
|
+
}
|
186
|
+
bucket = []
|
187
|
+
result = query_xpath(path, bucket, &blk)
|
188
|
+
procs[result.class.to_s.to_sym].call(result)
|
189
|
+
|
186
190
|
else
|
187
|
-
|
188
|
-
|
189
|
-
method(m.to_sym).call(xpath_value)
|
191
|
+
m, xpath_value = fn_match.captures
|
192
|
+
method(m.to_sym).call(xpath_value)
|
190
193
|
end
|
191
194
|
|
192
195
|
end
|
@@ -207,11 +210,11 @@ class Rexle
|
|
207
210
|
#return [(result.is_a?(Rexle::Element) ? true : false)]
|
208
211
|
end
|
209
212
|
|
213
|
+
#xpath_value.sub!(/^attribute::/,'*/attribute::')
|
210
214
|
raw_path, raw_condition = xpath_value.sub(/^\/(?!\/)/,'').match(/([^\[]+)(\[[^\]]+\])?/).captures
|
211
215
|
|
212
216
|
remaining_path = ($').to_s
|
213
217
|
a_path = raw_path.split('/')
|
214
|
-
|
215
218
|
condition = raw_condition if a_path.length <= 1
|
216
219
|
|
217
220
|
if raw_path[0,2] == '//' then
|
@@ -225,12 +228,10 @@ class Rexle
|
|
225
228
|
return [@attributes[attribute.to_sym]] if attribute and @attributes and @attributes.has_key?(attribute.to_sym)
|
226
229
|
|
227
230
|
s = a_path.shift
|
228
|
-
|
229
231
|
end
|
230
232
|
|
231
233
|
# isolate the xpath to return just the path to the current element
|
232
234
|
elmnt_path = s[/^([\w\*]+\[[^\]]+\])|[\/]+{,2}[^\/]+/]
|
233
|
-
|
234
235
|
element_part = elmnt_path[/(^@?[^\[]+)?/,1] if elmnt_path
|
235
236
|
|
236
237
|
if element_part then
|
@@ -243,6 +244,7 @@ class Rexle
|
|
243
244
|
|
244
245
|
end
|
245
246
|
|
247
|
+
#element_name ||= '*'
|
246
248
|
raw_condition = '' if condition
|
247
249
|
|
248
250
|
attr_search = format_condition(condition) if condition and condition.length > 0
|
@@ -254,17 +256,17 @@ class Rexle
|
|
254
256
|
return rs
|
255
257
|
else
|
256
258
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
259
|
+
return_elements = @child_lookup.map.with_index.select do |x|
|
260
|
+
(x[0][0] == element_name || element_name == '.') or \
|
261
|
+
(element_name == '*' && x[0].is_a?(Array))
|
262
|
+
end
|
263
|
+
|
261
264
|
end
|
262
265
|
|
263
266
|
if return_elements.length > 0 then
|
264
267
|
|
265
268
|
if (a_path + [remaining_path]).join.empty? then
|
266
269
|
rlist = return_elements.map.with_index {|x,i| filter(x, i+1, attr_search, &blk)}.compact
|
267
|
-
|
268
270
|
rlist = rlist[0] if rlist.length == 1
|
269
271
|
else
|
270
272
|
|
@@ -320,6 +322,7 @@ class Rexle
|
|
320
322
|
end
|
321
323
|
|
322
324
|
def inspect()
|
325
|
+
|
323
326
|
if self.xml.length > 30 then
|
324
327
|
"%s ... </>" % self.xml[/<[^>]+>/]
|
325
328
|
else
|
@@ -367,7 +370,7 @@ class Rexle
|
|
367
370
|
|
368
371
|
def elements(s=nil)
|
369
372
|
procs = {
|
370
|
-
NilClass: proc {Elements.new(@child_elements)},
|
373
|
+
NilClass: proc {Elements.new(@child_elements.select{|x| x.is_a? Rexle::Element })},
|
371
374
|
String: proc {|x| @child_elements[x]}
|
372
375
|
}
|
373
376
|
|
@@ -425,6 +428,7 @@ class Rexle
|
|
425
428
|
|
426
429
|
def format_condition(condition)
|
427
430
|
|
431
|
+
#raw_items = condition[1..-1].scan(/\'[^\']*\'|and|or|\d+|[!=]+|[@\w\.\/]+/)
|
428
432
|
raw_items = condition[1..-1].scan(/\'[^\']*\'|and|or|\d+|[!=<>]+|position\(\)|[@\w\.\/]+/)
|
429
433
|
|
430
434
|
if raw_items[0][/^\d+$/] then
|
@@ -448,7 +452,7 @@ class Rexle
|
|
448
452
|
"h[:'%s'] %s %s" % x
|
449
453
|
else
|
450
454
|
|
451
|
-
|
455
|
+
x.join[/^(and|or)$/] ? x : ("h[:'%s']" % x)
|
452
456
|
end
|
453
457
|
end
|
454
458
|
|
@@ -489,19 +493,17 @@ class Rexle
|
|
489
493
|
|
490
494
|
r = node.xpath(xpath[2..-1])
|
491
495
|
r << node.children.map {|n| scan_match(n, xpath) if n.is_a? Rexle::Element}
|
492
|
-
|
493
496
|
r
|
494
497
|
end
|
495
498
|
|
496
499
|
def filter(raw_element, i, attr_search, &blk)
|
497
500
|
|
498
501
|
x = raw_element
|
499
|
-
|
500
502
|
e = @child_elements[x.last]
|
501
|
-
name, value = e.name, e.value if e.is_a? Rexle::Element
|
502
|
-
h = x[0][1] # <-- fetch the attributes
|
503
|
-
|
503
|
+
name, value = e.name, e.value if e.is_a? Rexle::Element
|
504
504
|
|
505
|
+
h = x[0][1] # <-- fetch the attributes
|
506
|
+
|
505
507
|
if attr_search then
|
506
508
|
if attr_search.is_a? Fixnum then
|
507
509
|
block_given? ? blk.call(e) : e if i == attr_search
|
@@ -510,14 +512,12 @@ class Rexle
|
|
510
512
|
elsif h and attr_search[/^h\[/] and eval(attr_search)
|
511
513
|
block_given? ? blk.call(e) : e
|
512
514
|
elsif attr_search[/^\(name ==/] and e.child_lookup.select{|name, attributes, value| eval(attr_search) }.length > 0
|
513
|
-
|
514
515
|
block_given? ? blk.call(e) : e
|
515
516
|
elsif attr_search[/^\(name ==/] and eval(attr_search)
|
516
517
|
block_given? ? blk.call(e) : e
|
517
518
|
elsif attr_search[/^e\.value/] and eval(attr_search)
|
518
519
|
block_given? ? blk.call(e) : e
|
519
520
|
elsif attr_search[/^e\.xpath/] and eval(attr_search)
|
520
|
-
|
521
521
|
block_given? ? blk.call(e) : e
|
522
522
|
end
|
523
523
|
else
|
@@ -538,6 +538,10 @@ class Rexle
|
|
538
538
|
def [](i)
|
539
539
|
@elements[i-1]
|
540
540
|
end
|
541
|
+
|
542
|
+
def each(&blk) @elements.each(&blk) end
|
543
|
+
def to_a() @elements end
|
544
|
+
|
541
545
|
end # -- end of elements --
|
542
546
|
|
543
547
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rexle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.9.
|
5
|
+
version: 0.9.16
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Robertson
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2012-01-14 00:00:00 +00:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|