Class: Rexle::Element

Inherits:
Object
  • Object
show all
Includes:
XMLhelper
Defined in:
lib/rexle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XMLhelper

#doc_pretty_print, #doc_print, #pretty_print, #scan_print, #scan_to_a

Constructor Details

#initialize(name = nil, value = '', attributes = {}, rexle = nil) ⇒ Element

Returns a new instance of Element.



200
201
202
203
204
205
206
207
# File 'lib/rexle.rb', line 200

def initialize(name=nil, value='', attributes={}, rexle=nil)
  @rexle = rexle      
  super()
  @name, @value, @attributes = name.to_s, value, attributes
  raise "Element name must not be blank" unless name
  @child_elements = []
  @child_lookup = []
end

Instance Attribute Details

#child_elementsObject (readonly)

Returns the value of attribute child_elements.



196
197
198
# File 'lib/rexle.rb', line 196

def child_elements
  @child_elements
end

#child_lookupObject (readonly)

Returns the value of attribute child_lookup.



196
197
198
# File 'lib/rexle.rb', line 196

def child_lookup
  @child_lookup
end

#nameObject

Returns the value of attribute name.



195
196
197
# File 'lib/rexle.rb', line 195

def name
  @name
end

#parentObject

Returns the value of attribute parent.



195
196
197
# File 'lib/rexle.rb', line 195

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



195
196
197
# File 'lib/rexle.rb', line 195

def value
  @value
end

Instance Method Details

#add_attribute(*x) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/rexle.rb', line 463

def add_attribute(*x)

  procs = {
    Hash: lambda {|x| x[0] || {}},
    String: lambda {|x| Hash[*x]},
    Symbol: lambda {|x| Hash[*x]}
  }

  h = procs[x[0].class.to_s.to_sym].call(x)

  @attributes.merge! h
end

#add_element(item) ⇒ Object Also known as: add



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/rexle.rb', line 437

def add_element(item)
  if item.is_a? Rexle::Element then

    @child_lookup << [item.name, item.attributes, item.value]
    @child_elements << item
    # add a reference from this element (the parent) to the child
    item.parent = self
    item        
  elsif item.is_a? String then
    @child_lookup << item
    @child_elements << item             
  elsif item.is_a? Rexle then
    self.add_element(item.root)
  end
end

#add_text(s) ⇒ Object



476
477
478
479
480
481
482
483
# File 'lib/rexle.rb', line 476

def add_text(s)
  if @child_elements.length < 1 then
    @value = s; 
  else
    self.add s
  end
  self 
end

#attribute(key) ⇒ Object



485
486
487
488
# File 'lib/rexle.rb', line 485

def attribute(key) 
  key = key.to_sym if key.is_a? String
  @attributes[key].gsub('&lt;','<').gsub('&gt;','>')
end

#attributesObject



490
# File 'lib/rexle.rb', line 490

def attributes() @attributes end

#childrenObject



492
493
494
495
496
497
498
499
500
# File 'lib/rexle.rb', line 492

def children()
  return unless @value
  r = (@value.empty? ? [] : [@value])  + @child_elements
  def r.is_an_empty_string?()
    self.length == 1 and self.first == ''
  end      
  
  return r
end

#children=(a) ⇒ Object



502
# File 'lib/rexle.rb', line 502

def children=(a)   @child_elements = a   end

#cloneObject



505
# File 'lib/rexle.rb', line 505

def clone() Element.new(@name, @value, @attributes) end

#contains(raw_args) ⇒ Object



209
210
211
212
213
214
215
216
217
218
# File 'lib/rexle.rb', line 209

def contains(raw_args)
  path, raw_val = raw_args.split(',',2)
  val = raw_val.strip[/^["']?.*["']?$/]      
  
  anode = query_xpath(path)
  return unless anode
  a = scan_contents(anode.first)
 
  [a.grep(/#{val}/).length > 0]
end

#count(path) ⇒ Object



220
221
222
223
# File 'lib/rexle.rb', line 220

def count(path)
  length = query_xpath(path).flatten.compact.length
  length
end

#deep_cloneObject



504
# File 'lib/rexle.rb', line 504

def deep_clone() Rexle.new(self.xml).root end

#delete(obj = nil) ⇒ Object Also known as: remove



507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/rexle.rb', line 507

def delete(obj=nil)
  if obj then
    if obj.is_a? String then
      e = self.element obj
      e.delete if e
    else
      i = @child_elements.index(obj)
      [@child_elements, @child_lookup].each{|x| x.delete_at i} if i
    end
  else
    self.parent.delete self
  end
end

#doc_rootObject



537
# File 'lib/rexle.rb', line 537

def doc_root() @rexle.root end

#each(&blk) ⇒ Object



538
539
540
# File 'lib/rexle.rb', line 538

def each(&blk) 
  @child_elements.each(&blk) #unless @child_elements.empty?
end

#element(s) ⇒ Object



523
524
525
526
# File 'lib/rexle.rb', line 523

def element(s) 
  r = self.xpath(s)
  r.is_a?(Array) ? r.first : r
end

#elements(s = nil) ⇒ Object



528
529
530
531
532
533
534
535
# File 'lib/rexle.rb', line 528

def elements(s=nil)
  procs = {
    NilClass: proc {Elements.new(@child_elements.select{|x| x.is_a? Rexle::Element })},
    String: proc {|x| @child_elements[x]}
  }

  procs[s.class.to_s.to_sym].call(s)      
end

#filter_xpath(path, rlist = [], &blk) ⇒ Object



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
# File 'lib/rexle.rb', line 244

def filter_xpath(path, rlist=[], &blk)

  # is it a function
  fn_match = path.match(/^(\w+)\(["']?([^\)]*)["']?\)$/)

  #    Array: proc {|x| x.flatten.compact}, 
  if (fn_match and fn_match.captures.first[/^(attribute|@)/]) or fn_match.nil? then 
    procs = {
      #jr061012 Array: proc {|x| block_given? ? x : x.flatten.uniq },
      Array: proc { |x| 
        if block_given? then 
          x.flatten(1) 
        else
          rs = x.flatten
          rs.any?{|x| x == true or x == false} ? rs : rs.uniq(&:object_id) 
        end
      }, 
      String: proc {|x| x},
      Hash: proc {|x| x},
      TrueClass: proc{|x| x},
      FalseClass: proc{|x| x},
      :"Rexle::Element" => proc {|x| [x]}
    }
    bucket = []
    raw_results = path.split('|').map do |xp|
      query_xpath(xp, bucket, &blk)         
    end

    results = raw_results

    procs[results.class.to_s.to_sym].call(results) if results
    
  else
    m, xpath_value = fn_match.captures        
    xpath_value.empty? ? method(m.to_sym).call : method(m.to_sym).call(xpath_value) 
  end

end

#has_elements?Boolean

Returns:

  • (Boolean)


541
# File 'lib/rexle.rb', line 541

def has_elements?() !self.elements.empty? end

#insert_after(node) ⇒ Object



543
# File 'lib/rexle.rb', line 543

def insert_after(node)   insert(node, 1)   end

#insert_before(node) ⇒ Object



544
# File 'lib/rexle.rb', line 544

def insert_before(node)  insert(node)      end

#inspectObject



453
454
455
456
457
458
459
# File 'lib/rexle.rb', line 453

def inspect()
  if self.xml.length > 30 then
  "%s ... </>" % self.xml[/<[^>]+>/]
  else
    self.xml
  end  
end

#max(path) ⇒ Object



225
226
227
228
# File 'lib/rexle.rb', line 225

def max(path) 
  a = query_xpath(path).flatten.select{|x| x.is_a? String}.map(&:to_i)
  a.max 
end

#original_cloneObject



198
# File 'lib/rexle.rb', line 198

alias original_clone clone

#query_xpath(raw_xpath_value, rlist = [], &blk) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/rexle.rb', line 283

def query_xpath(raw_xpath_value, rlist=[], &blk)

  #remove any pre'fixes
 #@rexle.prefixes.each {|x| xpath_value.sub!(x + ':','') }
  flag_func = false            

  xpath_value = raw_xpath_value.sub('child::','./')
  #xpath_value.sub!(/\.\/(?=[\/])/,'')

  if xpath_value[/^[\w\/]+\s*=.*/] then        
    flag_func = true

    xpath_value.sub!(/^\w+\s*=.*/,'.[\0]')
    xpath_value.sub!(/\/([\w]+\s*=.*)/,'[\1]')

    #result = self.element xpath_value        
    #return [(result.is_a?(Rexle::Element) ? true : false)]
  end

  #xpath_value.sub!(/^attribute::/,'*/attribute::')
  raw_path, raw_condition = xpath_value.sub(/^\.?\/(?!\/)/,'')\
      .match(/([^\[]+)(\[[^\]]+\])?/).captures 

  remaining_path = ($').to_s
  
  r = raw_path[/([^\/]+)(?=\/\/)/,1] 
  if r then
    a_path = raw_path.split(/(?=\/\/)/,2)
  else
    a_path = raw_path.split('/',2)
  end
  
  condition = raw_condition if a_path.length <= 1

  if raw_path[0,2] == '//' then
    s = ''
  elsif raw_path == 'text()'        
    a_path.shift
    return @value
  else

    attribute = xpath_value[/^(attribute::|@)(.*)/,2] 
  
    return @attributes  if attribute == '*'
    return [@attributes[attribute.to_sym]] if attribute and @attributes and @attributes.has_key?(attribute.to_sym)
    s = a_path.shift
  end      

  # isolate the xpath to return just the path to the current element

  elmnt_path = s[/^([\w:\*]+\[[^\]]+\])|[\/]+{,2}[^\/]+/]
  element_part = elmnt_path[/(^@?[^\[]+)?/,1] if elmnt_path

  if element_part then

    unless element_part[/^(@|[@\.\w]+[\s=])/] then
      element_name = element_part[/^[\w:\*\.]+/]

    else
      if xpath_value[/^\[/] then
        condition = xpath_value
        element_name = nil
      else
        condition = element_part
        attr_search = format_condition('[' + condition + ']')
        return [attribute_search(attr_search, self, self.attributes) != nil]            
      end

    end

  end

  #element_name ||= '*'
  raw_condition = '' if condition

  attr_search = format_condition(condition) if condition and condition.length > 0      
  attr_search2 = xpath_value[/^\[(.*)\]$/,1]

  if attr_search2 then
    r4 = attribute_search(attr_search, self, self.attributes)
    return r4
  end
  
  
  return_elements = []

  if raw_path[0,2] == '//' then

    regex = /\[(\d+)\]/
    n = xpath_value[regex,1]
    xpath_value.slice!(regex)
    
    rs = scan_match(self, xpath_value).flatten.compact
    return n ? rs[n.to_i-1] : rs

  #jr101013 elsif (raw_path == '.' or raw_path == self.name) and attr_search.nil? then
  #jr101013  return  [self]
  else

    return_elements = @child_lookup.map.with_index.select do |x|    
      (x[0][0] == element_name || element_name == '.') or \
        (element_name == '*' && x[0].is_a?(Array))
    end

  end

  if return_elements.length > 0 then

    if (a_path + [remaining_path]).join.empty? then

      rlist = return_elements.map.with_index {|x,i| filter(x, i+1, attr_search, &blk)}.compact
      rlist = rlist[0] if rlist.length == 1

    else

      rlist << return_elements.map.with_index do |x,i| 

        rtn_element = filter(x, i+1, attr_search){|e| r = e.xpath(a_path.join('/') + raw_condition.to_s + remaining_path, &blk); (r || e) }
        next if rtn_element.nil? or (rtn_element.is_a? Array and rtn_element.empty?)

        if rtn_element.is_a? Hash then
          rtn_element
        elsif rtn_element.is_a? Array then
          rtn_element
        elsif (rtn_element.is_a? String) || (rtn_element.is_a?(Array) and not(rtn_element[0].is_a? String))
          rtn_element
        elsif rtn_element.is_a? Rexle::Element
          rtn_element
        end
      end
      #

      rlist = rlist.flatten(1) unless rlist.length > 1 and rlist[0].is_a? Array

    end

    rlist.compact! if rlist.is_a? Array

  else

    # strip off the 1st element from the XPath
    new_xpath = xpath_value[/^\/\/[\w:]+\/(.*)/,1]

    if new_xpath then
      self.xpath(new_xpath + raw_condition.to_s + remaining_path, rlist,&blk)
    end
  end

  rlist = rlist.flatten(1) unless not(rlist.is_a? Array) or (rlist.length > 1 and rlist[0].is_a? Array)
  rlist = [rlist] if rlist.is_a? Rexle::Element
  rlist = (rlist.length > 0 ? true : false) if flag_func == true
  rlist
end

#rootObject



546
# File 'lib/rexle.rb', line 546

def root() self end

#text(s = '') ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/rexle.rb', line 548

def text(s='')

  if s.empty? then
    result = @value
  else
    e = self.element(s)
    result = e.value if e
  end
  result = CGI.unescape_html result.to_s
 
  def result.unescape()
    s = self.clone
    %w(&lt; < &gt; > &amp; & &pos; ').each_slice(2){|x| s.gsub!(*x)}
    s
  end

  result
end

#textsObject



567
568
569
# File 'lib/rexle.rb', line 567

def texts()
  [@value] + @child_elements.select {|x| x.is_a? String}
end

#to_aObject



587
588
589
# File 'lib/rexle.rb', line 587

def to_a()
  [self.name, self.value, self.attributes, *scan_to_a(self.children)]
end

#xml(options = {}) ⇒ Object Also known as: to_s



591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/rexle.rb', line 591

def xml(options={})
  h = {
    Hash: lambda {|x|
      o = {pretty: false}.merge(x)
      msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
      method(msg).call(self.children)
    },
    String: lambda {|x| 
      r = self.element(x)
      r ? r.xml : ''
    }
  }
  h[options.class.to_s.to_sym].call options
end

#xpath(path, rlist = [], &blk) ⇒ Object



239
240
241
242
# File 'lib/rexle.rb', line 239

def xpath(path, rlist=[], &blk)
  r = filter_xpath(path, rlist=[], &blk)
  r.is_a?(Array) ? r.compact : r      
end