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