Class: Rexle::Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Element.



55
56
57
58
59
60
# File 'lib/rexle.rb', line 55

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

Instance Attribute Details

#child_lookupObject (readonly)

Returns the value of attribute child_lookup.



53
54
55
# File 'lib/rexle.rb', line 53

def child_lookup
  @child_lookup
end

#nameObject

Returns the value of attribute name.



52
53
54
# File 'lib/rexle.rb', line 52

def name
  @name
end

#parentObject

Returns the value of attribute parent.



52
53
54
# File 'lib/rexle.rb', line 52

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



52
53
54
# File 'lib/rexle.rb', line 52

def value
  @value
end

Instance Method Details

#add_attribute(h = {}) ⇒ Object



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

def add_attribute(h={}) @attributes.merge! h end

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



143
144
145
146
147
148
149
# File 'lib/rexle.rb', line 143

def add_element(item)
  @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
end

#add_text(s) ⇒ Object



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

def add_text(s) @value = s; self end

#attributesObject



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

def attributes() @attributes end

#childrenObject



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

def children() @child_elements end

#children=(a) ⇒ Object



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

def children=(a) @child_elements = a end

#delete(obj = nil) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/rexle.rb', line 159

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

#element(s) ⇒ Object



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

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

#rootObject



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

def root() self end

#text(s = '') ⇒ Object



170
171
172
# File 'lib/rexle.rb', line 170

def text(s='')
  s.empty? ? @value : self.xpath(s).first.value
end

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



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

def xpath(xpath_value, rlist=[], &blk)

  a_path = xpath_value.split('/')

  if xpath_value[0,2] == '//' then
    s = a_path[2]
  elsif xpath_value == 'text()' then      
    a_path.shift
    return @value
  else
    attribute = xpath_value[/^attribute::(.*)/,1] 
    return @attributes[attribute] if attribute and @attributes and @attributes.has_key?(attribute)
 
    s = a_path.shift
  end
  
  elmnt_path = s[/^([\w\*]+\[[^\]]+\])|[\/]+{,2}[^\/]+/]

  element_part, condition = elmnt_path.match(/(^@?[^\[]+)?(\[[^\]]+\])?/).captures

  if element_part then
    unless element_part[/^@/] then
      element_name = element_part
    else
      condition = element_part
      element_name = nil
    end
  end

  attr_search = format_attributes(condition) if condition

  if xpath_value[0,2] == '//'
    return_elements = scan_match(self, element_name, attr_search, condition, rlist) 
    return (xpath_value[/text\(\)$/] ? return_elements.map(&:value) : return_elements)
  end

  return_elements = @child_lookup.map.with_index.select do |x|
    x[0][0] == element_name or element_name == '*'
  end
    
  if return_elements.length > 0 then

    if a_path.empty? then
      rlist = return_elements.map.with_index {|x,i| filter(x, i+1, attr_search, &blk)}
      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('/'), &blk); (r || e) }
        next if rtn_element.nil? or (rtn_element.is_a? Array and rtn_element.empty?)

        if 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, rlist,&blk)
    end
  end

  #a.shift # added by jr 171110
  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
end