XML-SAX-Writer-0.56000755001750001750 012431176652 14637 5ustar00perigrinperigrin000000000000README100644001750001750 54112431176652 15560 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56 This archive contains the distribution XML-SAX-Writer, version 0.56: SAX2 XML Writer This software is copyright (c) 2014 by Robin Berjon. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. This README file was generated by Dist::Zilla::Plugin::Readme v5.025. Changes100755001750001750 671712431176652 16231 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56Revision history for XML::SAX::Writer ------------------------------------- 0.54 - 2013-12-13 - remove /o modifier on Regexps (RT#91382) - Update to Dist::Zilla for releasing - Clean up POD issues - Simplify Makefile.PL - Add cpanfile 0.53 - 2010-07-12 - Switch from Text::IconV to Encode (David Pinkowitz) - Update package Module::Install metadata discovery 0.52 - 2008-11-19 - Fix spelling of QuoteCharacter 0.51 - 2008-11-19 - Added QuoteCharecter parameter to new() to control quoting of attributes (perigrin) - Added Tests and Docs 0.50 - Sep 14, 2006 - fixed redundant skipped entity at start_dtd (RT.cpan.org #18546) - attribute_decl expects Mode instead of ValueDefault property to fit to Perl SAX 2.1 (ValueDefault still supported) - fixed make test failure with Text::Iconv 1.3 or higher (RT.cpan.org #7436 and #9690) - fixed make test failure on VMS (RT.cpan.org #18663) - fixed dropped output on encoding conversion errors (RT.cpan.org #17177) 0.43 - 0.44 fri 20020906 18:27 - made the basic tests work again despite the fact we now return a BufferText object - fixed a bug reported by Grant that prevented proper use of output FH under some conditions. 0.42 - thu 20020801 17:17 - fixed a serious bug in StringConsumer, thanks to help from Barrie Slaymaker, Adam Turoff, and Eric Cholet. 0.40 - 0.41 sat 20020706 18:01 - implemented the subclassable SAX writing framework - documented the converter API - made comments be escaped differently - fixed doctype writing (Michel Rodriguez) - fixed a warning (Joshua Keroes) - requires correct version of Test::More (Richard Dice) - fixed CDATA escaping - we return a BufferText object now, to make sure that multiple charater escapes work - lots of stuff thanks to Barrie Slaymaker: - Rewrite of all consumers to be smaller and faster. - XML::SAX::Writer::ConsumerInterface::new is now a real constructor which all derived classes should call. - finalize is now not called unless it exists. - A file name of "0" will not cause an exception. - One error message now reports the package name properly if it was subclassed. - base.pm is no longer used. - the test suite runs events through Writer and in to CodeConsumer and a custom consumer. - Custom consumers are better documented (barries). 0.39 wed 20020116 22:15:41 - fixed an FH problem (Dave Rolsky) - the null converter was so null that it converted strings to nothing (Graham Barr) 0.38 wed 20020116 21:41:41 - several major bugfixes that prevented some consumers (mostly Handle and File) from working at all. - the beginning of a test suite. It doesn't test the correctness of the XML that is created yet, but it does at least go through all the basic functions that are needed for that to work. 0.37 wed 20020116 16:11:15 - on Solaris it would seem that iconv has problems converting an encoding to itself (at least, utf-8 to utf-8). Given that this is a useless operation, it is now detected and replaced with a noop (thanks to David N. Blank-Edelman for reporting this). 0.02 - 0.36 - many bugfixes thanks to many helpful people, notably Barrie Slaymaker, Dave Rolsky, Sean M. Burke, and others. 0.01 mon 20011126 02:31:58 - original version LICENSE100644001750001750 4365512431176652 15762 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56This software is copyright (c) 2014 by Robin Berjon. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2014 by Robin Berjon. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2014 by Robin Berjon. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End dist.ini100644001750001750 63712431176652 16352 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56name = XML-SAX-Writer author = Robin Berjon author = Chris Prather license = Perl_5 copyright_holder = Robin Berjon copyright_year = 2014 [@Basic] [PkgVersion] [Prereqs::FromCPANfile] [PruneFiles] match = ^nytprof.* match = ^local match = ^perl5 match = ^cpan.* [Test::Compile] [PodWeaver] [@Git] exclude_filename = Makefile.PL [Git::NextVersion] [GithubMeta] META.yml100644001750001750 135712431176652 16177 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56--- abstract: 'SAX2 XML Writer' author: - 'Robin Berjon ' - 'Chris Prather ' build_requires: File::Spec: '0' IO::Handle: '0' IPC::Open3: '0' Test::More: '0.40' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.025, CPAN::Meta::Converter version 2.142690' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: XML-SAX-Writer requires: Encode: '2.12' XML::Filter::BufferText: '1.00' XML::NamespaceSupport: '1.00' XML::SAX::Exception: '1.01' resources: homepage: https://github.com/perigrin/xml-sax-writer repository: https://github.com/perigrin/xml-sax-writer.git version: '0.56' MANIFEST100644001750001750 41512431176652 16031 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.025. Changes LICENSE MANIFEST MANIFEST.SKIP META.yml Makefile.PL README README.md dist.ini lib/XML/SAX/Writer.pm lib/XML/SAX/Writer/XML.pm t/00-compile.t t/05basic.t t/10consumers.t t/20bugs.t README.md100755001750001750 235312431176652 16205 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56XML::SAX::Writer - SAX2 XML Writer ================================== About this module ----------------- This module has been developed by Robin Berjon . Since version 0.50, it is maintained by means of the Perl XML project [perl-xml.sourceforge.net]: - The sources are stored in the SourceForge Subversion repository: repository: https://svn.sourceforge.net/svnroot/perl-xml - Requests and comments should be sent to the Perl-XML@listserv.ActiveState.com mailing list - Bugs should be reported to RT.cpan.org Robin considered this module alpha but after years of testing on humans we believe it can be considered beta now. The version 0.50 has been created by Petr Cimprich , using patches and sugestions from RT.cpan.org. Thanks go to all those who reported bugs and suggested fixes. Usage ----- > use XML::SAX::Writer; > use XML::SAX::SomeDriver; > > my $w = XML::SAX::Writer->new; > my $d = XML::SAX::SomeDriver->new(Handler => $w); > > $d->parse('some options...'); See [http://perl-xml.sourceforge.net/perl-sax/]() for more details about Perl SAX 2. License ------- This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. t000755001750001750 012431176652 15023 5ustar00perigrinperigrin000000000000XML-SAX-Writer-0.5620bugs.t100755001750001750 334712431176652 16464 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/t ### # XML::SAX::Writer tests # Petr Cimprich # 09/11/2006 - v0.01 ### use strict; use Test::More tests => 2; use XML::SAX::Writer qw(); my $isoL1 = ($^O eq 'VMS') ? 'iso8859-1' : 'iso-8859-1'; my $out = ''; my $str1 = 'foo'; my $str2 = 'žščřďťňáéíóůúý'; # can't be encoded in iso-8859-1 ################################################## # encoding test my $w = XML::SAX::Writer->new({ EncodeFrom => 'utf-8', EncodeTo => $isoL1, Output => \$out, })->{Handler}; $w->start_document; $w->start_element({Name => 'root', Prefix => '', LocalName => 'root', NamespaceURI => '', Attributes => {}}); $w->characters({Data => $str1}); $w->end_element({Name => 'root', Prefix => '', LocalName => 'root', NamespaceURI => ''}); $w->end_document; #print $out; ok($out eq "$str1", 'ASCII characters'); ################################################## # encoding error - char does not exist in a codepage $w = XML::SAX::Writer->new({ EncodeFrom => 'utf-8', EncodeTo => $isoL1, Output => \$out, })->{Handler}; # silent warnings since now $SIG{__WARN__} = sub {}; $w->start_document; $w->start_element({Name => 'root', Prefix => '', LocalName => 'root', NamespaceURI => '', Attributes => {}}); $w->characters({Data => $str2}); $w->end_element({Name => 'root', Prefix => '', LocalName => 'root', NamespaceURI => ''}); $w->end_document; ok($out eq "_LOST_DATA_", 'Latin2 characters'); 05basic.t100755001750001750 1005212431176652 16617 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/t ### # XML::SAX::Writer tests # Robin Berjon # 06/01/2002 - v0.01 ### use strict; use Test::More tests => 30; BEGIN { use_ok('XML::SAX::Writer'); } # VMS has different names for codepages my $isoL1 = ($^O eq 'VMS') ? 'iso8859-1' : 'iso-8859-1'; my $isoL2 = ($^O eq 'VMS') ? 'iso8859-2' : 'iso-8859-2'; # default options of XML::SAX::Writer my $w1 = XML::SAX::Writer->new->{Handler}; ok( $w1->{EncodeFrom} eq 'utf-8', 'default EncodeFrom'); ok( $w1->{EncodeTo} eq 'utf-8', 'default EncodeTo'); isa_ok( $w1->{Output}, 'IO::Handle', 'default Output'); is_deeply( $w1->{Format}, {}, 'default Format'); is_deeply( $w1->{Escape}, \%XML::SAX::Writer::DEFAULT_ESCAPE, 'default Escape'); is( $w1->{QuoteCharacter}, q['], 'default QuoteCharacter'); # set default options of XML::SAX::Writer my %fmt2 = ( FooBar => 1 ); my $o2 = \''; my $w2 = XML::SAX::Writer->new({ EncodeFrom => $isoL1, EncodeTo => $isoL2, Output => $o2, Format => \%fmt2, Escape => {}, QuoteCharacter => q["], })->{Handler}; ok( $w2->{EncodeFrom} eq $isoL1, 'set EncodeFrom'); ok( $w2->{EncodeTo} eq $isoL2, 'set EncodeTo'); ok( "$w2->{Output}" eq "$o2", 'set Output'); is_deeply( $w2->{Format}, \%fmt2, 'set Format'); is_deeply( $w2->{Escape}, {}, 'set Escape'); is( $w2->{QuoteCharacter}, q["], 'set QuoteCharacter'); # options after initialisation $w1->start_document; isa_ok($w1->{Encoder}, 'XML::SAX::Writer::NullConverter', 'null converter for noop encoding'); my $w3 = XML::SAX::Writer->new({ EncodeFrom => $isoL1, EncodeTo => $isoL2, })->{Handler}; $w3->start_document; isa_ok($w3->{Encoder}, 'XML::SAX::Writer::Encode', 'converter for encoding using Encode'); isa_ok($w1->{NSHelper}, 'XML::NamespaceSupport', 'ns support'); ok(ref($w1->{EscaperRegex}) eq 'Regexp', 'escaper regex'); ok(ref($w1->{NSDecl}) eq 'ARRAY', 'ns stack'); ok(@{$w1->{NSDecl}} == 0, 'ns stack is clear'); isa_ok($w1->{Consumer}, 'XML::SAX::Writer::ConsumerInterface', 'consumer is set'); # different inits (mostly for Consumer DWIM) $w1->{EncodeFrom} = $isoL1; $w1->start_document; $w1->{Output} = 'test_file_for_output'; $w1->start_document; isa_ok($w1->{Consumer}, 'XML::SAX::Writer::FileConsumer', 'consumer is FileConsumer'); my $ot = ''; $w1->{Output} = \$ot; $w1->start_document; isa_ok($w1->{Consumer}, 'XML::SAX::Writer::StringConsumer', 'consumer is StringConsumer'); $w1->{Output} = []; $w1->start_document; isa_ok($w1->{Consumer}, 'XML::SAX::Writer::ArrayConsumer', 'consumer is ArrayConsumer'); $w1->{Output} = *STDOUT; $w1->start_document; isa_ok($w1->{Consumer}, 'XML::SAX::Writer::HandleConsumer', 'consumer is HandleConsumer'); $w1->{Output} = bless [], 'Test__XSW1'; sub Test__XSW1::output {} $w1->start_document; isa_ok($w1->{Consumer}, 'Test__XSW1', 'consumer is custom'); $w1->{Output} = bless [], 'Test__XSW2'; eval { $w1->start_document; }; ok($@, 'bad consumer'); isa_ok($@, 'XML::SAX::Writer::Exception', 'bad consumer exception'); # escaping my $esc1 = '<>&"\''; my $eq1 = '<>&"''; my $res1 = $w1->escape($esc1); is($res1, $eq1, 'escaping (default)'); # converting my $conv1 = XML::SAX::Writer::NullConverter->new; my $str1 = 'TEST'; my $res_1 = $conv1->convert($str1); is($str1, $res_1, 'noop converter'); my $conv2 = XML::SAX::Writer::Encode->new('iso-8859-1', 'utf-8'); my $str2 = 'Cnvert'; my $res_2 = $conv2->convert($str2); use Encode; Encode::from_to($str2, 'iso-8859-1', 'utf-8'); is($str2, $res_2, 'Encode converter'); Makefile.PL100644001750001750 273512431176652 16701 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56 # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.025. use strict; use warnings; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "SAX2 XML Writer", "AUTHOR" => "Robin Berjon , Chris Prather ", "BUILD_REQUIRES" => { "Test::More" => "0.40" }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "XML-SAX-Writer", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "XML::SAX::Writer", "PREREQ_PM" => { "Encode" => "2.12", "XML::Filter::BufferText" => "1.00", "XML::NamespaceSupport" => "1.00", "XML::SAX::Exception" => "1.01" }, "TEST_REQUIRES" => { "File::Spec" => 0, "IO::Handle" => 0, "IPC::Open3" => 0, "Test::More" => "0.40" }, "VERSION" => "0.56", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Encode" => "2.12", "ExtUtils::MakeMaker" => 0, "File::Spec" => 0, "IO::Handle" => 0, "IPC::Open3" => 0, "Test::More" => "0.40", "XML::Filter::BufferText" => "1.00", "XML::NamespaceSupport" => "1.00", "XML::SAX::Exception" => "1.01" ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); MANIFEST.SKIP100644001750001750 33512431176652 16577 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56^\.git ^_build ^Build$ ^blib ~$ \.bak$ CVS \.svn \.DS_Store cover_db \..*\.sw.?$ ^Makefile$ ^pm_to_blib$ ^MakeMaker-\d ^blibdirs$ \.old$ ^#.*#$ ^\.# ^TODO$ ^PLANS$ ^doc/ ^benchmarks ^\._.*$ ^t\/600_todo_tests\/$ \.shipit 00-compile.t100644001750001750 215712431176652 17222 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/tuse 5.006; use strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::Compile 2.051 use Test::More; plan tests => 2 + ($ENV{AUTHOR_TESTING} ? 1 : 0); my @module_files = ( 'XML/SAX/Writer.pm', 'XML/SAX/Writer/XML.pm' ); # no fake home requested my $inc_switch = -d 'blib' ? '-Mblib' : '-Ilib'; use File::Spec; use IPC::Open3; use IO::Handle; open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; my @warnings; for my $lib (@module_files) { # see L my $stderr = IO::Handle->new; my $pid = open3($stdin, '>&STDERR', $stderr, $^X, $inc_switch, '-e', "require q[$lib]"); binmode $stderr, ':crlf' if $^O eq 'MSWin32'; my @_warnings = <$stderr>; waitpid($pid, 0); is($?, 0, "$lib loaded ok"); if (@_warnings) { warn @_warnings; push @warnings, @_warnings; } } is(scalar(@warnings), 0, 'no warnings found') or diag 'got warnings: ', ( Test::More->can('explain') ? Test::More::explain(\@warnings) : join("\n", '', @warnings) ) if $ENV{AUTHOR_TESTING}; 10consumers.t100755001750001750 466712431176652 17547 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/t ### # XML::SAX::Writer tests # Robin Berjon # 06/01/2002 - v0.01 ### use strict; package MyConsumer; @MyConsumer::ISA = qw( XML::SAX::Writer::ConsumerInterface ); sub new { my $self = shift->SUPER::new( my $output ); $$self = '' ; # Note the extra '$' return $self; } sub output { my $self = shift; $$self .= uc shift; } sub get_output { my $self = shift; return $$self; } package main; use Test::More tests => 15; use XML::SAX::Writer qw(); # StringConsumer my $ref1 = 'MUST_CLEAR'; my $str = XML::SAX::Writer::StringConsumer->new(\$ref1); isa_ok($str, 'XML::SAX::Writer::StringConsumer', 'StringConsumer'); $str->output('CONTENT'); my $res1 = $str->finalize; ok($$res1 eq 'CONTENT', 'content is set'); # ArrayConsumer my $arr = XML::SAX::Writer::ArrayConsumer->new([]); isa_ok($arr, 'XML::SAX::Writer::ArrayConsumer', 'ArrayConsumer'); $arr->output('CONTENT0'); $arr->output('CONTENT1'); my $res2 = $arr->finalize; ok($res2->[0] eq 'CONTENT0', 'content (1)'); ok($res2->[1] eq 'CONTENT1', 'content (2)'); # HandleConsumer and FileConsumer my $fil1 = XML::SAX::Writer::FileConsumer->new('test_file1'); isa_ok($fil1, 'XML::SAX::Writer::FileConsumer', 'FileConsumer'); isa_ok($fil1, 'XML::SAX::Writer::HandleConsumer', 'HandleConsumer'); $fil1->output('FILE ONE'); my $fil2 = XML::SAX::Writer::FileConsumer->new('test_file2'); $fil2->output('FILE TWO'); $fil1->output(' FILE ONE'); $fil2->output(' FILE TWO'); $fil1->finalize; $fil2->finalize; open FH1, "test_file1" or die $!; my $cnt1 = ; close FH1; open FH2, "test_file2" or die $!; my $cnt2 = ; close FH2; ok($cnt1 eq 'FILE ONE FILE ONE', 'file content (1)'); ok($cnt2 eq 'FILE TWO FILE TWO', 'file content (2)'); ## ## Now, test some of the consumers "in situ" ## sub push_events { my $w = XML::SAX::Writer->new( Output => shift ); $w->start_document( {} ); $w->start_element( { Name => "foo" } ); $w->end_element( { Name => "foo" } ); $w->end_document( {} ); } { my @events; push_events( sub { push @events, [ @_ ] } ); ok @events >= 3; ok $events[0]->[0], "start_document"; ok $events[1]->[0], "data"; ok $events[-1]->[0], "end_document"; ## This next test might break id X::S::W ever stops ## putting that space in there. ok join( "", map $_->[1], @events ), ""; } ## ## A custom consumer ## { my $c = MyConsumer->new; push_events( $c ); ok $c->get_output, ""; } SAX000755001750001750 012431176652 16421 5ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/lib/XMLWriter.pm100755001750001750 5552412431176652 20431 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/lib/XML/SAXpackage XML::SAX::Writer; $XML::SAX::Writer::VERSION = '0.56'; use strict; use vars qw(%DEFAULT_ESCAPE %COMMENT_ESCAPE); # ABSTRACT: SAX2 XML Writer use Encode qw(); use XML::SAX::Exception qw(); use XML::SAX::Writer::XML qw(); use XML::Filter::BufferText qw(); @XML::SAX::Writer::Exception::ISA = qw(XML::SAX::Exception); %DEFAULT_ESCAPE = ( '&' => '&', '<' => '<', '>' => '>', '"' => '"', "'" => ''', ); %COMMENT_ESCAPE = ( '--' => '--', ); #-------------------------------------------------------------------# # new #-------------------------------------------------------------------# sub new { my $class = ref($_[0]) ? ref(shift) : shift; my $opt = (@_ == 1) ? { %{shift()} } : {@_}; # default the options $opt->{Writer} ||= 'XML::SAX::Writer::XML'; $opt->{Escape} ||= \%DEFAULT_ESCAPE; $opt->{CommentEscape} ||= \%COMMENT_ESCAPE; $opt->{EncodeFrom} = exists $opt->{EncodeFrom} ? $opt->{EncodeFrom} : 'utf-8'; $opt->{EncodeTo} = exists $opt->{EncodeTo} ? $opt->{EncodeTo} : 'utf-8'; $opt->{Format} ||= {}; # needs options w/ defaults, we'll see later $opt->{Output} ||= *{STDOUT}{IO}; $opt->{QuoteCharacter} ||= q[']; eval "use $opt->{Writer};"; my $obj = bless $opt, $opt->{Writer}; $obj->init; # we need to buffer the text to escape it right my $bf = XML::Filter::BufferText->new( Handler => $obj ); return $bf; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # init #-------------------------------------------------------------------# sub init {} # noop, for subclasses #-------------------------------------------------------------------# #-------------------------------------------------------------------# # setConverter #-------------------------------------------------------------------# sub setConverter { my $self = shift; if (lc($self->{EncodeFrom}) ne lc($self->{EncodeTo})) { $self->{Encoder} = XML::SAX::Writer::Encode->new($self->{EncodeFrom}, $self->{EncodeTo}); } else { $self->{Encoder} = XML::SAX::Writer::NullConverter->new; } return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # setConsumer #-------------------------------------------------------------------# sub setConsumer { my $self = shift; # create the Consumer my $ref = ref $self->{Output}; if ($ref eq 'SCALAR') { $self->{Consumer} = XML::SAX::Writer::StringConsumer->new($self->{Output}); } elsif ($ref eq 'CODE') { $self->{Consumer} = XML::SAX::Writer::CodeConsumer->new($self->{Output}); } elsif ($ref eq 'ARRAY') { $self->{Consumer} = XML::SAX::Writer::ArrayConsumer->new($self->{Output}); } elsif ( $ref eq 'GLOB' or UNIVERSAL::isa(\$self->{Output}, 'GLOB') or UNIVERSAL::isa($self->{Output}, 'IO::Handle')) { $self->{Consumer} = XML::SAX::Writer::HandleConsumer->new($self->{Output}); } elsif (not $ref) { $self->{Consumer} = XML::SAX::Writer::FileConsumer->new($self->{Output}); } elsif (UNIVERSAL::can($self->{Output}, 'output')) { $self->{Consumer} = $self->{Output}; } else { XML::SAX::Writer::Exception->throw( Message => 'Unknown option for Output' ); } return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # setEscaperRegex #-------------------------------------------------------------------# sub setEscaperRegex { my $self = shift; $self->{EscaperRegex} = eval 'qr/' . join( '|', map { $_ = "\Q$_\E" } keys %{$self->{Escape}}) . '/;' ; return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # setCommentEscaperRegex #-------------------------------------------------------------------# sub setCommentEscaperRegex { my $self = shift; $self->{CommentEscaperRegex} = eval 'qr/' . join( '|', map { $_ = "\Q$_\E" } keys %{$self->{CommentEscape}}) . '/;' ; return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # escape #-------------------------------------------------------------------# sub escape { my $self = shift; my $str = shift; $str =~ s/($self->{EscaperRegex})/$self->{Escape}->{$1}/ge; return $str; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # escapeComment #-------------------------------------------------------------------# sub escapeComment { my $self = shift; my $str = shift; $str =~ s/($self->{CommentEscaperRegex})/$self->{CommentEscape}->{$1}/ge; return $str; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # convert and checking the return value #-------------------------------------------------------------------# sub safeConvert { my $self = shift; my $str = shift; my $out = $self->{Encoder}->convert($str); if (!defined $out && defined $str) { warn "Conversion error returned by Encoder [$self->{Encoder}], string: '$str'"; $out = '_LOST_DATA_'; } return $out; } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, The Empty Consumer ,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# # this package is only there to provide a smooth upgrade path in case # new methods are added to the interface package XML::SAX::Writer::ConsumerInterface; $XML::SAX::Writer::ConsumerInterface::VERSION = '0.56'; sub new { my $class = shift; my $ref = shift; ## $self is a reference to the reference that we will send output ## to. This allows us to bless $self without blessing $$self. return bless \$ref, ref $class || $class; } sub output {} sub finalize {} #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, The String Consumer `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::StringConsumer; $XML::SAX::Writer::StringConsumer::VERSION = '0.56'; @XML::SAX::Writer::StringConsumer::ISA = qw(XML::SAX::Writer::ConsumerInterface); #-------------------------------------------------------------------# # new #-------------------------------------------------------------------# sub new { my $self = shift->SUPER::new( @_ ); ${${$self}} = ''; return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # output #-------------------------------------------------------------------# sub output { ${${$_[0]}} .= $_[1] } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # finalize #-------------------------------------------------------------------# sub finalize { ${$_[0]} } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, The Code Consumer `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::CodeConsumer; $XML::SAX::Writer::CodeConsumer::VERSION = '0.56'; @XML::SAX::Writer::CodeConsumer::ISA = qw(XML::SAX::Writer::ConsumerInterface ); #-------------------------------------------------------------------# # new #-------------------------------------------------------------------# sub new { my $self = shift->SUPER::new( @_ ); $$self->( 'start_document', '' ); return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # output #-------------------------------------------------------------------# sub output { ${$_[0]}->('data', pop) } ## Avoid an extra copy #-------------------------------------------------------------------# #-------------------------------------------------------------------# # finalize #-------------------------------------------------------------------# sub finalize { ${$_[0]}->('end_document', '') } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, The Array Consumer ,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::ArrayConsumer; $XML::SAX::Writer::ArrayConsumer::VERSION = '0.56'; @XML::SAX::Writer::ArrayConsumer::ISA = qw(XML::SAX::Writer::ConsumerInterface); #-------------------------------------------------------------------# # new #-------------------------------------------------------------------# sub new { my $self = shift->SUPER::new( @_ ); @$$self = (); return $self; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # output #-------------------------------------------------------------------# sub output { push @${$_[0]}, pop } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # finalize #-------------------------------------------------------------------# sub finalize { return ${$_[0]} } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, The Handle Consumer `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::HandleConsumer; $XML::SAX::Writer::HandleConsumer::VERSION = '0.56'; @XML::SAX::Writer::HandleConsumer::ISA = qw(XML::SAX::Writer::ConsumerInterface); #-------------------------------------------------------------------# # output #-------------------------------------------------------------------# sub output { my $fh = ${$_[0]}; print $fh pop or XML::SAX::Exception->throw( Message => "Could not write to handle: $fh ($!)" ); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # finalize #-------------------------------------------------------------------# sub finalize { return 0 } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, The File Consumer `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::FileConsumer; $XML::SAX::Writer::FileConsumer::VERSION = '0.56'; @XML::SAX::Writer::FileConsumer::ISA = qw(XML::SAX::Writer::HandleConsumer); #-------------------------------------------------------------------# # new #-------------------------------------------------------------------# sub new { my ( $proto, $file ) = ( shift, shift ); XML::SAX::Writer::Exception->throw( Message => "No filename provided to " . ref( $proto || $proto ) ) unless defined $file; local *XFH; open XFH, ">$file" or XML::SAX::Writer::Exception->throw( Message => "Error opening file $file: $!" ); return $proto->SUPER::new( *{XFH}{IO}, @_ ); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # finalize #-------------------------------------------------------------------# sub finalize { close ${$_[0]}; return 0; } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, Noop Converter ,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::NullConverter; $XML::SAX::Writer::NullConverter::VERSION = '0.56'; sub new { return bless [], __PACKAGE__ } sub convert { $_[1] } #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, Encode Converter ,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# package XML::SAX::Writer::Encode; $XML::SAX::Writer::Encode::VERSION = '0.56'; sub new { my ($class, $from, $to) = @_; my $self = { from_enc => $from, to_enc => $to, }; return bless $self, $class; } sub convert { my ($self, $data) = @_; eval { $data = Encode::decode($self->{from_enc}, $data) if $self->{from_enc}; $data = Encode::encode($self->{to_enc}, $data, Encode::FB_CROAK) if $self->{to_enc}; }; if ($@) { warn $@; return; } return $data; }; 1; =pod =encoding UTF-8 =head1 NAME XML::SAX::Writer - SAX2 XML Writer =head1 VERSION version 0.56 =head1 SYNOPSIS use XML::SAX::Writer; use XML::SAX::SomeDriver; my $w = XML::SAX::Writer->new; my $d = XML::SAX::SomeDriver->new(Handler => $w); $d->parse('some options...'); =head1 DESCRIPTION =head2 Why yet another XML Writer ? A new XML Writer was needed to match the SAX2 effort because quite naturally no existing writer understood SAX2. My first intention had been to start patching XML::Handler::YAWriter as it had previously been my favourite writer in the SAX1 world. However the more I patched it the more I realised that what I thought was going to be a simple patch (mostly adding a few event handlers and changing the attribute syntax) was turning out to be a rewrite due to various ideas I'd been collecting along the way. Besides, I couldn't find a way to elegantly make it work with SAX2 without breaking the SAX1 compatibility which people are probably still using. There are of course ways to do that, but most require user interaction which is something I wanted to avoid. So in the end there was a new writer. I think it's in fact better this way as it helps keep SAX1 and SAX2 separated. =head1 METHODS =over 4 =item * new(%hash) This is the constructor for this object. It takes a number of parameters, all of which are optional. =item * Output This parameter can be one of several things. If it is a simple scalar, it is interpreted as a filename which will be opened for writing. If it is a scalar reference, output will be appended to this scalar. If it is an array reference, output will be pushed onto this array as it is generated. If it is a filehandle, then output will be sent to this filehandle. Finally, it is possible to pass an object for this parameter, in which case it is assumed to be an object that implements the consumer interface L. If this parameter is not provided, then output is sent to STDOUT. =item * Escape This should be a hash reference where the keys are characters sequences that should be escaped and the values are the escaped form of the sequence. By default, this module will escape the ampersand (&), less than (<), greater than (>), double quote ("), and apostrophe ('). Note that some browsers don't support the ' escape used for apostrophes so that you should be careful when outputting XHTML. If you only want to add entries to the Escape hash, you can first copy the contents of %XML::SAX::Writer::DEFAULT_ESCAPE. =item * CommentEscape Comment content often needs to be escaped differently from other content. This option works exactly as the previous one except that by default it only escapes the double dash (--) and that the contents can be copied from %XML::SAX::Writer::COMMENT_ESCAPE. =item * EncodeFrom The character set encoding in which incoming data will be provided. This defaults to UTF-8, which works for US-ASCII as well. Set this to C if you do not wish to decode your data. =item * EncodeTo The character set encoding in which output should be encoded. Again, this defaults to UTF-8. Set this to C if you do not with to encode your data. =item * QuoteCharacter Set the character used to quote attributes. This defaults to single quotes (') for backwards compatibility. =back =head1 THE CONSUMER INTERFACE XML::SAX::Writer can receive pluggable consumer objects that will be in charge of writing out what is formatted by this module. Setting a Consumer is done by setting the Output option to the object of your choice instead of to an array, scalar, or file handle as is more commonly done (internally those in fact map to Consumer classes and and simply available as options for your convenience). If you don't understand this, don't worry. You don't need it most of the time. That object can be from any class, but must have two methods in its API. It is also strongly recommended that it inherits from XML::SAX::Writer::ConsumerInterface so that it will not break if that interface evolves over time. There are examples at the end of XML::SAX::Writer's code. The two methods that it needs to implement are: =over 4 =item * output STRING (Required) This is called whenever the Writer wants to output a string formatted in XML. Encoding conversion, character escaping, and formatting have already taken place. It's up to the consumer to do whatever it wants with the string. =item * finalize() (Optional) This is called once the document has been output in its entirety, during the end_document event. end_document will in fact return whatever finalize() returns, and that in turn should be returned by parse() for whatever parser was invoked. It might be useful if you need to provide feedback of some sort. =back Here's an example of a custom consumer. Note the extra C<$> signs in front of $self; the base class is optimized for the overwhelmingly common case where only one data member is required and $self is a reference to that data member. package MyConsumer; @ISA = qw( XML::SAX::Writer::ConsumerInterface ); use strict; sub new { my $self = shift->SUPER::new( my $output ); $$self = ''; # Note the extra '$' return $self; } sub output { my $self = shift; $$self .= uc shift; } sub get_output { my $self = shift; return $$self; } And here is one way to use it: my $c = MyConsumer->new; my $w = XML::SAX::Writer->new( Output => $c ); ## ... send events to $w ... print $c->get_output; If you need to store more that one data member, pass in an array or hash reference: my $self = shift->SUPER::new( {} ); and access it like: sub output { my $self = shift; $$self->{Output} .= uc shift; } =head1 THE ENCODER INTERFACE Encoders can be plugged in to allow one to use one's favourite encoder object. Presently there are two encoders: Encode and NullEncoder. They need to implement two methods, and may inherit from XML::SAX::Writer::NullConverter if they wish to =over 4 =item new FROM_ENCODING, TO_ENCODING Creates a new Encoder. The arguments are the chosen encodings. =item convert STRING Converts that string and returns it. =back Note that the return value of the convert method is B checked. Output may be truncated if a character couldn't be converted correctly. To avoid problems the encoder should take care encoding errors itself, for example by raising an exception. =head1 CUSTOM OUTPUT This module is generally used to write XML -- which it does most of the time -- but just like the rest of SAX it can be used as a generic framework to output data, the opposite of a non-XML SAX parser. Of course there's only so much that one can abstract, so depending on your format this may or may not be useful. If it is, you'll need to know the following API (and probably to have a look inside C, the default Writer). =over =item init Called before the writing starts, it's a chance for the subclass to do some initialisation if it needs it. =item setConverter This is used to set the proper converter for character encodings. The default implementation should suffice but you can override it. It must set C<< $self->{Encoder} >> to an Encoder object. Subclasses *should* call it. =item setConsumer Same as above, except that it is for the Consumer object, and that it must set C<< $self->{Consumer} >>. =item setEscaperRegex Will initialise the escaping regex C<< $self->{EscaperRegex} >> based on what is needed. =item escape STRING Takes a string and escapes it properly. =item setCommentEscaperRegex and escapeComment STRING These work exactly the same as the two above, except that they are meant to operate on comment contents, which often have different escaping rules than those that apply to regular content. =back =head1 TODO - proper UTF-16 handling - the formatting options need to be developed. - test, test, test (and then some tests) - doc, doc, doc (actually this part is in better shape) - remove the xml_decl and replace it with intelligent logic, as discussed on perl-xml - make a the Consumer selecting code available in the API, to avoid duplicating - add an Apache output Consumer, triggered by passing $r as Output =head1 CREDITS Michael Koehne (XML::Handler::YAWriter) for much inspiration and Barrie Slaymaker for the Consumer pattern idea, the coderef output option and miscellaneous bugfixes and performance tweaks. Of course the usual suspects (Kip Hampton and Matt Sergeant) helped in the usual ways. =head1 SEE ALSO XML::SAX::* =head1 AUTHORS =over 4 =item * Robin Berjon =item * Chris Prather =back =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2014 by Robin Berjon. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut __END__ #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, Documentation `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# Writer000755001750001750 012431176652 17675 5ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/lib/XML/SAXXML.pm100755001750001750 4407512431176652 21070 0ustar00perigrinperigrin000000000000XML-SAX-Writer-0.56/lib/XML/SAX/Writerpackage XML::SAX::Writer::XML; $XML::SAX::Writer::XML::VERSION = '0.56'; use strict; use XML::NamespaceSupport qw(); @XML::SAX::Writer::XML::ISA = qw(XML::SAX::Writer); # ABSTRACT: XML::SAX::Writer's SAX Handler ### # Robin Berjon ### #-------------------------------------------------------------------# # start_document #-------------------------------------------------------------------# sub start_document { my $self = shift; $self->setConverter; $self->setEscaperRegex; $self->setCommentEscaperRegex; $self->{NSDecl} = []; $self->{NSHelper} = XML::NamespaceSupport->new({ xmlns => 1, fatal_errors => 0 }); $self->{NSHelper}->pushContext; $self->setConsumer; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # end_document #-------------------------------------------------------------------# sub end_document { my $self = shift; # we may need to do a little more here $self->{NSHelper}->popContext; return $self->{Consumer}->finalize if $self->{Consumer}->can( 'finalize' ); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # start_element #-------------------------------------------------------------------# sub start_element { my $self = shift; my $data = shift; $self->_output_element; my $attr = $data->{Attributes}; # fix the namespaces and prefixes of what we're receiving, in case # something is wrong if ($data->{NamespaceURI}) { my $uri = $self->{NSHelper}->getURI($data->{Prefix}) || ''; if ($uri ne $data->{NamespaceURI}) { # ns has precedence $data->{Prefix} = $self->{NSHelper}->getPrefix($data->{NamespaceURI}); # random, but correct $data->{Name} = $data->{Prefix} ? "$data->{Prefix}:$data->{LocalName}" : "$data->{LocalName}"; } } elsif ($data->{Prefix}) { # we can't have a prefix and no NS $data->{Name} = $data->{LocalName}; $data->{Prefix} = ''; } # create a hash containing the attributes so that we can ensure there is # no duplication. Also, we check that ns are properly declared, that the # Name is good, etc... my %attr_hash; for my $at (values %$attr) { next unless length $at->{Name}; # people have trouble with autovivification if ($at->{NamespaceURI}) { my $uri = $self->{NSHelper}->getURI($at->{Prefix}); warn "Well formed error: prefix '$at->{Prefix}' is not bound to any URI" unless defined $uri; if (defined $uri and $uri ne $at->{NamespaceURI}) { # ns has precedence $at->{Prefix} = $self->{NSHelper}->getPrefix($at->{NamespaceURI}); # random, but correct $at->{Name} = $at->{Prefix} ? "$at->{Prefix}:$at->{LocalName}" : "$at->{LocalName}"; } } elsif ($at->{Prefix}) { # we can't have a prefix and no NS $at->{Name} = $at->{LocalName}; $at->{Prefix} = ''; } $attr_hash{$at->{Name}} = $at->{Value}; } for my $nd (@{$self->{NSDecl}}) { if ($nd->{Prefix}) { $attr_hash{'xmlns:' . $nd->{Prefix}} = $nd->{NamespaceURI}; } else { $attr_hash{'xmlns'} = $nd->{NamespaceURI}; } } $self->{NSDecl} = []; # build a string from what we have, and buffer it my $el = '<' . $data->{Name}; for my $k (keys %attr_hash) { $el .= ' ' . $k . qq[=$self->{QuoteCharacter}] . $self->escape($attr_hash{$k}) . qq[$self->{QuoteCharacter}]; } $self->{BufferElement} = $el; $self->{NSHelper}->pushContext; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # end_element #-------------------------------------------------------------------# sub end_element { my $self = shift; my $data = shift; my $el; if ($self->{BufferElement}) { $el = $self->{BufferElement} . ' />'; } else { $el = '{Name} . '>'; } $el = $self->safeConvert($el); $self->{Consumer}->output($el); $self->{NSHelper}->popContext; $self->{BufferElement} = ''; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # characters #-------------------------------------------------------------------# sub characters { my $self = shift; my $data = shift; $self->_output_element; my $char = $data->{Data}; if ($self->{InCDATA}) { # we must scan for ]]> in the CDATA and escape it if it # is present by close--opening # we need to have buffer text in front of this... $char = join ']]>]]<', $char; } else { $char = $self->escape($char); } $char = $self->safeConvert($char); $self->{Consumer}->output($char); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # start_prefix_mapping #-------------------------------------------------------------------# sub start_prefix_mapping { my $self = shift; my $data = shift; push @{$self->{NSDecl}}, $data; $self->{NSHelper}->declarePrefix($data->{Prefix}, $data->{NamespaceURI}); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # end_prefix_mapping #-------------------------------------------------------------------# sub end_prefix_mapping {} #-------------------------------------------------------------------# #-------------------------------------------------------------------# # processing_instruction #-------------------------------------------------------------------# sub processing_instruction { my $self = shift; my $data = shift; $self->_output_element; $self->_output_dtd; my $pi = "{Target} $data->{Data}?>"; $pi = $self->safeConvert($pi); $self->{Consumer}->output($pi); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # ignorable_whitespace #-------------------------------------------------------------------# sub ignorable_whitespace { my $self = shift; my $data = shift; $self->_output_element; my $char = $data->{Data}; $char = $self->escape($char); $char = $self->safeConvert($char); $self->{Consumer}->output($char); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # skipped_entity #-------------------------------------------------------------------# sub skipped_entity { my $self = shift; my $data = shift; $self->_output_element; $self->_output_dtd; my $ent; if ($data->{Name} =~ m/^%/) { $ent = $data->{Name} . ';'; } elsif ($data->{Name} eq '[dtd]') { # ignoring } else { $ent = '&' . $data->{Name} . ';'; } $ent = $self->safeConvert($ent); $self->{Consumer}->output($ent); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # notation_decl #-------------------------------------------------------------------# sub notation_decl { my $self = shift; my $data = shift; $self->_output_dtd; # I think that param entities are normalized before this my $not = " {Name}; if ($data->{PublicId} and $data->{SystemId}) { $not .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\''; } elsif ($data->{PublicId}) { $not .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\''; } else { $not .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\''; } $not .= " >\n"; $not = $self->safeConvert($not); $self->{Consumer}->output($not); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # unparsed_entity_decl #-------------------------------------------------------------------# sub unparsed_entity_decl { my $self = shift; my $data = shift; $self->_output_dtd; # I think that param entities are normalized before this my $ent = " {Name}; if ($data->{PublicId}) { $ent .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\''; } else { $ent .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\''; } $ent .= " NDATA $data->{Notation} >\n"; $ent = $self->safeConvert($ent); $self->{Consumer}->output($ent); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # element_decl #-------------------------------------------------------------------# sub element_decl { my $self = shift; my $data = shift; $self->_output_dtd; # I think that param entities are normalized before this my $eld = " {Name} . ' ' . $data->{Model} . " >\n"; $eld = $self->safeConvert($eld); $self->{Consumer}->output($eld); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # attribute_decl #-------------------------------------------------------------------# sub attribute_decl { my $self = shift; my $data = shift; $self->_output_dtd; # to be backward compatible with Perl SAX 2.0 $data->{Mode} = $data->{ValueDefault} if not(exists $data->{Mode}) and exists $data->{ValueDefault}; # I think that param entities are normalized before this my $atd = " {eName} . ' ' . $data->{aName} . ' '; $atd .= $data->{Type} . ' ' . $data->{Mode} . ' '; $atd .= $data->{Value} . ' ' if $data->{Value}; $atd .= " >\n"; $atd = $self->safeConvert($atd); $self->{Consumer}->output($atd); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # internal_entity_decl #-------------------------------------------------------------------# sub internal_entity_decl { my $self = shift; my $data = shift; $self->_output_dtd; # I think that param entities are normalized before this my $ent = " {Name} . ' \'' . $self->escape($data->{Value}) . "' >\n"; $ent = $self->safeConvert($ent); $self->{Consumer}->output($ent); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # external_entity_decl #-------------------------------------------------------------------# sub external_entity_decl { my $self = shift; my $data = shift; $self->_output_dtd; # I think that param entities are normalized before this my $ent = " {Name}; if ($data->{PublicId}) { $ent .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\''; } else { $ent .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\''; } $ent .= " >\n"; $ent = $self->safeConvert($ent); $self->{Consumer}->output($ent); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # comment #-------------------------------------------------------------------# sub comment { my $self = shift; my $data = shift; $self->_output_element; $self->_output_dtd; my $cmt = ''; $cmt = $self->safeConvert($cmt); $self->{Consumer}->output($cmt); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # start_dtd #-------------------------------------------------------------------# sub start_dtd { my $self = shift; my $data = shift; my $dtd = '{Name}; if ($data->{PublicId}) { $dtd .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\''; } elsif ($data->{SystemId}) { $dtd .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\''; } $self->{BufferDTD} = $dtd; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # end_dtd #-------------------------------------------------------------------# sub end_dtd { my $self = shift; my $data = shift; my $dtd; if ($self->{BufferDTD}) { $dtd = $self->{BufferDTD} . ' >'; } else { $dtd = ' ]>'; } $dtd = $self->safeConvert($dtd); $self->{Consumer}->output($dtd); $self->{BufferDTD} = ''; } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # start_cdata #-------------------------------------------------------------------# sub start_cdata { my $self = shift; $self->_output_element; $self->{InCDATA} = 1; my $cds = $self->{Encoder}->convert('{Consumer}->output($cds); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # end_cdata #-------------------------------------------------------------------# sub end_cdata { my $self = shift; $self->{InCDATA} = 0; my $cds = $self->{Encoder}->convert(']]>'); $self->{Consumer}->output($cds); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # start_entity #-------------------------------------------------------------------# sub start_entity { my $self = shift; my $data = shift; $self->_output_element; $self->_output_dtd; my $ent; if ($data->{Name} eq '[dtd]') { # we ignore the fact that we're dealing with an external # DTD entity here, and probably shouldn't write the DTD # events unless explicitly told to # this will probably change } elsif ($data->{Name} =~ m/^%/) { $ent = $data->{Name} . ';'; } else { $ent = '&' . $data->{Name} . ';'; } $ent = $self->safeConvert($ent); $self->{Consumer}->output($ent); } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # end_entity #-------------------------------------------------------------------# sub end_entity { # depending on what is done above, we might need to do sth here } #-------------------------------------------------------------------# ### SAX1 stuff ###################################################### #-------------------------------------------------------------------# # xml_decl #-------------------------------------------------------------------# sub xml_decl { my $self = shift; my $data = shift; # version info is compulsory, contrary to what some seem to think # also, there's order in the pseudo-attr my $xd = ''; if ($data->{Version}) { $xd .= "{Version}'"; if ($data->{Encoding}) { $xd .= " encoding='$data->{Encoding}'"; } if ($data->{Standalone}) { $xd .= " standalone='$data->{Standalone}'"; } $xd .= '?>'; } #$xd = $self->{Encoder}->convert($xd); # this may blow up $self->{Consumer}->output($xd); } #-------------------------------------------------------------------# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, Helpers `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# #-------------------------------------------------------------------# # _output_element #-------------------------------------------------------------------# sub _output_element { my $self = shift; if ($self->{BufferElement}) { my $el = $self->{BufferElement} . '>'; $el = $self->safeConvert($el); $self->{Consumer}->output($el); $self->{BufferElement} = ''; } } #-------------------------------------------------------------------# #-------------------------------------------------------------------# # _output_dtd #-------------------------------------------------------------------# sub _output_dtd { my $self = shift; if ($self->{BufferDTD}) { my $dtd = $self->{BufferDTD} . " [\n"; $dtd = $self->safeConvert($dtd); $self->{Consumer}->output($dtd); $self->{BufferDTD} = ''; } } #-------------------------------------------------------------------# 1; #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #`,`, Documentation `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# #```````````````````````````````````````````````````````````````````# __END__ =pod =encoding UTF-8 =head1 NAME XML::SAX::Writer::XML - XML::SAX::Writer's SAX Handler =head1 VERSION version 0.56 =head1 SYNOPSIS ... =head1 DESCRIPTION ... =head1 NAME XML::SAX::Writer::XML - SAX2 XML Writer =head1 AUTHOR Robin Berjon, robin@knowscape.com =head1 COPYRIGHT Copyright (c) 2001-2006 Robin Berjon and Perl XML project. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO XML::SAX::* =head1 AUTHORS =over 4 =item * Robin Berjon =item * Chris Prather =back =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2014 by Robin Berjon. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut