senotrusov-senotrusov-ruby-xml-mapper 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +204 -0
- data/README +23 -0
- data/lib/ruby-xml-mapper.rb +239 -0
- data/lib/ruby-xml-mapper/basic_containers.rb +118 -0
- data/test/rss.rb +49 -0
- metadata +69 -0
data/LICENSE
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
|
2
|
+
Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
|
4
|
+
Apache License
|
5
|
+
Version 2.0, January 2004
|
6
|
+
http://www.apache.org/licenses/
|
7
|
+
|
8
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
9
|
+
|
10
|
+
1. Definitions.
|
11
|
+
|
12
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
13
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
14
|
+
|
15
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
16
|
+
the copyright owner that is granting the License.
|
17
|
+
|
18
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
19
|
+
other entities that control, are controlled by, or are under common
|
20
|
+
control with that entity. For the purposes of this definition,
|
21
|
+
"control" means (i) the power, direct or indirect, to cause the
|
22
|
+
direction or management of such entity, whether by contract or
|
23
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
24
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
25
|
+
|
26
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
27
|
+
exercising permissions granted by this License.
|
28
|
+
|
29
|
+
"Source" form shall mean the preferred form for making modifications,
|
30
|
+
including but not limited to software source code, documentation
|
31
|
+
source, and configuration files.
|
32
|
+
|
33
|
+
"Object" form shall mean any form resulting from mechanical
|
34
|
+
transformation or translation of a Source form, including but
|
35
|
+
not limited to compiled object code, generated documentation,
|
36
|
+
and conversions to other media types.
|
37
|
+
|
38
|
+
"Work" shall mean the work of authorship, whether in Source or
|
39
|
+
Object form, made available under the License, as indicated by a
|
40
|
+
copyright notice that is included in or attached to the work
|
41
|
+
(an example is provided in the Appendix below).
|
42
|
+
|
43
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
44
|
+
form, that is based on (or derived from) the Work and for which the
|
45
|
+
editorial revisions, annotations, elaborations, or other modifications
|
46
|
+
represent, as a whole, an original work of authorship. For the purposes
|
47
|
+
of this License, Derivative Works shall not include works that remain
|
48
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
49
|
+
the Work and Derivative Works thereof.
|
50
|
+
|
51
|
+
"Contribution" shall mean any work of authorship, including
|
52
|
+
the original version of the Work and any modifications or additions
|
53
|
+
to that Work or Derivative Works thereof, that is intentionally
|
54
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
55
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
56
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
57
|
+
means any form of electronic, verbal, or written communication sent
|
58
|
+
to the Licensor or its representatives, including but not limited to
|
59
|
+
communication on electronic mailing lists, source code control systems,
|
60
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
61
|
+
Licensor for the purpose of discussing and improving the Work, but
|
62
|
+
excluding communication that is conspicuously marked or otherwise
|
63
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
64
|
+
|
65
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
66
|
+
on behalf of whom a Contribution has been received by Licensor and
|
67
|
+
subsequently incorporated within the Work.
|
68
|
+
|
69
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
70
|
+
this License, each Contributor hereby grants to You a perpetual,
|
71
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
72
|
+
copyright license to reproduce, prepare Derivative Works of,
|
73
|
+
publicly display, publicly perform, sublicense, and distribute the
|
74
|
+
Work and such Derivative Works in Source or Object form.
|
75
|
+
|
76
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
77
|
+
this License, each Contributor hereby grants to You a perpetual,
|
78
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
79
|
+
(except as stated in this section) patent license to make, have made,
|
80
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
81
|
+
where such license applies only to those patent claims licensable
|
82
|
+
by such Contributor that are necessarily infringed by their
|
83
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
84
|
+
with the Work to which such Contribution(s) was submitted. If You
|
85
|
+
institute patent litigation against any entity (including a
|
86
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
87
|
+
or a Contribution incorporated within the Work constitutes direct
|
88
|
+
or contributory patent infringement, then any patent licenses
|
89
|
+
granted to You under this License for that Work shall terminate
|
90
|
+
as of the date such litigation is filed.
|
91
|
+
|
92
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
93
|
+
Work or Derivative Works thereof in any medium, with or without
|
94
|
+
modifications, and in Source or Object form, provided that You
|
95
|
+
meet the following conditions:
|
96
|
+
|
97
|
+
(a) You must give any other recipients of the Work or
|
98
|
+
Derivative Works a copy of this License; and
|
99
|
+
|
100
|
+
(b) You must cause any modified files to carry prominent notices
|
101
|
+
stating that You changed the files; and
|
102
|
+
|
103
|
+
(c) You must retain, in the Source form of any Derivative Works
|
104
|
+
that You distribute, all copyright, patent, trademark, and
|
105
|
+
attribution notices from the Source form of the Work,
|
106
|
+
excluding those notices that do not pertain to any part of
|
107
|
+
the Derivative Works; and
|
108
|
+
|
109
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
110
|
+
distribution, then any Derivative Works that You distribute must
|
111
|
+
include a readable copy of the attribution notices contained
|
112
|
+
within such NOTICE file, excluding those notices that do not
|
113
|
+
pertain to any part of the Derivative Works, in at least one
|
114
|
+
of the following places: within a NOTICE text file distributed
|
115
|
+
as part of the Derivative Works; within the Source form or
|
116
|
+
documentation, if provided along with the Derivative Works; or,
|
117
|
+
within a display generated by the Derivative Works, if and
|
118
|
+
wherever such third-party notices normally appear. The contents
|
119
|
+
of the NOTICE file are for informational purposes only and
|
120
|
+
do not modify the License. You may add Your own attribution
|
121
|
+
notices within Derivative Works that You distribute, alongside
|
122
|
+
or as an addendum to the NOTICE text from the Work, provided
|
123
|
+
that such additional attribution notices cannot be construed
|
124
|
+
as modifying the License.
|
125
|
+
|
126
|
+
You may add Your own copyright statement to Your modifications and
|
127
|
+
may provide additional or different license terms and conditions
|
128
|
+
for use, reproduction, or distribution of Your modifications, or
|
129
|
+
for any such Derivative Works as a whole, provided Your use,
|
130
|
+
reproduction, and distribution of the Work otherwise complies with
|
131
|
+
the conditions stated in this License.
|
132
|
+
|
133
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
134
|
+
any Contribution intentionally submitted for inclusion in the Work
|
135
|
+
by You to the Licensor shall be under the terms and conditions of
|
136
|
+
this License, without any additional terms or conditions.
|
137
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
138
|
+
the terms of any separate license agreement you may have executed
|
139
|
+
with Licensor regarding such Contributions.
|
140
|
+
|
141
|
+
6. Trademarks. This License does not grant permission to use the trade
|
142
|
+
names, trademarks, service marks, or product names of the Licensor,
|
143
|
+
except as required for reasonable and customary use in describing the
|
144
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
145
|
+
|
146
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
147
|
+
agreed to in writing, Licensor provides the Work (and each
|
148
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
149
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
150
|
+
implied, including, without limitation, any warranties or conditions
|
151
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
152
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
153
|
+
appropriateness of using or redistributing the Work and assume any
|
154
|
+
risks associated with Your exercise of permissions under this License.
|
155
|
+
|
156
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
157
|
+
whether in tort (including negligence), contract, or otherwise,
|
158
|
+
unless required by applicable law (such as deliberate and grossly
|
159
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
160
|
+
liable to You for damages, including any direct, indirect, special,
|
161
|
+
incidental, or consequential damages of any character arising as a
|
162
|
+
result of this License or out of the use or inability to use the
|
163
|
+
Work (including but not limited to damages for loss of goodwill,
|
164
|
+
work stoppage, computer failure or malfunction, or any and all
|
165
|
+
other commercial damages or losses), even if such Contributor
|
166
|
+
has been advised of the possibility of such damages.
|
167
|
+
|
168
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
169
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
170
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
171
|
+
or other liability obligations and/or rights consistent with this
|
172
|
+
License. However, in accepting such obligations, You may act only
|
173
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
174
|
+
of any other Contributor, and only if You agree to indemnify,
|
175
|
+
defend, and hold each Contributor harmless for any liability
|
176
|
+
incurred by, or claims asserted against, such Contributor by reason
|
177
|
+
of your accepting any such warranty or additional liability.
|
178
|
+
|
179
|
+
END OF TERMS AND CONDITIONS
|
180
|
+
|
181
|
+
APPENDIX: How to apply the Apache License to your work.
|
182
|
+
|
183
|
+
To apply the Apache License to your work, attach the following
|
184
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
185
|
+
replaced with your own identifying information. (Don't include
|
186
|
+
the brackets!) The text should be enclosed in the appropriate
|
187
|
+
comment syntax for the file format. We also recommend that a
|
188
|
+
file or class name and description of purpose be included on the
|
189
|
+
same "printed page" as the copyright notice for easier
|
190
|
+
identification within third-party archives.
|
191
|
+
|
192
|
+
Copyright [yyyy] [name of copyright owner]
|
193
|
+
|
194
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
195
|
+
you may not use this file except in compliance with the License.
|
196
|
+
You may obtain a copy of the License at
|
197
|
+
|
198
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
199
|
+
|
200
|
+
Unless required by applicable law or agreed to in writing, software
|
201
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
202
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
203
|
+
See the License for the specific language governing permissions and
|
204
|
+
limitations under the License.
|
data/README
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain 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
|
+
Ruby XML mapper
|
18
|
+
===============
|
19
|
+
|
20
|
+
Sample of RSS mapping:
|
21
|
+
|
22
|
+
http://github.com/senotrusov/ruby-xml-mapper/blob/master/test/rss.rb
|
23
|
+
|
@@ -0,0 +1,239 @@
|
|
1
|
+
|
2
|
+
# Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain 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
|
+
# TODO Эта библиотека требует рефакторинга. Стоит выделить маппинг в отдельные объекты, а не держать всё размазанным по исходному классу. Опять же, это сделает проще обратный маппинг - из объектов в XML
|
18
|
+
|
19
|
+
require 'libxml'
|
20
|
+
|
21
|
+
module RubyXmlMapper
|
22
|
+
def self.included model
|
23
|
+
model.extend RubyXmlMapper::RubyXmlMapperClassMethods
|
24
|
+
|
25
|
+
model.class_inheritable_accessor :xml_name
|
26
|
+
model.class_inheritable_accessor :source_dir_path
|
27
|
+
|
28
|
+
model.class_inheritable_accessor :xml_self_mapping
|
29
|
+
model.class_inheritable_accessor :xml_content_mapping
|
30
|
+
model.class_inheritable_hash :xml_attr_mappings
|
31
|
+
model.class_inheritable_hash :xml_child_mappings
|
32
|
+
|
33
|
+
model.xml_attr_mappings = {}
|
34
|
+
model.xml_child_mappings = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Source loading is ugly
|
38
|
+
def initialize_from_xml node, load_source = true
|
39
|
+
initialize_from_xml_attr_mapping(node) if xml_attr_mappings.length
|
40
|
+
|
41
|
+
# TODO must not be a magic attribute
|
42
|
+
if @source && load_source
|
43
|
+
unless source_dir_path
|
44
|
+
raise "source_dir_path not defined in #{self.class.inspect}"
|
45
|
+
end
|
46
|
+
|
47
|
+
initialize_from_xml(self.class.parse_xml_file(File.expand_path_restricted(@source, source_dir_path)), false)
|
48
|
+
else
|
49
|
+
initialize_from_xml_child_mapping(node) if xml_child_mappings.length
|
50
|
+
initialize_from_xml_self_mapping(node) if xml_self_mapping
|
51
|
+
initialize_from_xml_content_mapping(node) if xml_content_mapping
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def initialize_from_xml_attr_mapping node
|
58
|
+
node.each_attr do |attr|
|
59
|
+
if (mapping = xml_attr_mappings[attr.name])
|
60
|
+
self.__send__(mapping[:writer_method], mapping[:type].new_from_xml_attr(attr))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Applying default values
|
65
|
+
xml_attr_mappings.each do |key, mapping|
|
66
|
+
if mapping[:default] && self.__send__(mapping[:reader_method]).nil?
|
67
|
+
self.__send__(mapping[:writer_method], mapping[:default])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def initialize_from_xml_child_mapping node
|
74
|
+
node.each_element do |child|
|
75
|
+
if (mapping = xml_child_mappings[child.name])
|
76
|
+
map_xml_node child, mapping
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Applying default values
|
81
|
+
xml_child_mappings.each do |key, mapping|
|
82
|
+
if mapping[:types] && !self.__send__(mapping[:reader_method]) # TODO: handle as default (below)
|
83
|
+
self.__send__(mapping[:writer_method], Array.new)
|
84
|
+
end
|
85
|
+
|
86
|
+
if mapping[:default] && self.__send__(mapping[:reader_method]).nil?
|
87
|
+
self.__send__(mapping[:writer_method], mapping[:default])
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def map_xml_node node, mapping
|
93
|
+
if (types = mapping[:types]) || mapping[:multiple]
|
94
|
+
unless (array = self.__send__(mapping[:reader_method]))
|
95
|
+
array = self.__send__(mapping[:writer_method], Array.new)
|
96
|
+
end
|
97
|
+
|
98
|
+
if mapping[:multiple]
|
99
|
+
array.push(mapping[:type].new_from_xml_node(node))
|
100
|
+
else
|
101
|
+
node.each_element do |child|
|
102
|
+
if(type = types[child.name])
|
103
|
+
array.push(type.new_from_xml_node(child))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
else
|
108
|
+
self.__send__(mapping[:writer_method], mapping[:type].new_from_xml_node(node))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
def initialize_from_xml_self_mapping node
|
114
|
+
if xml_self_mapping[:type].kind_of?(Array)
|
115
|
+
types = xml_self_mapping[:types]
|
116
|
+
|
117
|
+
node.each_element do |child|
|
118
|
+
if(type = types[child.name])
|
119
|
+
self.push(type.new_from_xml_node(child))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
elsif xml_self_mapping[:type] == :self
|
123
|
+
self.initialize_from_xml_node node
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def initialize_from_xml_content_mapping node
|
128
|
+
self.__send__(xml_content_mapping[:writer_method], xml_content_mapping[:type].new_from_xml_node(node))
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
module RubyXmlMapperClassMethods
|
134
|
+
def parse_xml_file(file_name)
|
135
|
+
check_file file_name
|
136
|
+
|
137
|
+
# NOTE: Don't know how to use XML::Document.file correctly.
|
138
|
+
# As in libxml-ruby-1.1.3 it does not close file descriptor, so you end up with "Error: Too many open files."
|
139
|
+
|
140
|
+
doc = LibXML::XML::Document.string(File.read(file_name))
|
141
|
+
|
142
|
+
# NOTE: If document does not created from file, it leads to "Segmentation fault"
|
143
|
+
# doc.xinclude
|
144
|
+
|
145
|
+
doc.root
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
def check_file file_name
|
150
|
+
unless File.file?(file_name)
|
151
|
+
raise "Unable to find file #{file_name.inspect}"
|
152
|
+
end
|
153
|
+
|
154
|
+
unless File.readable?(file_name)
|
155
|
+
raise "File is not readable #{file_name.inspect}"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def new_from_xml_file file_name
|
160
|
+
new_from_xml_node(parse_xml_file(file_name))
|
161
|
+
end
|
162
|
+
|
163
|
+
def new_from_xml_node node
|
164
|
+
allocated = allocate
|
165
|
+
allocated.initialize_from_xml node
|
166
|
+
allocated
|
167
|
+
end
|
168
|
+
|
169
|
+
def xml args
|
170
|
+
register_method = args.keys.detect {|key| self.respond_to?("register_#{key}_mapping", true)} || "self"
|
171
|
+
|
172
|
+
self.__send__("register_#{register_method}_mapping", args)
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
def register_attr_mapping args
|
179
|
+
args[:reader_method] = args[:attr]
|
180
|
+
args[:writer_method] = "#{args[:attr]}="
|
181
|
+
args[:tag_name] ||= args[:attr].to_s
|
182
|
+
|
183
|
+
attr_accessor args[:reader_method].to_sym
|
184
|
+
|
185
|
+
xml_attr_mappings[args[:tag_name]] = args
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
def register_child_mapping args
|
190
|
+
args[:tag_name] ||= args[:child].to_s
|
191
|
+
|
192
|
+
if args[:multiple]
|
193
|
+
args[:reader_method] = args[:multiple]
|
194
|
+
args[:writer_method] = "#{args[:multiple]}="
|
195
|
+
else
|
196
|
+
args[:reader_method] = args[:child]
|
197
|
+
args[:writer_method] = "#{args[:child]}="
|
198
|
+
end
|
199
|
+
|
200
|
+
register_type_array(args) if args[:type].kind_of?(Array)
|
201
|
+
|
202
|
+
attr_accessor args[:reader_method].to_sym
|
203
|
+
xml_child_mappings[args[:tag_name]] = args
|
204
|
+
end
|
205
|
+
|
206
|
+
|
207
|
+
def register_self_mapping args
|
208
|
+
if args[:type].kind_of?(Array)
|
209
|
+
register_type_array(args)
|
210
|
+
elsif args[:type] == :self && self < RubyXmlMapper::HashOfStringAndNumeric
|
211
|
+
|
212
|
+
else
|
213
|
+
raise(":type must be kind_of Array or :self with the following supported types: RubyXmlMapper::HashOfStringAndNumeric )")
|
214
|
+
end
|
215
|
+
|
216
|
+
self.xml_self_mapping = args
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
def register_content_mapping args
|
221
|
+
args[:reader_method] = args[:content]
|
222
|
+
args[:writer_method] = "#{args[:content]}="
|
223
|
+
|
224
|
+
attr_accessor args[:reader_method].to_sym
|
225
|
+
|
226
|
+
self.xml_content_mapping = args
|
227
|
+
end
|
228
|
+
|
229
|
+
def register_type_array args
|
230
|
+
args[:types] = {}
|
231
|
+
args[:type].each do |klass|
|
232
|
+
args[:types][klass.respond_to?(:xml_name) && klass.xml_name || klass.to_s.split('::').last.underscore] = klass
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
require 'ruby-xml-mapper/basic_containers'
|
239
|
+
|
@@ -0,0 +1,118 @@
|
|
1
|
+
|
2
|
+
# Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain 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
|
+
# TODO: Double check .strip - is it needed actually
|
18
|
+
|
19
|
+
class String
|
20
|
+
def self.new_from_xml_attr attr
|
21
|
+
new(attr.value.strip.gsub(/\s*[\r\n]\s*/, "\n"))
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.new_from_xml_node node
|
25
|
+
new(node.content.strip.gsub(/\s*[\r\n]\s*/, "\n"))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Symbol
|
30
|
+
def self.new_from_xml_attr attr
|
31
|
+
String.new_from_xml_attr(attr).to_sym
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.new_from_xml_node node
|
35
|
+
String.new_from_xml_node(node).to_sym
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Class
|
40
|
+
def self.new_from_xml_attr attr
|
41
|
+
String.new_from_xml_attr(attr).constantize
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.new_from_xml_node node
|
45
|
+
String.new_from_xml_node(node).constantize
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Integer
|
50
|
+
def self.new_from_xml_attr attr
|
51
|
+
attr.value.to_i
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.new_from_xml_node node
|
55
|
+
node.content.to_i
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class RubyXmlMapper::Boolean
|
60
|
+
def self.new_from_xml_attr attr
|
61
|
+
(attr.value =~ /\A\s*true\s*\z/i) ? true : false
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.new_from_xml_node node
|
65
|
+
(node.content =~ /\A\s*true\s*\z/i) ? true : false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class RubyXmlMapper::HashOfStringAndNumeric < Hash
|
70
|
+
def self.new_from_xml_node node
|
71
|
+
created = new
|
72
|
+
created.initialize_from_xml_node node
|
73
|
+
created
|
74
|
+
end
|
75
|
+
|
76
|
+
def initialize_from_xml_node node
|
77
|
+
node.each_element do |child|
|
78
|
+
self[child.name.to_sym] = autocast_value(child.content)
|
79
|
+
end
|
80
|
+
node.each_attr do |attr|
|
81
|
+
self[attr.name.to_sym] = autocast_value(attr.value)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def autocast_value value
|
86
|
+
if value =~ /\A\s*-*\d+\.\d+\s*\z/
|
87
|
+
value.to_f
|
88
|
+
|
89
|
+
elsif value =~ /\A\s*-*\d+\s*\z/
|
90
|
+
value.to_i
|
91
|
+
|
92
|
+
elsif matchdata = value.match(/\A\s*(-*\d+)\s*\.\.\s*(-*\d+)\s*\z/)
|
93
|
+
Range.new(matchdata[1].to_i, matchdata[2].to_i)
|
94
|
+
|
95
|
+
elsif value =~ /\A\s*true\s*\z/i
|
96
|
+
true
|
97
|
+
|
98
|
+
elsif value =~ /\A\s*false\s*\z/i
|
99
|
+
false
|
100
|
+
|
101
|
+
else
|
102
|
+
value.strip.gsub(/\s*[\r\n]\s*/, "\n").gsub(/\n\s*\n/, "\n")
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
require 'time'
|
109
|
+
|
110
|
+
class Time
|
111
|
+
def self.new_from_xml_attr attr
|
112
|
+
parse(attr.value)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.new_from_xml_node node
|
116
|
+
parse(node.content)
|
117
|
+
end
|
118
|
+
end
|
data/test/rss.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
# Copyright 2008-2009 Stanislav Senotrusov <senotrusov@gmail.com>
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain 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
|
+
=begin
|
17
|
+
$KCODE ="UTF8"
|
18
|
+
require 'rubygems'
|
19
|
+
require 'ruby-xml-mapper'
|
20
|
+
|
21
|
+
rss = RSS::RSS.new_from_xml_file('http://www.reddit.com/r/programming/.rss')
|
22
|
+
rss.channel.items.each {|item| puts item.title}; nil
|
23
|
+
=end
|
24
|
+
|
25
|
+
module RSS
|
26
|
+
class Item
|
27
|
+
include RubyXmlMapper
|
28
|
+
xml :child => :title, :type => String
|
29
|
+
xml :child => :link, :type => String
|
30
|
+
xml :child => :description, :type => String
|
31
|
+
xml :child => :category, :type => String
|
32
|
+
xml :child => :guid, :type => String
|
33
|
+
end
|
34
|
+
|
35
|
+
class Channel
|
36
|
+
include RubyXmlMapper
|
37
|
+
xml :child => :title, :type => String
|
38
|
+
xml :child => :link, :type => String
|
39
|
+
xml :child => :description, :type => String
|
40
|
+
xml :child => :ttl, :type => Integer
|
41
|
+
|
42
|
+
xml :child => :item, :multiple => :items, :type => RSS::Item
|
43
|
+
end
|
44
|
+
|
45
|
+
class RSS
|
46
|
+
include RubyXmlMapper
|
47
|
+
xml :child => :channel, :type => ::RSS::Channel
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: senotrusov-senotrusov-ruby-xml-mapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stanislav Senotrusov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: libxml-ruby
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.1.3
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: senotrusov@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README
|
33
|
+
- LICENSE
|
34
|
+
files:
|
35
|
+
- README
|
36
|
+
- LICENSE
|
37
|
+
- lib/ruby-xml-mapper
|
38
|
+
- lib/ruby-xml-mapper/basic_containers.rb
|
39
|
+
- lib/ruby-xml-mapper.rb
|
40
|
+
- test/rss.rb
|
41
|
+
has_rdoc: false
|
42
|
+
homepage: http://github.com/senotrusov
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --inline-source
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Mapping XML to Ruby in handy declarative manner using LibXML
|
68
|
+
test_files: []
|
69
|
+
|