DJabberd/XMLElement.pm

filter: Statementstmt 52.0% 65 / 125 Branchbran 59.5% 25 / 42 Conditioncond 60.0% 12 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 36.3% 8 / 22 Podpod 0.0% 0 / 18 totaltotal 48.1% 115 / 239 CC 34 SCAR 23.0
CC
34
Cov
55%
as_xml
19.4
first_element
12.0
_resolve_prefix
9.9
CRAP 10.0
« DJabberd/SASL/AuthenSASL.pm ↑ Summary DJabberd/Stanza.pm »
1 package DJabberd::XMLElement;
2 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 'ns', # namespace name
5 'element', # element name
6 'attrs', # hashref of {namespace}attr => value. NOTE: used by Stanza.pm directly.
7 'children', # arrayref of child elements of this same type, or scalars for text nodes
8 'raw', # in some cases we have the raw xml and we have to create a fake XMLElement object
9 # business logic is that as_xml returns the raw stuff if it is exists, children has to be empty -- sky
10 'prefix', # namepace prefix in use in this element
11 20 );
12  
13 20 ▶use DJabberd::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 727 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
16 727 my $class = shift;
17 727 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 727 727
18 # the down-classer that subclasses can inherit
19 0 return bless $_[0], $class;
20 }
21  
22 # constructing a new XMLElement:
23 727 my DJabberd::XMLElement $self = fields::new($class);
24 ($self->{ns},
25 $self->{element},
26 $self->{attrs},
27 $self->{children},
28 $self->{raw},
29 727 $self->{prefix}) = @_;
30 #my ($ns, $elementname, $attrs, $children) = @_;
31 #Carp::confess("children isn't an arrayref, is: $children") unless ref $children eq "ARRAY";
32  
33 #DJabberd->track_new_obj($self);
34 727 return $self;
35 }
36  
37 #sub DESTROY {
38 # my $self = shift;
39 # DJabberd->track_destroyed_obj($self);
40 #}
41  
42 0 ▶sub push_child {
Subroutine push_child: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my DJabberd::XMLElement $self = $_[0];
44 0 push @{$self->{children}}, $_[1]; # $node
45 }
46  
47 0 ▶sub set_raw {
Subroutine set_raw: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my DJabberd::XMLElement $self = shift;
49 0 $self->{raw} = shift;
50 0 $self->{children} = [];
51 }
52  
53 0 ▶sub children_elements {
Subroutine children_elements: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my DJabberd::XMLElement $self = $_[0];
55 0 return grep { ref $_ } @{ $self->{children} };
56 }
57  
58 0 ▶sub remove_child {
Subroutine remove_child: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my DJabberd::XMLElement $self = $_[0];
60 0 @{$self->{children}} = grep { $_ != $_[1] } @{$self->{children}};
61 }
62  
63 0 ▶sub children {
Subroutine children: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my DJabberd::XMLElement $self = $_[0];
65 0 return @{ $self->{children} };
66 }
67  
68 0 ▶sub first_child {
Subroutine first_child: not called CC=2 SCAR=17.9
Pod undocumented
69 0 my DJabberd::XMLElement $self = $_[0];
70 0 ▶ return @{ $self->{children} } ? $self->{children}[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'children'};} ? : 0 0 0
71 }
72  
73 0 ▶sub first_element {
Subroutine first_element: not called CC=3 SCAR=24.8
Pod undocumented
74 0 my DJabberd::XMLElement $self = $_[0];
75 0 foreach my $c (@{ $self->{children} }) {
76 0 ▶ return $c if ref $c;
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 0 0 0
77 }
78 0 return undef;
79 }
80  
81 0 ▶sub inner_ns {
Subroutine inner_ns: not called CC=1 SCAR=6.9
Pod undocumented
82 0 return $_[0]->{attrs}{'{}xmlns'};
83 }
84  
85 18 ▶sub attr {
Subroutine attr: called CC=1 SCAR=0.0
Pod undocumented
86 18 return $_[0]->{attrs}{$_[1]};
87 }
88  
89 0 ▶sub set_attr {
Subroutine set_attr: not called CC=1 SCAR=6.9
Pod undocumented
90 0 $_[0]->{attrs}{$_[1]} = $_[2];
91 }
92  
93 0 ▶sub attrs {
Subroutine attrs: not called CC=1 SCAR=6.9
Pod undocumented
94 0 return $_[0]->{attrs};
95 }
96  
97 0 ▶sub element {
Subroutine element: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my DJabberd::XMLElement $self = $_[0];
99 0 ▶ return ($self->{ns}, $self->{element}) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
100 0 return "{$self->{ns}}$self->{element}";
101 }
102  
103 0 ▶sub element_name {
Subroutine element_name: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my DJabberd::XMLElement $self = $_[0];
105 0 return $self->{element};
106 }
107  
108 0 ▶sub namespace {
Subroutine namespace: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my DJabberd::XMLElement $self = $_[0];
110 0 return $self->{ns};
111 }
112  
113 1581 ▶sub _resolve_prefix {
Subroutine _resolve_prefix: called CC=6 SCAR=23.0
114 1581 my ($self, $nsmap, $def_ns, $uri, $attr) = @_;
115 1581 ▶ if ($def_ns && $def_ns eq $uri) {
Branch: 3 branches
BranchTrueFalseTotal
if ($def_ns and $def_ns eq $uri) { } 683 898 1581
elsif ($uri eq '') { } 817 81 898
elsif ($nsmap->{$uri}) { } 81 0 81
Condition66%2/3
$def_ns and $def_ns eq $uri
!ll&&!rl&&r
0898683
MC/DC50%1/2
$def_ns and $def_ns eq $uri
$def_ns $def_ns eq $uri
Truth table66%2/3
A and B
A= $def_nsB= $def_ns eq $uri
ABresult
0X0
100
111
116 683 return '';
117 } elsif ($uri eq '') {
118 817 return '';
119 } elsif ($nsmap->{$uri}) {
120 81 $nsmap->{$uri} . ':';
121 } else {
122 0 ▶ $nsmap->{___prefix_count} ||= 0;
Condition0%0/2
$nsmap->{"___prefix_count"} ||= 0
l!l
00
MC/DC0%0/1
$nsmap->{"___prefix_count"} ||= 0
$nsmap->{"___prefix_count"}
Truth table0%0/2
A
A= $nsmap->{"___prefix_count"}
Aresult
11
00
123 0 my $count = $nsmap->{___prefix_count}++;
124 0 my $prefix = "nsp$count";
125 0 $nsmap->{$uri} = $prefix;
126 0 $nsmap->{$prefix} = $uri;
127 0 $attr->{'{http://www.w3.org/2000/xmlns}' . $prefix} = $uri;
128 0 return $prefix . ':';
129 }
130 }
131  
132 764 ▶sub as_xml {
Subroutine as_xml: called CC=18 SCAR=29.7
Pod undocumented
133 764 my DJabberd::XMLElement $self = shift;
134  
135 764 ▶ my $nsmap = shift || { }; # localname -> uri, uri -> localname
Condition100%2/2
shift() || {}
l!l
429335
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
136  
137 # tons of places call as_xml, but nobody seems to care about
138 # the default namespace. It seems, however, that it is a common
139 # usage for "jabber:client" to be this default ns.
140 764 ▶ my $def_ns = shift || 'jabber:client';
Condition100%2/2
shift() || 'jabber:client'
l!l
429335
MC/DC100%1/1
shift() || 'jabber:client'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
141  
142 764 my ($ns, $el) = ($self->{ns}, $self->{element});
143 764 ▶ if ($self->{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prefix"}) 81 683 764
144 81 $nsmap->{ $self->{prefix} } = $ns;
145 81 $nsmap->{$ns} = $self->{prefix};
146 }
147  
148 764 my $attr_str = "";
149 764 my $attr = $self->{attrs};
150  
151 764 $nsmap->{xmlns} = 'http://www.w3.org/2000/xmlns';
152 764 $nsmap->{'http://www.w3.org/2000/xmlns'} = 'xmlns';
153  
154 # let's feed the nsmap...
155 764 foreach my $k (keys %$attr) {
156 1008 ▶ if ($k =~ /^\{(.*)\}(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ /^\{(.*)\}(.+)$/) { } 1008 0 1008
elsif ($k eq 'xmlns') { } 0 0 0
157 1008 my ($nsuri, $name) = ($1, $2);
158 1008 ▶ if ($nsuri eq 'xmlns' ||
Branch: 2 branches
BranchTrueFalseTotal
if ($nsuri eq 'xmlns' or $nsuri eq 'http://www.w3.org/2000/xmlns/') { } 18 990 1008
elsif ($k eq '{}xmlns') { } 173 817 990
Condition66%2/3
$nsuri eq "xmlns" or $nsuri eq "http://www.w3.org/2000/xmlns/"
l!l&&r!l&&!r
018990
MC/DC50%1/2
$nsuri eq "xmlns" or $nsuri eq "http://www.w3.org/2000/xmlns/"
$nsuri eq "xmlns" $nsuri eq "http://www.w3.org/2000/xmlns/"
Truth table66%2/3
A or B
A= $nsuri eq "xmlns"B= $nsuri eq "http://www.w3.org/2000/xmlns/"
ABresult
1X1
011
000
159 $nsuri eq 'http://www.w3.org/2000/xmlns/') {
160 18 $nsmap->{$name} = $attr->{$k};
161 18 $nsmap->{ $attr->{$k} } = $name;
162 } elsif ($k eq '{}xmlns') {
163 173 $def_ns = $attr->{$k};
164 }
165 } elsif ($k eq 'xmlns') {
166 0 $def_ns = $attr->{$k};
167 }
168 }
169  
170 764 my $nsprefix = $self->_resolve_prefix($nsmap, $def_ns, $ns, $attr);
171  
172 764 foreach my $k (keys %$attr) {
173 1008 my $value = $attr->{$k};
174 1008 ▶ if ($k =~ /^\{(.*)\}(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^\{(.*)\}(.+)$/) { } 1008 0 1008
175 1008 my ($nsuri, $name) = ($1, $2);
176 1008 ▶ if ($nsuri eq 'xmlns' ||
Branch: 2 branches
BranchTrueFalseTotal
if ($nsuri eq 'xmlns' or $nsuri eq 'http://www.w3.org/2000/xmlns/') { } 18 990 1008
elsif ($k eq '{}xmlns') { } 173 817 990
Condition66%2/3
$nsuri eq "xmlns" or $nsuri eq "http://www.w3.org/2000/xmlns/"
l!l&&r!l&&!r
018990
MC/DC50%1/2
$nsuri eq "xmlns" or $nsuri eq "http://www.w3.org/2000/xmlns/"
$nsuri eq "xmlns" $nsuri eq "http://www.w3.org/2000/xmlns/"
Truth table66%2/3
A or B
A= $nsuri eq "xmlns"B= $nsuri eq "http://www.w3.org/2000/xmlns/"
ABresult
1X1
011
000
177 $nsuri eq 'http://www.w3.org/2000/xmlns/') {
178 18 $attr_str .= " xmlns:$name='" . DJabberd::Util::exml($value)
179 . "'";
180 } elsif ($k eq '{}xmlns') {
181 173 $attr_str .= " xmlns='" . DJabberd::Util::exml($value) . "'";
182 } else {
183 817 my $nsprefix = $self->_resolve_prefix($nsmap, $def_ns, $nsuri);
184 817 $attr_str .= " $nsprefix$name='" . DJabberd::Util::exml($value)
185 ."'";
186 }
187 } else {
188 0 $attr_str .= " $k='" . DJabberd::Util::exml($value) . "'";
189 }
190 }
191  
192 764 my $innards = $self->innards_as_xml($nsmap, $def_ns);
193 764 ▶ $innards = "..." if $DJabberd::ASXML_NO_INNARDS && $innards;
Branch: 1 branch
BranchTrueFalseTotal
if $DJabberd::ASXML_NO_INNARDS and $innards 0 764 764
Condition33%1/3
$DJabberd::ASXML_NO_INNARDS and $innards
!ll&&!rl&&r
76400
MC/DC0%0/2
$DJabberd::ASXML_NO_INNARDS and $innards
$DJabberd::ASXML_NO_INNARDS $innards
Truth table33%1/3
A and B
A= $DJabberd::ASXML_NO_INNARDSB= $innards
ABresult
0X0
100
111
194  
195 764 ▶ my $result = length $innards ?
Branch: 1 branch
BranchTrueFalseTotal
length $innards ? : 395 369 764
196 "<$nsprefix$el$attr_str>$innards</$nsprefix$el>" :
197 "<$nsprefix$el$attr_str/>";
198  
199 764 return $result;
200  
201 }
202  
203 764 ▶sub innards_as_xml {
Subroutine innards_as_xml: called CC=6 SCAR=18.6
Pod undocumented
204 764 my DJabberd::XMLElement $self = shift;
205 764 ▶ my $nsmap = shift || {};
Condition50%1/2
shift() || {}
l!l
7640
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
206 764 my $def_ns = shift;
207  
208 764 ▶ if ($self->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"raw"}) 0 764 764
209 0 return $self->{raw};
210 }
211  
212 764 my $ret = "";
213 764 foreach my $c (@{ $self->{children} }) {
214 536 ▶ if (ref $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 429 107 536
215 429 $ret .= $c->as_xml($nsmap, $def_ns);
216 } else {
217 107 ▶ if ($DJabberd::ASXML_NO_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DJabberd::ASXML_NO_TEXT) { } 0 107 107
218 0 $ret .= "...";
219 } else {
220 107 $ret .= DJabberd::Util::exml($c);
221 }
222 }
223 }
224 764 return $ret;
225 }
226  
227 0 ▶sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
Pod undocumented
228 0 my $self = shift;
229 0 my $clone = fields::new(ref($self));
230 0 $clone->{ns} = $self->{ns};
231 0 $clone->{element} = $self->{element};
232 0 $clone->{attrs} = { %{ $self->{attrs} } };
233 0 ▶ $clone->{children} = [ map { ref($_) ? $_->clone : $_ } @{ $self->{children} } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
234 0 $clone->{raw} = $self->{raw};
235 0 $clone->{prefix} = $self->{prefix};
236 0 return $clone;
237 }
238  
239 20 1;
« DJabberd/SASL/AuthenSASL.pm ↑ Summary DJabberd/Stanza.pm »