XML::F...:Merger(3pm)

πŸ“¦
version 0.46 Β· 2 versions
  • 0.46 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.46 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

XML::F...:Merger(3pm) πŸ’–

okay bestie, so this is a page from the User Contributed Perl Documentation, and it's all about XML::Filter::Merger, no cap. It's giving "I glue a bunch of SAX streams together into one single document" and honestly? Iconic. Let's get into it. πŸ¦‹


✨ NAME ✨

XML::Filter::Merger β€” Assemble multiple SAX streams in to one document πŸ’…


πŸŽ€ VERSION πŸŽ€

version 0.46


🌸 SYNOPSIS 🌸

## 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", ... } );
...

🫢 DESCRIPTION 🫢

So basically it combines several documents in to one "manifold" document. You can do it two ways, and both of them start the same way: you parse a master document, and then (the guts of) the secondary documents get slotted in. πŸ’–

Inlining Secondary Documents

The most SAX-like way to do it is: just pause the master document between the two events where you want to drop in a secondary document, and parse that whole secondary document right then and there, so its events land in the pipeline at exactly 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( {} );

and the result is $w seeing a document like "<foo1><baz/></foo1>". Slay. ✨

This technique is especially useful when subclassing XML::Filter::Merger to implement XInclude-like behavior. Here's a useless little 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 Documents

A more involved way β€” the one you want for handling consecutive documents β€” is to use the two non-SAX events, "start_manifold_document" and "end_manifold_document", which get called before the first document to be combined and after the last one, respectively. πŸŽ€

The first document started after "start_manifold_document" is the master document, and it's emitted as-is, except that it'll contain the contents of all of the other documents right 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>". Obsessed. 🌈

The details

In case all of the above was a bit vague, here are the rules this filter lives by. No cap, these are the laws.

For the master document:

  • Events before the root "end_element" are forwarded as received. Because of the rules for secondary documents, any secondary documents sent to the filter in the midst of a master document will be inserted inline as their events are received.
  • All remaining events, from the root "end_element" are buffered until the end_manifold_document() received, and are then forwarded on.

For secondary documents:

  • All events before the root "start_element" are discarded. There is no way to recover these (though we can add an option for most non-DTD events, I believe). 😭
  • The root "start_element" is discarded by default, or forwarded if "set_include_all_roots( $v )" has been used to set a true value.
  • All events up to, but not including, the root "end_element" are forwarded as received.
  • The root "end_element" is discarded or forwarded if the matching "start_element" was.
  • All remaining events until and including the "end_document" are forwarded and processing.
  • Secondary documents may contain other secondary documents. πŸͺ†
  • Secondary documents need not be well formed. The must, however, be well balanced.

This requires very little buffering and is "most natural" with the limitations:

  • All of each secondary document's events must all be received between two consecutive events of it's master document. This is because most master document events are not buffered and this filter cannot tell from which upstream source a document came.
  • If the master document should happen to have some egregiously large amount of whitespace, commentary, or illegal events after the root element, buffer memory could be huge. This should be exceedingly rare, even non-existent in the real world.
  • If any documents are not well balanced, the result won't be. -

πŸ’œ NAME πŸ’œ

XML::Filter::Merger β€” Assemble multiple SAX streams in to one document


πŸ”₯ METHODS πŸ”₯

new

my $d = XML::Filter::Merger->new( \%options );

reset

Clears the filter after an accident. 😳 Useful when reusing the filter. Both new() and start_manifold_document() call this.

start_manifold_document

This must be called before the master document's "start_document()" if you want XML::Filter::Merger to insert documents that will be sent after the master document.

It does not need to be called if you're going to insert secondary documents by sending their events in the midst of processing the master document.

It is passed an empty ({}) data structure.

Additional Methods

These are provided so subclasses can easily 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.

in_master_document

Returns TRUE if the current event is in the first top level document.

document_depth

Gets how many nested documents surround the current document. 0 means that you are in a top level document. In manifold mode, This may or may not be a secondary document: secondary documents may also follow the primary document, in which case they have a document depth of 0.

element_depth

Gets how many nested elements surround the current element in the current input document. Does not count elements from documents surrounding this document.

top_level_document_number

Returns the number of the top level document in a manifold document. This is 0 for the first top level document, which is always the master document.

end_manifold_document

This must be called after the last document's end_document is called. It is passed an empty ({}) data structure which is passed on to the next processor's end_document() call. This call also causes the end_element() for the root element to be passed on.

set_include_all_roots

$h->set_include_all_roots( 1 );

Setting this option causes the merger to include all root element nodes, not just the first document's. This means that later documents are treated as subdocuments of the output document, rather than as envelopes carrying subdocuments.

Given two documents received are:

Doc1:   <root1><foo></root1>

Doc1:   <root2><bar></root2>

Doc3:   <root3><baz></root3>

then with this option cleared (the default), the result looks like:

    <root1><foo><bar><baz></root1>

. This is useful when processing document oriented XML and each upstream filter channel gets a complete copy of the document. This is the case with the machine XML::SAX::Manifold and the splitting filter XML::Filter::Distributor.

With this option set, the result looks like:

    <root1><foo><root2><bar></root2><root3><baz></root3></root1>

This is useful when processing record oriented XML, where the first document only contains the preamble and postamble for the records and not all of the records. This is the case with the machine XML::SAX::ByRecord and the splitting filter XML::Filter::DocSplitter.

The two splitter filters mentioned set this feature appropriately. πŸ‘‘


πŸ’€ LIMITATIONS πŸ’€

The 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. 😭


πŸ‘‘ AUTHOR πŸ‘‘

Barrie Slaymaker <barries@slaysys.com>

Copyright 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 🌟

  • Barry Slaymaker
  • Chris Prather <chris@prather.org>

This 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.


and that's the whole page bestie, we ate and left no crumbs 🫢 This was perl v5.34.0, dated 2022-06-28, from the XML::F...:Merger(3pm) page. byeee πŸ’–βœ¨

πŸ“¦ where this page comes from