xml-motor 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/xml-motor.rb +199 -0
- data/lib/xml-motor/version.rb +5 -0
- data/make_my_gem.sh +2 -0
- data/xml-motor.gemspec +46 -0
- metadata +63 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/xml-motor.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
module XMLIndexHandler
|
4
|
+
def self.get_node_indexes(xml_motor, tag)
|
5
|
+
xml_idx_to_find = []
|
6
|
+
xml_motor.xmltags[tag.split(".")[0]].each_value do |v|
|
7
|
+
xml_idx_to_find.push v
|
8
|
+
end
|
9
|
+
xml_idx_to_find = xml_idx_to_find.flatten
|
10
|
+
|
11
|
+
tag.split(".")[1..-1].each do |tag_i|
|
12
|
+
outer_idx = xml_idx_to_find
|
13
|
+
osize=outer_idx.size/2 -1
|
14
|
+
x_curr=[]
|
15
|
+
xml_motor.xmltags[tag_i].each_value do |v|
|
16
|
+
x_curr.push v
|
17
|
+
end
|
18
|
+
x_curr = x_curr.flatten
|
19
|
+
xsize=x_curr.size/2 -1
|
20
|
+
|
21
|
+
xml_idx_to_find = expand_node_indexes outer_idx, osize, x_curr, xsize
|
22
|
+
end
|
23
|
+
xml_idx_to_find
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.expand_node_indexes(outer_idx, osize, x_curr, xsize)
|
27
|
+
exapnded_node_indexes = []
|
28
|
+
0.upto osize do |o|
|
29
|
+
o1=outer_idx[o*2]
|
30
|
+
o2=outer_idx[o*2 +1]
|
31
|
+
0.upto xsize do |x|
|
32
|
+
x1=x_curr[x*2]
|
33
|
+
x2=x_curr[x*2 +1]
|
34
|
+
unless o1>x1 or o2<x2
|
35
|
+
exapnded_node_indexes.push x1
|
36
|
+
exapnded_node_indexes.push x2
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
exapnded_node_indexes.flatten
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module XMLChopper
|
45
|
+
def self.get_tag_attrib_value(tag_value)
|
46
|
+
tag_value_split = tag_value.split(/>/)
|
47
|
+
in_tag = tag_value_split.first
|
48
|
+
out_tag = tag_value_split[1..-1].join
|
49
|
+
in_tag_split = in_tag.split(/[ \t]/)
|
50
|
+
tag_name = in_tag_split.first
|
51
|
+
attribzone = in_tag_split[1..-1].flatten.join(' ')
|
52
|
+
attrs = get_attribute_hash attribzone
|
53
|
+
[[tag_name,attrs],out_tag]
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.get_attribute_hash(attribzone)
|
57
|
+
attribzone = attribzone.strip unless attribzone.nil?
|
58
|
+
return nil if attribzone.nil? or attribzone==""
|
59
|
+
attrs = {}
|
60
|
+
broken_attrib = attribzone.split(/=/)
|
61
|
+
attribs = broken_attrib.first.strip
|
62
|
+
values = nil
|
63
|
+
broken_attrib[1..-2].each do |attrib_part|
|
64
|
+
value_n_attrib = attrib_part.split(/[ \t]/)
|
65
|
+
values = value_n_attrib[0..-2].join(' ')
|
66
|
+
attrs[attribs] = values
|
67
|
+
attribs = value_n_attrib[-1].strip
|
68
|
+
end
|
69
|
+
values = broken_attrib.last.strip
|
70
|
+
attrs[attribs] = values
|
71
|
+
attrs
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
module XMLJoiner
|
76
|
+
def self.dejavu_attributes(attrib_hash)
|
77
|
+
return nil if attrib_hash.nil?
|
78
|
+
attributes=""
|
79
|
+
attrib_hash.each_key do |hash_key|
|
80
|
+
attributes += " " + hash_key + "=" + attrib_hash[hash_key]
|
81
|
+
end
|
82
|
+
attributes
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# main class
|
88
|
+
##
|
89
|
+
|
90
|
+
module XMLMotorHandler
|
91
|
+
def self._splitter_(xmldata)
|
92
|
+
@xmlnodes=[xmldata.split(/</)[0]]
|
93
|
+
xmldata.split(/</)[1..-1].each do |x1|
|
94
|
+
@xmlnodes.push XMLChopper.get_tag_attrib_value(x1)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def self._indexify_
|
99
|
+
@xmltags = {}
|
100
|
+
idx = 1
|
101
|
+
depth = 0
|
102
|
+
@xmlnodes[1..-1].each do |xnode|
|
103
|
+
tag_name = xnode[0][0]
|
104
|
+
unless tag_name.match(/^\/.*/) then
|
105
|
+
@xmltags[tag_name] ||= {}
|
106
|
+
@xmltags[tag_name][depth] ||= []
|
107
|
+
@xmltags[tag_name][depth].push idx
|
108
|
+
depth += 1
|
109
|
+
else
|
110
|
+
depth -= 1
|
111
|
+
@xmltags[tag_name[1..-1]][depth] ||= []
|
112
|
+
@xmltags[tag_name[1..-1]][depth].push idx
|
113
|
+
end
|
114
|
+
idx +=1
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def self._grab_my_node_ (xml_to_find, attrib_to_find=nil)
|
119
|
+
unless attrib_to_find.nil?
|
120
|
+
attrib_key = attrib_to_find.split(/=/)[0].strip
|
121
|
+
attrib_val = attrib_to_find.split(/=/)[1..-1].join.strip
|
122
|
+
end
|
123
|
+
nodes = []
|
124
|
+
node_count = xml_to_find.size/2 -1
|
125
|
+
0.upto node_count do |ncount|
|
126
|
+
node_start = xml_to_find[ncount*2]
|
127
|
+
node_stop = xml_to_find[ncount*2 +1]
|
128
|
+
unless attrib_to_find.nil? or @xmlnodes[node_start][0][1].nil?
|
129
|
+
next unless @xmlnodes[node_start][0][1][attrib_key] == attrib_val
|
130
|
+
end
|
131
|
+
nodes[ncount] ||= ""
|
132
|
+
nodes[ncount] += @xmlnodes[node_start][1] unless @xmlnodes[node_start][1].nil?
|
133
|
+
(node_start+1).upto (node_stop-1) do |node_idx|
|
134
|
+
any_attrib ||= ""
|
135
|
+
any_attrib = XMLJoiner.dejavu_attributes(@xmlnodes[node_idx][0][1]).to_s unless @xmlnodes[node_idx][0][1].nil?
|
136
|
+
nodes[ncount] += "<" + @xmlnodes[node_idx][0][0] + any_attrib + ">"
|
137
|
+
nodes[ncount] += @xmlnodes[node_idx][1] unless @xmlnodes[node_idx][1].nil?
|
138
|
+
end
|
139
|
+
end
|
140
|
+
nodes.delete(nil) unless attrib_to_find.nil?
|
141
|
+
nodes
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.xml_handler(xmldata, tag_to_find=nil, attrib_to_find=nil)
|
145
|
+
_splitter_ xmldata
|
146
|
+
_indexify_
|
147
|
+
my_nodes = nil
|
148
|
+
if attrib_to_find.nil?
|
149
|
+
xml_to_find = XMLIndexHandler.get_node_indexes self, tag_to_find
|
150
|
+
my_nodes = _grab_my_node_ xml_to_find
|
151
|
+
elsif tag_to_find.nil?
|
152
|
+
elsif !attrib_to_find.nil? and !tag_to_find.nil?
|
153
|
+
xml_to_find = XMLIndexHandler.get_node_indexes self, tag_to_find
|
154
|
+
my_nodes = _grab_my_node_ xml_to_find, attrib_to_find
|
155
|
+
end
|
156
|
+
my_nodes
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.xmlnodes
|
160
|
+
@xmlnodes
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.xmltags
|
164
|
+
@xmltags
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
##
|
169
|
+
# XMLMotor_EXECUTIONER ;)
|
170
|
+
##
|
171
|
+
|
172
|
+
module XMLMotor
|
173
|
+
def self.get_node_from_file(file, my_tag=nil, my_attrib=nil)
|
174
|
+
unless file.nil?
|
175
|
+
if File.readable? file
|
176
|
+
begin
|
177
|
+
return XMLMotorHandler.xml_handler File.read(file), my_tag, my_attrib
|
178
|
+
rescue
|
179
|
+
puts "Error: problem parsing File Content"
|
180
|
+
end
|
181
|
+
else
|
182
|
+
puts "Error: #{file} is not readable."
|
183
|
+
end
|
184
|
+
end
|
185
|
+
return ""
|
186
|
+
end
|
187
|
+
|
188
|
+
def self.get_node_from_content(content, my_tag=nil, my_attrib=nil)
|
189
|
+
unless content.nil?
|
190
|
+
begin
|
191
|
+
return XMLMotorHandler.xml_handler content, my_tag, my_attrib
|
192
|
+
rescue
|
193
|
+
puts "Error problem parsing String Content #{content}"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
return ""
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
data/make_my_gem.sh
ADDED
data/xml-motor.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "xml-motor/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "xml-motor"
|
7
|
+
s.version = Xml::Motor::VERSION
|
8
|
+
s.authors = ["abhishekkr"]
|
9
|
+
s.email = ["abhishk@thoughtworks.com"]
|
10
|
+
s.homepage = "http://github.com/abhishekkr/rubygem_xml_motor"
|
11
|
+
s.summary = %q{An easy-to-use XML Parser without any Native Dependencies}
|
12
|
+
s.description = %q{A new short XML Parsing Algorithm implemented directly in >500 lines. An easy-to-use XML Parser without any Native Dependencies.
|
13
|
+
[How To Use]:
|
14
|
+
|
15
|
+
Loading:
|
16
|
+
+ $ gem install xml-motor
|
17
|
+
+ 'require' the 'xml-motor'
|
18
|
+
|
19
|
+
Usage:
|
20
|
+
+ To find values of an xml node from an xml file
|
21
|
+
XMLMotor.get_node_from_file <file_with_path>, <node>
|
22
|
+
+ To find values of an xml node from an xml string
|
23
|
+
XMLMotor.get_node_from_content <xml_string>, <node>
|
24
|
+
+ To find values of an xml node with a tag_name having required attribute
|
25
|
+
XMLMotor.get_node_from_content <xml_string>, <node>, "<attrib_key>=<attrib_value>"
|
26
|
+
|
27
|
+
Example Calls As Code:
|
28
|
+
+ XMLMotor.get_node_from_content "<A>a</A><B><A>ba</A></B>", "A"
|
29
|
+
RETURNS: ["a", "ba"]
|
30
|
+
+ XMLMotor.get_node_from_content "<A>a</A><B><A>ba</A></B>", "B.A"
|
31
|
+
RETURNS: ["ba"]
|
32
|
+
+ XMLMotor.get_node_from_content "<A i='1'>a</A><B><A i='2'>ba</A></B>", "A", "i='1'"
|
33
|
+
RETURNS: ["a"]
|
34
|
+
}
|
35
|
+
|
36
|
+
s.rubyforge_project = "xml-motor"
|
37
|
+
|
38
|
+
s.files = `git ls-files`.split("\n")
|
39
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
40
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
|
43
|
+
# specify any dependencies here; for example:
|
44
|
+
# s.add_development_dependency "rspec"
|
45
|
+
# s.add_runtime_dependency "rest-client"
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml-motor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- abhishekkr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-06 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "A new short XML Parsing Algorithm implemented directly in >500 lines.
|
15
|
+
An easy-to-use XML Parser without any Native Dependencies.\n [How To Use]:\n\n
|
16
|
+
\ Loading:\n + $ gem install xml-motor\n + 'require' the 'xml-motor'\n\n
|
17
|
+
\ Usage:\n + To find values of an xml node from an xml file\n XMLMotor.get_node_from_file
|
18
|
+
<file_with_path>, <node>\n + To find values of an xml node from an xml string\n
|
19
|
+
\ XMLMotor.get_node_from_content <xml_string>, <node>\n + To find
|
20
|
+
values of an xml node with a tag_name having required attribute\n XMLMotor.get_node_from_content
|
21
|
+
<xml_string>, <node>, \"<attrib_key>=<attrib_value>\"\n\n Example Calls As
|
22
|
+
Code:\n + XMLMotor.get_node_from_content \"<A>a</A><B><A>ba</A></B>\", \"A\"\n
|
23
|
+
\ RETURNS: [\"a\", \"ba\"]\n + XMLMotor.get_node_from_content
|
24
|
+
\"<A>a</A><B><A>ba</A></B>\", \"B.A\"\n RETURNS: [\"ba\"]\n +
|
25
|
+
XMLMotor.get_node_from_content \"<A i='1'>a</A><B><A i='2'>ba</A></B>\", \"A\",
|
26
|
+
\"i='1'\"\n RETURNS: [\"a\"]\n "
|
27
|
+
email:
|
28
|
+
- abhishk@thoughtworks.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/xml-motor.rb
|
36
|
+
- lib/xml-motor/version.rb
|
37
|
+
- make_my_gem.sh
|
38
|
+
- xml-motor.gemspec
|
39
|
+
homepage: http://github.com/abhishekkr/rubygem_xml_motor
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project: xml-motor
|
59
|
+
rubygems_version: 1.8.11
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: An easy-to-use XML Parser without any Native Dependencies
|
63
|
+
test_files: []
|