DOM/Parser.pm

filter: Statementstmt 18.4% 19 / 103 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 24.0% 6 / 25 Podpod 0.0% 0 / 19 totaltotal 12.0% 25 / 207 CC 28 SCAR 46.8
CC
28
Cov
12%
Start
306.0
forward_event
30.5
End
20.0
CRAP 107.3
↑ Summary Access/ssl.pm »
1 #
2 # $Id: Parser.pm,v 1.6 2003/03/02 11:12:12 dsw Exp $
3 #
4 # COPYRIGHT AND LICENSE
5 # Copyright (c) 2001-2003, Juniper Networks, Inc.
6 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 # 1. Redistributions of source code must retain the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer.
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 # 3. The name of the copyright owner may not be used to
18 # endorse or promote products derived from this software without specific
19 # prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32  
33 # ----------------------------------------------------------------------
34 #
35 # This package inserts itself as a filter between XML::Parser and
36 # XML::DOM in order to allow it to accept XML parser events and to
37 # selectively forward them to either the native JUNOScript logic or
38 # the DOM logic.
39 #
40  
41 package JUNOS::DOM::Parser;
42  
43 2 ▶use JUNOS::Trace;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 #
46 # Forward events from the expat parser to the DOM parser. The expat
47 # parser is basically doing the lexical analysis for us, breaking the
48 # input into xml tokens and doing callbacks to the hand up these tokens.
49 # Normally, these are forwarded directly to the DOM parser, which builds
50 # a DOM tree of DOM nodes that are manipulated thru DOM's API. If the
51 # DOM parser isn't active, we toss the events.
52 #
53 sub forward_event
54 0 ▶{
Subroutine forward_event: not called CC=6 SCAR=34.2
Pod undocumented
55 0 tracept("Parse");
56  
57 0 my $func = shift;
58 0 my $self = shift;
59 0 my $parser = shift;
60  
61 0 ▶ $self = $parser->{JUNOS_Device} unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
62 0 ▶ return unless $self->{JUNOS_DomMode} && $self->{JUNOS_Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"JUNOS_DomMode"} and $self->{"JUNOS_Active"} 0 0 0
Condition0%0/3
$self->{"JUNOS_DomMode"} and $self->{"JUNOS_Active"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"JUNOS_DomMode"} and $self->{"JUNOS_Active"}
$self->{"JUNOS_DomMode"} $self->{"JUNOS_Active"}
Truth table0%0/3
A and B
A= $self->{"JUNOS_DomMode"}B= $self->{"JUNOS_Active"}
ABresult
0X0
100
111
63 0 ▶ return if $parser->{tag} eq "rpc-reply";
Branch: 1 branch
BranchTrueFalseTotal
if $parser->{"tag"} eq "rpc-reply" 0 0 0
64  
65 0 my $name = "XML::Parser::Dom::" . $func;
66  
67 {
68 0 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.2
69  
70 0 ▶ return unless defined &${name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$name 0 0 0
71 0 &${name}($parser, @_);
72 }
73 }
74  
75 #
76 # The Init and Final events are of no interest to us, since we're
77 # creating them by hand for the DOM parser.
78 #
79 0 ▶sub Init { }
Subroutine Init: not called CC=1 SCAR=0.0
Pod undocumented
80 0 ▶sub Final { }
Subroutine Final: not called CC=1 SCAR=0.0
Pod undocumented
81  
82 #
83 # Normal events are forwarded to the DOM parser
84 #
85 0 ▶sub Proc { forward_event("Proc", undef, @_); }
Subroutine Proc: not called CC=1 SCAR=6.9
Pod undocumented
86 0 ▶sub Comment { forward_event("Comment", undef, @_); }
Subroutine Comment: not called CC=1 SCAR=6.9
Pod undocumented
87 0 ▶sub CdataStart { forward_event("CdataStart", undef, @_); }
Subroutine CdataStart: not called CC=1 SCAR=6.9
Pod undocumented
88 0 ▶sub CdataEnd { forward_event("CdataEnd", undef, @_); }
Subroutine CdataEnd: not called CC=1 SCAR=6.9
Pod undocumented
89 0 ▶sub Default { forward_event("Default", undef, @_); }
Subroutine Default: not called CC=1 SCAR=6.9
Pod undocumented
90 0 ▶sub Unparsed { forward_event("Unparsed", undef, @_); }
Subroutine Unparsed: not called CC=1 SCAR=6.9
Pod undocumented
91 0 ▶sub Notation { forward_event("Notation", undef, @_); }
Subroutine Notation: not called CC=1 SCAR=6.9
Pod undocumented
92 0 ▶sub ExternEnt { forward_event("ExternEnt", undef, @_); }
Subroutine ExternEnt: not called CC=1 SCAR=6.9
Pod undocumented
93 0 ▶sub Entity { forward_event("Entity", undef, @_); }
Subroutine Entity: not called CC=1 SCAR=6.9
Pod undocumented
94 0 ▶sub Element { forward_event("Element", undef, @_); }
Subroutine Element: not called CC=1 SCAR=6.9
Pod undocumented
95 0 ▶sub Attlist { forward_event("Attlist", undef, @_); }
Subroutine Attlist: not called CC=1 SCAR=6.9
Pod undocumented
96 0 ▶sub Doctype { forward_event("Doctype", undef, @_); }
Subroutine Doctype: not called CC=1 SCAR=6.9
Pod undocumented
97  
98 #
99 # Record values from the xml declaration so that we can pass these
100 # values to the DOM parser when we start/restart it.
101 #
102 sub XMLDecl
103 0 ▶{
Subroutine XMLDecl: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my($parser, $version, $encoding, $standalone) = @_;
105  
106 0 my $device = $parser->{JUNOS_Device};
107 0 $device->{XMLDecl_Version} = $version;
108 0 $device->{XMLDecl_Encoding} = $encoding;
109 0 $device->{XMLDecl_Standalone} = $standalone;
110 }
111  
112 #
113 # Handle the start event. If the tag is the start of the
114 # JUNOScript session, mark us as active. If we're starting
115 # an rpc-reply, open a reply and forward the event.
116 # Otherwise just forward the event to the DOM parser.
117 #
118 2 ▶use constant XNM_NAMESPACE_OPTION => 'namespace-action';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 ▶use constant XNM_NAMESPACE_REMOVE => 'remove-namespace';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 2 ▶use constant XNM_NAMESPACE_UPDATE => 'update-namespace';
Subroutine BEGIN: called CC=1 SCAR=0.0
121 2 ▶use constant JUNOS_NAMESPACE => 'xmlns:junos';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 sub Start
123 0 ▶{
Subroutine Start: not called CC=17 SCAR=57.2
Pod undocumented
124 0 tracept("Parse");
125  
126 0 my ($parser, $tag, %attrs) = @_;
127 0 my $self = $parser->{JUNOS_Device};
128 0 $parser->{tag} = $tag;
129  
130 0 my $nsoption = XNM_NAMESPACE_OPTION;
131 0 ▶ if ($self->{$nsoption} && ($self->{$nsoption} eq XNM_NAMESPACE_REMOVE
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$nsoption} and $self->{$nsoption} eq "remove-namespace" || $self->{$nsoption} eq "update-namespace") 0 0 0
Condition0%0/6
$self->{$nsoption} and $self->{$nsoption} eq "remove-namespace" || $self->{$nsoption} eq "update-namespace"
!ll&&!rl&&r
000
$self->{$nsoption} eq "remove-namespace" or $self->{$nsoption} eq "update-namespace"
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{$nsoption} and $self->{$nsoption} eq "remove-namespace" || $self->{$nsoption} eq "update-namespace"
$self->{$nsoption} $self->{$nsoption} eq "remove-namespace" $self->{$nsoption} eq "update-namespace"
Truth table0%0/4
A and B or C
A= $self->{$nsoption}B= $self->{$nsoption} eq "remove-namespace"C= $self->{$nsoption} eq "update-namespace"
ABCresult
0XX0
1000
11X1
1011
132 || $self->{$nsoption} eq XNM_NAMESPACE_UPDATE)) {
133 0 foreach my $k (keys(%attrs)) {
134 0 ▶ if ($k =~ /^xmlns/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^xmlns/) 0 0 0
135 0 undef $attrs{$k};
136 0 next;
137 }
138 0 ▶ if ($k =~ /([\w-]+):([\w-]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /([\w-]+):([\w-]+)/) 0 0 0
139 0 $attrs{$2} = $attrs{$k};
140 0 undef $attrs{$k};
141 }
142 0 ▶ if ($k eq 'schemaLocation') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "schemaLocation") 0 0 0
143 0 ▶ if ($self->{$nsoption} eq 'XNM_NAMESPACE_UPDATE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$nsoption} eq "XNM_NAMESPACE_UPDATE") 0 0 0
144 0 my @schemaLocation = split(/\s+/, $attrs{$k});
145 0 $attrs{'noNamespaceSchemaLocation'} = $schemaLocation[1];
146 }
147 0 undef $attrs{$k};
148 }
149 }
150 }
151  
152 0 ▶ if ($tag eq "junoscript") {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag eq 'junoscript') { } 0 0 0
elsif ($tag eq 'rpc-reply') { } 0 0 0
elsif ($tag eq 'rpc') { } 0 0 0
elsif ($self->{'JUNOS_DomMode'}) { } 0 0 0
153 0 $self->{JUNOS_Active} = 1;
154 0 my $junosns = JUNOS_NAMESPACE;
155 0 ▶ $self->{$junosns} = $attrs{$junosns} if $attrs{$junosns};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{$junosns} 0 0 0
156  
157  
158 } elsif ($tag eq "rpc-reply") {
159 0 $self->{JUNOS_DomMode} = 1;
160 0 $self->openReply($parser);
161 0 forward_event("Start", $self, $parser, $tag, @_);
162 0 my $conn = $self->{JUNOS_Conn};
163 0 $conn->send("\n");
164  
165 } elsif ($tag eq "rpc") {
166 0 ▶ die "No RPC callback" unless $self->{JUNOS_CallbackHandler};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"JUNOS_CallbackHandler"} 0 0 0
167 0 &{$self->{JUNOS_CallbackHandler}}($self, $parser, $tag);
168  
169 } elsif ($self->{JUNOS_DomMode}) {
170 0 ▶ if ($self->{JUNOS_expect_1st_element}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"JUNOS_expect_1st_element"}) 0 0 0
171 0 undef $self->{JUNOS_expect_1st_element};
172 0 my $junosns = JUNOS_NAMESPACE;
173 0 ▶ $attrs{$junosns} = $self->{$junosns} if $self->{$junosns};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$junosns} 0 0 0
174 }
175 0 forward_event("Start", $self, $parser, $tag, %attrs);
176 }
177 }
178  
179 #
180 # Handle the end event. If the tag is the end of the
181 # JUNOScript session, mark us as inactive. If we're ending
182 # an rpc-reply, forward the event, close a reply and hand
183 # the results off to the reply handler. Otherwise just forward
184 # the event to the DOM parser.
185 #
186 sub End
187 0 ▶{
Subroutine End: not called CC=4 SCAR=30.0
Pod undocumented
188 0 tracept("Parse");
189  
190 0 my $parser = shift;
191 0 my $self = $parser->{JUNOS_Device};
192 0 my $tag = shift;
193  
194 0 ▶ if ($tag eq "junoscript") {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag eq 'junoscript') { } 0 0 0
elsif ($tag eq 'rpc-reply') { } 0 0 0
elsif ($self->{'JUNOS_DomMode'}) { } 0 0 0
195 0 $self->{JUNOS_Active} = 0;
196  
197 } elsif ($tag eq "rpc-reply") {
198 0 forward_event("End", $self, $parser, $tag, @_);
199 0 $self->{JUNOS_DomMode} = 0;
200 0 my $reply = $self->closeReply($parser);
201  
202 0 &{$self->{JUNOS_ReplyHandler}}($self, $parser, $reply);
203  
204 } elsif ($self->{JUNOS_DomMode}) {
205 0 forward_event("End", $self, $parser, $tag, @_);
206 }
207 }
208  
209 #
210 # Handle character (text) input: if we're active, forward
211 # the event to the DOM parser. Otherwise pass it to the
212 # registered callback.
213 #
214 0 ▶sub Char {
Subroutine Char: not called CC=4 SCAR=30.0
Pod undocumented
215 0 tracept("Parse");
216  
217 0 my($parser, $string) = @_;
218 0 ▶ return unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
219  
220 0 my $self = $parser->{JUNOS_Device};
221  
222 0 ▶ if ($self->{JUNOS_Active}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'JUNOS_Active'}) { } 0 0 0
223 0 forward_event("Char", $self, $parser, $string);
224 } else {
225 0 ▶ die "No Char callback" unless $self->{JUNOS_CharHandler};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"JUNOS_CharHandler"} 0 0 0
226 0 &{$self->{JUNOS_CharHandler}}($self, $string);
227 }
228 }
229  
230 2 1;
231  
232 __END__
↑ Summary Access/ssl.pm »