khrl 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1faa3ce3e6ef4077bc2827ea44d4c1d34ca9b448
4
- data.tar.gz: ffa294e7c9fea7bdced04d77989819e294fe5d36
3
+ metadata.gz: fb2a9d22000286cb0b9770246be887d24aa35dac
4
+ data.tar.gz: 17ee13ae884eec3e5e02d9762143cee888ab0667
5
5
  SHA512:
6
- metadata.gz: 56fe3f34ea956d5db38e4246ffd41a9ecc5faa21230606fdd1ff37086045f0e9bd50084585560b28719dd75b10ebb5021826fce6f86a240b4562b3a9fca1ddd7
7
- data.tar.gz: 5946cbcf8fffd4373fa94ab15dfdfed92da2326abf7ceb6c142b45306a9a28e24153772379658a83c5d1207e0c16fbfdbf6b5b10bba697af44bfbbf7ddebd2d9
6
+ metadata.gz: 413a2bf4974a8fb2f4ab719c4992f67e9596055b934f91040608ebe2126d191e009fa00a2c6f0386dc5d6865c64fe4fc98fe2bc5c631e41e5fe6a839c5df06f5
7
+ data.tar.gz: 12fb11b25bd175a37ceea324cacca996eafca442e935111853f01a7efe186150ab83e543268c362dbb2e55678b7211b8d274f69b67b31f51acf5a3b1b99346d2
Binary file
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'khrl'
3
- s.version = '0.0.3'
4
- s.date = '2014-12-03'
3
+ s.version = '0.0.4'
4
+ s.date = '2015-04-22'
5
5
  s.summary = "KHRL"
6
6
  s.description = "private ruby lib"
7
7
  s.authors = ["Koihik"]
@@ -0,0 +1,5 @@
1
+ require File.expand_path("../file/kfile.rb",__FILE__)
2
+ require File.expand_path("../xml/kxml.rb",__FILE__)
3
+
4
+ include KXML
5
+
@@ -1,56 +1,51 @@
1
1
  module KFile
2
- require "fileutils"
3
-
4
- def self.delete(dir)
5
- FileUtils.remove_dir(dir) if File.exists? dir
6
- end
7
-
8
- def self.clear(dir)
9
- return if !File.exists? dir
10
- Dir.foreach(dir) do |path|
11
- if path !="." and path !=".."
12
- delete(dir+"/"+path)
13
- end
14
- end
15
- end
16
-
17
- def self.copy(src,dest)
18
- return if !File.exists? src
19
- if File.directory? src
20
- Dir.mkdir(dest) if !File.exist? dest
21
- Dir.foreach(src) do |path|
22
- if path !="." and path !=".."
23
- copy(src+"/"+path,dest+"/"+path)
24
- end
25
- end
26
- else
27
- FileUtils.cp(src,dest)
28
- end
29
- end
30
-
31
- def self.list(dir,recursive = true,&proc)
32
- return if !File.exists? dir
33
- if File.directory?(dir)
34
- Dir.foreach(dir) do |path|
35
- if path !="." && path !=".."
36
- full_path = dir + "/" + path
37
- if recursive
38
- list(full_path,recursive,&proc)
39
- else
40
- yield full_path
41
- end
42
- end
43
- end
44
- else
45
- yield dir
46
- end
47
- end
48
-
49
- def self.read(file_path,encoding = 'utf-8')
50
- File.open(file_path,"r:#{encoding}"){|f|
51
- return f.read
52
- }
53
- end
54
-
2
+ require "fileutils"
3
+
4
+ def self.delete(dir)
5
+ FileUtils.rm_r(dir) if File.exists? dir
6
+ end
7
+
8
+ def self.clear(dir)
9
+ FileUtils.rm_r Dir.glob("#{dir}/.")
10
+ end
11
+
12
+ def self.copy(src,dest)
13
+ return if !File.exists? src
14
+ if File.directory? src
15
+ Dir.mkdir(dest) if !File.exist? dest
16
+ Dir.foreach(src) do |path|
17
+ if path !="." and path !=".."
18
+ copy(src+"/"+path,dest+"/"+path)
19
+ end
20
+ end
21
+ else
22
+ FileUtils.cp(src,dest)
23
+ end
24
+ end
25
+
26
+ def self.list(dir,recursive = true,&proc)
27
+ return if !File.exists? dir
28
+ if File.directory?(dir)
29
+ Dir.foreach(dir) do |path|
30
+ if path !="." && path !=".."
31
+ full_path = dir + "/" + path
32
+ if recursive
33
+ list(full_path,recursive,&proc)
34
+ else
35
+ yield full_path
36
+ end
37
+ end
38
+ end
39
+ else
40
+ yield dir
41
+ end
42
+ end
43
+
44
+ def self.read(file_path,encoding = 'utf-8')
45
+ File.open(file_path,"r:#{encoding}"){|f|
46
+ return f.read
47
+ }
48
+ end
49
+
55
50
  end
56
51
 
@@ -0,0 +1,15 @@
1
+ <Root>
2
+ <NodeA
3
+ a="1"
4
+ b="2"/>
5
+
6
+ <NodeB>
7
+ <NodeC c="qwe"/>
8
+ </NodeB>
9
+
10
+ <NodeB>
11
+ <NodeC c="asd"/>
12
+ </NodeB>
13
+
14
+
15
+ </Root>
@@ -1,276 +1,337 @@
1
1
  module KXML
2
- class Document
2
+ class Document
3
3
  attr_accessor :encoding
4
4
  attr_accessor :version
5
5
  attr_accessor :root
6
6
 
7
7
  def initialize(data)
8
8
  case data
9
- when KXML::Node
10
- @root = data
11
- when String
12
- if data =~ /^[<>\/]/
13
- parse_from_string(data)
14
- else
15
- @root = Node.new(data)
16
- end
17
- when File
18
- parse_from_file(data)
19
- else
20
- raise "Unsupport Param Type : #{data.class}"
21
- end
22
- yield @root if block_given?
23
- end
24
-
25
- def to_s(format = Format::MINIMUM)
9
+ when KXML::Node
10
+ @root = data
11
+ when String
12
+ if data =~ /^[<>\/]/
13
+ parse_from_string(data)
14
+ else
15
+ @root = Node.new(data)
16
+ end
17
+ when File
18
+ parse_from_file(data)
19
+ else
20
+ raise "Unsupport Param Type : #{data.class}"
21
+ end
22
+ yield @root if block_given?
23
+ end
24
+
25
+ def save_to(file_path)
26
+ File.open(file_path,"w+"){|f|
27
+ f.write to_s
28
+ }
29
+ end
30
+
31
+ def to_s(format = Format::PRETTY)
26
32
  return @root.to_s(format)
27
33
  end
28
34
 
29
- private
35
+ private
30
36
 
31
37
  def parse_from_string(data)
32
- pre_parse()
33
- push_data(data)
34
- post_parse()
35
- end
36
-
37
- def parse_from_file(file)
38
- pre_parse()
39
- line = file.gets
40
- while(line != nil)
41
- push_data(line)
42
- line = file.gets
43
- end
44
- post_parse()
45
- end
38
+ pre_parse()
39
+ push_data(data)
40
+ post_parse()
41
+ end
42
+
43
+ def parse_from_file(file)
44
+ pre_parse()
45
+ line = file.gets
46
+ while(line != nil)
47
+ push_data(line)
48
+ line = file.gets
49
+ end
50
+ post_parse()
51
+ end
46
52
 
47
53
  def pre_parse
48
- require File.expand_path('../xml_parser.rb',__FILE__)
49
- @__parser = SAXParser.new
50
- @__tag_stack = []
51
- @__parser.handler=->(param){
52
- case param[:type]
53
- when SAXParser::DOCUMENT_STATEMENT
54
- @encoding = param[:attributes]['encoding']
55
- @version = param[:attributes]['version']
56
- when SAXParser::START_TAG
57
- node = Node.new(param[:name])
58
- node.attributes.merge!(param[:attributes]) if param[:attributes] != nil
59
- if @root == nil
60
- @root = node
61
- else
62
- @__tag_stack.last << node
63
- end
64
- @__tag_stack << node
65
- when SAXParser::END_TAG
66
- raise "Tag mismatch : <#{@__tag_stack.last.name}> - </#{param[:name]}>" if @__tag_stack.last ==nil || @__tag_stack.last.name != param[:name]
67
- @__tag_stack.pop
68
- when SAXParser::SELF_CLOSE_TAG
69
- node = Node.new(param[:name])
70
- node.attributes.merge!(param[:attributes]) if param[:attributes] != nil
71
- if @root == nil
72
- @root = node
73
- else
74
- @__tag_stack.last << node
75
- end
76
- when SAXParser::COMMENTS
77
- #ignore
78
- when SAXParser::CONTENT
79
- next if @__tag_stack.last == nil
80
- @__tag_stack.last.content = '' if @__tag_stack.last.content == nil
81
- @__tag_stack.last.content += param[:name].gsub(/\s/,"")
82
- end
83
- }
84
- end
85
-
86
- def push_data(data)
87
- @__parser << data
88
- end
89
-
90
- def post_parse
91
- @__parser.end
92
-
93
- @__parser = nil
94
- @__tag_stack = nil
95
- end
96
- end
97
-
98
- class Node
99
- # @_children
100
- # @_content
101
- # @_name
102
- # @_attributes
103
- def initialize(data)
104
- @_children = []
105
- @_attributes = {}
106
- case data
107
- when String
108
- if data =~ /^[<>\/]/
109
- parse_from_string(data)
110
- else
111
- @_name = data
112
- end
113
- when File
114
- parse_from_file(data)
115
- else
116
- raise "Unsupport Param Type : #{data.class}"
117
- end
118
- yield self if block_given?
119
- end
120
-
121
- def add_child(node)
122
- @_children << node
123
- return self
124
- end
125
-
126
- def <<(data)
127
- case data
128
- when Hash
129
- data.each{|k,v|
130
- k = k.to_s if k.is_a? Symbol
131
- @_attributes[k] = v
132
- }
133
- when KXML::Node
134
- add_child(data)
135
- when String
136
- if data =~ /^[<>\/]/
137
- self << Node.new(data)
138
- else
139
- append_content(data)
140
- end
141
- when Symbol
142
- append_content(data.to_s)
143
- when Array
144
- data.each{|e|
145
- self << e
146
- }
147
- else
148
- raise "method << not support this type : #{data.class}"
149
- end
150
- return self
151
- end
152
-
153
- def append_content(data)
154
- @_content = "" if @_content == nil
155
- @_content += data
156
- end
157
-
158
- def to_ary
159
- return [] << to_s
160
- end
161
-
162
- def to_s(format = Format::MINIMUM)
163
- case format
164
- when Format::MINIMUM
165
- attributes_str = ""
166
- @_attributes.each{|key,value|
167
- attributes_str += " #{key}=\"#{value}\""
168
- }
169
- content = ""
170
- @_children.each{|child|
171
- content += child.to_s
172
- }
173
- content += @_content if @_content != nil
174
- return "<#{@_name}#{attributes_str}>#{content}</#{@_name}>"
175
- when Format::PRETTY
176
- return _to_s_pretty
177
- else
178
- raise "Unsupport Format : #{format}"
179
- end
180
- end
181
-
182
- def _to_s_pretty(deepth = 0)
183
- attributes_str = ""
184
- indent = "\t" * deepth
185
- wrap = "\n"
186
- @_attributes.each{|key,value|
187
- attributes_str += " #{key}=\"#{value}\""
188
- }
189
- content = ""
190
- @_children.each{|child|
191
- content += wrap
192
- content += child._to_s_pretty(deepth+1)
193
- }
194
- content += wrap if @_children.size > 0
195
- content += @_content if @_content != nil
196
- return "#{indent}<#{@_name}#{attributes_str}>#{content}#{@_children.size > 0 ? indent : ""}</#{@_name}>"
197
- end
198
-
199
- # getter setter
200
-
201
- def [](key)
202
- return @_attributes[key.to_s]
203
- end
204
-
205
- def []=(key,value)
206
- @_attributes[key.to_s] = value
207
- end
208
-
209
- def attributes
210
- return @_attributes
211
- end
212
-
213
- def content
214
- return @_content
215
- end
216
-
217
- def content=(value)
218
- @_content = value
219
- end
220
-
221
- def name
222
- return @_name
223
- end
224
-
225
- def name=(value)
226
- @_name = value
227
- end
228
-
229
- def children
230
- return @_children
231
- end
232
-
233
- private
234
-
235
- def parse_from_string(data)
236
- document = Document.new(data)
237
- @_name = document.root.name
238
- @_children = document.root.children
239
- @_content = document.root.content
240
- @_attributes = document.root.attributes
241
- end
242
-
243
- def parse_from_file(file)
244
- document = Document.new(file)
245
- @_name = document.root.name
246
- @_children = document.root.children
247
- @_content = document.root.content
248
- @_attributes = document.root.attributes
249
- end
250
-
251
- def find_children_by_name(name)
252
- ret = []
253
- @_children.each{|child|
254
- ret << child if name.to_s == child.name.to_s
255
- }
256
- return nil if ret.size == 0
257
- return ret[0] if ret.size == 1
258
- return ret
259
- end
260
-
261
- def method_missing(method_name,*args,&block)
262
- super if args.size != 0
263
- child = find_children_by_name(method_name)
264
- raise "<#{@_name}> have not this child : <#{method_name}>" if child == nil
265
- return child
266
- end
267
-
268
- end
269
-
270
- class Format
271
- MINIMUM = 'minimum'
272
- PRETTY = 'pretty'
273
- end
274
-
54
+ require File.expand_path('../xml_parser.rb',__FILE__)
55
+ @__parser = SAXParser.new
56
+ @__tag_stack = []
57
+ @__parser.handler=->(param){
58
+ case param[:type]
59
+ when SAXParser::DOCUMENT_STATEMENT
60
+ @encoding = param[:attributes]['encoding']
61
+ @version = param[:attributes]['version']
62
+ when SAXParser::START_TAG
63
+ node = Node.new(param[:name])
64
+ node.attributes.merge!(param[:attributes]) if param[:attributes] != nil
65
+ if @root == nil
66
+ @root = node
67
+ else
68
+ @__tag_stack.last << node
69
+ end
70
+ @__tag_stack << node
71
+ when SAXParser::END_TAG
72
+ raise "Tag mismatch : <#{@__tag_stack.last.name}> - </#{param[:name]}>" if @__tag_stack.last ==nil || @__tag_stack.last.name != param[:name]
73
+ @__tag_stack.pop
74
+ when SAXParser::SELF_CLOSE_TAG
75
+ node = Node.new(param[:name])
76
+ node.attributes.merge!(param[:attributes]) if param[:attributes] != nil
77
+ if @root == nil
78
+ @root = node
79
+ else
80
+ @__tag_stack.last << node
81
+ end
82
+ when SAXParser::COMMENTS
83
+ #ignore
84
+ when SAXParser::CONTENT
85
+ next if @__tag_stack.last == nil
86
+ @__tag_stack.last.content = '' if @__tag_stack.last.content == nil
87
+ @__tag_stack.last.content += param[:name].gsub(/\s/,"")
88
+ end
89
+ }
90
+ end
91
+
92
+ def push_data(data)
93
+ @__parser << data
94
+ end
95
+
96
+ def post_parse
97
+ @__parser.end
98
+
99
+ @__parser = nil
100
+ @__tag_stack = nil
101
+ end
102
+ end
103
+
104
+ class Node
105
+ # @_children
106
+ # @_content
107
+ # @_name
108
+ # @_attributes
109
+ # @_parent
110
+ def initialize(data)
111
+ @_children = []
112
+ @_attributes = {}
113
+ case data
114
+ when String
115
+ if data =~ /^[<>\/]/
116
+ parse_from_string(data)
117
+ else
118
+ @_name = data
119
+ end
120
+ when File
121
+ parse_from_file(data)
122
+ else
123
+ raise "Unsupport Param Type : #{data.class}"
124
+ end
125
+ yield self if block_given?
126
+ end
127
+
128
+ def add_child(node)
129
+ @_children << node
130
+ node.parent = self
131
+ return self
132
+ end
133
+
134
+ def <<(data)
135
+ case data
136
+ when Hash
137
+ data.each{|k,v|
138
+ k = k.to_s if k.is_a? Symbol
139
+ @_attributes[k] = v
140
+ }
141
+ when KXML::Node
142
+ add_child(data)
143
+ when String
144
+ if data =~ /^[<>\/]/
145
+ self << Node.new(data)
146
+ else
147
+ append_content(data)
148
+ end
149
+ when Symbol
150
+ append_content(data.to_s)
151
+ when Array
152
+ data.each{|e|
153
+ self << e
154
+ }
155
+ else
156
+ raise "method << not support this type : #{data.class}"
157
+ end
158
+ return self
159
+ end
160
+
161
+ def append_content(data)
162
+ @_content = "" if @_content == nil
163
+ @_content += data
164
+ end
165
+
166
+ def next_node
167
+ p = parent
168
+ return nil if p==nil
169
+ index = -1
170
+ p.children.each_with_index{|c,i|
171
+ index = i if c==self
172
+ }
173
+ return nil if index == -1
174
+ return p.children[index+1] if p.children[index+1]!=nil
175
+ return nil
176
+ end
177
+
178
+ def last_node
179
+ p = parent
180
+ return nil if p==nil
181
+ index = -1
182
+ p.children.each_with_index{|c,i|
183
+ index = i if c==self
184
+ }
185
+ return nil if index == -1 or index == 0
186
+ return p.children[index-1] if p.children[index-1]!=nil
187
+ return nil
188
+ end
189
+
190
+ def to_ary
191
+ return [] << to_s
192
+ end
193
+
194
+ def to_s(format = Format::PRETTY)
195
+ case format
196
+ when Format::MINIMUM
197
+ attributes_str = ""
198
+ @_attributes.each{|key,value|
199
+ attributes_str += " #{key}=\"#{value}\""
200
+ }
201
+ content = ""
202
+ @_children.each{|child|
203
+ content += child.to_s
204
+ }
205
+ content += @_content if @_content != nil
206
+ return "<#{@_name}#{attributes_str}>#{content}</#{@_name}>"
207
+ when Format::PRETTY
208
+ return _to_s_pretty
209
+ else
210
+ raise "Unsupport Format : #{format}"
211
+ end
212
+ end
213
+
214
+ def _to_s_pretty(deepth = 0)
215
+ attributes_str = ""
216
+ indent = "\t" * deepth
217
+ wrap = "\n"
218
+ @_attributes.each{|key,value|
219
+ attributes_str += " #{key}=\"#{value}\""
220
+ }
221
+ content = ""
222
+ @_children.each{|child|
223
+ content += wrap
224
+ content += child._to_s_pretty(deepth+1)
225
+ }
226
+ content += wrap if @_children.size > 0
227
+ content += @_content if @_content != nil
228
+ return "#{indent}<#{@_name}#{attributes_str}>#{content}#{@_children.size > 0 ? indent : ""}</#{@_name}>"
229
+ end
230
+
231
+ # getter setter
232
+
233
+ def parent
234
+ return @_parent
235
+ end
236
+
237
+ def [](key)
238
+ return @_attributes[key.to_s]
239
+ end
240
+
241
+ def []=(key,value)
242
+ @_attributes[key.to_s] = value
243
+ end
244
+
245
+ def attributes
246
+ return @_attributes
247
+ end
248
+
249
+ def content
250
+ return @_content
251
+ end
252
+
253
+ def content=(value)
254
+ @_content = value
255
+ end
256
+
257
+ def name
258
+ return @_name
259
+ end
260
+
261
+ def name=(value)
262
+ @_name = value
263
+ end
264
+
265
+ def children
266
+ return @_children
267
+ end
268
+
269
+ def call(*arg)
270
+ if arg[1].class == Array then
271
+ find_children_by_name(arg[0])
272
+ else
273
+ return find_child_by_name(arg[0])
274
+ end
275
+ end
276
+
277
+ protected
278
+
279
+ def parent=(p)
280
+ @_parent = p
281
+ end
282
+
283
+ private
284
+
285
+ def parse_from_string(data)
286
+ document = Document.new(data)
287
+ @_name = document.root.name
288
+ @_children = document.root.children
289
+ @_content = document.root.content
290
+ @_attributes = document.root.attributes
291
+ end
292
+
293
+ def parse_from_file(file)
294
+ document = Document.new(file)
295
+ @_name = document.root.name
296
+ @_children = document.root.children
297
+ @_content = document.root.content
298
+ @_attributes = document.root.attributes
299
+ end
300
+
301
+ def find_child_by_name(name)
302
+ @_children.each{|child|
303
+ return child if name.to_s == child.name.to_s
304
+ }
305
+ return nil
306
+ end
307
+
308
+ def find_children_by_name(name)
309
+ ret = []
310
+ @_children.each{|child|
311
+ ret << child if name.to_s == child.name.to_s
312
+ }
313
+ return ret
314
+ end
315
+
316
+
317
+ def method_missing(method_name,*args,&block)
318
+ if respond_to? method_name then
319
+ super
320
+ else
321
+ if args[0].class == Array then
322
+ return find_children_by_name(method_name)
323
+ else
324
+ return find_child_by_name(method_name)
325
+ end
326
+ end
327
+ end
328
+
329
+ end
330
+
331
+ class Format
332
+ MINIMUM = 'minimum'
333
+ PRETTY = 'pretty'
334
+ end
335
+
275
336
  end
276
337
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: khrl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koihik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2015-04-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: private ruby lib
14
14
  email: koihik@hotmail.com
@@ -16,10 +16,12 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - khrl-0.0.2.gem
19
+ - khrl-0.0.3.gem
20
20
  - khrl.gemspec
21
+ - lib/Test.rb
21
22
  - lib/file/kfile.rb
22
23
  - lib/khrl.rb
24
+ - lib/test.xml
23
25
  - lib/xml/kxml.rb
24
26
  - lib/xml/xml_parser.rb
25
27
  homepage: https://rubygems.org/gems/khrl
@@ -42,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
44
  version: '0'
43
45
  requirements: []
44
46
  rubyforge_project:
45
- rubygems_version: 2.0.14
47
+ rubygems_version: 2.4.5
46
48
  signing_key:
47
49
  specification_version: 4
48
50
  summary: KHRL
Binary file