File Coverage

blib/lib/XML/SAX/SimpleDispatcher.pm
Criterion Covered Total %
statement 58 63 92.0
branch 9 14 64.2
condition 2 3 66.6
subroutine 10 10 100.0
pod 5 5 100.0
total 84 95 88.4


line stmt bran cond sub pod time code
1             package XML::SAX::SimpleDispatcher;
2 3     3   1257 use strict;
  3         5  
  3         105  
3 3     3   81 use 5.008_001;
  3         13  
  3         299  
4             our $VERSION = '0.02';
5              
6 3     3   16 use base qw(XML::SAX::Base);
  3         5  
  3         2224  
7              
8 3     3   42 use constant CALLBACK => 0;
  3         7  
  3         296  
9 3     3   14 use constant EXPR => 1;
  3         7  
  3         2070  
10              
11             sub new {
12 2     2 1 36 my $class = shift;
13 2         8 my $self = bless {}, $class;
14 2         8 my %param = @_;
15 2         5 my $proc = $param{process};
16 2         9 for my $key (keys %$proc) {
17 1         13 $self->{__proc}{$key} = $proc->{$key};
18 1         6 $self->{__cb}{$key} = $proc->{$key}->[CALLBACK];
19             }
20 2         11 return $self;
21             }
22              
23             sub start_document {
24 1     1 1 1 my $self = shift;
25 1         6 $self->{__elements} = [];
26             }
27              
28             sub start_element {
29 7     7 1 12 my $self = shift;
30 7         10 my ($data) = @_;
31              
32 7         78 my $elements = $self->{__elements};
33 7         24 my $parent_path = '/' . join ('/', @$elements);
34              
35 7         14 my $name = $data->{Name};
36 0         0 my %attrs =
37 7         21 map { $data->{Attributes}{$_}{Name} => $data->{Attributes}{$_}{Value} }
38 7         8 keys %{ $data->{Attributes} };
39              
40 7 100 66     51 if ( $self->{__proc}{$parent_path} and ! $self->{__stash}{$parent_path}) {
41 3         18 $self->{__stash}{$parent_path} =
42 3         6 { map { $_ => undef } @{ $self->{__proc}{$parent_path}->[EXPR] } };
  3         44  
43             }
44              
45 7         32 push @$elements, $name;
46             }
47              
48             sub characters {
49 13     13 1 18 my $self = shift;
50 13         20 my ($data) = @_;
51              
52 13         19 my $elements = $self->{__elements};
53 13 50       38 my $current = $elements->[-1] or return;
54 13         46 my $parent_path = '/' . join ('/', @$elements[0..$#$elements-1]);
55 13         23 my $chars = $data->{Data};
56 13 100       128 if ($self->{__stash}{$parent_path}) {
57 3 50       15 if (my $val = $self->{__stash}{$parent_path}{$current}) {
58 0 0       0 if (ref $val eq 'ARRAY') {
59 0         0 push @{$self->{__stash}{$parent_path}{$current}}, $chars;
  0         0  
60             }
61             else {
62 0         0 $self->{__stash}{$parent_path}{$current} = [$val, $chars];
63             }
64             }
65             else {
66 3         82 $self->{__stash}{$parent_path}{$current} = $chars;
67             }
68             }
69             }
70              
71             sub end_element {
72 7     7 1 11 my $self = shift;
73 7         9 my ($data) = @_;
74              
75 7         13 my $elements = $self->{__elements};
76 7 50       23 my $current = $elements->[-1] or return;
77 7         20 my $path = '/' . join ('/', @$elements);
78              
79 7 100       26 if ($self->{__stash}{$path}) {
80 3         9 my $stash = delete $self->{__stash}{$path};
81 3         5 my @ret = map {$stash->{$_}} @{$self->{__proc}{$path}->[EXPR]};
  3         12  
  3         13  
82 3         17 $self->{__cb}{$path}->(@ret);
83             }
84              
85 7         36 pop @$elements;
86             }
87              
88             1;
89             __END__