xml-registry 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xml-registry.rb +127 -0
- metadata +55 -0
data/lib/xml-registry.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# file: xml-registry.rb
|
4
|
+
|
5
|
+
require 'rexml/document'
|
6
|
+
include REXML
|
7
|
+
|
8
|
+
class Registry
|
9
|
+
|
10
|
+
def initialize()
|
11
|
+
@template = '<root><system/><app/><user/><ip_address/></root>'
|
12
|
+
@doc = Document.new(@template)
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialise_registry()
|
16
|
+
@doc = Document.new(@template)
|
17
|
+
end
|
18
|
+
|
19
|
+
alias :reset_registry :initialise_registry
|
20
|
+
|
21
|
+
def set_key(path, value)
|
22
|
+
create_path = find_path(path)
|
23
|
+
|
24
|
+
if not create_path.empty? then
|
25
|
+
parent_path = (path.split('/') - create_path.reverse).join('/')
|
26
|
+
key_builder(parent_path, create_path)
|
27
|
+
end
|
28
|
+
|
29
|
+
add_value(path, value)
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_key(path)
|
33
|
+
XPath.first(@doc.root, path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def delete_key(path)
|
37
|
+
node = XPath.first(@doc.root, path)
|
38
|
+
node.parent.delete node if node
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_xml()
|
42
|
+
@doc.to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
alias :display_xml :to_xml
|
46
|
+
|
47
|
+
def load_xml(s='')
|
48
|
+
@doc = Document.new(read(s))
|
49
|
+
end
|
50
|
+
|
51
|
+
def save(s)
|
52
|
+
File.open(s){|f| @doc.write f}
|
53
|
+
end
|
54
|
+
|
55
|
+
def import(s)
|
56
|
+
reg_buffer = read(s)
|
57
|
+
|
58
|
+
reg_items = reg_buffer.gsub(/\n/,'').split(/(?=\[.[^\]]+\])/).map do |x|
|
59
|
+
[x[/^\[(.[^\]]+)\]/,1], Hash[*($').scan(/"([^"]+)"="(.[^"]+)?"/).flatten]]
|
60
|
+
end
|
61
|
+
|
62
|
+
reg_items.each do |path, items|
|
63
|
+
items.each {|k,value| self.set_key("%s/%s" % [path,k], value)}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def export(s=nil)
|
68
|
+
reg = print_scan(@doc.root).join("\n")
|
69
|
+
File.open(s){|f| f.write reg} if s
|
70
|
+
reg
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def add_key(path='app', key='untitled')
|
76
|
+
node = @doc.root.elements[path]
|
77
|
+
node.add_element Element.new(key)
|
78
|
+
end
|
79
|
+
|
80
|
+
def add_value(key, value)
|
81
|
+
@doc.root.elements[key].text = value
|
82
|
+
end
|
83
|
+
|
84
|
+
def find_path(path, create_path=[])
|
85
|
+
return create_path if @doc.root.elements[path]
|
86
|
+
apath = path.split('/')
|
87
|
+
create_path << apath.pop
|
88
|
+
find_path(apath.join('/'), create_path)
|
89
|
+
end
|
90
|
+
|
91
|
+
def key_builder(parent_path, create_path)
|
92
|
+
key = create_path.pop
|
93
|
+
add_key(parent_path, key)
|
94
|
+
key_builder("#{parent_path}/#{key}", create_path) unless create_path.empty?
|
95
|
+
end
|
96
|
+
|
97
|
+
def read(s)
|
98
|
+
if s[/^https?:\/\//] then
|
99
|
+
buffer = open(s, "UserAgent" => "Ruby Registry-reader").read
|
100
|
+
elsif File.exists? s then
|
101
|
+
buffer = File.open(s).read
|
102
|
+
else
|
103
|
+
buffer = s
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def print_scan(node, parent=[])
|
108
|
+
out = []
|
109
|
+
parent << node.name
|
110
|
+
items = []
|
111
|
+
node.elements.each do |e|
|
112
|
+
if e.has_elements? then
|
113
|
+
out << print_scan(e, parent.clone)
|
114
|
+
else
|
115
|
+
items << [e.name, e.text]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
if parent.length > 1 and items.length > 0 then
|
119
|
+
out << "[%s]\n%s\n" % [parent[1..-1].join("/"),
|
120
|
+
items.map {|x| "\"%s\"=\"%s\"" % x}.join("\n")]
|
121
|
+
end
|
122
|
+
out
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml-registry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-07-08 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/xml-registry.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: xml-registry
|
54
|
+
test_files: []
|
55
|
+
|