xml-motor 0.0.4 → 0.0.5

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.
Files changed (3) hide show
  1. data/lib/xml-motor.rb +46 -31
  2. data/lib/xml-motor/version.rb +1 -1
  3. metadata +33 -42
@@ -1,39 +1,48 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ module XMLStdout
4
+ def self._err(mesag); puts "ERROR:: #{mesag}"; end
5
+ def self._nfo(mesag); puts "INFORMATION:: #{mesag}"; end
6
+ end
7
+
3
8
  module XMLIndexHandler
4
9
  def self.get_node_indexes(xml_motor, tag)
5
- xml_idx_to_find = []
10
+ xml_idx_to_find = []
11
+ begin
6
12
  xml_motor.xmltags[tag.split(".")[0]].each_value {|val| xml_idx_to_find.push val }
7
13
  xml_idx_to_find = xml_idx_to_find.flatten
8
14
 
9
15
  tag.split(".")[1..-1].each do |tag_i|
10
16
  outer_idx = xml_idx_to_find
11
- osize=outer_idx.size/2 -1
12
- x_curr=[]
17
+ x_curr = []
13
18
  xml_motor.xmltags[tag_i].each_value {|val| x_curr.push val }
14
19
  x_curr = x_curr.flatten
15
- xsize=x_curr.size/2 -1
16
20
 
17
- xml_idx_to_find = expand_node_indexes outer_idx, osize, x_curr, xsize
21
+ xml_idx_to_find = expand_node_indexes outer_idx, x_curr
18
22
  end
19
- xml_idx_to_find
23
+ rescue
24
+ XMLStdout._err "Finding index for tag:#{tag}.\nLook if it's actually present in the provided XML."
25
+ end
26
+ xml_idx_to_find
20
27
  end
21
28
 
22
- def self.expand_node_indexes(outer_idx, osize, x_curr, xsize)
23
- exapnded_node_indexes = []
29
+ def self.expand_node_indexes(outer_idx, x_curr)
30
+ osize = outer_idx.size/2 -1
31
+ xsize = x_curr.size/2 -1
32
+ expanded_node_indexes = []
24
33
  0.upto osize do |o|
25
- o1=outer_idx[o*2]
26
- o2=outer_idx[o*2 +1]
34
+ o1 = outer_idx[o*2]
35
+ o2 = outer_idx[o*2 +1]
27
36
  0.upto xsize do |x|
28
- x1=x_curr[x*2]
29
- x2=x_curr[x*2 +1]
37
+ x1 = x_curr[x*2]
38
+ x2 = x_curr[x*2 +1]
30
39
  unless o1>x1 or o2<x2
31
- exapnded_node_indexes.push x1
32
- exapnded_node_indexes.push x2
40
+ expanded_node_indexes.push x1
41
+ expanded_node_indexes.push x2
33
42
  end
34
43
  end
35
44
  end
36
- exapnded_node_indexes.flatten
45
+ expanded_node_indexes.flatten
37
46
  end
38
47
  end
39
48
 
@@ -71,7 +80,7 @@ end
71
80
  module XMLJoiner
72
81
  def self.dejavu_attributes(attrib_hash)
73
82
  return nil if attrib_hash.nil?
74
- attributes=""
83
+ attributes = ""
75
84
  attrib_hash.each_key do |hash_key|
76
85
  attributes += " " + hash_key + "=" + attrib_hash[hash_key]
77
86
  end
@@ -84,10 +93,11 @@ end
84
93
 
85
94
  module XMLMotorEngine
86
95
  def self._splitter_(xmldata)
87
- @xmlnodes=[xmldata.split(/</)[0]]
96
+ @xmlnodes = [xmldata.split(/</)[0]]
88
97
  xmldata.split(/</)[1..-1].each do |x1|
89
98
  @xmlnodes.push XMLChopper.get_tag_attrib_value(x1)
90
99
  end
100
+ @xmlnodes
91
101
  end
92
102
 
93
103
  def self._indexify_(_nodes=nil)
@@ -96,7 +106,7 @@ module XMLMotorEngine
96
106
  idx = 1
97
107
  depth = 0
98
108
  @xmlnodes[1..-1].each do |xnode|
99
- tag_name = xnode[0][0]
109
+ tag_name = xnode[0][0].strip
100
110
  unless tag_name.match(/^\/.*/) then
101
111
  @xmltags[tag_name] ||= {}
102
112
  @xmltags[tag_name][depth] ||= []
@@ -109,6 +119,7 @@ module XMLMotorEngine
109
119
  end
110
120
  idx +=1
111
121
  end
122
+ @xmltags
112
123
  end
113
124
 
114
125
  def self._grab_my_node_ (xml_to_find, attrib_to_find=nil)
@@ -129,7 +140,7 @@ module XMLMotorEngine
129
140
  nodes[ncount] += @xmlnodes[node_start][1] unless @xmlnodes[node_start][1].nil?
130
141
  (node_start+1).upto (node_stop-1) do |node_idx|
131
142
  any_attrib ||= ""
132
- any_attrib = XMLJoiner.dejavu_attributes(@xmlnodes[node_idx][0][1]).to_s unless @xmlnodes[node_idx][0][1].nil?
143
+ any_attrib = XMLJoiner.dejavu_attributes(@xmlnodes[node_idx][0][1]).to_s unless @xmlnodes[node_idx][0][1].nil?
133
144
  nodes[ncount] += "<" + @xmlnodes[node_idx][0][0] + any_attrib + ">"
134
145
  nodes[ncount] += @xmlnodes[node_idx][1] unless @xmlnodes[node_idx][1].nil?
135
146
  end
@@ -140,14 +151,15 @@ module XMLMotorEngine
140
151
 
141
152
  def self.xml_extracter(tag_to_find=nil, attrib_to_find=nil)
142
153
  my_nodes = nil
143
- if attrib_to_find.nil?
154
+ if attrib_to_find.nil? and tag_to_find.nil?
155
+ elsif attrib_to_find.nil?
144
156
  xml_to_find = XMLIndexHandler.get_node_indexes self, tag_to_find
145
157
  my_nodes = _grab_my_node_ xml_to_find
146
158
  elsif tag_to_find.nil?
147
159
  #
148
- #just attrib-based search to come
160
+ XMLStdout._nfo "Just attrib-based search to come"
149
161
  #
150
- elsif !attrib_to_find.nil? and !tag_to_find.nil?
162
+ else
151
163
  xml_to_find = XMLIndexHandler.get_node_indexes self, tag_to_find
152
164
  my_nodes = _grab_my_node_ xml_to_find, attrib_to_find
153
165
  end
@@ -155,17 +167,20 @@ module XMLMotorEngine
155
167
  end
156
168
 
157
169
  def self.xml_miner(xmldata, tag_to_find=nil, attrib_to_find=nil)
170
+ return nil if xmldata.nil?
158
171
  _splitter_ xmldata
159
172
  _indexify_
160
173
  xml_extracter tag_to_find, attrib_to_find
161
174
  end
162
175
 
163
- def self.xmlnodes(xmlnodes=nil)
164
- @xmlnodes ||= xmlnodes
176
+ def self.xmlnodes(xml_nodes=nil)
177
+ @xmlnodes = xml_nodes unless xml_nodes.nil?
178
+ @xmlnodes
165
179
  end
166
180
 
167
- def self.xmltags(xmltags=nil)
168
- @xmltags ||= xmltags
181
+ def self.xmltags(xml_tags=nil)
182
+ @xmltags = xml_tags unless xml_tags.nil?
183
+ @xmltags
169
184
  end
170
185
 
171
186
  def self.pre_processed_content(_nodes, _tags=nil, tag_to_find=nil, attrib_to_find=nil)
@@ -174,13 +189,13 @@ module XMLMotorEngine
174
189
  unless _tags.nil?
175
190
  xmltags _tags
176
191
  else
177
- _indexify_ _nodes
192
+ _indexify_
178
193
  end
179
194
  return xml_extracter tag_to_find, attrib_to_find
180
195
  rescue
181
- puts "Error problem parsing processed XML Nodes."
196
+ XMLStdout._err "Parsing processed XML Nodes."
182
197
  end
183
- return ""
198
+ return nil
184
199
  end
185
200
  end
186
201
 
@@ -192,7 +207,7 @@ module XMLMotor
192
207
  begin
193
208
  return get_node_from_content(File.read(file.to_s), my_tag, my_attrib) if File.readable? file.to_s
194
209
  rescue
195
- puts "Error: #{file} is not readable."
210
+ XMLStdout._err "#{file} is not readable."
196
211
  end
197
212
  return ""
198
213
  end
@@ -201,7 +216,7 @@ module XMLMotor
201
216
  begin
202
217
  return XMLMotorEngine.xml_miner content, my_tag, my_attrib unless content.nil?
203
218
  rescue
204
- puts "Error problem parsing String Content #{content}"
219
+ XMLStdout._err "Parsing String Content #{content}"
205
220
  end
206
221
  return ""
207
222
  end
@@ -1,5 +1,5 @@
1
1
  module Xml
2
2
  module Motor
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,40 +1,28 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: xml-motor
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - abhishekkr
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2011-12-12 00:00:00.000000000Z
11
+
12
+ date: 2011-12-25 00:00:00 +05:30
13
+ default_executable:
13
14
  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 \n [How
16
- To Use]: https://github.com/abhishekkr/rubygem_xml_motor/raw/master/README \n\n
17
- \ Loading:\n + $ gem install xml-motor\n + 'require' the 'xml-motor'\n\n
18
- \ Usage:\n [[ To Search Just One QUERY ]]\n nodes_array
19
- = XMLMotor.get_node_from_file \"_XML_FILE_\"\n nodes_array = XMLMotor.get_node_from_file
20
- \"_XML_FILE_\", \"ATTRIB_KEY=ATTRIB_VALUE\"\n nodes_array = XMLMotor.get_node_from_content
21
- \"_XML_DATA_\"\n nodes_array = XMLMotor.get_node_from_content \"_XML_DATA_\",
22
- \"ATTRIB_KEY=ATTRIB_VALUE\"\n [[ To Search More Than One QUERIES ]]\n
23
- \ str = {XML_DATA}\n nodes_ = XMLMotorEngine._splitter_ str\n
24
- \ tags_ = XMLMotorEngine._indexify_ nodes_\n nodes_array
25
- = XMLMotorEngine.pre_processed_content nodes_, tags_, \"_TAG_\"\n nodes_array
26
- = XMLMotorEngine.pre_processed_content nodes_, tags_, \"_TAG_\", \"ATTRIB_KEY=ATTRIB_VALUE\"\n\n
27
- \ Example Calls As Code:\n + XMLMotor.get_node_from_content \"<A>a</A><B><A>ba</A></B>\",
28
- \"A\"\n RETURNS: [\"a\", \"ba\"]\n + XMLMotor.get_node_from_content
29
- \"<A>a</A><B><A>ba</A></B>\", \"B.A\"\n RETURNS: [\"ba\"]\n +
30
- XMLMotor.get_node_from_content \"<A i='1'>a</A><B><A i='2'>ba</A></B>\", \"A\",
31
- \"i='1'\"\n RETURNS: [\"a\"]\n "
32
- email:
15
+
16
+ description: "A new short XML Parsing Algorithm implemented directly in >500 lines. An easy-to-use XML Parser without any Native Dependencies. [How To Use]: https://github.com/abhishekkr/rubygem_xml_motor/raw/master/README Loading: + $ gem install xml-motor + 'require' the 'xml-motor' Usage: [[ To Search Just One QUERY ]] nodes_array = XMLMotor.get_node_from_file \"_XML_FILE_\" nodes_array = XMLMotor.get_node_from_file \"_XML_FILE_\", \"ATTRIB_KEY=ATTRIB_VALUE\" nodes_array = XMLMotor.get_node_from_content \"_XML_DATA_\" nodes_array = XMLMotor.get_node_from_content \"_XML_DATA_\", \"ATTRIB_KEY=ATTRIB_VALUE\" [[ To Search More Than One QUERIES ]] str = {XML_DATA} nodes_ = XMLMotorEngine._splitter_ str tags_ = XMLMotorEngine._indexify_ nodes_ nodes_array = XMLMotorEngine.pre_processed_content nodes_, tags_, \"_TAG_\" nodes_array = XMLMotorEngine.pre_processed_content nodes_, tags_, \"_TAG_\", \"ATTRIB_KEY=ATTRIB_VALUE\" Example Calls As Code: + XMLMotor.get_node_from_content \"<A>a</A><B><A>ba</A></B>\", \"A\" RETURNS: [\"a\", \"ba\"] + XMLMotor.get_node_from_content \"<A>a</A><B><A>ba</A></B>\", \"B.A\" RETURNS: [\"ba\"] + XMLMotor.get_node_from_content \"<A i='1'>a</A><B><A i='2'>ba</A></B>\", \"A\", \"i='1'\" RETURNS: [\"a\"]"
17
+ email:
33
18
  - abhishk@thoughtworks.com
34
19
  executables: []
20
+
35
21
  extensions: []
22
+
36
23
  extra_rdoc_files: []
37
- files:
24
+
25
+ files:
38
26
  - Gemfile
39
27
  - README
40
28
  - Rakefile
@@ -42,28 +30,31 @@ files:
42
30
  - lib/xml-motor/version.rb
43
31
  - make_my_gem.sh
44
32
  - xml-motor.gemspec
33
+ has_rdoc: false
45
34
  homepage: http://github.com/abhishekkr/rubygem_xml_motor
46
- licenses: []
47
35
  post_install_message:
48
36
  rdoc_options: []
49
- require_paths:
37
+
38
+ require_paths:
50
39
  - lib
51
- required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ! '>='
55
- - !ruby/object:Gem::Version
56
- version: '0'
57
- required_rubygems_version: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ! '>='
61
- - !ruby/object:Gem::Version
62
- version: '0'
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
63
52
  requirements: []
53
+
64
54
  rubyforge_project: xml-motor
65
- rubygems_version: 1.8.11
55
+ rubygems_version: 1.3.1
66
56
  signing_key:
67
- specification_version: 3
57
+ specification_version: 2
68
58
  summary: An easy-to-use XML Parser without any Native Dependencies
69
59
  test_files: []
60
+