khrl 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/khrl-0.0.3.gem +0 -0
- data/khrl.gemspec +2 -2
- data/lib/Test.rb +5 -0
- data/lib/file/kfile.rb +48 -53
- data/lib/test.xml +15 -0
- data/lib/xml/kxml.rb +321 -260
- metadata +6 -4
- data/khrl-0.0.2.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb2a9d22000286cb0b9770246be887d24aa35dac
|
4
|
+
data.tar.gz: 17ee13ae884eec3e5e02d9762143cee888ab0667
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 413a2bf4974a8fb2f4ab719c4992f67e9596055b934f91040608ebe2126d191e009fa00a2c6f0386dc5d6865c64fe4fc98fe2bc5c631e41e5fe6a839c5df06f5
|
7
|
+
data.tar.gz: 12fb11b25bd175a37ceea324cacca996eafca442e935111853f01a7efe186150ab83e543268c362dbb2e55678b7211b8d274f69b67b31f51acf5a3b1b99346d2
|
data/khrl-0.0.3.gem
ADDED
Binary file
|
data/khrl.gemspec
CHANGED
data/lib/Test.rb
ADDED
data/lib/file/kfile.rb
CHANGED
@@ -1,56 +1,51 @@
|
|
1
1
|
module KFile
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
|
data/lib/test.xml
ADDED
data/lib/xml/kxml.rb
CHANGED
@@ -1,276 +1,337 @@
|
|
1
1
|
module KXML
|
2
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
def
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
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.
|
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:
|
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.
|
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.
|
47
|
+
rubygems_version: 2.4.5
|
46
48
|
signing_key:
|
47
49
|
specification_version: 4
|
48
50
|
summary: KHRL
|
data/khrl-0.0.2.gem
DELETED
Binary file
|