Class: AXML::El

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/axml.rb

Constant Summary collapse

Indent =

use AXML::El::Indent.replace to swap without warning

“”, “ ”, “ ”, “ ”, “ ”, “ ”, …
'  '
Indentation =

use AXML::El::Indentation.replace to replace w/o warning

(0...30).to_a.map {|num| Indent*num }
EscapeCharsRe =
/['"&><]/
@@depth =

current depth

0

Instance Method Summary collapse

Instance Method Details

#[](attribute_string) ⇒ Object



59
60
61
# File 'lib/axml.rb', line 59

def [](attribute_string)
  attrs[attribute_string]
end

#[]=(attribute_string, value) ⇒ Object



63
64
65
# File 'lib/axml.rb', line 63

def []=(attribute_string, value)
  attrs[attribute_string] = value
end

#add_node(node) ⇒ Object



175
176
177
178
# File 'lib/axml.rb', line 175

def add_node(node)
  node.array_index = children.size
  children.push( node )
end

#childObject

the first child (equivalent to children.first)



171
172
173
# File 'lib/axml.rb', line 171

def child
  children.first
end

#children?Boolean Also known as: child?

Returns:

  • (Boolean)


72
73
74
# File 'lib/axml.rb', line 72

def children?
  children.size > 0
end

#dropObject

drops the current element from the list of its parents children



97
98
99
# File 'lib/axml.rb', line 97

def drop
  parent.children.delete(self)
end

#drop_child(node) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/axml.rb', line 101

def drop_child(node)
  found_it = false
  found_index = nil
  children.each_with_index do |v,i| 
    if found_it
      v.array_index = i - 1
    end
    if v.object_id == node.object_id 
      found_index = i
      found_it = true
    end
  end
  children.delete_at(found_index) if found_index
end

#each(&block) ⇒ Object



90
91
92
93
94
# File 'lib/axml.rb', line 90

def each(&block)
  children.each do |child|
    block.call(child)
  end
end

#escape(data) ⇒ Object

returns data escaped if necessary



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/axml.rb', line 123

def escape(data)
  # modified slightly from xmlsimple.rb
  return data if !data.is_a?(String) || data.nil? || data == ''
  result = data.dup
  if EscapeCharsRe.match(data)
    result.gsub!('&', '&amp;')
    result.gsub!('<', '&lt;')
    result.gsub!('>', '&gt;')
    result.gsub!('"', '&quot;')
    result.gsub!("'", '&apos;')
  end
  result
end

#find(string) ⇒ Object

Returns an array of nodes. Accepts same xpath strings as find_first.



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/axml.rb', line 185

def find(string)
  (tp, name) = string.split('::')
  case tp
  when 'child'
    find_children(name)
  when 'descendant'
    find_descendants(name)
  when 'following-sibling'
    find_following_siblings(name)
  end
end

#find_children(name) ⇒ Object



231
232
233
# File 'lib/axml.rb', line 231

def find_children(name)
  children.select {|v| v.name == name }
end

#find_descendants(name, collect_descendants = []) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/axml.rb', line 212

def find_descendants(name, collect_descendants=[])
  children.each do |child|
    collect_descendants.push(child) if child.name == name
    child.find_descendants(name, collect_descendants)
  end
  collect_descendants
end

#find_first(string) ⇒ Object

currently must be called with descendant
or child

string prefix! e.g.

“descendant::<name>” and “child::<name>” where <name> is the name of the node you seek)



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/axml.rb', line 200

def find_first(string)
  (tp, name) = string.split('::')
  case tp
  when 'child'
    find_first_child(name)
  when 'descendant'
    find_first_descendant(name)
  when 'following-sibling'
    find_first_following_sibling(name)
  end
end

#find_first_child(name) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/axml.rb', line 235

def find_first_child(name)
  self.each do |child_node| 
    if child_node.name == name
      return child_node
    end
  end
  return nil
end

#find_first_descendant(name) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/axml.rb', line 220

def find_first_descendant(name)
  self.each do |child_node| 
    if child_node.name == name
      return child_node
    else
      return child_node.find_first_descendant(name) 
    end
  end
  return nil
end

#find_first_following_sibling(name) ⇒ Object



248
249
250
251
252
253
254
255
256
257
# File 'lib/axml.rb', line 248

def find_first_following_sibling(name)
  node = nil
  parent.children[(array_index+1)..-1].each do |sibling| 
    if sibling.name == name 
      node = sibling
      break
    end
  end
  node
end

#find_following_siblings(name) ⇒ Object



244
245
246
# File 'lib/axml.rb', line 244

def find_following_siblings(name)
  parent.children[(array_index+1)..-1].select {|v| v.name == name }
end

#inspectObject



161
162
163
# File 'lib/axml.rb', line 161

def inspect
  "<name='#{name}' attrs='#{attrs.inspect}' children.size=#{children.size}>"
end

#nextObject

the next node



166
167
168
# File 'lib/axml.rb', line 166

def next
  parent.children[array_index+1]
end

#tabsObject



116
117
118
# File 'lib/axml.rb', line 116

def tabs
  Indentation[@@depth]
end

#text?Boolean

has text?

Returns:

  • (Boolean)


68
69
70
# File 'lib/axml.rb', line 68

def text?
  !!text
end

#to_sObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/axml.rb', line 137

def to_s
  attstring = ""
  if attrs.size > 0
    attstring = " " + attrs.collect { |k,v| "#{k}=\"#{escape(v)}\"" }.join(" ")
  end
  string = "#{tabs}<#{name}#{attstring}"
  if children.size > 0
    string << ">"
    if text?
      string << escape(text)
    end
    string << "\n"
    @@depth += 1
    string << children.collect {|child| child.to_s }.join("")
    @@depth -= 1
    string << "#{tabs}</#{name}>\n"
  elsif text?
    string << ">" << escape(text) << "</#{name}>\n"
  else
    string << "/>\n"
  end
  string
end

#traverse(type = :pre, &block) ⇒ Object

full traversal from the initial node



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/axml.rb', line 78

def traverse(type=:pre, &block)
  if type == :pre
    block.call(self)
  end
  children.each do |child|
    child.traverse(type, &block)
  end
  if type == :post
    block.call(self)
  end
end