Class: Rexle

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

Defined Under Namespace

Classes: Element, Elements

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XMLhelper

#doc_pretty_print, #doc_print, #pretty_print, #scan_print

Constructor Details

#initialize(x = nil) ⇒ Rexle

Returns a new instance of Rexle.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rexle.rb', line 95

def initialize(x=nil)
  super()

  # what type of input is it? Is it a string, array, or REXML doc?
  if x then
    procs = {
      String: proc {|x| parse_string(x)},
      Array: proc {|x| x},
      :"REXML::Document" =>  proc {|x| scan_doc x.root}
    }

    @a = procs[x.class.to_s.to_sym].call(x)
    @doc = scan_element(*@a)      
    
    # fetch the namespaces
    @prefixes = []
    if @doc.root.attributes then
      #puts 'attrs : '  + @doc.root.attributes.inspect
      xmlns = @doc.root.attributes.select {|k,v| k[/^xmlns:/]}
      @prefixes = xmlns.keys.map{|x| x[/\w+$/]}
    end
  end
  
end

Instance Attribute Details

#prefixesObject (readonly)

Returns the value of attribute prefixes.



93
94
95
# File 'lib/rexle.rb', line 93

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



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

def add_attribute(x) @doc.attribute(x) end

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



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

def add_element(element) @doc.root.add_element(element) end

#add_text(s) ⇒ Object



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

def add_text(s) end

#attribute(key) ⇒ Object



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

def attribute(key) @doc.attribute(key) end

#attributesObject



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

def attributes() @doc.attributes end

#delete(xpath) ⇒ Object



527
528
529
530
# File 'lib/rexle.rb', line 527

def delete(xpath)
  e = @doc.element(xpath)
  e.delete if e
end

#element(xpath) ⇒ Object



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

def element(xpath) self.xpath(xpath).first end

#elements(s = nil) ⇒ Object



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

def elements(s=nil) @doc.elements(s) end

#nameObject



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

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/rexle.rb', line 500

def parse(x=nil)
  
  a = []
  
  if x then
    procs = {
      String: proc {|x| parse_string(x)},
      Array: proc {|x| x},
      :"REXML::Document" =>  proc {|x| scan_doc x.root}
    }
    a = procs[x.class.to_s.to_sym].call(x)
  else    
    a = yield
  end

  @doc = scan_element(*a)
  self
end

#rootObject



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

def root() @doc end

#text(xpath) ⇒ Object



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

def text(xpath) @doc.text(xpath) end

#to_aObject



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

def to_a() @a end

#to_s(options = {}) ⇒ Object



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

def to_s(options={}) self.xml options end

#write(f) ⇒ Object



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

def write(f) 
  f.write xml 
end

#xml(options = {}) ⇒ Object



544
545
546
547
548
549
550
551
# File 'lib/rexle.rb', line 544

def xml(options={})
  o = {pretty: false, declaration: true}.merge(options)
  msg = o[:pretty] == false ? :doc_print : :doc_pretty_print
  r = ''
  r = "<?xml version='1.0' encoding='UTF-8'?>\n" if o[:declaration] == true
  r << method(msg).call(self.root.children)
  r
end

#xpath(path, &blk) ⇒ Object



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

def xpath(path, &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 = {
      Array: proc {|x| block_given? ? x : x.flatten }, 
      String: proc {|x| x},
      :"Rexle::Element" => proc {|x| [x]}
    }
    bucket = []
    result = @doc.xpath(path, bucket, &blk)

    procs[result.class.to_s.to_sym].call(result)
    
  else
    #puts 'fn ' + fn_match.captures.inspect
    m, xpath_value = fn_match.captures
    method(m.to_sym).call(xpath_value)
  end


end