khrl 0.0.1
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 +7 -0
- data/khrl-0.0.1.gem +0 -0
- data/khrl.gemspec +12 -0
- data/lib/file/kfile.rb +56 -0
- data/lib/khrl.rb +2 -0
- data/lib/xml/kxml.rb +276 -0
- data/lib/xml/xml_parser.rb +114 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 84ee2b18b834c7130cb718543e5c996b54881904
|
4
|
+
data.tar.gz: 317741454576c9dc155af71f8135fad014c04bf7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7bb17aa382e2e958a980d51d1d502b76b4a9126c7ccf0653cd5099c621b6517fe051838a413a3d1ccdc2bed935ad8db0123196010a4ecef774f000c2e876c334
|
7
|
+
data.tar.gz: 42f75450dcf26de2c9d28a9f31f2298f7f046955e497dcd88b7e691b213ce1dafccb84b80ddc0cc5d443cea3293729c0049390aa8df713b47dd2bb7e03d8547d
|
data/khrl-0.0.1.gem
ADDED
Binary file
|
data/khrl.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'khrl'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2014-10-14'
|
5
|
+
s.summary = "KHRL"
|
6
|
+
s.description = "private ruby lib"
|
7
|
+
s.authors = ["Koihik"]
|
8
|
+
s.email = 'koihik@hotmail.com'
|
9
|
+
s.files = Dir['**/*']
|
10
|
+
s.homepage = 'https://rubygems.org/gems/khrl'
|
11
|
+
s.license = 'MIT'
|
12
|
+
end
|
data/lib/file/kfile.rb
ADDED
@@ -0,0 +1,56 @@
|
|
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? src
|
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 if !File.directory?(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
|
+
|
55
|
+
end
|
56
|
+
|
data/lib/khrl.rb
ADDED
data/lib/xml/kxml.rb
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
module KXML
|
2
|
+
class Document
|
3
|
+
attr_accessor :encoding
|
4
|
+
attr_accessor :version
|
5
|
+
attr_accessor :root
|
6
|
+
|
7
|
+
def initialize(data)
|
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)
|
26
|
+
return @root.to_s(format)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
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
|
46
|
+
|
47
|
+
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
|
+
|
275
|
+
end
|
276
|
+
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module KXML
|
2
|
+
class SAXParser
|
3
|
+
START_TAG = 'START_TAG'
|
4
|
+
END_TAG = 'END_TAG'
|
5
|
+
COMMENTS = 'COMMENTS'
|
6
|
+
SELF_CLOSE_TAG = 'SELF_COLSE_TAG'
|
7
|
+
DOCUMENT_STATEMENT = 'DOCUMENT_STATEMENT'
|
8
|
+
CONTENT = 'CONTENT'
|
9
|
+
|
10
|
+
attr_accessor :handler
|
11
|
+
def initialize
|
12
|
+
reset()
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset
|
16
|
+
@state = :NONE
|
17
|
+
@temp_str = nil
|
18
|
+
@is_start_tag = true
|
19
|
+
@quote_state = nil
|
20
|
+
@data = ''
|
21
|
+
@end = false
|
22
|
+
end
|
23
|
+
|
24
|
+
def <<(data)
|
25
|
+
push(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def push(data)
|
29
|
+
@data += data
|
30
|
+
parse()
|
31
|
+
end
|
32
|
+
|
33
|
+
def end
|
34
|
+
@end = true
|
35
|
+
parse()
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def parse
|
41
|
+
raise "Cannot parse without handler" if @handler == nil
|
42
|
+
return if @data == nil || @data == ''
|
43
|
+
|
44
|
+
while(true)
|
45
|
+
result = @data.match(/^<([\s\S.]*?)>/)
|
46
|
+
if result !=nil && @data[0] == '<'
|
47
|
+
dispath_tag(result.to_s)
|
48
|
+
@data = @data[result[1].size+2,@data.size]
|
49
|
+
next
|
50
|
+
end
|
51
|
+
result = @data.match(/([\s\S.]+?)</)
|
52
|
+
if result !=nil
|
53
|
+
dispath_content(result[1])
|
54
|
+
@data = @data[result[1].size,@data.size]
|
55
|
+
next
|
56
|
+
end
|
57
|
+
break
|
58
|
+
end
|
59
|
+
dispath_content(@data) if @end && @data != nil && @data != ''
|
60
|
+
end
|
61
|
+
|
62
|
+
def dispath_tag(data)
|
63
|
+
if data =~ /<\?xml\s+(.*?)\?>/
|
64
|
+
param = parse_tag("STATEMENT #{data.match(/<\?xml\s+(.*?)\?>/)[1]}")
|
65
|
+
param[:type] = DOCUMENT_STATEMENT
|
66
|
+
@handler.call(param)
|
67
|
+
return
|
68
|
+
end
|
69
|
+
if data =~ /<([\s\S.]*?)\/>/
|
70
|
+
param = parse_tag(data.match(/<([\s\S.]*?)\/>/)[1])
|
71
|
+
param[:type] = SELF_CLOSE_TAG
|
72
|
+
@handler.call(param)
|
73
|
+
return
|
74
|
+
end
|
75
|
+
if data =~ /<\/([\s\S.]*?)>/
|
76
|
+
param = parse_tag(data.match(/<\/([\s\S.]*?)>/)[1])
|
77
|
+
param[:type] = END_TAG
|
78
|
+
@handler.call(param)
|
79
|
+
return
|
80
|
+
end
|
81
|
+
if data =~ /<\!--([\s\S.]*?)-->/
|
82
|
+
@handler.call({:type => COMMENTS , :name => data.match(/<\!--([\s\S.]*?)-->/)[1]})
|
83
|
+
return
|
84
|
+
end
|
85
|
+
param = parse_tag(data.match(/<([\s\S.]*?)>/)[1])
|
86
|
+
param[:type] = START_TAG
|
87
|
+
@handler.call(param)
|
88
|
+
end
|
89
|
+
|
90
|
+
def dispath_content(data)
|
91
|
+
@handler.call({:type => CONTENT , :name => data})
|
92
|
+
end
|
93
|
+
|
94
|
+
def parse_tag(data)
|
95
|
+
arr = data.split(' ')
|
96
|
+
if arr.size == 1
|
97
|
+
return {:name => arr[0]}
|
98
|
+
else
|
99
|
+
ret = {}
|
100
|
+
ret[:name] = arr[0]
|
101
|
+
ret[:attributes] = {}
|
102
|
+
1.upto(arr.size-1){|index|
|
103
|
+
attribute = arr[index]
|
104
|
+
attribute_arr = attribute.split('=')
|
105
|
+
raise "Parse Attribute Failed : #{attribute}" if attribute_arr.size!=2 || !attribute_arr[1].match(/[\'\"](.*)[\'\"]/)
|
106
|
+
ret[:attributes].store(attribute_arr[0],attribute_arr[1].match(/[\'\"](.*)[\'\"]/)[1])
|
107
|
+
}
|
108
|
+
return ret
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: khrl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Koihik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: private ruby lib
|
14
|
+
email: koihik@hotmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- khrl-0.0.1.gem
|
20
|
+
- khrl.gemspec
|
21
|
+
- lib/file/kfile.rb
|
22
|
+
- lib/khrl.rb
|
23
|
+
- lib/xml/kxml.rb
|
24
|
+
- lib/xml/xml_parser.rb
|
25
|
+
homepage: https://rubygems.org/gems/khrl
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.0.14
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: KHRL
|
49
|
+
test_files: []
|