class KXML::Node
Public Class Methods
new(data) { |self| ... }
click to toggle source
@_children @_content @_name @_attributes @_parent
# File lib/xml/kxml.rb, line 110 def initialize(data) @_children = [] @_attributes = {} case data when String if data =~ /^[<>\/]/ parse_from_string(data) else @_name = data end when File parse_from_file(data) else raise "Unsupport Param Type : #{data.class}" end yield self if block_given? end
Public Instance Methods
<<(data)
click to toggle source
# File lib/xml/kxml.rb, line 134 def <<(data) case data when Hash data.each{|k,v| k = k.to_s if k.is_a? Symbol @_attributes[k] = v } when KXML::Node add_child(data) when String if data =~ /^[<>\/]/ self << Node.new(data) else append_content(data) end when Symbol append_content(data.to_s) when Array data.each{|e| self << e } else raise "method << not support this type : #{data.class}" end return self end
[](key)
click to toggle source
# File lib/xml/kxml.rb, line 237 def [](key) return @_attributes[key.to_s] end
[]=(key,value)
click to toggle source
# File lib/xml/kxml.rb, line 241 def []=(key,value) @_attributes[key.to_s] = value end
_to_s_pretty(deepth = 0)
click to toggle source
# File lib/xml/kxml.rb, line 214 def _to_s_pretty(deepth = 0) attributes_str = "" indent = "\t" * deepth wrap = "\n" @_attributes.each{|key,value| attributes_str += " #{key}=\"#{value}\"" } content = "" @_children.each{|child| content += wrap content += child._to_s_pretty(deepth+1) } content += wrap if @_children.size > 0 content += @_content if @_content != nil return "#{indent}<#{@_name}#{attributes_str}>#{content}#{@_children.size > 0 ? indent : ""}</#{@_name}>" end
add_child(node)
click to toggle source
# File lib/xml/kxml.rb, line 128 def add_child(node) @_children << node node.parent = self return self end
append_content(data)
click to toggle source
# File lib/xml/kxml.rb, line 161 def append_content(data) @_content = "" if @_content == nil @_content += data end
attributes()
click to toggle source
# File lib/xml/kxml.rb, line 245 def attributes return @_attributes end
call(*arg)
click to toggle source
# File lib/xml/kxml.rb, line 269 def call(*arg) if arg[1].class == Array then find_children_by_name(arg[0]) else return find_child_by_name(arg[0]) end end
children()
click to toggle source
# File lib/xml/kxml.rb, line 265 def children return @_children end
content()
click to toggle source
# File lib/xml/kxml.rb, line 249 def content return @_content end
content=(value)
click to toggle source
# File lib/xml/kxml.rb, line 253 def content=(value) @_content = value end
last_node()
click to toggle source
# File lib/xml/kxml.rb, line 178 def last_node p = parent return nil if p==nil index = -1 p.children.each_with_index{|c,i| index = i if c==self } return nil if index == -1 or index == 0 return p.children[index-1] if p.children[index-1]!=nil return nil end
name()
click to toggle source
# File lib/xml/kxml.rb, line 257 def name return @_name end
name=(value)
click to toggle source
# File lib/xml/kxml.rb, line 261 def name=(value) @_name = value end
next_node()
click to toggle source
# File lib/xml/kxml.rb, line 166 def next_node p = parent return nil if p==nil index = -1 p.children.each_with_index{|c,i| index = i if c==self } return nil if index == -1 return p.children[index+1] if p.children[index+1]!=nil return nil end
parent()
click to toggle source
getter setter
# File lib/xml/kxml.rb, line 233 def parent return @_parent end
to_ary()
click to toggle source
# File lib/xml/kxml.rb, line 190 def to_ary return [] << to_s end
to_s(format = Format::PRETTY)
click to toggle source
# File lib/xml/kxml.rb, line 194 def to_s(format = Format::PRETTY) case format when Format::MINIMUM attributes_str = "" @_attributes.each{|key,value| attributes_str += " #{key}=\"#{value}\"" } content = "" @_children.each{|child| content += child.to_s } content += @_content if @_content != nil return "<#{@_name}#{attributes_str}>#{content}</#{@_name}>" when Format::PRETTY return _to_s_pretty else raise "Unsupport Format : #{format}" end end
Protected Instance Methods
parent=(p)
click to toggle source
# File lib/xml/kxml.rb, line 279 def parent=(p) @_parent = p end
Private Instance Methods
find_child_by_name(name)
click to toggle source
# File lib/xml/kxml.rb, line 301 def find_child_by_name(name) @_children.each{|child| return child if name.to_s == child.name.to_s } return nil end
find_children_by_name(name)
click to toggle source
# File lib/xml/kxml.rb, line 308 def find_children_by_name(name) ret = [] @_children.each{|child| ret << child if name.to_s == child.name.to_s } return ret end
method_missing(method_name,*args,&block)
click to toggle source
Calls superclass method
# File lib/xml/kxml.rb, line 317 def method_missing(method_name,*args,&block) if respond_to? method_name then super else if args[0].class == Array then return find_children_by_name(method_name) else return find_child_by_name(method_name) end end end
parse_from_file(file)
click to toggle source
# File lib/xml/kxml.rb, line 293 def parse_from_file(file) document = Document.new(file) @_name = document.root.name @_children = document.root.children @_content = document.root.content @_attributes = document.root.attributes end
parse_from_string(data)
click to toggle source
# File lib/xml/kxml.rb, line 285 def parse_from_string(data) document = Document.new(data) @_name = document.root.name @_children = document.root.children @_content = document.root.content @_attributes = document.root.attributes end