xmlstruct 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/GPL2 ADDED
@@ -0,0 +1,340 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
+ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Library General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ <one line to give the program's name and a brief idea of what it does.>
294
+ Copyright (C) <year> <name of author>
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License
307
+ along with this program; if not, write to the Free Software
308
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
309
+
310
+
311
+ Also add information on how to contact you by electronic and paper mail.
312
+
313
+ If the program is interactive, make it output a short notice like this
314
+ when it starts in an interactive mode:
315
+
316
+ Gnomovision version 69, Copyright (C) year name of author
317
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
318
+ This is free software, and you are welcome to redistribute it
319
+ under certain conditions; type `show c' for details.
320
+
321
+ The hypothetical commands `show w' and `show c' should show the appropriate
322
+ parts of the General Public License. Of course, the commands you use may
323
+ be called something other than `show w' and `show c'; they could even be
324
+ mouse-clicks or menu items--whatever suits your program.
325
+
326
+ You should also get your employer (if you work as a programmer) or your
327
+ school, if any, to sign a "copyright disclaimer" for the program, if
328
+ necessary. Here is a sample; alter the names:
329
+
330
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
331
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
332
+
333
+ <signature of Ty Coon>, 1 April 1989
334
+ Ty Coon, President of Vice
335
+
336
+ This General Public License does not permit incorporating your program into
337
+ proprietary programs. If your program is a subroutine library, you may
338
+ consider it more useful to permit linking proprietary applications with the
339
+ library. If this is what you want to do, use the GNU Library General
340
+ Public License instead of this License.
data/README ADDED
@@ -0,0 +1,21 @@
1
+ # XmlStruct is a specialised version of OpenStruct that allows you to create
2
+ # XML layout automagically using Ruby assignment.
3
+ #
4
+ # ----
5
+ # Copyright (c) 2006 Neil Wilson, Aldur Systems Ltd
6
+ #
7
+ # Part of the 3accounts system
8
+ #
9
+ # This program is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU General Public License
11
+ # as published by the Free Software Foundation; either version 2
12
+ # of the License, or (at your option) any later version.
13
+ #
14
+ # This program is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with this program; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
data/lib/xml_char.rb ADDED
@@ -0,0 +1,110 @@
1
+ # =xml_char.rb: XmlChar implementation
2
+ #
3
+ # XmlChar provides mechanisms to escape special characters in a string so
4
+ # that they can be placed in an XML stream without causing havoc.
5
+ #
6
+ # XmlChar is based on Sam Ruby's XChar module
7
+ #
8
+ # ----
9
+ # Copyright (c) 2006 Neil Wilson, Aldur Systems Ltd
10
+ #
11
+ # Part of the 3accounts system
12
+ #
13
+ # Licensed under the GNU Public License v2. No warranty is provided.
14
+
15
+
16
+ # = Purpose
17
+ # This module provides Module procedures to escape special characters in
18
+ # a string so that it can be placed in an XML stream without causing havoc.
19
+ #
20
+ # A true object-oriented version would reopen +String+ and +Fixnum+ and stick
21
+ # the methods in there. Personally I prefer to keep these special
22
+ # functions on standard objects in their own namespace where they can't
23
+ # cause any nasty name clashes.
24
+ #
25
+ # = Usage
26
+ #
27
+ # unclean_string="<ShowMe>this < that && that > this</ShowMe>"
28
+ # XmlChar::xml_string(unclean_string)
29
+ # => "&lt;ShowMe&gt;this &lt; that &amp;&amp; that &gt; this&lt;/ShowMe&gt;"
30
+ #
31
+ module XmlChar
32
+
33
+ # :stopdoc:
34
+ #
35
+ # http://intertwingly.net/stories/2004/04/14/i18n.html#CleaningWindows
36
+ #
37
+ CP1252 = { # :nodoc:
38
+ 128 => 8364, # euro sign
39
+ 130 => 8218, # single low-9 quotation mark
40
+ 131 => 402, # latin small letter f with hook
41
+ 132 => 8222, # double low-9 quotation mark
42
+ 133 => 8230, # horizontal ellipsis
43
+ 134 => 8224, # dagger
44
+ 135 => 8225, # double dagger
45
+ 136 => 710, # modifier letter circumflex accent
46
+ 137 => 8240, # per mille sign
47
+ 138 => 352, # latin capital letter s with caron
48
+ 139 => 8249, # single left-pointing angle quotation mark
49
+ 140 => 338, # latin capital ligature oe
50
+ 142 => 381, # latin capital letter z with caron
51
+ 145 => 8216, # left single quotation mark
52
+ 146 => 8217, # right single quotation mark
53
+ 147 => 8220, # left double quotation mark
54
+ 148 => 8221, # right double quotation mark
55
+ 149 => 8226, # bullet
56
+ 150 => 8211, # en dash
57
+ 151 => 8212, # em dash
58
+ 152 => 732, # small tilde
59
+ 153 => 8482, # trade mark sign
60
+ 154 => 353, # latin small letter s with caron
61
+ 155 => 8250, # single right-pointing angle quotation mark
62
+ 156 => 339, # latin small ligature oe
63
+ 158 => 382, # latin small letter z with caron
64
+ 159 => 376 # latin capital letter y with diaeresis
65
+ }
66
+
67
+ # http://www.w3.org/TR/REC-xml/#dt-chardata
68
+ PREDEFINED = { # :nodoc:
69
+ ?' => '&apos;', # single quote
70
+ ?" => '&quot;', # double quote
71
+ ?& => '&amp;', # ampersand
72
+ ?< => '&lt;', # left angle bracket
73
+ ?> => '&gt;'} # right angle bracket
74
+
75
+ # http://www.w3.org/TR/REC-xml/#charsets
76
+ VALID = [[0x9, 0xA, 0xD], (0x20..0xD7FF), # :nodoc:
77
+ (0xE000..0xFFFD), (0x10000..0x10FFFF)]
78
+ #
79
+ # :startdoc:
80
+ #
81
+ # Create an XML escaped version of +normal_string+
82
+ #
83
+ # === Contract
84
+ #
85
+ # require:: !normal_string.nil?
86
+ #
87
+ def self.xml_string(normal_string)
88
+ normal_string.unpack('U*').map {|n| XmlChar::xml_char(n)}.join # ASCII, UTF-8
89
+ rescue
90
+ normal_string.unpack('C*').map {|n| XmlChar::xml_char(n)}.join # ISO-8859-1, WIN-1252
91
+ end
92
+
93
+ #
94
+ # Create an XML escaped version of +normal_char+
95
+ #
96
+ # === Contract
97
+ #
98
+ # require: normal_char && normal_char.integer?
99
+ #
100
+ def self.xml_char(normal_char)
101
+ char_value = XmlChar::CP1252[normal_char] || normal_char
102
+ if XmlChar::VALID.find { |range| range.include? char_value }
103
+ XmlChar::PREDEFINED[char_value] ||
104
+ (char_value<0x80 ? char_value.chr : "&##{char_value};")
105
+ else
106
+ "*"
107
+ end
108
+ end
109
+
110
+ end
data/lib/xml_struct.rb ADDED
@@ -0,0 +1,387 @@
1
+ # =xml_struct.rb: XmlStruct implementation
2
+ #
3
+ # XmlStruct is a specialised version of OpenStruct that allows you to create
4
+ # XML layout automagically using Ruby assignment.
5
+ #
6
+ # ----
7
+ # Copyright (c) 2006 Neil Wilson, Aldur Systems Ltd
8
+ #
9
+ # Part of the 3accounts system
10
+ #
11
+ # Licensed under the GNU Public License v2. No warranty is provided.
12
+
13
+ # = Purpose
14
+ # XMLStruct is a specialised version of OpenStruct that allows you to create
15
+ # XML layout automagically using Ruby assignment. The class supports XML tag
16
+ # attributes and element content.
17
+ #
18
+ # The class has been designed so that you can rapidly change a flat dataset
19
+ # into an XML layout without all that tedious messing about with Xpaths,
20
+ # tree traversal, nested blocks and the like.
21
+ #
22
+ # = Usage
23
+ #
24
+ # Create a blank document using the usual #new method.
25
+ #
26
+ # xml = XmlStruct.new
27
+ #
28
+ # Add items as required and output using the #to_s command
29
+ #
30
+ # xml.GovTalkMessage = {:xmlns => "http://www.govtalk.gov.uk/CM/envelope"}
31
+ # md = xml.GovTalkMessage.Header.MessageDetails
32
+ # md.Class="IR-PAYE-EOY"
33
+ # md.Transformation="XML"
34
+ # xml.GovTalkMessage.GovTalkDetails
35
+ # xml.GovTalkMessage.Body
36
+ # xml.to_s
37
+ #
38
+ # gives
39
+ #
40
+ # <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
41
+ # <Header>
42
+ # <MessageDetails>
43
+ # <Class>IR-PAYE-EOY</Class>
44
+ # <Transformation>XML</Transformation>
45
+ # </MessageDetails>
46
+ # </Header>
47
+ # <GovTalkDetails>
48
+ # </GovTalkDetails>
49
+ # <Body>
50
+ # </Body>
51
+ # </GovTalkMessage>
52
+ #
53
+ # == Content
54
+ #
55
+ # Xml content is added using standard Ruby assign. Any wrapper tags are
56
+ # created automatically.
57
+ #
58
+ # xml.GovTalkMessage.Header.MessageDetails.Class="IR-PAYE-EOY"
59
+ #
60
+ # XML is output using the #to_s method. The above would output as:
61
+ #
62
+ # <GovTalkMessage>
63
+ # <Header>
64
+ # <MessageDetails>
65
+ # <Class>IR-PAYE-EOY</Class>
66
+ # </MessageDetails>
67
+ # </Header>
68
+ # </GovTalkMessage>
69
+ #
70
+ # Blank elements with no content are created by simply referencing the element.
71
+ #
72
+ # xml.GovTalkMessage.Body
73
+ #
74
+ # Content is stored in an array like manner within the structure so that
75
+ # you can have multiple elements with the same tag, e.g.
76
+ #
77
+ # xml.Address.Line[0] = "1 Some Street"
78
+ # xml.Address.Line[1] = "Somewhere"
79
+ # xml.Address.Line[2] = "Sometown"
80
+ # xml.to_s
81
+ #
82
+ # gives
83
+ #
84
+ # <Address>
85
+ # <Line>1 Some Street</Line>
86
+ # <Line>Somewhere</Line>
87
+ # <Line>Sometown</Line>
88
+ # </Address>
89
+ #
90
+ # Any element can be referenced to a variable as a shortcut.
91
+ # That address example again.
92
+ #
93
+ # ln = xml.Address.Line
94
+ # ln[0] = "1 Some Street"
95
+ # ln[1] = "Somewhere"
96
+ # ln[2] = "Sometown"
97
+ #
98
+ # To make things really simple assigning an array of values to an element
99
+ # creates multiple elements with the same tag.
100
+ #
101
+ # xml.Address.Line = ["1 Some Street", "Somewhere", "Sometown"]
102
+ #
103
+ # ==Attributes
104
+ #
105
+ # You set attributes for elements by assigning a hash of attributes
106
+ # to the element, e.g
107
+ #
108
+ # xml.GovTalkMessage = {:xmlns => "http://www.govtalk.gov.uk/CM/envelope"}
109
+ # xml.to_s
110
+ #
111
+ # gives
112
+ #
113
+ # <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
114
+ # </GovTalkMessage>
115
+ #
116
+ # Each element can have its own set of attributes - even those with the same
117
+ # element name, eg.
118
+ #
119
+ # xml.Keys.Key[0]={:Type => "VendorNumber"}
120
+ # xml.Keys.Key[0]=275687
121
+ # xml.Keys.Key[1]={:Type => "MainCPH"}
122
+ # xml.Keys.Key[1]="14/02/0327"
123
+ # xml.to_s
124
+ #
125
+ # gives
126
+ #
127
+ # <Keys>
128
+ # <Key Type="VendorNumber">275687</Key>
129
+ # <Key Type="MainCPH">14/02/0327</Key>
130
+ # </Keys>
131
+ #
132
+ # Multiple attributes are simply a matter of enumerating all the attributes in
133
+ # the hash, eg.
134
+ #
135
+ # xml.Keys.Key = {:Type => "Unique Tax Reference", :Length => "10"}
136
+ #
137
+ # gives
138
+ #
139
+ # <Keys>
140
+ # <Key Type="Unique Tax Reference" Length="10">
141
+ # </Key>
142
+ # </Keys
143
+ #
144
+ # ==Caveats
145
+ #
146
+ # XMLStruct undefines all the instance methods that may clash with XML tags
147
+ # except for <tt>inspect</tt>. So if you have a file with an <tt><inspect></tt> XML tag then
148
+ # the class won't work without modification.
149
+ #
150
+ # If you need to add methods to this class or any subclass, then use the !
151
+ # and ? suffixes. Otherwise the method will clash with the XML tag of the
152
+ # same name. (Fortunately ! and ? are not valid XML tag characters).
153
+ #
154
+ # There is no 'delete' or 'reorder' methods within this class. The
155
+ # class is designed to take flat data and impose an XML structure on
156
+ # it statically. So the original purpose doesn't require anything other
157
+ # than assign and output facilities. Still I hope you find it useful.
158
+ #
159
+ class XmlStruct
160
+
161
+ require 'xml_char'
162
+ # Undefine any instance method that may clash with an XML tag - all except
163
+ # the vital +__id__+ and +__send__+ methods.
164
+ instance_methods.each do |m|
165
+ unless m =~ /^__|inspect/ then
166
+ undef_method m if m =~ /^[a-z:_][\w:\.-]*$/i
167
+ end
168
+ end
169
+
170
+ #
171
+ # Creates a new XmlStruct document. The optional +tag+ can be used to set
172
+ # the initial tag for the document. However normally it is omitted.
173
+ #
174
+ # xml = XmlStruct.new
175
+ # xml.FirstTag
176
+ # xml.to_s
177
+ #
178
+ # and
179
+ #
180
+ # xml = XmlStruct.new("FirstTag")
181
+ # xml.to_s
182
+ #
183
+ # both give the same output, viz:
184
+ #
185
+ # <FirstTag>
186
+ # </FirstTag>
187
+ #
188
+ def initialize (tag = "")
189
+ @my_tag = tag
190
+ @content = []
191
+ @attributes = []
192
+ @children = {}
193
+ @create_order = []
194
+ end
195
+
196
+ #
197
+ # Allows you to access the content values for a particular element.
198
+ # The default assign places content in [0], and you use that index to read
199
+ # the content back, e.g.
200
+ #
201
+ # xml.Address.PostCode = "EC1A 5SW"
202
+ # xml.Address.PostCode[0]
203
+ # => "EC1A 5SW"
204
+ #
205
+ #
206
+ def [](index)
207
+ @content[index]
208
+ end
209
+
210
+ # Assigning to [0] is the same as the default assign.
211
+ # The following are equivalent
212
+ #
213
+ # xml.Address.PostCode = "EC1A 5SW"
214
+ # xml.Address.PostCode[0] = "EC1A 5SW"
215
+ #
216
+ # Assigning to indexes greater than zero creates an additional element with
217
+ # the same tag but different content
218
+ #
219
+ # xml.Address.Line[0] = "1 Some Street"
220
+ # xml.Address.Line[1] = "Somewhere"
221
+ #
222
+ # However the default assign with an array is easier to use.
223
+ #
224
+ # xml.Address.Line = [ "1 Some Street", "Somewhere" ]
225
+ #
226
+ def []=(index, rval)
227
+ case
228
+ when rval.kind_of?(Hash)
229
+ @attributes[index] = rval
230
+ when rval.kind_of?(Array)
231
+ rval.each_index { |i| @content[i] = rval[i] }
232
+ else
233
+ @content[index] = rval
234
+ end
235
+ end
236
+
237
+ #
238
+ # The +method_missing+ method implements the Proxy pattern for this class.
239
+ # It automatically creates any attribute if it is missing and handles
240
+ # the default assignment by passing the assignment onto the [0] element.
241
+ #
242
+ # Methods that cannot be XML tags (essentially those ending in ? and !) are
243
+ # raised as errors.
244
+ def method_missing(method_id, *args) # :nodoc:
245
+ method_name = method_id.id2name
246
+ len = args.length
247
+ if method_name[-1] == ?=
248
+ unless len == 1
249
+ raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
250
+ end
251
+ if frozen?
252
+ raise TypeError, "can't modify frozen object", caller(1)
253
+ end
254
+ method_name.chop!
255
+ unless method_name =~ /^[a-z:_][\w:\.-]*$/i
256
+ raise NoMethodError, "undefined method '#{method_name}'", caller(1)
257
+ end
258
+ child = obtain_accessor!(method_name)
259
+ child[0] = args[0]
260
+ elsif method_name =~ /^[a-z:_][\w:\.-]*$/i
261
+ if len == 0
262
+ obtain_accessor!(method_name)
263
+ else
264
+ raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
265
+ end
266
+ else
267
+ raise NoMethodError, "undefined method '#{method_name}'", caller(1)
268
+ end
269
+ end
270
+
271
+ #
272
+ # Converts the XMLStruct into XML format. Returns an empty string if
273
+ # the XMLStruct is empty.
274
+ #
275
+ def to_s
276
+ %{#{open_my_tags!}#{
277
+ @create_order.collect do |tag_id|
278
+ @children[tag_id].to_s
279
+ end.join}#{end_my_tags!}}
280
+ end
281
+
282
+ private
283
+
284
+ #
285
+ # Recover the child XMLStruct from the +@children+ hash if it exists,
286
+ # otherwise create one.
287
+ #
288
+ def obtain_accessor!(name)
289
+ access_method = name.intern
290
+ if ptr = @children[access_method]
291
+ ptr
292
+ else
293
+ @create_order.push(access_method)
294
+ @children[access_method] = XmlStruct.new(name)
295
+ end
296
+ end
297
+
298
+ #
299
+ # If we're in an XMLStruct with a name, then layout the attributes and
300
+ # contents in XML format and always leave at least one tag open.
301
+ #
302
+ def open_my_tags!
303
+ if @my_tag.empty?
304
+ ""
305
+ else
306
+ (0...max_xsval_length?).collect do |x|
307
+ %{<#{@my_tag}#{
308
+ format_attributes!(@attributes[x])
309
+ }>#{
310
+ format_content!(@content[x])
311
+ }}
312
+ end.join(end_my_tags!)
313
+ end
314
+ end
315
+
316
+ #
317
+ # If we're in an XMLStruct with a name, then close our XML tag
318
+ #
319
+ # ==Contract
320
+ #
321
+ # require: !@my_tag.nil?
322
+ #
323
+ def end_my_tags!
324
+ if @my_tag.empty?
325
+ ""
326
+ else
327
+ "</#{@my_tag}>\n"
328
+ end
329
+ end
330
+
331
+ #
332
+ # Layout the XML attributes from the supplied attributes collection
333
+ # in XML format, in the order supplied by the collection.
334
+ #
335
+ # ==Contract
336
+ #
337
+ # require: attributes.nil? || attributes.respond_to?("collect")
338
+ #
339
+ def format_attributes!(attributes)
340
+ if attributes
341
+ attributes.collect do |attrib, value|
342
+ " #{attrib}=\"#{XmlChar::xml_string(value)}\""
343
+ end.join
344
+ end
345
+ end
346
+
347
+ #
348
+ # Layout the content in string format from the supplied content object
349
+ #
350
+ # ==Contract
351
+ #
352
+ # require: content.respond_to?("to_s")
353
+ #
354
+ def format_content!(content)
355
+ if content.to_s.empty?
356
+ "\n"
357
+ else
358
+ XmlChar::xml_string(content.to_s)
359
+ end
360
+ end
361
+
362
+ # Returns the maxium value in the range 1 to the maximum length of
363
+ # either the +@attributes+ collection or the +@content+ collection
364
+ #
365
+ # ==Contract
366
+ #
367
+ # require: @content && @content.respond_to?("length")
368
+ # require: @attributes && @attributes.respond_to?("length")
369
+ #
370
+ # ensure: Result == 1 && @attributes.length <= 1 && @content.length <= 1
371
+ # ensure: Result == @attributes.length && @attributes.length >= @content.length
372
+ # ensure: Result == @content.length && @content.length >= @attributes.length
373
+ #
374
+ def max_xsval_length?
375
+ x= @content.length
376
+ y= @attributes.length
377
+ if x<=1 && y<=1
378
+ 1
379
+ elsif x>=y
380
+ x
381
+ else
382
+ y
383
+ end
384
+ end
385
+
386
+ end
387
+
@@ -0,0 +1,27 @@
1
+ require 'profile'
2
+ require 'lib/xml_struct'
3
+
4
+ LOOP_COUNT=1000
5
+
6
+ LOOP_COUNT.times do
7
+ xml=XmlStruct.new
8
+ xml.GovTalkMessage={:xmlns => "http://www.govtalk.gov.uk/CM/envelope"}
9
+ xml.GovTalkMessage.EnvelopeVersion="2.0"
10
+ md=xml.GovTalkMessage.Header.MessageDetails
11
+ md.Class="MAFF-IACS-AAPS2001"
12
+ md.Qualifier='request'
13
+ md.Function='list'
14
+ md.CorrelationID
15
+ md.Transformation='XML'
16
+ ia=xml.GovTalkMessage.Header.SenderDetails.IDAuthentication
17
+ ia.SenderID="VendorID"
18
+ ia.Authentication.Method="clear"
19
+ ia.Authentication.Role
20
+ ia.Authentication.Value="password"
21
+ xml.GovTalkMessage.GovTalkDetails.Keys
22
+ xml.GovTalkMessage.Body.StartDate="01/02/1999"
23
+ xml.GovTalkMessage.Body.EndDate="01/0r32/1999"
24
+ xml.GovTalkMessage.Body.StartTime="01:02:44"
25
+ xml.GovTalkMessage.Body.EndTime="01:02:44"
26
+ xml.to_s
27
+ end
@@ -0,0 +1,42 @@
1
+ require 'lib/xml_char'
2
+ require 'test/unit'
3
+
4
+ class TestXmlEscaping < Test::Unit::TestCase
5
+ def test_ascii
6
+ assert_equal 'abc', XmlChar::xml_string('abc')
7
+ end
8
+
9
+ def test_predefined
10
+ assert_equal '&amp;', XmlChar::xml_string('&') # ampersand
11
+ assert_equal '&lt;', XmlChar::xml_string('<') # left angle bracket
12
+ assert_equal '&gt;', XmlChar::xml_string('>') # right angle bracket
13
+ assert_equal '&quot;', XmlChar::xml_string('"') # Double Quote
14
+ assert_equal '&apos;', XmlChar::xml_string("'") # Single Quote
15
+ end
16
+
17
+ def test_invalid
18
+ assert_equal '*', XmlChar::xml_string("\x00") # null
19
+ assert_equal '*', XmlChar::xml_string("\x0C") # form feed
20
+ assert_equal '*', XmlChar::xml_string("\xEF\xBF\xBF") # U+FFFF
21
+ end
22
+
23
+ def test_iso_8859_1
24
+ assert_equal '&#231;', XmlChar::xml_string("\xE7") # small c cedilla
25
+ assert_equal '&#169;', XmlChar::xml_string("\xA9") # copyright symbol
26
+ end
27
+
28
+ def test_win_1252
29
+ assert_equal '&#8217;', XmlChar::xml_string("\x92") # smart quote
30
+ assert_equal '&#8364;', XmlChar::xml_string("\x80") # euro
31
+ end
32
+
33
+ def test_utf8
34
+ assert_equal '&#8217;', XmlChar::xml_string("\xE2\x80\x99") # right single quote
35
+ assert_equal '&#169;', XmlChar::xml_string("\xC2\xA9") # copy
36
+ end
37
+
38
+ def test_the_lot
39
+ assert_equal("I&#241;t&#235;rn&#226;ti&#244;n&#224;liz&#230;ti&#248;n",
40
+ XmlChar::xml_string("Iñtërnâtiônàlizætiøn"))
41
+ end
42
+ end
@@ -0,0 +1,58 @@
1
+ require 'test/unit'
2
+ require 'lib/xml_struct'
3
+
4
+ class TestXmlStruct < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @xmldoc = XmlStruct.new
8
+ end
9
+
10
+ def test_empty
11
+ #Empty root node
12
+ #assert_equal("",xmldoc.to_s)
13
+ assert_not_nil(@xmldoc)
14
+ assert_nil(@xmldoc[0])
15
+ end
16
+
17
+ def test_autocreate_read
18
+ #Autocreate nodes by reading them
19
+ @xmldoc.Header
20
+ @xmldoc.Header.MessageDetails.Class
21
+ #Assign to already created node
22
+ assert_equal("IR-PAYE-EOY",@xmldoc.Header.MessageDetails.Class="IR-PAYE-EOY")
23
+ #element[0] reads the value.
24
+ assert_equal("IR-PAYE-EOY",@xmldoc.Header.MessageDetails.Class[0])
25
+ end
26
+
27
+ def test_autocreate_assign
28
+ #Autocreate nodes by assigning to them.
29
+ assert_equal("IR-PAYE-EOY",@xmldoc.Header.MessageDetails.Class="IR-PAYE-EOY")
30
+ assert_equal("IR-PAYE-EOY",@xmldoc.Header.MessageDetails.Class[0])
31
+ end
32
+
33
+ def test_failures
34
+ assert_raise(NoMethodError){ @xmldoc.fred? }
35
+ assert_raise(NoMethodError){ @xmldoc.hello! }
36
+ assert_raise(ArgumentError){ @xmldoc.Header(45) }
37
+ @xmldoc.Header
38
+ assert_raise(ArgumentError){ @xmldoc.Header(45) }
39
+ end
40
+
41
+ def test_key_creation
42
+ @xmldoc.GovTalkDetails.Keys.Key[0]={:Type => "VendorNumber"}
43
+ @xmldoc.GovTalkDetails.Keys.Key[0]=275687
44
+ @xmldoc.GovTalkDetails.Keys.Key[1]={:Type => "MainCPH"}
45
+ @xmldoc.GovTalkDetails.Keys.Key[1]="14/02/0327"
46
+ @xmldoc.GovTalkDetails.Address.Line=["1 Some Street", "Somewhere", "SomeTown"]
47
+ @xmldoc.Body
48
+ print @xmldoc.to_s
49
+ end
50
+
51
+ def test_escapes
52
+ @xmldoc.TestEntity={:Test1 => "\"", :Test2 => "'", :Test3 => "Iñtërnâtiônàlizætiøn" }
53
+ @xmldoc.TestEntity="Iñtërnâtiônàlizætiøn"
54
+ print @xmldoc.to_s
55
+ end
56
+
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: xmlstruct
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-03-22 00:00:00 +00:00
8
+ summary: A Struct that allows you to create an XML layout quickly and easily
9
+ require_paths:
10
+ - lib
11
+ email: aldursys@gmail.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: xml_struct
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Neil Wilson
30
+ files:
31
+ - test/test_xml_char.rb
32
+ - test/test_xml_struct.rb
33
+ - test/perf_test_xml_struct.rb
34
+ - lib/xml_struct.rb
35
+ - lib/xml_char.rb
36
+ - README
37
+ - GPL2
38
+ test_files:
39
+ - test/test_xml_struct.rb
40
+ - test/test_xml_char.rb
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files:
44
+ - README
45
+ - GPL2
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+