File Coverage

blib/lib/Net/DAVTalk/XMLParser.pm
Criterion Covered Total %
statement 33 44 75.0
branch 13 20 65.0
condition 2 2 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 54 72 75.0


line stmt bran cond sub pod time code
1              
2             use base 'Exporter';
3 2     2   94391  
  2         13  
  2         199  
4             =head1 NAME
5              
6             Net::DAVTalk - Interface to talk to DAV servers
7              
8             =head1 SYNOPSIS
9              
10             Net::DAVTalk::XMLParser is a simple wrapper around XML::Fast, returning
11             a more usable structure like that created by XML::Simple, but running
12             approximately 10 times faster in testing.
13              
14             =head1 SUBROUTINES/METHODS
15              
16             =head2 $hashref = xmlToHash($xmlstring);
17              
18             Converts an XML string to a hashref of the content.
19              
20             =head1 ACKNOWLEDGEMENTS
21              
22              
23             =head1 LICENSE AND COPYRIGHT
24              
25             Copyright 2015 FastMail Pty. Ltd.
26              
27             This program is free software; you can redistribute it and/or modify it
28             under the terms of the the Artistic License (2.0). You may obtain a
29             copy of the full license at:
30              
31             L<http://www.perlfoundation.org/artistic_license_2_0>
32              
33             Any use, modification, and distribution of the Standard or Modified
34             Versions is governed by this Artistic License. By using, modifying or
35             distributing the Package, you accept this license. Do not use, modify,
36             or distribute the Package, if you do not accept this license.
37              
38             If your Modified Version has been derived from a Modified Version made
39             by someone other than you, you are nevertheless required to ensure that
40             your Modified Version complies with the requirements of this license.
41              
42             This license does not grant you the right to use any trademark, service
43             mark, tradename, or logo of the Copyright Holder.
44              
45             This license includes the non-exclusive, worldwide, free-of-charge
46             patent license to make, have made, use, offer to sell, sell, import and
47             otherwise transfer the Package with respect to any patent claims
48             licensable by the Copyright Holder that are necessarily infringed by the
49             Package. If you institute patent litigation (including a cross-claim or
50             counterclaim) against any party alleging that the Package constitutes
51             direct or contributory patent infringement, then this Artistic License
52             to you shall terminate on the date that such litigation is filed.
53              
54             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
55             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
56             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
57             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
58             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
59             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
60             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
61             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62              
63             =cut
64              
65              
66             our @EXPORT = qw(xmlToHash);
67              
68             use XML::Fast;
69 2     2   726 use Carp qw(confess);
  2         10627  
  2         105  
70 2     2   13  
  2         5  
  2         834  
71             my $data = shift;
72             my $ns = shift || {};
73 8     8   12  
74 8   100     21 if (ref($data) eq 'HASH') {
75             my @keys;
76 8 100       21 my %res;
    50          
77 6         7 foreach my $key (keys %$data) {
78             if ($key eq '@xmlns') {
79 6         15 $ns->{''} = $data->{$key};
80 10 100       25 }
    100          
    50          
81 2         6 elsif ($key eq '#text') {
82             $res{'content'} = $data->{$key};
83             }
84 2         5 elsif (substr($key, 0, 7) eq '@xmlns:') {
85             my $namespace = substr($key, 7);
86             $ns->{$namespace} = $data->{$key};
87 0         0 # this is what XML::Simple does with existing namespaces
88 0         0 $res{"{http://www.w3.org/2000/xmlns/}$namespace"} = $data->{$key};
89             }
90 0         0 else {
91             push @keys, $key;
92             }
93 6         10 }
94             foreach my $key (@keys) {
95             my %ns = %$ns; # copy, woot
96 6         11 my $sub = _nsexpand($data->{$key}, \%ns);
97 6         13 my $pos = index($key, ':');
98 6         17 if ($pos > 0) {
99 6         14 my $namespace = substr($key, 0, $pos);
100 6 50       23 my $rest = substr($key, $pos+1);
    100          
    100          
101 0         0 # move attribute sigil from namespace to value
102 0         0 $rest = "\@$rest" if $namespace =~ s/^\@//;
103             my $expanded = $ns{$namespace};
104 0 0       0 confess "Unknown namespace $namespace" unless $expanded;
105 0         0 $key = "{$expanded}$rest";
106 0 0       0 }
107 0         0 elsif ($key =~ m/^\@/) {
108             # Attributes are never subject to the default namespace.
109             # An attribute without an explicit namespace prefix is
110             # considered not to be in any namespace.
111             }
112             elsif ($ns{''}) {
113             my $expanded = $ns{''};
114             $key = "{$expanded}$key";
115 2         7 }
116 2         12 $res{$key} = $sub;
117             }
118 6         18 return \%res;
119             }
120 6         42 elsif (ref($data) eq 'ARRAY') {
121             return [ map { _nsexpand($_, $ns) } @$data ];
122             }
123 0         0 else {
  0         0  
124             # like XML::Simple's ExpandContent option
125             return { content => $data };
126             }
127 2         7 }
128              
129             my $text = shift;
130              
131             my $Raw = XML::Fast::xml2hash($text, attr => '@');
132 2     2 1 1559 # like XML::Simple's NSExpand option
133             my $Xml = _nsexpand($Raw);
134 2         8  
135             # XML::Simple returns the content of the top level key
136 2         75 # (there should only be one)
137             my ($key) = keys %$Xml;
138              
139             return $Xml->{$key};
140 2         6 }
141              
142 2         9 1;