DJabberd/XMLElement.pm

filter: Statementstmt 46.8% 44 / 94 Branchbran 50.0% 12 / 24 Conditioncond 72.7% 16 / 22 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 33.3% 7 / 21 Podpod 0.0% 0 / 18 totaltotal 43.9% 83 / 189 CC 24 SCAR 19.6
CC
24
Cov
51%
as_xml
13.4
first_element
12.0
innards_as_xml
6.4
CRAP 7.1
« DJabberd/Callback.pm ↑ Summary DJabberd/PresenceChecker/Local.pm »
1 package DJabberd::XMLElement;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 use fields (
4 17 '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 17 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 ▶use DJabberd::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new {
15 683 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
16 683 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 683 683
17 # the down-classer that subclasses can inherit
18 0 return bless $_[0], $class;
19 }
20  
21 # constructing a new XMLElement:
22 683 my DJabberd::XMLElement $self = fields::new($class);
23 ($self->{ns},
24 $self->{element},
25 $self->{attrs},
26 683 $self->{children}) = @_;
27 #my ($ns, $elementname, $attrs, $children) = @_;
28 #Carp::confess("children isn't an arrayref, is: $children") unless ref $children eq "ARRAY";
29  
30 #DJabberd->track_new_obj($self);
31 683 return $self;
32 }
33  
34 #sub DESTROY {
35 # my $self = shift;
36 # DJabberd->track_destroyed_obj($self);
37 #}
38  
39 sub push_child {
40 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine push_child: not called CC=1 SCAR=6.9
Pod undocumented
41 0 push @{$self->{children}}, $_[1]; # $node
42 }
43  
44 sub set_raw {
45 0 ▶ my DJabberd::XMLElement $self = shift;
Subroutine set_raw: not called CC=1 SCAR=6.9
Pod undocumented
46 0 $self->{raw} = shift;
47 0 $self->{children} = [];
48 }
49  
50 sub children_elements {
51 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine children_elements: not called CC=1 SCAR=6.9
Pod undocumented
52 0 return grep { ref $_ } @{ $self->{children} };
53 }
54  
55 sub remove_child {
56 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine remove_child: not called CC=1 SCAR=6.9
Pod undocumented
57 0 @{$self->{children}} = grep { $_ != $_[1] } @{$self->{children}};
58 }
59  
60 sub children {
61 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine children: not called CC=1 SCAR=6.9
Pod undocumented
62 0 return @{ $self->{children} };
63 }
64  
65 sub first_child {
66 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine first_child: not called CC=2 SCAR=17.9
Pod undocumented
67 0 ▶ return @{ $self->{children} } ? $self->{children}[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'children'};} ? : 0 0 0
68 }
69  
70 sub first_element {
71 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine first_element: not called CC=3 SCAR=24.8
Pod undocumented
72 0 foreach my $c (@{ $self->{children} }) {
73 0 ▶ return $c if ref $c;
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 0 0 0
74 }
75 0 return undef;
76 }
77  
78 sub inner_ns {
79 0 ▶ return $_[0]->{attrs}{'{}xmlns'};
Subroutine inner_ns: not called CC=1 SCAR=6.9
Pod undocumented
80 }
81  
82 sub attr {
83 18 ▶ return $_[0]->{attrs}{$_[1]};
Subroutine attr: called CC=1 SCAR=0.0
Pod undocumented
84 }
85  
86 sub set_attr {
87 0 ▶ $_[0]->{attrs}{$_[1]} = $_[2];
Subroutine set_attr: not called CC=1 SCAR=6.9
Pod undocumented
88 }
89  
90 sub attrs {
91 0 ▶ return $_[0]->{attrs};
Subroutine attrs: not called CC=1 SCAR=6.9
Pod undocumented
92 }
93  
94 sub element {
95 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine element: not called CC=2 SCAR=17.9
Pod undocumented
96 0 ▶ return ($self->{ns}, $self->{element}) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
97 0 return "{$self->{ns}}$self->{element}";
98 }
99  
100 sub element_name {
101 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine element_name: not called CC=1 SCAR=6.9
Pod undocumented
102 0 return $self->{element};
103 }
104  
105 sub namespace {
106 0 ▶ my DJabberd::XMLElement $self = $_[0];
Subroutine namespace: not called CC=1 SCAR=6.9
Pod undocumented
107 0 return $self->{ns};
108 }
109  
110 sub as_xml {
111 724 ▶ my DJabberd::XMLElement $self = shift;
Subroutine as_xml: called CC=13 SCAR=26.0
Pod undocumented
112 724 ▶ my $nsmap = shift || {}; # localname -> uri, uri -> localname
Condition100%2/2
shift() || {}
l!l
404320
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
113 724 my $def_ns = shift;
114  
115 724 my ($ns, $el) = ($self->{ns}, $self->{element});
116  
117 724 my $attr_str = "";
118 724 my $attr = $self->{attrs};
119 724 foreach my $k (keys %$attr) {
120 962 ▶ next if $k eq "{}xmlns";
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "{}xmlns" 160 802 962
121 802 my $value = $attr->{$k};
122 # FIXME: ignoring all namespaces on attributes
123 802 $k =~ s!^\{(.*)\}!!;
124 802 my $ns = $1;
125 802 $attr_str .= " $k='" . DJabberd::Util::exml($value) . "'";
126 }
127  
128 724 ▶ my $xmlns = (!$ns ||
Branch: 1 branch
BranchTrueFalseTotal
!$ns || $def_ns && $ns eq $def_ns || $ns eq 'jabber:server' || $ns eq 'jabber:component:accept' || $ns eq 'jabber:client' ? : 509 215 724
Condition80%12/15
not $ns or $def_ns and $ns eq $def_ns or $ns eq "jabber:server" or $ns eq "jabber:component:accept" or $ns eq "jabber:client"
l!l&&r!l&&!r
244265215
not $ns or $def_ns and $ns eq $def_ns or $ns eq "jabber:server" or $ns eq "jabber:component:accept"
l!l&&r!l&&!r
2440480
not $ns or $def_ns and $ns eq $def_ns or $ns eq "jabber:server"
l!l&&r!l&&!r
2440480
not $ns or $def_ns and $ns eq $def_ns
l!l&&r!l&&!r
0244480
$def_ns and $ns eq $def_ns
!ll&&!rl&&r
320160244
MC/DC50%3/6
not $ns or $def_ns and $ns eq $def_ns or $ns eq "jabber:server" or $ns eq "jabber:component:accept" or $ns eq "jabber:client"
not $ns $def_ns $ns eq $def_ns $ns eq "jabber:server" $ns eq "jabber:component:accept" $ns eq "jabber:client"
Truth table40%4/10
not(A) or B and C or D or E or F
A= not $nsB= $def_nsC= $ns eq $def_nsD= $ns eq "jabber:server"E= $ns eq "jabber:component:accept"F= $ns eq "jabber:client"
ABCDEFresult
1XXXXX1
011XXX1
00X1XX1
0101XX1
00X01X1
01001X1
00X0011
0100011
00X0000
0100000
129 ($def_ns && $ns eq $def_ns) ||
130 $ns eq "jabber:server" ||
131 $ns eq "jabber:component:accept" ||
132 $ns eq "jabber:client") ?
133 "" : " xmlns='$ns'";
134 724 my $innards = $self->innards_as_xml($nsmap, $ns, $def_ns);
135 724 ▶ $innards = "..." if $DJabberd::ASXML_NO_INNARDS && $innards;
Branch: 1 branch
BranchTrueFalseTotal
if $DJabberd::ASXML_NO_INNARDS and $innards 0 724 724
Condition33%1/3
$DJabberd::ASXML_NO_INNARDS and $innards
!ll&&!rl&&r
72400
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
136 724 ▶ return length $innards ?
Branch: 1 branch
BranchTrueFalseTotal
length $innards ? : 381 343 724
137 "<$el$xmlns$attr_str>$innards</$el>" :
138 "<$el$xmlns$attr_str/>";
139 }
140  
141 sub innards_as_xml {
142 724 ▶ my DJabberd::XMLElement $self = shift;
Subroutine innards_as_xml: called CC=6 SCAR=18.6
Pod undocumented
143 724 ▶ my $nsmap = shift || {};
Condition50%1/2
shift() || {}
l!l
7240
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
144 724 my $def_ns = shift;
145  
146 724 ▶ if ($self->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"raw"}) 0 724 724
147 0 return $self->{raw};
148 }
149  
150 724 my $ret = "";
151 724 foreach my $c (@{ $self->{children} }) {
152 512 ▶ if (ref $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 404 108 512
153 404 $ret .= $c->as_xml($nsmap, $def_ns);
154 } else {
155 108 ▶ if ($DJabberd::ASXML_NO_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DJabberd::ASXML_NO_TEXT) { } 0 108 108
156 0 $ret .= "...";
157 } else {
158 108 $ret .= DJabberd::Util::exml($c);
159 }
160 }
161 }
162 724 return $ret;
163 }
164  
165 sub clone {
166 0 ▶ my $self = shift;
Subroutine clone: not called CC=2 SCAR=17.9
Pod undocumented
167 0 my $clone = fields::new(ref($self));
168 0 $clone->{ns} = $self->{ns};
169 0 $clone->{element} = $self->{element};
170 0 $clone->{attrs} = { %{ $self->{attrs} } };
171 0 ▶ $clone->{children} = [ map { ref($_) ? $_->clone : $_ } @{ $self->{children} } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
172 0 $clone->{raw} = $self->{raw};
173 0 return $clone;
174 }
175  
176 17 1;
« DJabberd/Callback.pm ↑ Summary DJabberd/PresenceChecker/Local.pm »