evma_xmlpushparser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,281 @@
1
+ .
2
+
3
+ GNU GENERAL PUBLIC LICENSE
4
+ Version 2, June 1991
5
+
6
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
7
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8
+ Everyone is permitted to copy and distribute verbatim copies
9
+ of this license document, but changing it is not allowed.
10
+
11
+ Preamble
12
+
13
+ The licenses for most software are designed to take away your
14
+ freedom to share and change it. By contrast, the GNU General Public
15
+ License is intended to guarantee your freedom to share and change free
16
+ software--to make sure the software is free for all its users. This
17
+ General Public License applies to most of the Free Software
18
+ Foundation's software and to any other program whose authors commit to
19
+ using it. (Some other Free Software Foundation software is covered by
20
+ the GNU Lesser General Public License instead.) You can apply it to
21
+ your programs, too.
22
+
23
+ When we speak of free software, we are referring to freedom, not
24
+ price. Our General Public Licenses are designed to make sure that you
25
+ have the freedom to distribute copies of free software (and charge for
26
+ this service if you wish), that you receive source code or can get it
27
+ if you want it, that you can change the software or use pieces of it
28
+ in new free programs; and that you know you can do these things.
29
+
30
+ To protect your rights, we need to make restrictions that forbid
31
+ anyone to deny you these rights or to ask you to surrender the rights.
32
+ These restrictions translate to certain responsibilities for you if you
33
+ distribute copies of the software, or if you modify it.
34
+
35
+ For example, if you distribute copies of such a program, whether
36
+ gratis or for a fee, you must give the recipients all the rights that
37
+ you have. You must make sure that they, too, receive or can get the
38
+ source code. And you must show them these terms so they know their
39
+ rights.
40
+
41
+ We protect your rights with two steps: (1) copyright the software, and
42
+ (2) offer you this license which gives you legal permission to copy,
43
+ distribute and/or modify the software.
44
+
45
+ Also, for each author's protection and ours, we want to make certain
46
+ that everyone understands that there is no warranty for this free
47
+ software. If the software is modified by someone else and passed on, we
48
+ want its recipients to know that what they have is not the original, so
49
+ that any problems introduced by others will not reflect on the original
50
+ authors' reputations.
51
+
52
+ Finally, any free program is threatened constantly by software
53
+ patents. We wish to avoid the danger that redistributors of a free
54
+ program will individually obtain patent licenses, in effect making the
55
+ program proprietary. To prevent this, we have made it clear that any
56
+ patent must be licensed for everyone's free use or not licensed at all.
57
+
58
+ The precise terms and conditions for copying, distribution and
59
+ modification follow.
60
+
61
+ GNU GENERAL PUBLIC LICENSE
62
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
63
+
64
+ 0. This License applies to any program or other work which contains
65
+ a notice placed by the copyright holder saying it may be distributed
66
+ under the terms of this General Public License. The "Program", below,
67
+ refers to any such program or work, and a "work based on the Program"
68
+ means either the Program or any derivative work under copyright law:
69
+ that is to say, a work containing the Program or a portion of it,
70
+ either verbatim or with modifications and/or translated into another
71
+ language. (Hereinafter, translation is included without limitation in
72
+ the term "modification".) Each licensee is addressed as "you".
73
+
74
+ Activities other than copying, distribution and modification are not
75
+ covered by this License; they are outside its scope. The act of
76
+ running the Program is not restricted, and the output from the Program
77
+ is covered only if its contents constitute a work based on the
78
+ Program (independent of having been made by running the Program).
79
+ Whether that is true depends on what the Program does.
80
+
81
+ 1. You may copy and distribute verbatim copies of the Program's
82
+ source code as you receive it, in any medium, provided that you
83
+ conspicuously and appropriately publish on each copy an appropriate
84
+ copyright notice and disclaimer of warranty; keep intact all the
85
+ notices that refer to this License and to the absence of any warranty;
86
+ and give any other recipients of the Program a copy of this License
87
+ along with the Program.
88
+
89
+ You may charge a fee for the physical act of transferring a copy, and
90
+ you may at your option offer warranty protection in exchange for a fee.
91
+
92
+ 2. You may modify your copy or copies of the Program or any portion
93
+ of it, thus forming a work based on the Program, and copy and
94
+ distribute such modifications or work under the terms of Section 1
95
+ above, provided that you also meet all of these conditions:
96
+
97
+ a) You must cause the modified files to carry prominent notices
98
+ stating that you changed the files and the date of any change.
99
+
100
+ b) You must cause any work that you distribute or publish, that in
101
+ whole or in part contains or is derived from the Program or any
102
+ part thereof, to be licensed as a whole at no charge to all third
103
+ parties under the terms of this License.
104
+
105
+ c) If the modified program normally reads commands interactively
106
+ when run, you must cause it, when started running for such
107
+ interactive use in the most ordinary way, to print or display an
108
+ announcement including an appropriate copyright notice and a
109
+ notice that there is no warranty (or else, saying that you provide
110
+ a warranty) and that users may redistribute the program under
111
+ these conditions, and telling the user how to view a copy of this
112
+ License. (Exception: if the Program itself is interactive but
113
+ does not normally print such an announcement, your work based on
114
+ the Program is not required to print an announcement.)
115
+
116
+ These requirements apply to the modified work as a whole. If
117
+ identifiable sections of that work are not derived from the Program,
118
+ and can be reasonably considered independent and separate works in
119
+ themselves, then this License, and its terms, do not apply to those
120
+ sections when you distribute them as separate works. But when you
121
+ distribute the same sections as part of a whole which is a work based
122
+ on the Program, the distribution of the whole must be on the terms of
123
+ this License, whose permissions for other licensees extend to the
124
+ entire whole, and thus to each and every part regardless of who wrote it.
125
+
126
+ Thus, it is not the intent of this section to claim rights or contest
127
+ your rights to work written entirely by you; rather, the intent is to
128
+ exercise the right to control the distribution of derivative or
129
+ collective works based on the Program.
130
+
131
+ In addition, mere aggregation of another work not based on the Program
132
+ with the Program (or with a work based on the Program) on a volume of
133
+ a storage or distribution medium does not bring the other work under
134
+ the scope of this License.
135
+
136
+ 3. You may copy and distribute the Program (or a work based on it,
137
+ under Section 2) in object code or executable form under the terms of
138
+ Sections 1 and 2 above provided that you also do one of the following:
139
+
140
+ a) Accompany it with the complete corresponding machine-readable
141
+ source code, which must be distributed under the terms of Sections
142
+ 1 and 2 above on a medium customarily used for software interchange; or,
143
+
144
+ b) Accompany it with a written offer, valid for at least three
145
+ years, to give any third party, for a charge no more than your
146
+ cost of physically performing source distribution, a complete
147
+ machine-readable copy of the corresponding source code, to be
148
+ distributed under the terms of Sections 1 and 2 above on a medium
149
+ customarily used for software interchange; or,
150
+
151
+ c) Accompany it with the information you received as to the offer
152
+ to distribute corresponding source code. (This alternative is
153
+ allowed only for noncommercial distribution and only if you
154
+ received the program in object code or executable form with such
155
+ an offer, in accord with Subsection b above.)
156
+
157
+ The source code for a work means the preferred form of the work for
158
+ making modifications to it. For an executable work, complete source
159
+ code means all the source code for all modules it contains, plus any
160
+ associated interface definition files, plus the scripts used to
161
+ control compilation and installation of the executable. However, as a
162
+ special exception, the source code distributed need not include
163
+ anything that is normally distributed (in either source or binary
164
+ form) with the major components (compiler, kernel, and so on) of the
165
+ operating system on which the executable runs, unless that component
166
+ itself accompanies the executable.
167
+
168
+ If distribution of executable or object code is made by offering
169
+ access to copy from a designated place, then offering equivalent
170
+ access to copy the source code from the same place counts as
171
+ distribution of the source code, even though third parties are not
172
+ compelled to copy the source along with the object code.
173
+
174
+ 4. You may not copy, modify, sublicense, or distribute the Program
175
+ except as expressly provided under this License. Any attempt
176
+ otherwise to copy, modify, sublicense or distribute the Program is
177
+ void, and will automatically terminate your rights under this License.
178
+ However, parties who have received copies, or rights, from you under
179
+ this License will not have their licenses terminated so long as such
180
+ parties remain in full compliance.
181
+
182
+ 5. You are not required to accept this License, since you have not
183
+ signed it. However, nothing else grants you permission to modify or
184
+ distribute the Program or its derivative works. These actions are
185
+ prohibited by law if you do not accept this License. Therefore, by
186
+ modifying or distributing the Program (or any work based on the
187
+ Program), you indicate your acceptance of this License to do so, and
188
+ all its terms and conditions for copying, distributing or modifying
189
+ the Program or works based on it.
190
+
191
+ 6. Each time you redistribute the Program (or any work based on the
192
+ Program), the recipient automatically receives a license from the
193
+ original licensor to copy, distribute or modify the Program subject to
194
+ these terms and conditions. You may not impose any further
195
+ restrictions on the recipients' exercise of the rights granted herein.
196
+ You are not responsible for enforcing compliance by third parties to
197
+ this License.
198
+
199
+ 7. If, as a consequence of a court judgment or allegation of patent
200
+ infringement or for any other reason (not limited to patent issues),
201
+ conditions are imposed on you (whether by court order, agreement or
202
+ otherwise) that contradict the conditions of this License, they do not
203
+ excuse you from the conditions of this License. If you cannot
204
+ distribute so as to satisfy simultaneously your obligations under this
205
+ License and any other pertinent obligations, then as a consequence you
206
+ may not distribute the Program at all. For example, if a patent
207
+ license would not permit royalty-free redistribution of the Program by
208
+ all those who receive copies directly or indirectly through you, then
209
+ the only way you could satisfy both it and this License would be to
210
+ refrain entirely from distribution of the Program.
211
+
212
+ If any portion of this section is held invalid or unenforceable under
213
+ any particular circumstance, the balance of the section is intended to
214
+ apply and the section as a whole is intended to apply in other
215
+ circumstances.
216
+
217
+ It is not the purpose of this section to induce you to infringe any
218
+ patents or other property right claims or to contest validity of any
219
+ such claims; this section has the sole purpose of protecting the
220
+ integrity of the free software distribution system, which is
221
+ implemented by public license practices. Many people have made
222
+ generous contributions to the wide range of software distributed
223
+ through that system in reliance on consistent application of that
224
+ system; it is up to the author/donor to decide if he or she is willing
225
+ to distribute software through any other system and a licensee cannot
226
+ impose that choice.
227
+
228
+ This section is intended to make thoroughly clear what is believed to
229
+ be a consequence of the rest of this License.
230
+
231
+ 8. If the distribution and/or use of the Program is restricted in
232
+ certain countries either by patents or by copyrighted interfaces, the
233
+ original copyright holder who places the Program under this License
234
+ may add an explicit geographical distribution limitation excluding
235
+ those countries, so that distribution is permitted only in or among
236
+ countries not thus excluded. In such case, this License incorporates
237
+ the limitation as if written in the body of this License.
238
+
239
+ 9. The Free Software Foundation may publish revised and/or new versions
240
+ of the General Public License from time to time. Such new versions will
241
+ be similar in spirit to the present version, but may differ in detail to
242
+ address new problems or concerns.
243
+
244
+ Each version is given a distinguishing version number. If the Program
245
+ specifies a version number of this License which applies to it and "any
246
+ later version", you have the option of following the terms and conditions
247
+ either of that version or of any later version published by the Free
248
+ Software Foundation. If the Program does not specify a version number of
249
+ this License, you may choose any version ever published by the Free Software
250
+ Foundation.
251
+
252
+ 10. If you wish to incorporate parts of the Program into other free
253
+ programs whose distribution conditions are different, write to the author
254
+ to ask for permission. For software which is copyrighted by the Free
255
+ Software Foundation, write to the Free Software Foundation; we sometimes
256
+ make exceptions for this. Our decision will be guided by the two goals
257
+ of preserving the free status of all derivatives of our free software and
258
+ of promoting the sharing and reuse of software generally.
259
+
260
+ NO WARRANTY
261
+
262
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
263
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
264
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
265
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
266
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
267
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
268
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
269
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
270
+ REPAIR OR CORRECTION.
271
+
272
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
273
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
274
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
275
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
276
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
277
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
278
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
279
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
280
+ POSSIBILITY OF SUCH DAMAGES.
281
+
data/README ADDED
@@ -0,0 +1,3 @@
1
+ = evma_xmlpushparser
2
+
3
+ This is a Ruby extension which wraps the xmlParseChunk functionality of the Gnome libxml2 library.
@@ -0,0 +1,3 @@
1
+ $Id: RELEASE_NOTES 3539 2006-12-31 03:05:56Z francis $
2
+
3
+
@@ -0,0 +1,66 @@
1
+ # $Id: Rakefile 3546 2006-12-31 21:01:27Z francis $
2
+ # Rakefile for the Bayshore configurable LDAP server.
3
+ #
4
+
5
+ require 'rake/gempackagetask'
6
+ require 'rake/clean'
7
+
8
+ $can_minitar = false
9
+ begin
10
+ require 'archive/tar/minitar'
11
+ require 'zlib'
12
+ $can_minitar = true
13
+ rescue LoadError
14
+ end
15
+
16
+ $version = "0.0.1"
17
+ $distdir = "eventmachine_xmlpushparser-#{$version}"
18
+ $tardist = "#$distdir.tar.gz"
19
+ $name = "eventmachine_xmlpushparser"
20
+
21
+ spec = eval(File.read("evma_xmlpushparser.gemspec"))
22
+ spec.version = $version
23
+ desc "Build the RubyGem for EventMachine XML push-parser"
24
+ task :gem => ["pkg/evma_xmlpushparser-#{$version}.gem"]
25
+ Rake::GemPackageTask.new(spec) do |g|
26
+ if $can_minitar
27
+ g.need_tar = true
28
+ g.need_zip = true
29
+ end
30
+ g.package_dir = "pkg"
31
+ end
32
+
33
+ def run_test_package test, filename_array
34
+ require 'test/unit/testsuite'
35
+ require 'test/unit/ui/console/testrunner'
36
+
37
+ runner = Test::Unit::UI::Console::TestRunner
38
+
39
+ $LOAD_PATH.unshift('test')
40
+ $stderr.puts "Checking for test cases:" if test.verbose
41
+ filename_array.each do |testcase|
42
+ $stderr.puts "\t#{testcase}" if test.verbose
43
+ load testcase
44
+ end
45
+
46
+ suite = Test::Unit::TestSuite.new($name)
47
+
48
+ ObjectSpace.each_object(Class) do |testcase|
49
+ suite << testcase.suite if testcase < Test::Unit::TestCase
50
+ end
51
+
52
+ runner.run(suite)
53
+ end
54
+
55
+ desc "Run the tests for #$name."
56
+ task :test do |t|
57
+ run_test_package t, Dir['test/*.rb']
58
+ end
59
+
60
+ desc "Run the application tests"
61
+ task :test_application do |t|
62
+ run_test_package t, Dir['test/app.rb']
63
+ end
64
+
65
+ task :default => [:test]
66
+
@@ -0,0 +1,113 @@
1
+ # $Id: extconf.rb 63 2006-05-17 06:03:18Z blackhedd $
2
+ #
3
+ #----------------------------------------------------------------------------
4
+ #
5
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
6
+ #
7
+ # Gmail: garbagecat20
8
+ #
9
+ # This program is free software; you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation; either version 2 of the License, or
12
+ # (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 St, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #
23
+ #---------------------------------------------------------------------------
24
+ #
25
+ # We have to munge LDSHARED because this code needs a C++ link.
26
+ #
27
+
28
+ require 'mkmf'
29
+
30
+ flags = []
31
+
32
+ case RUBY_PLATFORM.split('-',2)[1]
33
+ when 'mswin32', 'mingw32', 'bccwin32'
34
+ unless have_header('windows.h') and
35
+ have_header('winsock.h') and
36
+ have_library('kernel32') and
37
+ have_library('rpcrt4') and
38
+ have_library('gdi32')
39
+ exit
40
+ end
41
+
42
+ flags << "-D OS_WIN32"
43
+ flags << '-D BUILD_FOR_RUBY'
44
+ flags << "-EHs"
45
+ flags << "-GR"
46
+
47
+ dir_config('xml2')
48
+ unless have_library('xml2') and
49
+ have_header('libxml/parser.h') and
50
+ have_header('libxml/tree.h')
51
+ exit
52
+ end
53
+
54
+ when /solaris/
55
+ unless have_library('pthread') and
56
+ have_library('nsl') and
57
+ have_library('socket')
58
+ exit
59
+ end
60
+
61
+ flags << '-D OS_UNIX'
62
+ flags << '-D OS_SOLARIS8'
63
+ flags << '-D BUILD_FOR_RUBY'
64
+
65
+ dir_config('xml2')
66
+ unless have_library('xml2') and
67
+ find_header('libxml/parser.h', '/usr/include/libxml2') and
68
+ find_header('libxml/tree.h', '/usr/include/libxml2')
69
+ exit
70
+ end
71
+
72
+ # on Unix we need a g++ link, not gcc.
73
+ CONFIG['LDSHARED'] = "$(CXX) -shared"
74
+
75
+ when /darwin/
76
+ flags << '-DOS_UNIX'
77
+ flags << '-DBUILD_FOR_RUBY'
78
+
79
+ dir_config('xml2')
80
+ unless have_library('xml2') and
81
+ find_header('libxml/parser.h', '/usr/include/libxml2') and
82
+ find_header('libxml/tree.h', '/usr/include/libxml2')
83
+ exit
84
+ end
85
+ # on Unix we need a g++ link, not gcc.
86
+ CONFIG['LDSHARED'] = "$(CXX) " + CONFIG['LDSHARED'].split[1..-1].join(' ')
87
+
88
+ else
89
+ unless have_library('pthread')
90
+ exit
91
+ end
92
+
93
+ flags << '-DOS_UNIX'
94
+ flags << '-DBUILD_FOR_RUBY'
95
+
96
+ dir_config('xml2')
97
+ unless have_library('xml2') and
98
+ find_header('libxml/parser.h', '/usr/include/libxml2') and
99
+ find_header('libxml/tree.h', '/usr/include/libxml2')
100
+ exit
101
+ end
102
+ # on Unix we need a g++ link, not gcc.
103
+ CONFIG['LDSHARED'] = "$(CXX) -shared"
104
+ end
105
+
106
+ if $CPPFLAGS
107
+ $CPPFLAGS += ' ' + flags.join(' ')
108
+ else
109
+ $CFLAGS += ' ' + flags.join(' ')
110
+ end
111
+
112
+
113
+ create_makefile "rubyxmlpushparser"
@@ -0,0 +1,466 @@
1
+ /*****************************************************************************
2
+
3
+ $Id: rubymain.cpp 307 2007-04-28 13:52:16Z blackhedd $
4
+
5
+ File: libmain.cpp
6
+ Date: 06Apr06
7
+
8
+ Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: garbagecat20
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of the GNU General Public License as published by
13
+ the Free Software Foundation; either version 2 of the License, or
14
+ (at your option) any later version.
15
+
16
+ This program is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ GNU General Public License for more details.
20
+
21
+ You should have received a copy of the GNU General Public License
22
+ along with this program; if not, write to the Free Software
23
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
+
25
+ *****************************************************************************/
26
+
27
+
28
+ #include <iostream>
29
+ #include <stdexcept>
30
+ #include <libxml/parser.h>
31
+ #include <libxml/tree.h>
32
+ #include <ruby.h>
33
+
34
+
35
+ static xmlSAXHandler saxHandler;
36
+
37
+ /*************************
38
+ class RubyXmlPushParser_t
39
+ *************************/
40
+
41
+ class RubyXmlPushParser_t
42
+ {
43
+ public:
44
+ RubyXmlPushParser_t (VALUE v);
45
+
46
+ void ConsumeData (const char *, int);
47
+ void Close();
48
+ void ScheduleReset();
49
+
50
+ void SaxStartDocument();
51
+ void SaxEndDocument();
52
+ void SaxStartElement (const xmlChar*, const xmlChar**);
53
+ void SaxEndElement (const xmlChar*);
54
+ void SaxCharacters (const xmlChar*, int);
55
+ void SaxError();
56
+
57
+ private:
58
+ VALUE Myself;
59
+ xmlParserCtxtPtr Context;
60
+ bool bReset;
61
+ };
62
+
63
+
64
+
65
+ /*****************
66
+ rubyStartDocument
67
+ *****************/
68
+
69
+ static void rubyStartDocument (void *ctx)
70
+ {
71
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*) ctx;
72
+ if (!pp)
73
+ throw std::runtime_error ("bad ptr in rubyStartDocument");
74
+ pp->SaxStartDocument();
75
+ }
76
+
77
+ /***************
78
+ rubyEndDocument
79
+ ***************/
80
+
81
+ static void rubyEndDocument (void *ctx)
82
+ {
83
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*) ctx;
84
+ if (!pp)
85
+ throw std::runtime_error ("bad ptr in rubyEndDocument");
86
+ pp->SaxEndDocument();
87
+ }
88
+
89
+ /****************
90
+ rubyStartElement
91
+ ****************/
92
+
93
+ static void rubyStartElement (void *ctx, const xmlChar *name, const xmlChar **attrs)
94
+ {
95
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*) ctx;
96
+ if (!pp)
97
+ throw std::runtime_error ("bad ptr in rubyStartElement");
98
+ pp->SaxStartElement (name, attrs);
99
+ }
100
+
101
+ /**************
102
+ rubyEndElement
103
+ **************/
104
+
105
+ static void rubyEndElement (void *ctx, const xmlChar *name)
106
+ {
107
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*) ctx;
108
+ if (!pp)
109
+ throw std::runtime_error ("bad ptr in rubyEndElement");
110
+ pp->SaxEndElement (name);
111
+ }
112
+
113
+
114
+ /**************
115
+ rubyCharacters
116
+ **************/
117
+
118
+ static void rubyCharacters (void *ctx, const xmlChar *val, int len)
119
+ {
120
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*) ctx;
121
+ if (!pp)
122
+ throw std::runtime_error ("bad ptr in rubyCharacters");
123
+ pp->SaxCharacters (val, len);
124
+ }
125
+
126
+ /*********
127
+ rubyError
128
+ *********/
129
+
130
+ static void rubyError (void *ctx, const char *msg, ...)
131
+ {
132
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*) ctx;
133
+ if (!pp)
134
+ throw std::runtime_error ("bad ptr in rubyError");
135
+ pp->SaxError();
136
+ }
137
+
138
+
139
+ /****************************************
140
+ RubyXmlPushParser_t::RubyXmlPushParser_t
141
+ ****************************************/
142
+
143
+ RubyXmlPushParser_t::RubyXmlPushParser_t (VALUE v):
144
+ Myself (v),
145
+ bReset (false)
146
+ {
147
+ /* Note that we're bypassing the typical convention of passing the
148
+ * first four bytes of the document to the parser-create function.
149
+ * Obviously we may not have them yet. We could delay calling this
150
+ * until the bytes are available but this seems to work fine.
151
+ * Note that passing in a SAX2 handler means that this parser
152
+ * will NOT accumulate a document tree, which in turn means we
153
+ * don't need to worry about freeing the document tree later.
154
+ * (We will of course need to free the parser itself.)
155
+ */
156
+ Context = xmlCreatePushParserCtxt (&saxHandler, (void*)this, "", 0, "");
157
+ if (!Context)
158
+ throw std::runtime_error ("no push-parser context");
159
+ }
160
+
161
+
162
+ /********************************
163
+ RubyXmlPushParser_t::ConsumeData
164
+ ********************************/
165
+
166
+ void RubyXmlPushParser_t::ConsumeData (const char *data, int length)
167
+ {
168
+ /* Avoid passing more than a few bytes to xmlParseChunk at a time.
169
+ * We know that at some points (such as while initially determining
170
+ * the encoding of a new document), the library's internal buffers
171
+ * are very small.
172
+ */
173
+ for (int i=0; i < length; i++) {
174
+ if (bReset) {
175
+ // don't send a good-bye kiss to the existing context because it'll
176
+ // probably kick out a malformation error.
177
+ xmlFreeParserCtxt (Context);
178
+ Context = xmlCreatePushParserCtxt (&saxHandler, (void*)this, "", 0, "");
179
+ if (!Context)
180
+ throw std::runtime_error ("no push-parser context");
181
+ bReset = false;
182
+ }
183
+ xmlParseChunk (Context, data+i, 1, 0);
184
+ }
185
+ }
186
+
187
+
188
+ /**********************************
189
+ RubyXmlPushParser_t::ScheduleReset
190
+ **********************************/
191
+
192
+ void RubyXmlPushParser_t::ScheduleReset()
193
+ {
194
+ bReset = true;
195
+ }
196
+
197
+ /**************************
198
+ RubyXmlPushParser_t::Close
199
+ **************************/
200
+
201
+ void RubyXmlPushParser_t::Close()
202
+ {
203
+ /* We come here automatically when a connection gets an unbind event.
204
+ * User code can inadvertently cut this off and leak memory by implementing #unbind.
205
+ * All user code that implements unbind MUST call super within the unbind.
206
+ *
207
+ * It's a requirement that this code be called. We're relying on the (hopefully
208
+ * reliable) fact that a network connection will always get closed sometime.
209
+ * If this extension ever needs to handle cases that are not matched to a network
210
+ * connection, then we'll need to arrange some other way for this to get called.
211
+ */
212
+ xmlParseChunk (Context, "", 0, 1);
213
+ xmlFreeParserCtxt (Context);
214
+ // No need to call xmlFreeDoc (Content->myDoc) because it's always NULL when a SAX handler is present.
215
+ }
216
+
217
+
218
+ /*************************************
219
+ RubyXmlPushParser_t::SaxStartDocument
220
+ *************************************/
221
+
222
+ void RubyXmlPushParser_t::SaxStartDocument()
223
+ {
224
+ rb_funcall (Myself, rb_intern ("start_document"), 0);
225
+ }
226
+
227
+ /***********************************
228
+ RubyXmlPushParser_t::SaxEndDocument
229
+ ***********************************/
230
+
231
+ void RubyXmlPushParser_t::SaxEndDocument()
232
+ {
233
+ /* It's problematical for user code to rely on receiving this event.
234
+ * If a network peer sends a complete XML document but then sends
235
+ * nothing more beyond the closing tag of the root element, then
236
+ * libxml does NOT call endDocument. endDocument will be called in the
237
+ * following two cases: first, if the caller sends at least one byte beyond
238
+ * the end of the actual document (this may be a syntactically-valid "Misc"
239
+ * paragraph at the tail of the document, or the start of a new document, or
240
+ * erroneous matter); and second, if the remote peer closes the network connection,
241
+ * which causes us to receive and respond to the unbind event, and in turn
242
+ * resulting in a call to xmlParseChunk with the terminate flag set.
243
+ *
244
+ * This is probably not a problem for a protocol like XMPP, in which the
245
+ * whole stream is syntactically a single document. But other protocols
246
+ * might have problems.
247
+ */
248
+ rb_funcall (Myself, rb_intern ("end_document"), 0);
249
+ }
250
+
251
+ /************************************
252
+ RubyXmlPushParser_t::SaxStartElement
253
+ ************************************/
254
+
255
+ void RubyXmlPushParser_t::SaxStartElement (const xmlChar *name, const xmlChar **attribs)
256
+ {
257
+ if (!name)
258
+ name = (const xmlChar*)"";
259
+
260
+ VALUE atts = rb_hash_new();
261
+ if (attribs) {
262
+ while (attribs[0] && attribs[1]) {
263
+ rb_hash_aset (atts, rb_str_new2((const char*)attribs[0]), rb_str_new2((const char*)attribs[1]));
264
+ attribs += 2;
265
+ }
266
+ }
267
+ rb_funcall (Myself, rb_intern ("start_element"), 2, rb_str_new2((const char*)name), atts);
268
+ }
269
+
270
+ /**********************************
271
+ RubyXmlPushParser_t::SaxEndElement
272
+ **********************************/
273
+
274
+ void RubyXmlPushParser_t::SaxEndElement (const xmlChar *name)
275
+ {
276
+ if (!name)
277
+ name = (const xmlChar*)"";
278
+ rb_funcall (Myself, rb_intern ("end_element"), 1, rb_str_new2((const char*)name));
279
+ }
280
+
281
+ /**********************************
282
+ RubyXmlPushParser_t::SaxCharacters
283
+ **********************************/
284
+
285
+ void RubyXmlPushParser_t::SaxCharacters (const xmlChar *name, int length)
286
+ {
287
+ if (!name) {
288
+ name = (const xmlChar*)"";
289
+ length = 0;
290
+ }
291
+ rb_funcall (Myself, rb_intern ("characters"), 1, rb_str_new((const char*)name, length));
292
+ }
293
+
294
+ /*****************************
295
+ RubyXmlPushParser_t::SaxError
296
+ *****************************/
297
+
298
+ void RubyXmlPushParser_t::SaxError()
299
+ {
300
+ int e = xmlCtxtGetLastError (Context)->code;
301
+ if (e == XML_ERR_DOCUMENT_END)
302
+ ;
303
+ else {
304
+ rb_funcall (Myself, rb_intern ("error"), 1, INT2FIX (e));
305
+ rb_funcall (Myself, rb_intern ("close_connection"), 0);
306
+ }
307
+ }
308
+
309
+ /***********
310
+ t_post_init
311
+ ***********/
312
+
313
+ static VALUE t_post_init (VALUE self)
314
+ {
315
+ RubyXmlPushParser_t *pp = new RubyXmlPushParser_t (self);
316
+ if (!pp)
317
+ throw std::runtime_error ("no xml push-parser object");
318
+
319
+ rb_ivar_set (self, rb_intern ("@xml__push__parser__object"), INT2NUM ((long)pp));
320
+ return Qnil;
321
+ }
322
+
323
+ /**************
324
+ t_receive_data
325
+ **************/
326
+
327
+ static VALUE t_receive_data (VALUE self, VALUE data)
328
+ {
329
+ int length = NUM2INT (rb_funcall (data, rb_intern ("length"), 0));
330
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*)(NUM2INT (rb_ivar_get (self, rb_intern ("@xml__push__parser__object"))));
331
+ if (!pp)
332
+ throw std::runtime_error ("bad push-parser object");
333
+ pp->ConsumeData (StringValuePtr (data), length);
334
+
335
+ return Qnil;
336
+ }
337
+
338
+ /********
339
+ t_unbind
340
+ ********/
341
+
342
+ static VALUE t_unbind (VALUE self)
343
+ {
344
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*)(NUM2INT (rb_ivar_get (self, rb_intern ("@xml__push__parser__object"))));
345
+ if (!pp)
346
+ throw std::runtime_error ("no xml push-parser object");
347
+ pp->Close();
348
+ return Qnil;
349
+ }
350
+
351
+
352
+ /**************
353
+ t_reset_parser
354
+ **************/
355
+
356
+ static VALUE t_reset_parser (VALUE self)
357
+ {
358
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*)(NUM2INT (rb_ivar_get (self, rb_intern ("@xml__push__parser__object"))));
359
+ if (!pp)
360
+ throw std::runtime_error ("no xml push-parser object");
361
+ pp->ScheduleReset();
362
+
363
+ return Qnil;
364
+ }
365
+
366
+ /****************
367
+ t_start_document
368
+ ****************/
369
+
370
+ static VALUE t_start_document (VALUE self)
371
+ {
372
+ // STUB. Override this in user code if needed.
373
+ return Qnil;
374
+ }
375
+
376
+ /**************
377
+ t_end_document
378
+ **************/
379
+
380
+ static VALUE t_end_document (VALUE self)
381
+ {
382
+ // STUB. Override this in user code if needed.
383
+ return Qnil;
384
+ }
385
+
386
+ /***************
387
+ t_start_element
388
+ ***************/
389
+
390
+ static VALUE t_start_element (VALUE self, VALUE name, VALUE attributes)
391
+ {
392
+ // STUB. Override this in user code if needed.
393
+ return Qnil;
394
+ }
395
+
396
+ /*************
397
+ t_end_element
398
+ *************/
399
+
400
+ static VALUE t_end_element (VALUE self, VALUE name)
401
+ {
402
+ // STUB. Override this in user code if needed.
403
+ return Qnil;
404
+ }
405
+
406
+ /************
407
+ t_characters
408
+ ************/
409
+
410
+ static VALUE t_characters (VALUE self, VALUE data)
411
+ {
412
+ // STUB. Override this in user code if needed.
413
+ return Qnil;
414
+ }
415
+
416
+ /*******
417
+ t_error
418
+ *******/
419
+
420
+ static VALUE t_error (VALUE self, VALUE code)
421
+ {
422
+ // STUB. Override this in user code if needed.
423
+ return Qnil;
424
+ }
425
+
426
+
427
+ /**********************
428
+ Init_rubyxmlpushparser
429
+ **********************/
430
+
431
+ extern "C" void Init_rubyxmlpushparser()
432
+ {
433
+ LIBXML_TEST_VERSION
434
+ #ifndef LIBXML_PUSH_ENABLED
435
+ throw std::runtime_error ("XML push not enabled on this system");
436
+ #endif
437
+
438
+ // saxHandler is statically defined and readonly after we initialize it here.
439
+ // We DEPEND on the fact that the C runtime will zero-initialize it.
440
+ saxHandler.startDocument = rubyStartDocument;
441
+ saxHandler.startElement = rubyStartElement;
442
+ saxHandler.endElement = rubyEndElement;
443
+ saxHandler.characters = rubyCharacters;
444
+ saxHandler.endDocument = rubyEndDocument;
445
+ saxHandler.error = rubyError;
446
+ saxHandler.warning = rubyError;
447
+
448
+ // Define a module XmlPushParser that can be used standalone and modified,
449
+ // or else included into EventMachine::Connection or a subclass of same.
450
+ // NB that we define #post_init and #unbind here. This means that any user
451
+ // subclass that includes EventMachine::XmlPushParser MUST call super
452
+ // inside subclassed implementations of these two methods.
453
+
454
+ VALUE EmModule = rb_define_module ("EventMachine");
455
+ VALUE XmlModule = rb_define_module_under (EmModule, "XmlPushParser");
456
+ rb_define_method (XmlModule, "post_init", (VALUE(*)(...))t_post_init, 0);
457
+ rb_define_method (XmlModule, "receive_data", (VALUE(*)(...))t_receive_data, 1);
458
+ rb_define_method (XmlModule, "unbind", (VALUE(*)(...))t_unbind, 0);
459
+ rb_define_method (XmlModule, "start_document", (VALUE(*)(...))t_start_document, 0);
460
+ rb_define_method (XmlModule, "end_document", (VALUE(*)(...))t_end_document, 0);
461
+ rb_define_method (XmlModule, "start_element", (VALUE(*)(...))t_start_element, 2);
462
+ rb_define_method (XmlModule, "end_element", (VALUE(*)(...))t_end_element, 1);
463
+ rb_define_method (XmlModule, "characters", (VALUE(*)(...))t_characters, 1);
464
+ rb_define_method (XmlModule, "error", (VALUE(*)(...))t_error, 1);
465
+ rb_define_method (XmlModule, "reset_parser", (VALUE(*)(...))t_reset_parser, 0);
466
+ }
@@ -0,0 +1,2 @@
1
+ require 'rubyxmlpushparser'
2
+
@@ -0,0 +1,67 @@
1
+ # $Id: app.rb 3893 2007-03-06 20:12:09Z francis $
2
+ #
3
+ #
4
+
5
+
6
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
7
+
8
+ require 'eventmachine'
9
+ require 'evma_xmlpushparser'
10
+
11
+
12
+ class TestApp < Test::Unit::TestCase
13
+
14
+ Host = "127.0.0.1"
15
+ Port = 9250
16
+
17
+ class Xml < EventMachine::Connection
18
+ include EventMachine::XmlPushParser
19
+ attr_reader :elements, :element_ends
20
+ def initialize *args
21
+ super
22
+ @elements = {}
23
+ @element_ends = []
24
+ end
25
+ def start_document
26
+ end
27
+ def start_element nm, attrs
28
+ if ["aaa","bbb"].include?(nm)
29
+ @elements[nm] = attrs
30
+ else
31
+ raise "error"
32
+ end
33
+ end
34
+ def end_element nm
35
+ @element_ends << nm
36
+ end
37
+ def end_document
38
+ EventMachine.stop
39
+ end
40
+ end
41
+
42
+ module XmlClient
43
+ def post_init
44
+ send_data '<?xml version="1.0"?><aaa'
45
+ send_data ' attr1="1" attr2="2">chars<bbb/'
46
+ send_data '></a'
47
+ send_data 'aa>'
48
+ close_connection_after_writing
49
+ end
50
+ end
51
+
52
+ def test_a
53
+ obj = nil
54
+ EventMachine.run {
55
+ EventMachine.start_server(Host, Port, Xml) {|xml| obj = xml}
56
+ EventMachine.connect Host, Port, XmlClient
57
+ EventMachine.add_timer(2) {EventMachine.stop} # avoid hang in case of error
58
+ }
59
+
60
+ assert_equal( ["aaa","bbb"], obj.elements.keys.sort )
61
+ assert_equal( {"attr1"=>"1","attr2"=>"2"}, obj.elements["aaa"] )
62
+ assert_equal( {}, obj.elements["bbb"] )
63
+ assert_equal( ["bbb","aaa"], obj.element_ends)
64
+ end
65
+ end
66
+
67
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evma_xmlpushparser
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Francis Cianfrocca
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-21 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This is a Ruby extension which wraps the xmlParseChunk functionality of the Gnome libxml2 library.
23
+ email: garbagecat10@gmail.com
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/extconf.rb
28
+ extra_rdoc_files:
29
+ - README
30
+ - RELEASE_NOTES
31
+ - COPYING
32
+ - Rakefile
33
+ files:
34
+ - test/app.rb
35
+ - ext/extconf.rb
36
+ - ext/rubymain.cpp
37
+ - lib/evma_xmlpushparser.rb
38
+ - README
39
+ - RELEASE_NOTES
40
+ - COPYING
41
+ - Rakefile
42
+ has_rdoc: true
43
+ homepage: http://www.eventmachine.com
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --title
49
+ - EventMachine_XMLPushParser
50
+ - --main
51
+ - README
52
+ - --line-numbers
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: EventMachine XML Push Parser
80
+ test_files: []
81
+