| 1 | package Net::DAVTalk::XMLParser; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 5 | ▶ | 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 | 5 | our @EXPORT = qw(xmlToHash); | |||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 5 | ▶ | use XML::Fast; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 42 | 5 | ▶ | use Carp qw(confess); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 179 | ▶ | sub _nsexpand { | ||||||||||||||||
|
Subroutine
_nsexpand: called CC=14 SCAR=26.4
| |||||||||||||||||||
| 45 | 179 | my $data = shift; | |||||||||||||||||
| 46 | 179 | ▶ | my $ns = shift || {}; | ||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | 179 | ▶ | if (ref($data) eq 'HASH') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 49 | 89 | my @keys; | |||||||||||||||||
| 50 | my %res; | ||||||||||||||||||
| 51 | 89 | foreach my $key (keys %$data) { | |||||||||||||||||
| 52 | 159 | ▶ | if ($key eq '@xmlns') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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 | 14 | my $namespace = substr($key, 7); | |||||||||||||||||
| 60 | 14 | $ns->{$namespace} = $data->{$key}; | |||||||||||||||||
| 61 | # this is what XML::Simple does with existing namespaces | ||||||||||||||||||
| 62 | 14 | $res{"{http://www.w3.org/2000/xmlns/}$namespace"} = $data->{$key}; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | else { | ||||||||||||||||||
| 65 | 141 | push @keys, $key; | |||||||||||||||||
| 66 | } | ||||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | 89 | foreach my $key (@keys) { | |||||||||||||||||
| 69 | 141 | my %ns = %$ns; # copy, woot | |||||||||||||||||
| 70 | 141 | my $sub = _nsexpand($data->{$key}, \%ns); | |||||||||||||||||
| 71 | 141 | my $pos = index($key, ':'); | |||||||||||||||||
| 72 | 141 | ▶ | if ($pos > 0) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 73 | 135 | my $namespace = substr($key, 0, $pos); | |||||||||||||||||
| 74 | 135 | my $rest = substr($key, $pos+1); | |||||||||||||||||
| 75 | # move attribute sigil from namespace to value | ||||||||||||||||||
| 76 | 135 | ▶ | $rest = "\@$rest" if $namespace =~ s/^\@//; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 77 | 135 | my $expanded = $ns{$namespace}; | |||||||||||||||||
| 78 | 135 | ▶ | confess "Unknown namespace $namespace" unless $expanded; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | 135 | $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 | 141 | $res{$key} = $sub; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | 89 | return \%res; | |||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | elsif (ref($data) eq 'ARRAY') { | ||||||||||||||||||
| 95 | 9 | return [ map { _nsexpand($_, $ns) } @$data ]; | |||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | else { | ||||||||||||||||||
| 98 | # like XML::Simple's ExpandContent option | ||||||||||||||||||
| 99 | 81 | return { content => $data }; | |||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 16 | ▶ | sub xmlToHash { | ||||||||||||||||
|
Subroutine
xmlToHash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 104 | 16 | my $text = shift; | |||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 16 | my $Raw = XML::Fast::xml2hash($text, attr => '@'); | |||||||||||||||||
| 107 | # like XML::Simple's NSExpand option | ||||||||||||||||||
| 108 | 16 | my $Xml = _nsexpand($Raw); | |||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | # XML::Simple returns the content of the top level key | ||||||||||||||||||
| 111 | # (there should only be one) | ||||||||||||||||||
| 112 | 16 | my ($key) = keys %$Xml; | |||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 16 | return $Xml->{$key}; | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 5 | 1; | |||||||||||||||||