File Coverage

blib/lib/XML/Filter/Tee.pm
Criterion Covered Total %
statement 19 53 35.8
branch 2 30 6.6
condition 1 3 33.3
subroutine 6 8 75.0
pod 2 4 50.0
total 30 98 30.6


line stmt bran cond sub pod time code
1             package XML::Filter::Tee;
2             {
3             $XML::Filter::Tee::VERSION = '0.46';
4             }
5             # ABSTRACT: Send SAX events to multiple processor, with switching
6              
7              
8              
9 2     2   12 use strict;
  2         5  
  2         81  
10 2     2   67 use Carp;
  2         3  
  2         175  
11 2     2   1449 use XML::SAX::Base;
  2         22042  
  2         27  
12 2     2   72 use XML::SAX::EventMethodMaker qw( compile_methods sax_event_names );
  2         4  
  2         1548  
13              
14              
15             sub new {
16 2     2 1 3 my $proto = shift;
17 2   33     26 my $self = bless {}, ref $proto || $proto;
18              
19 2         20 $self->{DisabledHandlers} = [];
20              
21 2         10 $self->set_handlers( @_ );
22              
23 2         47 return $self;
24             }
25              
26              
27              
28             sub set_handlers {
29 3     3 1 6 my $self = shift;
30              
31 3 100       19 $self->{Handlers} = [
32             map XML::SAX::Base->new(
33             ref $_ eq "HASH"
34             ? %$_
35             : { Handler => $_ }
36             ), @_
37             ];
38             }
39              
40              
41              
42             sub disable_handlers {
43 0     0 0   my $self = shift;
44              
45 0 0         croak "Can only disable one handler" if @_ > 1;
46 0           my ( $which ) = @_;
47              
48 0           my $hs = $self->{Handlers};
49              
50 0 0         if ( ref $which ) {
    0          
51 0           for my $i ( 0..$#$hs ) {
52 0 0         next unless $hs->[$i];
53 0 0         if ( $hs->[$i] == $which ) {
54 0           $self->{DisabledHandlers}->[$i] = $hs->[$i];
55 0           $hs->[$i] = undef;
56             }
57             }
58             }
59             elsif ( $which =~ /^\d+(?!\n)$/ ) {
60 0           $self->{DisabledHandlers}->[$which] = $hs;
61 0           $self->{Handlers}->[$which] = undef;
62             }
63             else {
64 0           for my $i ( 0..$#$hs ) {
65 0 0         next unless $hs->[$i];
66 0 0         if ( $hs->[$i]->{Name} eq $which ) {
67 0           $self->{DisabledHandlers}->[$i] = $hs->[$i];
68 0           $hs->[$i] = undef;
69             }
70             }
71             }
72             }
73              
74              
75             sub enable_handlers {
76 0     0 0   my $self = shift;
77              
78 0 0         croak "Can only enable one handler" if @_ > 1;
79 0           my ( $which ) = @_;
80              
81 0           my $hs = $self->{Handlers};
82              
83 0 0         if ( ref $which ) {
    0          
84 0           for my $i ( 0..$#$hs ) {
85 0 0         next unless $hs->[$i];
86 0 0         if ( $hs->[$i] == $which ) {
87 0           $hs->[$i] = $self->{DisabledHandlers}->[$i];
88 0           $self->{DisabledHandlers}->[$i] = undef;
89             }
90             }
91             }
92             elsif ( $which =~ /^\d+(?!\n)$/ ) {
93 0           $hs->[$which] = $self->{DisabledHandlers}->[$which];
94 0           $self->{DisabledHandlers}->[$which] = undef;
95             }
96             else {
97 0           for my $i ( 0..$#$hs ) {
98 0 0         next unless $hs->[$i];
99 0 0         if ( $hs->[$i]->{Name} eq $which ) {
100 0           $hs->[$i] = $self->{DisabledHandlers}->[$i];
101 0           $self->{DisabledHandlers}->[$i] = undef;
102             }
103             }
104             }
105             }
106              
107              
108             compile_methods( __PACKAGE__, <<'FOO', sax_event_names );
109             sub {
110             my $self = shift;
111             for ( @{$self->{Handlers}} ) {
112             next unless defined $_;
113             $_->( @_ );
114             }
115             }
116             FOO
117              
118              
119              
120             1;
121              
122             __END__