/usr/share/perl5/XML/Filter/Distributor.pm is in libxml-sax-machines-perl 0.46-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | package XML::Filter::Distributor;
{
$XML::Filter::Distributor::VERSION = '0.46';
}
# ABSTRACT: Multipass processing of documents
use XML::SAX::Base;
@ISA = qw( XML::SAX::Base );
@EXPORT_OK = qw( Distributor );
use strict;
use Carp;
use XML::SAX::EventMethodMaker qw( sax_event_names missing_methods compile_methods );
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
my $self = bless {}, $class;
$self->{Channels} = [];
for ( @_ ) {
push @{$self->{Channels}}, $_;
}
return $self;
}
sub set_handlers {
my $self = shift;
@{$self->{Channels}} = map { { Handler => $_ } } @_;
}
sub set_handler {
shift()->set_handlers( @_ );
}
sub _buffer {
my $self = shift;
push @{$self->{BUFFER}}, [ @_ ];
}
sub set_aggregator {
my $self = shift;
$self->{Aggregator} = shift;
}
sub get_aggregator {
my $self = shift;
return $self->{Aggregator};
}
sub _change_channels {
my $self = shift;
my ( $desired_channel ) = @_;
$desired_channel = $self->{CurChannelNum} + 1
unless defined $desired_channel;
$desired_channel = undef
if $desired_channel < 0 || $desired_channel > $#{$self->{Channels}};
## Mess with XML::SAX::Base's internals a bit (ugh).
## TODO: Get less messy when the X::S::B in CVS makes it in to the
## real world.
$self->{Methods} = {};
$self->{Handler} = undef;
if ( defined $desired_channel ) {
$self->{CurChannel} = $self->{Channels}->[$desired_channel];
$self->{$_} = $self->{CurChannel}->{$_}
for keys %{$self->{CurChannel}};
}
$self->{CurChannelNum} = $desired_channel;
return $desired_channel;
}
sub _replay {
my $self = shift;
my $r;
for ( @{$self->{BUFFER}} ) {
my $event = shift @$_;
## This is ugly, must be a faster way, too tired to think of one.
my $meth = "SUPER::$event";
$self->$meth( @$_ );
unshift @$_, $event;
}
return $r;
}
sub start_document {
my $self = shift;
@{$self->{BUFFER}} = ();
$self->_buffer( "start_document", @_ );
$self->_change_channels( 0 );
my $aggie = $self->get_aggregator;
$aggie->start_manifold_document( @_ )
if $aggie && $aggie->can( "start_manifold_document" );
return $self->SUPER::start_document( @_ );
}
sub end_document {
my $self = shift;
$self->_buffer( "end_document", @_ );
$self->SUPER::end_document( @_ );
$self->_replay
while $self->_change_channels;
@{$self->{BUFFER}} = ();
my $aggie = $self->get_aggregator;
return $aggie->end_manifold_document( @_ )
if $aggie && $aggie->can( "end_manifold_document" );
return ;
}
compile_methods __PACKAGE__, <<'TPL_END', missing_methods __PACKAGE__, sax_event_names ;
sub <EVENT> {
my $self = shift;
$self->_buffer( "<EVENT>", @_ );
return $self->SUPER::<EVENT>( @_ );
}
TPL_END
1;
__END__
=pod
=head1 NAME
XML::Filter::Distributor - Multipass processing of documents
=head1 VERSION
version 0.46
=head1 SYNOPSIS
## See XML::SAX::Manifold for an easier way to use this filter.
use XML::SAX::Machines qw( Machine ) ;
## See the wondrous ASCII ART below for help visualizing this
## XML::SAX::Manifold makes this a lot easier.
my $m = Machine(
[ Intake => "XML::Filter::Distributor" => qw( V TOC Body ) ],
[ V => "My::Validator" ],
[ TOC => "My::TOCExtractor" => qw( Merger ) ],
[ Body => "My::BodyMasseuse" => qw( Merger ) ],
[ Merger => "XML::Filter::Merger" => qw( Output ) ],
[ Output => \*STDOUT ],
);
## Let the distributor coordinate with the merger.
## XML::SAX::Manifold does this for you.
$m->Intake->set_aggregator( $m->Merger );
$m->parse_file( "foo" );
=head1 DESCRIPTION
XML::Filter::Distributor is a SAX filter that allows "multipass" processing
of a document by sending the document through several channels of SAX
processors one channel at a time. A channel may be a single SAX
processor or a machine like a pipeline (see L<XML::SAX::Pipeline>).
This can be used to send the source document through one entire
processing chain before beginning the next one, for instance if the
first channel is a validator or linter that throws exceptions on error.
It can also be used to run the document through multiple processing
chains and glue all of the chains' output documents back together with
something like XML::Filter::Merger. The SYNOPSIS does both.
This differs from L<XML::Filter::SAXT> in that the channels are
prioritized and each channel receives all events for a document before
the next channel receives any events. XML::Filter::Distributor buffers all
events while feeding them to the highest priority channel
(C<$processor1> in the synopsis), and replays them for each lower
priority channel one at a time.
The event flow for the example in the SYNOPSIS would look like, with the
numbers next to the connection arrow indicating when the document's
events flow along that arrow.
+-------------+
+->| Validator |
1/ +-------------+
/
1 +-------+ 2 +--------------+ 2 +--------+
upstream ----| Dist. |---->| TOCExtractor |--*-->| Merger |-> STDOUT
+-------+ +--------------+ / +--------+
\3 /3
\ +--------------+
+->| BodyMasseuse |
+--------------+ |
Here's the timing of the event flows:
1: upstream -> Dist -> Validator
2: Dist -> TOCExtractorc -> Merger -> STDOUT
3: Dist -> BodyMassseuse -> Merger -> STDOUT
When the document arrives from upstream, the events all arrive during time
period 1 and are buffered and also passed through processor 1. After all
events have been received (as indicated by an C<end_document> event from
upstream), all events are then played back through processor 2, and then
through processor 3.
=head1 NAME
XML::Filter::Distributor - Multipass processing of documents
=head1 METHODS
=over
=item new
my $d = XML::Filter::Distributor->new(
{ Handler => $h1 },
{ Handler => $h2 },
...
);
A channel may be any SAX machine, frequently they are pipelines.
=item set_handlers
$p->set_handlers( $handler1, $handler2 );
Provided for compatibility with other SAX processors, use set_handlers
instead.
=item set_handler
Provided for compatibility with other SAX processors, use set_handlers
instead.
=back
=head1 LIMITATIONS
Can only feed a single aggregator at the moment :). I can fix this with
a bit of effort.
=head1 AUTHOR
Barrie Slaymaker <barries@slaysys.com>
=head1 COPYRIGHT
Copyright 2000, Barrie Slaymaker, All Rights Reserved.
You may use this module under the terms of the Artistic, GPL, or the BSD
licenses.
=head1 AUTHORS
=over 4
=item *
Barry Slaymaker
=item *
Chris Prather <chris@prather.org>
=back
=head1 COPYRIGHT AND LICENSE
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.
=cut
|