Netconf/SAXHandler.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 184 CC 25 SCAR 40.4
CC
25
Cov
0%
start_element
110.0
characters
90.0
end_element
42.0
CRAP 56.9
1 package Net::Netconf::SAXHandler;
2  
3 use strict;
4 use Carp;
5 our $VERSION ='1.02';
6  
7 use base qw(XML::SAX::Base);
8  
9 sub attlist_decl
10 {
11 my $self = shift;
12 }
13  
14 sub ignorable_whitespace
15 {
16 my $self = shift;
17 }
18  
19 sub start_document
20 {
21 my ($self) = shift;
22 }
23  
24 sub end_document
25 {
26 my ($self) = shift;
27 }
28  
29 sub comment
30 {
31 my $self=shift;
32 }
33  
34 #change %rpc_errors = undef into %rpc_errors=()
35 #overriding start_element class of handler
36 sub start_element
37 {
38 my($self, $data) = @_;
39 if ($data->{'LocalName'} eq 'hello') {
40 $self->{'seen_hello'} = 1;
41 } elsif ($data->{'LocalName'} eq 'package-information') {
42 $self->{'get_pkg'} = 1;
43 } elsif ($self->{'get_pkg'} && ($data->{'LocalName'} eq 'comment')) {
44 $self->{'get_junos_ver'} = 1;
45 } elsif ($data->{'LocalName'} eq 'rpc-reply') {
46 $self->{'found_error'} = 0;
47 $self->{'no_error'} = 0;
48 $self->{'rpc_errors'} = ();
49 } elsif ($data->{'LocalName'} eq 'capability') {
50 $self->{'add_capability'} = 1;
51 } elsif (($data->{'LocalName'} eq 'session-id')
52 && ($self->{'seen_hello'})) {
53 $self->{'get_session_id'} = 1;
54 } elsif ($data->{'LocalName'} eq 'rpc-error') {
55 $self->{'found_error'}++;
56 $self->{'get_error'} = 1;
57 } elsif ($data->{'LocalName'} eq 'ok') {
58 $self->{'no_error'} = 1;
59 } elsif ($self->{'get_error'}) {
60 # Insert this field into the hash
61 $self->{'capture_error'} = $data->{'LocalName'};
62 }
63 $self->SUPER::start_element($data);
64 }
65  
66 sub end_element
67 {
68 my ($self, $data) = @_;
69 if ($data->{'LocalName'} eq 'capability') {
70 if ($self->{'current_cap'}) {
71 push @{$self->{'parsed_cap'}}, $self->{'current_cap'};
72 undef $self->{'current_cap'};
73 }
74 $self->{'add_capability'} = 0;
75 } elsif (($data->{'LocalName'} eq 'session-id')
76 && ($self->{'get_session_id'})) {
77 $self->{'seen_hello'} = 0;
78 $self->{'get_session_id'} = 0;
79 } elsif ($data->{'LocalName'} eq 'package-information') {
80 $self->{'get_pkg'} = 0;
81 $self->{'get_junos_ver'} = 0;
82 } elsif ($data->{'LocalName'} eq 'rpc-error') {
83 $self->{'get_error'} = 0;
84 $self->{'capture_error'} = undef;
85 }
86 $self->SUPER::end_element($data);
87 }
88  
89 sub characters
90 {
91 my ($self, $data) = @_;
92 if ($self->{'add_capability'} && $data->{'Data'} =~ /\S/) {
93 my $capability = $data->{'Data'};
94 my $cap_urn;
95 ($cap_urn,) = split(/\?/, $capability) if ($capability);
96 $capability = $cap_urn;
97 $self->{'current_cap'} .= $capability;
98 } elsif ($self->{'get_session_id'}) {
99 if ($data->{'Data'} =~ /\S/) {
100 $self->{'session_id'} = $data->{'Data'};
101 }
102 } elsif ($self->{'get_pkg'} && $self->{'get_junos_ver'}) {
103 if ($data->{'Data'} =~ /JUNOS Base OS/) {
104 my @comment;
105 @comment = split(/\[/, $data->{'Data'});
106 $self->{'junos_version'} = $comment[1];
107 $self->{'junos_version'} = substr($self->{'junos_version'}, 0, 3);
108 }
109 } elsif ($self->{'get_error'}) { #Get the error value
110 if ($data->{'Data'} =~ /\S/) {
111 $self->{'capture_error'} =~ s/-/_/gs;
112 $self->{'rpc_errors'}{$self->{'found_error'}}{$self->{'capture_error'}}=$data->{'Data'};
113 }
114 }
115 $self->SUPER::characters($data);
116 }
117  
118 sub fatal_error
119 {
120 my ($self) = shift;
121 carp 'Parser FATAL ERROR: ' . @_ . "\n";
122 }
123  
124 sub error
125 {
126 my ($self) = shift;
127 carp 'Parser ERROR: ' . @_ . "\n";
128 }
129  
130 sub warning
131 {
132 my ($self) = shift;
133 carp 'Parser WARNING: ' . @_ . "\n";
134 }
135  
136 sub parse
137 {
138 my ($self, $data) = @_;
139 eval {
140 if (ref($data)) {
141 $self->parse({'Source' => {'ByteStream' => $data}});
142 } else {
143 $self->parse({'Source' => {'String' => $data}});
144 }
145 };
146 if (@_) {
147 carp 'Parser ERROR: ' . $@ . "\n";
148 }
149 }
150  
151 1;
152  
153 __END__