Class: KXML::Node

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

Instance Method Summary collapse

Constructor Details

#initialize(data) {|_self| ... } ⇒ Node

Returns a new instance of Node.

Yields:

  • (_self)

Yield Parameters:

  • _self (KXML::Node)

    the object that the method was called on



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



317
318
319
320
321
322
323
324
325
326
327
# 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

Instance Method Details

#<<(data) ⇒ Object



134
135
136
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/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) ⇒ Object



237
238
239
# File 'lib/xml/kxml.rb', line 237

def [](key)
	return @_attributes[key.to_s]
end

#[]=(key, value) ⇒ Object



241
242
243
# File 'lib/xml/kxml.rb', line 241

def []=(key,value)
	@_attributes[key.to_s] = value
end

#_to_s_pretty(deepth = 0) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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) ⇒ Object



128
129
130
131
132
# File 'lib/xml/kxml.rb', line 128

def add_child(node)
	@_children << node
	node.parent = self
	return self
end

#append_content(data) ⇒ Object



161
162
163
164
# File 'lib/xml/kxml.rb', line 161

def append_content(data)
	@_content = "" if @_content == nil
	@_content += data
end

#attributesObject



245
246
247
# File 'lib/xml/kxml.rb', line 245

def attributes
	return @_attributes
end

#call(*arg) ⇒ Object



269
270
271
272
273
274
275
# 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

#childrenObject



265
266
267
# File 'lib/xml/kxml.rb', line 265

def children
	return @_children
end

#contentObject



249
250
251
# File 'lib/xml/kxml.rb', line 249

def content
	return @_content
end

#content=(value) ⇒ Object



253
254
255
# File 'lib/xml/kxml.rb', line 253

def content=(value)
	@_content = value
end

#last_nodeObject



178
179
180
181
182
183
184
185
186
187
188
# 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

#nameObject



257
258
259
# File 'lib/xml/kxml.rb', line 257

def name
	return @_name
end

#name=(value) ⇒ Object



261
262
263
# File 'lib/xml/kxml.rb', line 261

def name=(value)
	@_name = value
end

#next_nodeObject



166
167
168
169
170
171
172
173
174
175
176
# 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

#parentObject

getter setter



233
234
235
# File 'lib/xml/kxml.rb', line 233

def parent
	return @_parent
end

#to_aryObject



190
191
192
# File 'lib/xml/kxml.rb', line 190

def to_ary
	return [] << to_s
end

#to_s(format = Format::PRETTY) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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