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