| 1 | #!/bin/false | ||
| 2 | # PODNAME: BZ::Client::XMLRPC::Parser | ||
| 3 | # ABSTRACT: A parser for an XML-RPC response. | ||
| 4 | # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | package BZ::Client::XMLRPC::Parser; | ||
| 10 | $BZ::Client::XMLRPC::Parser::VERSION = '4.91'; | ||
| 11 | 1 | ▶ | use BZ::Client::XMLRPC::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use BZ::Client::Exception; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use XML::Parser (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | return bless({ @_ }, ref($class) || $class); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub parse { | ||
| 21 | my($self, $content) = @_; | ||
| 22 | $self->{'stack'} = []; | ||
| 23 | my $handler = BZ::Client::XMLRPC::Response->new(); | ||
| 24 | $self->register($self, $handler, sub { | ||
| 25 | my($self, $handler) = @_; | ||
| 26 | $self->{'exception'} = $handler->exception(); | ||
| 27 | $self->{'result'} = $handler->result(); | ||
| 28 | }); | ||
| 29 | my $start = sub { | ||
| 30 | my(undef, $name) = @_; | ||
| 31 | my $current = $self->{'current'}; | ||
| 32 | $self->error('Illegal state, no more handlers available on stack.') unless $current; | ||
| 33 | $current->start($name); | ||
| 34 | }; | ||
| 35 | my $end = sub { | ||
| 36 | my(undef, $name) = @_; | ||
| 37 | my $current = $self->{'current'}; | ||
| 38 | $self->error('Illegal state, no more handlers available on stack.') unless $current; | ||
| 39 | $current->end($name); | ||
| 40 | }; | ||
| 41 | my $chars = sub { | ||
| 42 | my(undef, $text) = @_; | ||
| 43 | my $current = $self->{'current'}; | ||
| 44 | $self->error('Illegal state, no more handlers available on stack.') | ||
| 45 | unless $current; | ||
| 46 | $current->characters($text); | ||
| 47 | }; | ||
| 48 | my $parser = XML::Parser->new( | ||
| 49 | Handlers => { | ||
| 50 | Start => $start, | ||
| 51 | End => $end, | ||
| 52 | Char => $chars | ||
| 53 | }); | ||
| 54 | $parser->parse($content); | ||
| 55 | die $self->{'exception'} | ||
| 56 | if $self->{'exception'}; | ||
| 57 | return $self->{'result'} | ||
| 58 | } | ||
| 59 | |||
| 60 | sub register { | ||
| 61 | my($self, $parent, $handler, $code) = @_; | ||
| 62 | my $current = [$parent, $handler, $code]; | ||
| 63 | if ($parent->can('dec_level')) { | ||
| 64 | $parent->dec_level(); | ||
| 65 | } | ||
| 66 | $self->{'current'} = $handler; | ||
| 67 | push @{$self->{'stack'}}, $current; | ||
| 68 | $handler->init($self) | ||
| 69 | } | ||
| 70 | |||
| 71 | sub remove { | ||
| 72 | my($self, $handler) = @_; | ||
| 73 | my $stack = $self->{'stack'}; | ||
| 74 | my $top = pop @$stack; | ||
| 75 | $self->{'current'} = @$stack ? $stack->[@$stack-1]->[1] : undef; | ||
| 76 | $self->error('Illegal state, no more handlers available on stack.') unless $top; | ||
| 77 | my($parent, $h, $code) = @$top; | ||
| 78 | $self->error('Illegal state, the current handler is not the topmost.') unless $h eq $handler; | ||
| 79 | &$code($parent, $h) | ||
| 80 | } | ||
| 81 | |||
| 82 | sub error { | ||
| 83 | my($self, $message) = @_; | ||
| 84 | BZ::Client::Exception->throw('message' => $message) | ||
| 85 | } | ||
| 86 | |||
| 87 | sub result { | ||
| 88 | my $self = shift; | ||
| 89 | my $res = $self->{'result'}; | ||
| 90 | return $res | ||
| 91 | } | ||
| 92 | |||
| 93 | sub exception { | ||
| 94 | my $self = shift; | ||
| 95 | return $self->{'exception'} | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ | ||