File Coverage

blib/lib/BZ/Client/XMLRPC/Parser.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             #!/bin/false
2             # PODNAME: BZ::Client::XMLRPC::Parser
3             # ABSTRACT: A parser for an XML-RPC response.
4              
5 1     1   5 use strict;
  1         1  
  1         35  
6 1     1   5 use warnings 'all';
  1         1  
  1         78  
7              
8             package BZ::Client::XMLRPC::Parser;
9             $BZ::Client::XMLRPC::Parser::VERSION = '4.4001_003'; # TRIAL
10              
11 1     1   344 $BZ::Client::XMLRPC::Parser::VERSION = '4.4001003';use BZ::Client::XMLRPC::Response;
  1         2  
  1         24  
12 1     1   4 use BZ::Client::Exception;
  1         1  
  1         12  
13 1     1   270 use XML::Parser ();
  0            
  0            
14              
15             sub new {
16             my $class = shift;
17             my $self = { @_ };
18             bless($self, ref($class) || $class);
19             return $self
20             }
21              
22             sub parse {
23             my($self, $content) = @_;
24             $self->{'stack'} = [];
25             my $handler = BZ::Client::XMLRPC::Response->new();
26             $self->register($self, $handler, sub {
27             my($self, $handler) = @_;
28             $self->{'exception'} = $handler->exception();
29             $self->{'result'} = $handler->result();
30             });
31             my $start = sub {
32             my(undef, $name) = @_;
33             my $current = $self->{'current'};
34             $self->error('Illegal state, no more handlers available on stack.') unless $current;
35             $current->start($name);
36             };
37             my $end = sub {
38             my(undef, $name) = @_;
39             my $current = $self->{'current'};
40             $self->error('Illegal state, no more handlers available on stack.') unless $current;
41             $current->end($name);
42             };
43             my $chars = sub {
44             my(undef, $text) = @_;
45             my $current = $self->{'current'};
46             $self->error('Illegal state, no more handlers available on stack.') unless $current;
47             $current->characters($text);
48             };
49             my $parser = XML::Parser->new(Handlers => {Start => $start,
50             End => $end,
51             Char => $chars});
52             $parser->parse($content);
53             die $self->{'exception'} if ($self->{'exception'});
54             return $self->{'result'}
55             }
56              
57             sub register {
58             my($self, $parent, $handler, $code) = @_;
59             my $current = [$parent, $handler, $code];
60             if ($parent->can('dec_level')) {
61             $parent->dec_level();
62             }
63             $self->{'current'} = $handler;
64             push(@{$self->{'stack'}}, $current);
65             $handler->init($self)
66             }
67              
68             sub remove {
69             my($self, $handler) = @_;
70             my $stack = $self->{'stack'};
71             my $top = pop @$stack;
72             $self->{'current'} = @$stack ? $stack->[@$stack-1]->[1] : undef;
73             $self->error('Illegal state, no more handlers available on stack.') unless $top;
74             my($parent, $h, $code) = @$top;
75             $self->error('Illegal state, the current handler is not the topmost.') unless $h eq $handler;
76             &$code($parent, $h)
77             }
78              
79             sub error {
80             my($self, $message) = @_;
81             BZ::Client::Exception->throw('message' => $message)
82             }
83              
84             sub result {
85             my $self = shift;
86             my $res = $self->{'result'};
87             return $res
88             }
89              
90             sub exception {
91             my $self = shift;
92             return $self->{'exception'}
93             }
94              
95             1;
96              
97             __END__
98              
99             =pod
100              
101             =encoding UTF-8
102              
103             =head1 NAME
104              
105             BZ::Client::XMLRPC::Parser - A parser for an XML-RPC response.
106              
107             =head1 VERSION
108              
109             version 4.4001_003
110              
111             =head1 AUTHORS
112              
113             =over 4
114              
115             =item *
116              
117             Dean Hamstead <dean@bytefoundry.com.au>
118              
119             =item *
120              
121             Jochen Wiedmann <jochen.wiedmann@gmail.com>
122              
123             =back
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is copyright (c) 2017 by Dean Hamstad.
128              
129             This is free software; you can redistribute it and/or modify it under
130             the same terms as the Perl 5 programming language system itself.
131              
132             =cut