Netconf/SAXHandler.pm untested

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