|
NAMEXML::Filter::Merger - Assemble multiple SAX streams in to one documentVERSIONversion 0.46SYNOPSIS## See XML::SAX::Manifold and XML::SAX::ByRecord for easy ways ## to use this processor. my $w = XML::SAX::Writer->new( Output => \*STDOUT ); my $h = XML::Filter::Merger->new( Handler => $w ); my $p = XML::SAX::ParserFactory->parser( Handler => $h ); ## To insert second and later docs in to the first doc: $h->start_manifold_document( {} ); $p->parse_file( $_ ) for @ARGV; $h->end_manifold_document( {} ); ## To insert multiple docs inline (especially useful if ## a subclass does the inline parse): $h->start_document( {} ); $h->start_element( { ... } ); .... $h->start_element( { Name => "foo", ... } ); $p->parse_uri( $uri ); ## Body of $uri inserted in <foo>...</foo> $h->end_element( { Name => "foo", ... } ); ... DESCRIPTIONCombines several documents in to one "manifold" document. This can be done in two ways, both of which start by parsing a master document in to which (the guts of) secondary documents will be inserted.Inlining Secondary DocumentsThe most SAX-like way is to simply pause the parsing of the master document between the two events where you want to insert a secondard document and parse the complete secondard document right then and there so it's events are inserted in the pipeline at the right spot. XML::Filter::Merger only passes the content of the secondary document's root element:my $h = XML::Filter::Merger->new( Handler => $w ); $h->start_document( {} ); $h->start_element( { Name => "foo1" } ); $p->parse_string( "<foo2><baz /></foo2>" ); $h->end_element( { Name => "foo1" } ); $h->end_document( {} ); results in $w seeing a document like "<foo1><baz/></foo1>". This technique is especially useful when subclassing XML::Filter::Merger to implement XInclude-like behavior. Here's a useless example that inserts some content after each "characters()" event: package Subclass; use vars qw( @ISA ); @ISA = qw( XML::Filter::Merger ); sub characters { my $self = shift; return $self->SUPER::characters( @_ ) ## ** unless $self->in_master_document; ## ** my $r = $self->SUPER::characters( @_ ); $self->set_include_all_roots( 1 ); XML::SAX::PurePerl->new( Handler => $self )->parse_string( "<hey/>" ); return $r; } ## **: It is often important to use the recursion guard shown here ## to protect the decision making logic that should only be run on ## the events in the master document from being run on events in the ## subdocument. Of course, if you want to apply the logic ## recursively, just leave the guard code out (and, yes, in this ## example, th guard code is phrased in a slightly redundant fashion, ## but we want to make the idiom clear). Feeding this filter "<foo> </foo>" results in "<foo> <hey/></foo>". We've called set_include_all_roots( 1 ) to get the secondary document's root element included. Inserting Manifold DocumentsA more involved way suitable to handling consecutive documents it to use the two non-SAX events--"start_manifold_document" and "end_manifold_document"--that are called before the first document to be combined and after the last one, respectively.The first document to be started after the "start_manifold_document" is the master document and is emitted as-is except that it will contain the contents of all of the other documents just before the root "end_element()" tag. For example: $h->start_manifold_document( {} ); $p->parse_string( "<foo1><bar /></foo1>" ); $p->parse_string( "<foo2><baz /></foo2>" ); $h->end_manifold_document( {} ); results in "<foo><bar /><baz /></foo>". The detailsIn case the above was a bit vague, here are the rules this filter lives by.For the master document:
For secondary documents:
This requires very little buffering and is "most natural" with the limitations:
NAMEXML::Filter::Merger - Assemble multiple SAX streams in to one documentMETHODS
Additional MethodsThese are provided to make it easy for subclasses to find out roughly where they are in the document structure. Generally, these should be called after calling SUPER::start_...() and before calling SUPER::end_...() to be accurate.
LIMITATIONSThe events before and after a secondary document's root element events are discarded. It is conceivable that characters, PIs and commentary outside the root element might need to be kept. This may be added as an option.The DocumentLocators are not properly managed: they should be saved and restored around each each secondary document. Does not yet buffer all events after the first document's root end_element event. If these bite you, contact me. AUTHORBarrie Slaymaker <barries@slaysys.com> COPYRIGHTCopyright 2002, Barrie Slaymaker, All Rights Reserved. You may use this module under the terms of the Artistic, GNU Public, or BSD licenses, you choice. AUTHORS
COPYRIGHT AND LICENSEThis software is copyright (c) 2013 by Barry Slaymaker.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |