DAVTalk/XMLParser.pm

filter: Statementstmt 76.0% 35 / 46 Branchbran 65.0% 13 / 20 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 76.0% 57 / 75 CC 14 SCAR 28.6
CC
14
Cov
74%
_nsexpand
22.1
BEGIN
1.0
BEGIN
1.0
CRAP 17.4
« DAVTalk.pm ↑ Summary
1 package Net::DAVTalk::XMLParser;
2  
3 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Net::DAVTalk - Interface to talk to DAV servers
8  
9 =head1 SYNOPSIS
10  
11 Net::DAVTalk::XMLParser is a simple wrapper around XML::Fast, returning
12 a more usable structure like that created by XML::Simple, but running
13 approximately 10 times faster in testing.
14  
15 =head1 SUBROUTINES/METHODS
16  
17 =head2 $hashref = xmlToHash($xmlstring);
18  
19 Converts an XML string to a hashref of the content.
20  
21 =head1 ACKNOWLEDGEMENTS
22  
23  
24 =head1 LICENSE AND COPYRIGHT
25  
26 Copyright 2015-2026 Fastmail Pty Ltd.
27  
28 This program is free software; you can redistribute it and/or modify it
29 under the same terms as Perl itself; that is, either the GNU General
30 Public License as published by the Free Software Foundation (version 1,
31 or at your option any later version), or the Artistic License.
32  
33 See L<http://dev.perl.org/licenses/> and the LICENSE file included with
34 this distribution for more information.
35  
36 =cut
37  
38  
39 3 our @EXPORT = qw(xmlToHash);
40  
41 3 ▶use XML::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 8 ▶sub _nsexpand {
Subroutine _nsexpand: called CC=14 SCAR=31.0
45 8 my $data = shift;
46 8 ▶ my $ns = shift || {};
Condition100%2/2
shift() || {}
l!l
62
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
47  
48 8 ▶ if (ref($data) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 6 2 8
elsif (ref $data eq 'ARRAY') { } 0 2 2
49 6 my @keys;
50 my %res;
51 6 foreach my $key (keys %$data) {
52 10 ▶ if ($key eq '@xmlns') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq '@xmlns') { } 2 8 10
elsif ($key eq '#text') { } 2 6 8
elsif (substr($key, 0, 7) eq '@xmlns:') { } 0 6 6
53 2 $ns->{''} = $data->{$key};
54 }
55 elsif ($key eq '#text') {
56 2 $res{'content'} = $data->{$key};
57 }
58 elsif (substr($key, 0, 7) eq '@xmlns:') {
59 0 my $namespace = substr($key, 7);
60 0 $ns->{$namespace} = $data->{$key};
61 # this is what XML::Simple does with existing namespaces
62 0 $res{"{http://www.w3.org/2000/xmlns/}$namespace"} = $data->{$key};
63 }
64 else {
65 6 push @keys, $key;
66 }
67 }
68 6 foreach my $key (@keys) {
69 6 my %ns = %$ns; # copy, woot
70 6 my $sub = _nsexpand($data->{$key}, \%ns);
71 6 my $pos = index($key, ':');
72 6 ▶ if ($pos > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pos > 0) { } 0 6 6
elsif ($key =~ /^\@/) { } 2 4 6
elsif ($ns{''}) { } 2 2 4
73 0 my $namespace = substr($key, 0, $pos);
74 0 my $rest = substr($key, $pos+1);
75 # move attribute sigil from namespace to value
76 0 ▶ $rest = "\@$rest" if $namespace =~ s/^\@//;
Branch: 1 branch
BranchTrueFalseTotal
if $namespace =~ s/^\@// 0 0 0
77 0 my $expanded = $ns{$namespace};
78 0 ▶ confess "Unknown namespace $namespace" unless $expanded;
Branch: 1 branch
BranchTrueFalseTotal
unless $expanded 0 0 0
79 0 $key = "{$expanded}$rest";
80 }
81 elsif ($key =~ m/^\@/) {
82 # Attributes are never subject to the default namespace.
83 # An attribute without an explicit namespace prefix is
84 # considered not to be in any namespace.
85 }
86 elsif ($ns{''}) {
87 2 my $expanded = $ns{''};
88 2 $key = "{$expanded}$key";
89 }
90 6 $res{$key} = $sub;
91 }
92 6 return \%res;
93 }
94 elsif (ref($data) eq 'ARRAY') {
95 0 return [ map { _nsexpand($_, $ns) } @$data ];
96 }
97 else {
98 # like XML::Simple's ExpandContent option
99 2 return { content => $data };
100 }
101 }
102  
103 2 ▶sub xmlToHash {
Subroutine xmlToHash: called CC=1 SCAR=0.0
Pod documented
104 2 my $text = shift;
105  
106 2 my $Raw = XML::Fast::xml2hash($text, attr => '@');
107 # like XML::Simple's NSExpand option
108 2 my $Xml = _nsexpand($Raw);
109  
110 # XML::Simple returns the content of the top level key
111 # (there should only be one)
112 2 my ($key) = keys %$Xml;
113  
114 2 return $Xml->{$key};
115 }
116  
117 3 1;
« DAVTalk.pm ↑ Summary