Class: AXML::El
- Inherits:
-
Object
- Object
- AXML::El
- Includes:
- Enumerable, Traverseable
- Defined in:
- lib/axml/el.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
- #[](attribute_string) ⇒ Object
- #[]=(attribute_string, value) ⇒ Object
- #add_node(node) ⇒ Object
-
#child ⇒ Object
the first child (equivalent to children.first).
- #children? ⇒ Boolean (also: #child?)
-
#drop ⇒ Object
drops the current element from the list of its parents children.
- #drop_child(node) ⇒ Object
- #each(&block) ⇒ Object
-
#escape(data) ⇒ Object
returns data escaped if necessary.
-
#find(string) ⇒ Object
Returns an array of nodes.
- #find_children(name) ⇒ Object
- #find_descendants(name, collect_descendants = []) ⇒ Object
-
#find_first(string) ⇒ Object
- currently must be called with descendant
- or child
-
string prefix! e.g.
- #find_first_child(name) ⇒ Object
- #find_first_descendant(name) ⇒ Object
- #find_first_following_sibling(name) ⇒ Object
- #find_following_siblings(name) ⇒ Object
- #inspect ⇒ Object
-
#next ⇒ Object
the next node.
-
#text? ⇒ Boolean
has text?.
-
#to_doc(filename = nil) ⇒ Object
returns the xml string with the xml header in place if given a filename, writes it to the file.
- #to_s(indent = 0) ⇒ Object
Methods included from Traverseable
Instance Method Details
#[](attribute_string) ⇒ Object
26 27 28 |
# File 'lib/axml/el.rb', line 26 def [](attribute_string) attrs[attribute_string] end |
#[]=(attribute_string, value) ⇒ Object
30 31 32 |
# File 'lib/axml/el.rb', line 30 def []=(attribute_string, value) attrs[attribute_string] = value end |
#add_node(node) ⇒ Object
138 139 140 141 |
# File 'lib/axml/el.rb', line 138 def add_node(node) node.array_index = children.size children.push( node ) end |
#child ⇒ Object
the first child (equivalent to children.first)
134 135 136 |
# File 'lib/axml/el.rb', line 134 def child children.first end |
#children? ⇒ Boolean Also known as: child?
39 40 41 |
# File 'lib/axml/el.rb', line 39 def children? children.size > 0 end |
#drop ⇒ Object
drops the current element from the list of its parents children
51 52 53 |
# File 'lib/axml/el.rb', line 51 def drop parent.children.delete(self) end |
#drop_child(node) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/axml/el.rb', line 55 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
44 45 46 47 48 |
# File 'lib/axml/el.rb', line 44 def each(&block) children.each do |child| block.call(child) end end |
#escape(data) ⇒ Object
returns data escaped if necessary
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/axml/el.rb', line 73 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!('&', '&') result.gsub!('<', '<') result.gsub!('>', '>') result.gsub!('"', '"') result.gsub!("'", ''') end result end |
#find(string) ⇒ Object
Returns an array of nodes. Accepts same xpath strings as find_first.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/axml/el.rb', line 148 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
194 195 196 |
# File 'lib/axml/el.rb', line 194 def find_children(name) children.select {|v| v.name == name } end |
#find_descendants(name, collect_descendants = []) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/axml/el.rb', line 175 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)
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/axml/el.rb', line 163 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
198 199 200 201 202 203 204 205 |
# File 'lib/axml/el.rb', line 198 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
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/axml/el.rb', line 183 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
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/axml/el.rb', line 211 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
207 208 209 |
# File 'lib/axml/el.rb', line 207 def find_following_siblings(name) parent.children[(array_index+1)..-1].select {|v| v.name == name } end |
#inspect ⇒ Object
124 125 126 |
# File 'lib/axml/el.rb', line 124 def inspect "<name='#{name}' attrs='#{attrs.inspect}' children.size=#{children.size}>" end |
#next ⇒ Object
the next node
129 130 131 |
# File 'lib/axml/el.rb', line 129 def next parent.children[array_index+1] end |
#text? ⇒ Boolean
has text?
35 36 37 |
# File 'lib/axml/el.rb', line 35 def text? !!text end |
#to_doc(filename = nil) ⇒ Object
returns the xml string with the xml header in place if given a filename, writes it to the file
89 90 91 92 93 94 95 96 97 |
# File 'lib/axml/el.rb', line 89 def to_doc(filename=nil) output = '<?xml version="1.0" encoding="UTF-8"?>' output << self.to_s if filename File.open(filename, 'w') do |out| out.print output end end end |
#to_s(indent = 0) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/axml/el.rb', line 99 def to_s(indent=0) if indent.is_a? Hash to_doc end attstring = "" if attrs.size > 0 attstring = " " + attrs.collect { |k,v| "#{k}=\"#{escape(v)}\"" }.join(" ") end string = "#{Indentation[indent]}<#{name}#{attstring}" if children.size > 0 string << ">" if text? string << escape(text) end string << "\n" string << children.collect {|child| child.to_s(indent+1) }.join("") string << "#{Indentation[indent]}</#{name}>\n" elsif text? string << ">" << escape(text) << "</#{name}>\n" else string << "/>\n" end string end |