Yet another perl sax xml writer
use XML::Handler::YAWriter; my $ya = new XML::Handler::YAWriter( %options ); my $perlsax = new XML::Parser::PerlSAX( 'Handler' => $ya );
YAWriter implements Yet Another XML::Handler::Writer. The reasons for this one are that I needed a flexible escaping technique, and want some kind of pretty printing. If an instance of YAWriter is created without any options, the default behavior is to produce an array of strings containing the \s-1XML\s0 in :
@{$ya->{Strings}}
Options are given in the usual 'key' => 'value' idiom.
This option tells YAWriter to use an already open file for output, instead of using $ya->{Strings} to store the array of strings. It should be noted that the only thing the object needs to implement is the print method. So anything can be used to receive a stream of strings from YAWriter.
This option will cause start_document to open named file and end_document to close it. Use the literal dash \*(L"-\*(R" if you want to print on standard output.
This option will cause start_document to open a pipe and end_document to close it. The pipe is a normal shell command. Secure shell comes handy but has a 2GB limit on most systems.
This option will force storage of the \s-1XML\s0 in $ya->{Strings}, even if the Output option is given.
This option will cause end_document to return the complete \s-1XML\s0 document in a single string. Most \s-1SAX\s0 drivers return the value of end_document as a result of their parse method. As this may not work with some combinations of \s-1SAX\s0 drivers and filters, a join of $ya->{Strings} in the controlling method is preferred.
This will change the default encoding from \s-1UTF-8\s0 to anything you like. You should ensure that given data are already in this encoding or provide an Escape hash, to tell YAWriter about the recoding.
The Escape hash defines substitutions that have to be done to any string, with the exception of the processing_instruction and doctype_decl methods, where I think that escaping of target and data would cause more trouble than necessary. The default value for Escape is $XML::Handler::YAWriter::escape = { '&' => '&', '<' => '<', '>' => '>', '"' => '"', '--' => '--' }; YAWriter will use an evaluated sub to make the recoding based on a given Escape hash reasonably fast. Future versions may use \s-1XS\s0 to improve this performance bottleneck.
Hash of string => boolean tuples, to define kind of prettyprinting. Default to undef. Possible string values:
Add hidden newline before \*(L">\*(R"
Add hidden tabulation for attributes
Catch empty Elements, apply \*(L"/>\*(R" compression
Catch whitespace with comments
Places Attributes on the same line as the Element
This option will cause start_document, processing_instruction and doctype_decl to appear as \s-1SGML\s0. The \s-1SGML\s0 is still well-formed of course, if your \s-1SAX\s0 events are well-formed.
Supress Comments
Supress \s-1DTD\s0
Supress Processing Instructions
Supress <?xml ... ?> Prolog
Supress WhiteSpace to clean documents from prior pretty printing.
Add visible indent before any eventstring
Add visible newlines before any eventstring
Output only \s-1SAX1\s0 compliant eventstrings
Correct handling of start_document and end_document is required!
The YAWriter Object initialises its structures during start_document and does its cleanup during end_document. If you forget to call start_document, any other method will break during the run. Most likely place is the encode method, trying to eval undef as a subroutine. If you forget to call end_document, you should not use a single instance of YAWriter more than once.
For small documents AsArray may be the fastest method and AsString the easiest one to receive the output of YAWriter. But AsString and AsArray may run out of memory with infinite \s-1SAX\s0 streams. The only method XML::Handler::Writer calls on a given Output object is the print method. So it's easy to use a self written Output object to improve streaming.
A single instance of XML::Handler::YAWriter is able to produce more than one file in a single run. Be sure to provide a fresh IO::File as Output before you call start_document and close this File after calling end_document. Or provide a filename in AsFile, so start_document and end_document can open and close its own filehandle.
Automatic recoding between 8bit and 16bit does not work in any Perl correctly !
I have Perl-5.00563 at home and here I can specify \*(L"use utf8;\*(R" in the right places to make recoding work. But I dislike saying \*(L"use 5.00555;\*(R" because many systems run 5.00503.
If you use some 8bit character set internally and want use national characters, either state your character as Encoding to be \s-1ISO-8859-1\s0, or provide an Escape hash similar to the following :
$ya->{'Escape'} = { '&' => '&', '<' => '<', '>' => '>', '"' => '"', '--' => '--' 'o\*:' => 'ö' 'a\*:' => 'ä' 'u\*:' => 'ü' 'O\*:' => 'Ö' 'A\*:' => 'Ä' 'U\*:' => 'Ü' '\*8' => 'ß' };
You may abuse YAWriter to clean whitespace from \s-1XML\s0 documents. Take a look at test.pl, doing just that with an XML::Edifact message, without querying the \s-1DTD\s0. This may work in 99% of the cases where you want to get rid of ignorable whitespace caused by the various forms of pretty printing.
my $ya = new XML::Handler::YAWriter( 'Output' => new IO::File ( ">-" ); 'Pretty' => { 'NoWhiteSpace'=>1, 'NoComments'=>1, 'AddHiddenNewline'=>1, 'AddHiddenAttrTab'=>1, } );
XML::Handler::Writer implements any method XML::Parser::PerlSAX wants. This extends the Java \s-1SAX1\s0.0 specification. I have in mind using Pretty=>SAX1=>1 to disable this feature, if abusing YAWriter for a \s-1SAX\s0 proxy.
Michael Koehne, [email protected]
\*(L"Derksen, Eduard (Enno), \s-1CSCIO\s0\*(R" <[email protected]> helped me with the Escape hash and gave quite a lot of useful comments.
perl and XML::Parser::PerlSAX