occi 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/.gitignore +9 -0
  2. data/.project +32 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +8 -0
  5. data/.yardopts +1 -0
  6. data/AUTHORS +8 -0
  7. data/Gemfile +35 -14
  8. data/Gemfile.lock +89 -32
  9. data/LICENSE +13 -4
  10. data/README.md +70 -21
  11. data/Rakefile +25 -21
  12. data/lib/OpenNebula/Acl.rb +256 -0
  13. data/lib/OpenNebula/AclPool.rb +53 -0
  14. data/lib/OpenNebula/Group.rb +147 -0
  15. data/lib/OpenNebula/GroupPool.rb +54 -0
  16. data/lib/OpenNebula/Host.rb +143 -0
  17. data/lib/OpenNebula/HostPool.rb +55 -0
  18. data/lib/OpenNebula/Image.rb +256 -0
  19. data/lib/OpenNebula/ImagePool.rb +74 -0
  20. data/lib/OpenNebula/OpenNebula.rb +137 -0
  21. data/lib/OpenNebula/Pool.rb +285 -0
  22. data/lib/OpenNebula/Template.rb +173 -0
  23. data/lib/OpenNebula/TemplatePool.rb +74 -0
  24. data/lib/OpenNebula/User.rb +157 -0
  25. data/lib/OpenNebula/UserPool.rb +53 -0
  26. data/lib/OpenNebula/VirtualMachine.rb +319 -0
  27. data/lib/OpenNebula/VirtualMachinePool.rb +120 -0
  28. data/lib/OpenNebula/VirtualNetwork.rb +229 -0
  29. data/lib/OpenNebula/VirtualNetworkPool.rb +74 -0
  30. data/lib/OpenNebula/XMLUtils.rb +337 -0
  31. data/lib/occi/antlr/.gitignore +18 -0
  32. data/lib/occi/antlr/OCCI.g +164 -0
  33. data/lib/occi/antlr/OCCI.tokens +75 -0
  34. data/lib/occi/antlr/OCCILexer.rb +1532 -0
  35. data/lib/occi/antlr/OCCIParser.rb +2472 -0
  36. data/lib/occi/antlr/README.md +2 -0
  37. data/lib/occi/backend/dummy.rb +232 -0
  38. data/lib/occi/backend/ec2/Compute.rb +310 -0
  39. data/lib/occi/backend/ec2/compute.rb +310 -0
  40. data/lib/occi/backend/ec2/ec2.rb +215 -0
  41. data/lib/occi/backend/manager.rb +131 -0
  42. data/lib/occi/backend/opennebula/compute.rb +360 -0
  43. data/lib/occi/backend/opennebula/network.rb +143 -0
  44. data/lib/occi/backend/opennebula/opennebula.rb +188 -0
  45. data/lib/occi/backend/opennebula/storage.rb +175 -0
  46. data/lib/occi/configuration.rb +118 -0
  47. data/lib/occi/core/action.rb +29 -0
  48. data/lib/occi/core/attribute_properties.rb +54 -0
  49. data/lib/occi/core/attributes.rb +40 -0
  50. data/lib/occi/core/category.rb +62 -0
  51. data/lib/occi/core/collection.rb +27 -0
  52. data/lib/occi/core/entity.rb +135 -0
  53. data/lib/occi/core/kind.rb +55 -0
  54. data/lib/occi/core/link.rb +88 -0
  55. data/lib/occi/core/mixin.rb +43 -0
  56. data/lib/occi/core/resource.rb +73 -0
  57. data/lib/occi/exceptions.rb +59 -0
  58. data/lib/occi/extensions/monitoring/cpu.rb +51 -0
  59. data/lib/occi/extensions/monitoring/memory.rb +49 -0
  60. data/lib/occi/extensions/monitoring/metric.rb +54 -0
  61. data/lib/occi/extensions/monitoring/netrx.rb +49 -0
  62. data/lib/occi/extensions/monitoring/nettx.rb +55 -0
  63. data/lib/occi/extensions/one/VNC.rb +58 -0
  64. data/lib/occi/extensions/one/vnc.rb +58 -0
  65. data/lib/occi/log.rb +47 -0
  66. data/lib/occi/parse.rb +164 -0
  67. data/lib/occi/registry.rb +87 -0
  68. data/lib/occi/server.rb +594 -0
  69. data/lib/occi/version.rb +3 -0
  70. data/occi.gemspec +18 -96
  71. data/spec/occi/antlr/parser_spec.rb +82 -0
  72. data/spec/spec_helper.rb +6 -0
  73. metadata +100 -208
  74. data/.autotest +0 -21
  75. data/.rvmrc +0 -1
  76. data/VERSION +0 -1
  77. data/lib/occi.rb +0 -1
  78. data/lib/occi/client.rb +0 -36
  79. data/lib/occi/compute.rb +0 -4
  80. data/lib/occi/network.rb +0 -4
  81. data/lib/occi/resource.rb +0 -59
  82. data/lib/occi/storage.rb +0 -7
  83. data/test/fixtures/cassettes/compute_all.yml +0 -28
  84. data/test/fixtures/cassettes/compute_create.yml +0 -45
  85. data/test/fixtures/cassettes/compute_destroy.yml +0 -22
  86. data/test/fixtures/cassettes/compute_find.yml +0 -28
  87. data/test/fixtures/cassettes/compute_update.yml +0 -35
  88. data/test/fixtures/cassettes/network_all.yml +0 -28
  89. data/test/fixtures/cassettes/network_create.yml +0 -37
  90. data/test/fixtures/cassettes/network_destroy.yml +0 -22
  91. data/test/fixtures/cassettes/network_find.yml +0 -28
  92. data/test/fixtures/cassettes/storage_all.yml +0 -28
  93. data/test/fixtures/cassettes/storage_create.yml +0 -50
  94. data/test/fixtures/cassettes/storage_destroy.yml +0 -22
  95. data/test/fixtures/cassettes/storage_find.yml +0 -28
  96. data/test/lib/occi/compute_test.rb +0 -91
  97. data/test/lib/occi/network_test.rb +0 -57
  98. data/test/lib/occi/storage_test.rb +0 -67
  99. data/test/test_helper.rb +0 -48
@@ -0,0 +1,120 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class VirtualMachinePool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ VM_POOL_METHODS = {
28
+ :info => "vmpool.info"
29
+ }
30
+
31
+ # Constants for info queries (include/RequestManagerPoolInfoFilter.h)
32
+ INFO_NOT_DONE = -1
33
+ INFO_ALL_VM = -2
34
+
35
+ #######################################################################
36
+ # Class constructor & Pool Methods
37
+ #######################################################################
38
+
39
+
40
+ # +client+ a Client object that represents a XML-RPC connection
41
+ # +user_id+ is to refer to a Pool with VirtualMachines from that user
42
+ def initialize(client, user_id=0)
43
+ super('VM_POOL','VM',client)
44
+
45
+ @user_id = user_id
46
+ end
47
+
48
+ # Default Factory Method for the Pools
49
+ def factory(element_xml)
50
+ OpenNebula::VirtualMachine.new(element_xml,@client)
51
+ end
52
+
53
+ #######################################################################
54
+ # XML-RPC Methods for the Virtual Network Object
55
+ #######################################################################
56
+
57
+ # Retrieves all or part of the VirtualMachines in the pool.
58
+ # No arguments, returns the not-in-done VMs for the user
59
+ # [user_id, start_id, end_id]
60
+ # [user_id, start_id, end_id, state]
61
+ def info(*args)
62
+ case args.size
63
+ when 0
64
+ info_filter(VM_POOL_METHODS[:info],
65
+ @user_id,
66
+ -1,
67
+ -1,
68
+ INFO_NOT_DONE)
69
+ when 1
70
+ info_filter(VM_POOL_METHODS[:info],
71
+ args[0],
72
+ -1,
73
+ -1,
74
+ INFO_NOT_DONE)
75
+ when 3
76
+ info_filter(VM_POOL_METHODS[:info],
77
+ args[0],
78
+ args[1],
79
+ args[2],
80
+ INFO_NOT_DONE)
81
+ when 4
82
+ info_filter(VM_POOL_METHODS[:info],
83
+ args[0],
84
+ args[1],
85
+ args[2],
86
+ args[3])
87
+ end
88
+ end
89
+
90
+ def info_all()
91
+ return info_filter(VM_POOL_METHODS[:info],
92
+ INFO_ALL,
93
+ -1,
94
+ -1,
95
+ INFO_NOT_DONE)
96
+ end
97
+
98
+ def info_mine()
99
+ return info_filter(VM_POOL_METHODS[:info],
100
+ INFO_MINE,
101
+ -1,
102
+ -1,
103
+ INFO_NOT_DONE)
104
+ end
105
+
106
+ def info_group()
107
+ return info_filter(VM_POOL_METHODS[:info],
108
+ INFO_GROUP,
109
+ -1,
110
+ -1,
111
+ INFO_NOT_DONE)
112
+ end
113
+
114
+ private
115
+
116
+ def info_filter(xml_method, who, start_id, end_id, state)
117
+ return xmlrpc_info(xml_method, who, start_id, end_id, state)
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,229 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class VirtualNetwork < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+
27
+ VN_METHODS = {
28
+ :info => "vn.info",
29
+ :allocate => "vn.allocate",
30
+ :publish => "vn.publish",
31
+ :delete => "vn.delete",
32
+ :addleases => "vn.addleases",
33
+ :rmleases => "vn.rmleases",
34
+ :chown => "vn.chown",
35
+ :chmod => "vn.chmod",
36
+ :update => "vn.update",
37
+ :hold => "vn.hold",
38
+ :release => "vn.release"
39
+ }
40
+
41
+ VN_TYPES=%w{RANGED FIXED}
42
+
43
+ SHORT_VN_TYPES={
44
+ "RANGED" => "R",
45
+ "FIXED" => "F"
46
+ }
47
+
48
+ # Creates a VirtualNetwork description with just its identifier
49
+ # this method should be used to create plain VirtualNetwork objects.
50
+ # +id+ the id of the network
51
+ #
52
+ # Example:
53
+ # vnet = VirtualNetwork.new(VirtualNetwork.build_xml(3),rpc_client)
54
+ #
55
+ def VirtualNetwork.build_xml(pe_id=nil)
56
+ if pe_id
57
+ vn_xml = "<VNET><ID>#{pe_id}</ID></VNET>"
58
+ else
59
+ vn_xml = "<VNET></VNET>"
60
+ end
61
+
62
+ XMLElement.build_xml(vn_xml, 'VNET')
63
+ end
64
+
65
+ # Class constructor
66
+ def initialize(xml, client)
67
+ super(xml,client)
68
+ end
69
+
70
+ #######################################################################
71
+ # XML-RPC Methods for the Virtual Network Object
72
+ #######################################################################
73
+
74
+ # Retrieves the information of the given VirtualNetwork.
75
+ def info()
76
+ super(VN_METHODS[:info], 'VNET')
77
+ end
78
+
79
+ # Allocates a new VirtualNetwork in OpenNebula
80
+ #
81
+ # +description+ A string containing the template of the VirtualNetwork.
82
+ def allocate(description)
83
+ super(VN_METHODS[:allocate],description)
84
+ end
85
+
86
+ # Replaces the template contents
87
+ #
88
+ # +new_template+ New template contents
89
+ def update(new_template)
90
+ super(VN_METHODS[:update], new_template)
91
+ end
92
+
93
+ # Publishes the VirtualNetwork, to be used by other users
94
+ def publish
95
+ set_publish(true)
96
+ end
97
+
98
+ # Unplubishes the VirtualNetwork
99
+ def unpublish
100
+ set_publish(false)
101
+ end
102
+
103
+ # Deletes the VirtualNetwork
104
+ def delete()
105
+ super(VN_METHODS[:delete])
106
+ end
107
+
108
+ # Adds a lease to the VirtualNetwork
109
+ def addleases(ip, mac = nil)
110
+ return Error.new('ID not defined') if !@pe_id
111
+
112
+ lease_template = "LEASES = [ IP = #{ip}"
113
+ lease_template << ", MAC = #{mac}" if mac
114
+ lease_template << " ]"
115
+
116
+ rc = @client.call(VN_METHODS[:addleases], @pe_id, lease_template)
117
+ rc = nil if !OpenNebula.is_error?(rc)
118
+
119
+ return rc
120
+ end
121
+
122
+ # Removes a lease from the VirtualNetwork
123
+ def rmleases(ip)
124
+ return Error.new('ID not defined') if !@pe_id
125
+
126
+ lease_template = "LEASES = [ IP = #{ip} ]"
127
+
128
+ rc = @client.call(VN_METHODS[:rmleases], @pe_id, lease_template)
129
+ rc = nil if !OpenNebula.is_error?(rc)
130
+
131
+ return rc
132
+ end
133
+
134
+ # Holds a virtual network Lease as used
135
+ # @param ip [String] IP to hold
136
+ def hold(ip)
137
+ return Error.new('ID not defined') if !@pe_id
138
+
139
+ lease_template = "LEASES = [ IP = #{ip} ]"
140
+
141
+ rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template)
142
+ rc = nil if !OpenNebula.is_error?(rc)
143
+
144
+ return rc
145
+ end
146
+
147
+ # Releases a virtual network Lease on hold
148
+ # @param ip [String] IP to release
149
+ def release(ip)
150
+ return Error.new('ID not defined') if !@pe_id
151
+
152
+ lease_template = "LEASES = [ IP = #{ip} ]"
153
+
154
+ rc = @client.call(VN_METHODS[:release], @pe_id, lease_template)
155
+ rc = nil if !OpenNebula.is_error?(rc)
156
+
157
+ return rc
158
+ end
159
+
160
+ # Changes the owner/group
161
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
162
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
163
+ # [return] nil in case of success or an Error object
164
+ def chown(uid, gid)
165
+ super(VN_METHODS[:chown], uid, gid)
166
+ end
167
+
168
+ # Changes the virtual network permissions.
169
+ #
170
+ # @param octet [String] Permissions octed , e.g. 640
171
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
172
+ # otherwise
173
+ def chmod_octet(octet)
174
+ super(VN_METHODS[:chmod], octet)
175
+ end
176
+
177
+ # Changes the virtual network permissions.
178
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
179
+ #
180
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
181
+ # otherwise
182
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
183
+ other_m, other_a)
184
+ super(VN_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
185
+ group_m, group_a, other_u, other_m, other_a)
186
+ end
187
+
188
+ #######################################################################
189
+ # Helpers to get VirtualNetwork information
190
+ #######################################################################
191
+
192
+ # Returns the group identifier
193
+ # [return] _Integer_ the element's group ID
194
+ def gid
195
+ self['GID'].to_i
196
+ end
197
+
198
+ # Returns the type of the Virtual Network (numeric value)
199
+ def type
200
+ self['TYPE'].to_i
201
+ end
202
+
203
+ # Returns the type of the Virtual Network (string value)
204
+ def type_str
205
+ VN_TYPES[type]
206
+ end
207
+
208
+ # Returns the state of the Virtual Network (string value)
209
+ def short_type_str
210
+ SHORT_VN_TYPES[type_str]
211
+ end
212
+
213
+ def public?
214
+ if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
215
+ true
216
+ else
217
+ false
218
+ end
219
+ end
220
+
221
+ private
222
+ def set_publish(published)
223
+ group_u = published ? 1 : 0
224
+
225
+ chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
226
+ end
227
+
228
+ end
229
+ end
@@ -0,0 +1,74 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class VirtualNetworkPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ VN_POOL_METHODS = {
28
+ :info => "vnpool.info"
29
+ }
30
+
31
+ #######################################################################
32
+ # Class constructor & Pool Methods
33
+ #######################################################################
34
+
35
+ # +client+ a Client object that represents a XML-RPC connection
36
+ # +user_id+ is to refer to a Pool with VirtualNetworks from that user
37
+ def initialize(client, user_id=0)
38
+ super('VNET_POOL','VNET',client)
39
+
40
+ @user_id = user_id
41
+ end
42
+
43
+ # Default Factory Method for the Pools
44
+ def factory(element_xml)
45
+ OpenNebula::VirtualNetwork.new(element_xml,@client)
46
+ end
47
+
48
+ #######################################################################
49
+ # XML-RPC Methods for the Virtual Network Object
50
+ #######################################################################
51
+
52
+ # Retrieves all or part of the VirtualMachines in the pool.
53
+ def info(*args)
54
+ case args.size
55
+ when 0
56
+ info_filter(VN_POOL_METHODS[:info],@user_id,-1,-1)
57
+ when 3
58
+ info_filter(VN_POOL_METHODS[:info],args[0],args[1],args[2])
59
+ end
60
+ end
61
+
62
+ def info_all()
63
+ return super(VN_POOL_METHODS[:info])
64
+ end
65
+
66
+ def info_mine()
67
+ return super(VN_POOL_METHODS[:info])
68
+ end
69
+
70
+ def info_group()
71
+ return super(VN_POOL_METHODS[:info])
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,337 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ module OpenNebula
19
+
20
+ begin
21
+ require 'nokogiri'
22
+ NOKOGIRI=true
23
+ rescue LoadError
24
+ NOKOGIRI=false
25
+ end
26
+
27
+ begin
28
+ require 'rexml/formatters/pretty'
29
+ REXML_FORMATTERS=true
30
+ rescue LoadError
31
+ REXML_FORMATTERS=false
32
+ end
33
+
34
+ # The XMLElement class provides an abstraction of the underlying
35
+ # XML parser engine. It provides XML-related methods for the Pool and
36
+ # PoolElement classes
37
+ class XMLElement
38
+
39
+ # xml:: _opaque xml object_ an xml object as returned by build_xml
40
+ def initialize(xml=nil)
41
+ @xml = xml
42
+ end
43
+
44
+ # Initialize a XML document for the element
45
+ # xml:: _String_ the XML document of the object
46
+ # root_element:: _String_ Base xml element
47
+ def initialize_xml(xml, root_element)
48
+ @xml = XMLElement.build_xml(xml, root_element)
49
+
50
+ if OpenNebula.is_error?(@xml)
51
+ @xml = nil
52
+ else
53
+ if NOKOGIRI
54
+ if @xml.size == 0
55
+ @xml = nil
56
+ end
57
+ else
58
+ if @xml.name != root_element
59
+ @xml = nil
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ # Builds a XML document
66
+ # xml:: _String_ the XML document of the object
67
+ # root_element:: _String_ Base xml element
68
+ # [return] _XML_ object for the underlying XML engine
69
+ def self.build_xml(xml, root_element)
70
+ begin
71
+ if NOKOGIRI
72
+ doc = Nokogiri::XML(xml).xpath("/#{root_element}")
73
+ else
74
+ doc = REXML::Document.new(xml).root
75
+ end
76
+ rescue Exception => e
77
+ return OpenNebula::Error.new(e.message)
78
+ end
79
+
80
+ return doc
81
+ end
82
+ # Extract an element from the XML description of the PoolElement.
83
+ # key::_String_ The name of the element
84
+ # [return] _String_ the value of the element
85
+ # Examples:
86
+ # ['VID'] # gets VM id
87
+ # ['HISTORY/HOSTNAME'] # get the hostname from the history
88
+ def [](key)
89
+ if NOKOGIRI
90
+ element=@xml.xpath(key.to_s)
91
+
92
+ if element.size == 0
93
+ return nil
94
+ end
95
+ else
96
+ element=@xml.elements[key.to_s]
97
+ end
98
+
99
+ if element
100
+ element.text
101
+ end
102
+ end
103
+
104
+ # Gets an array of text from elemenets extracted
105
+ # using the XPATH expression passed as filter
106
+ def retrieve_elements(filter)
107
+ elements_array = Array.new
108
+
109
+ if NOKOGIRI
110
+ @xml.xpath(filter.to_s).each { |pelem|
111
+ elements_array << pelem.text if pelem.text
112
+ }
113
+ else
114
+ @xml.elements.each(filter.to_s) { |pelem|
115
+ elements_array << pelem.text if pelem.text
116
+ }
117
+ end
118
+
119
+ if elements_array.size == 0
120
+ return nil
121
+ else
122
+ return elements_array
123
+ end
124
+
125
+ end
126
+
127
+ # Gets an attribute from an elemenT
128
+ # key:: _String_ xpath for the element
129
+ # name:: _String_ name of the attribute
130
+ def attr(key,name)
131
+ value = nil
132
+
133
+ if NOKOGIRI
134
+ element=@xml.xpath(key.to_s.upcase)
135
+ if element.size == 0
136
+ return nil
137
+ end
138
+
139
+ attribute = element.attr(name)
140
+
141
+ value = attribute.text if attribute != nil
142
+ else
143
+ element=@xml.elements[key.to_s.upcase]
144
+
145
+ value = element.attributes[name] if element != nil
146
+ end
147
+
148
+ return value
149
+ end
150
+
151
+ # Iterates over every Element in the XPath and calls the block with a
152
+ # a XMLElement
153
+ # block:: _Block_
154
+ def each(xpath_str,&block)
155
+ if NOKOGIRI
156
+ @xml.xpath(xpath_str).each { |pelem|
157
+ block.call XMLElement.new(pelem)
158
+ }
159
+ else
160
+ @xml.elements.each(xpath_str) { |pelem|
161
+ block.call XMLElement.new(pelem)
162
+ }
163
+ end
164
+ end
165
+
166
+ def each_xpath(xpath_str,&block)
167
+ if NOKOGIRI
168
+ @xml.xpath(xpath_str).each { |pelem|
169
+ block.call pelem.text
170
+ }
171
+ else
172
+ @xml.elements.each(xpath_str) { |pelem|
173
+ block.call pelem.text
174
+ }
175
+ end
176
+ end
177
+
178
+ def name
179
+ @xml.name
180
+ end
181
+
182
+ def text
183
+ if NOKOGIRI
184
+ @xml.content
185
+ else
186
+ @xml.text
187
+ end
188
+ end
189
+
190
+ def has_elements?(xpath_str)
191
+ if NOKOGIRI
192
+ element = @xml.xpath(xpath_str.to_s.upcase)
193
+ return element != nil && element.children.size > 0
194
+ else
195
+ element = @xml.elements[xpath_str.to_s]
196
+ return element != nil && element.has_elements?
197
+ end
198
+ end
199
+
200
+ def template_str(indent=true)
201
+ template_like_str('TEMPLATE', indent)
202
+ end
203
+
204
+ def template_like_str(root_element, indent=true, xpath_exp=nil)
205
+ if NOKOGIRI
206
+ xml_template=@xml.xpath(root_element).to_s
207
+ rexml=REXML::Document.new(xml_template).root
208
+ else
209
+ rexml=@xml.elements[root_element]
210
+ end
211
+
212
+ if indent
213
+ ind_enter="\n"
214
+ ind_tab=' '
215
+ else
216
+ ind_enter=''
217
+ ind_tab=' '
218
+ end
219
+
220
+ str=rexml.elements.collect(xpath_exp) {|n|
221
+ if n.class==REXML::Element
222
+ str_line=""
223
+ if n.has_elements?
224
+ str_line << n.name << "=[" << ind_enter
225
+
226
+ str_line << n.collect {|n2|
227
+ if n2 && n2.class==REXML::Element
228
+ str = ""
229
+ str << ind_tab << n2.name << '='
230
+ str << attr_to_str(n2.text) if n2.text
231
+ str
232
+ end
233
+ }.compact.join(','+ind_enter)
234
+ str_line<<" ]"
235
+ else
236
+ str_line << n.name << '=' << attr_to_str(n.text.to_s)
237
+ end
238
+ str_line
239
+ end
240
+ }.compact.join("\n")
241
+
242
+ str
243
+ end
244
+
245
+ def to_xml(pretty=false)
246
+ if NOKOGIRI && pretty
247
+ str = @xml.to_xml
248
+ elsif REXML_FORMATTERS && pretty
249
+ str = String.new
250
+
251
+ formatter = REXML::Formatters::Pretty.new
252
+ formatter.compact = true
253
+
254
+ formatter.write(@xml,str)
255
+ else
256
+ str = @xml.to_s
257
+ end
258
+
259
+ return str
260
+ end
261
+
262
+ def to_hash(hash={}, element=nil)
263
+ element ||= @xml.document.root
264
+
265
+ if NOKOGIRI
266
+ array = element.children
267
+ if array.length==1 and (array.first.text? or array.first.cdata?)
268
+ r = array.first.text
269
+ else
270
+ r = {}
271
+ array.each { |c|
272
+ if c.element?
273
+ to_hash(r, c)
274
+ end
275
+ }
276
+ end
277
+ else
278
+ r = {}
279
+ if element.has_elements?
280
+ element.each_element { |c| to_hash(r, c) }
281
+ elsif element.has_text?
282
+ r = element.text
283
+ end
284
+ end
285
+
286
+ key = element.name
287
+ if hash.has_key?(key)
288
+ if hash[key].instance_of?(Array)
289
+ hash[key] << r
290
+ else
291
+ hash[key] = [hash[key], r]
292
+ end
293
+ else
294
+ hash[key] = r
295
+ end
296
+
297
+ hash
298
+ end
299
+
300
+ private
301
+ def attr_to_str(attr)
302
+ attr.gsub!('"',"\\\"")
303
+
304
+ if attr.match(/[=,' ']/)
305
+ return '"' + attr + '"'
306
+ end
307
+
308
+ return attr
309
+ end
310
+ end
311
+
312
+ # The XMLUtilsPool module provides an abstraction of the underlying
313
+ # XML parser engine. It provides XML-related methods for the Pools
314
+ class XMLPool < XMLElement
315
+
316
+ def initialize(xml=nil)
317
+ super(xml)
318
+ end
319
+
320
+ #Executes the given block for each element of the Pool
321
+ #block:: _Block_
322
+ def each_element(block)
323
+ if NOKOGIRI
324
+ @xml.xpath(
325
+ "#{@element_name}").each {|pelem|
326
+ block.call self.factory(pelem)
327
+ }
328
+ else
329
+ @xml.elements.each(
330
+ "#{@element_name}") {|pelem|
331
+ block.call self.factory(pelem)
332
+ }
333
+ end
334
+ end
335
+ end
336
+
337
+ end