line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Braintree::Xml; |
2
|
|
|
|
|
|
|
$WebService::Braintree::Xml::VERSION = '0.92'; |
3
|
1
|
|
|
1
|
|
10
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
46
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
688
|
use XML::Simple; |
|
1
|
|
|
|
|
11414
|
|
|
1
|
|
|
|
|
12
|
|
6
|
1
|
|
|
1
|
|
136
|
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
95
|
|
7
|
1
|
|
|
1
|
|
11
|
use Exporter; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
96
|
|
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT = qw(hash_to_xml xml_to_hash array collect_from_array); |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(); |
11
|
1
|
|
|
1
|
|
12
|
use WebService::Braintree::Util; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
117
|
|
12
|
1
|
|
|
1
|
|
498
|
use DateTime::Format::Atom; |
|
1
|
|
|
|
|
5079
|
|
|
1
|
|
|
|
|
40
|
|
13
|
1
|
|
|
1
|
|
283
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub hash_to_xml { |
16
|
|
|
|
|
|
|
my $hash = shift; |
17
|
|
|
|
|
|
|
my $doc = XML::LibXML::Document->createDocument('1.0', 'UTF-8'); |
18
|
|
|
|
|
|
|
my $root_key = (keys %$hash)[0]; |
19
|
|
|
|
|
|
|
my $element = XML::LibXML::Element->new($root_key); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $value = $hash->{$root_key}; |
22
|
|
|
|
|
|
|
my $node = add_node($element, $value); |
23
|
|
|
|
|
|
|
$doc->setDocumentElement($element); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
return $doc->toString(1); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub add_node { |
29
|
|
|
|
|
|
|
my ($parent, $value) = @_; |
30
|
|
|
|
|
|
|
if (is_hashref($value)) { |
31
|
|
|
|
|
|
|
build_from_hash($parent, $value); |
32
|
|
|
|
|
|
|
} elsif (is_arrayref($value)) { |
33
|
|
|
|
|
|
|
build_from_array($parent, $value); |
34
|
|
|
|
|
|
|
} else { |
35
|
|
|
|
|
|
|
$parent->appendText($value) if $value; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub build_from_hash { |
40
|
|
|
|
|
|
|
my($parent, $value) = @_; |
41
|
|
|
|
|
|
|
while (my($key, $child_value) = each(%$value)) { |
42
|
|
|
|
|
|
|
build_node($key, $child_value, $parent); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub build_from_array { |
47
|
|
|
|
|
|
|
my($parent, $value) = @_; |
48
|
|
|
|
|
|
|
$parent->setAttribute('type', 'array'); |
49
|
|
|
|
|
|
|
foreach my $child_value (@$value) { |
50
|
|
|
|
|
|
|
build_node('item', $child_value, $parent); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub build_node { |
55
|
|
|
|
|
|
|
my($node_name, $child_value, $parent) = @_; |
56
|
|
|
|
|
|
|
my $child = XML::LibXML::Element->new($node_name); |
57
|
|
|
|
|
|
|
add_node($child, $child_value); |
58
|
|
|
|
|
|
|
$parent->appendChild($child); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub xml_to_hash { |
62
|
|
|
|
|
|
|
my $return = XMLin(shift, KeyAttr => [], KeepRoot => 1); |
63
|
|
|
|
|
|
|
my $scrubbed = scrubbed($return); |
64
|
|
|
|
|
|
|
return $scrubbed; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub scrubbed { |
68
|
|
|
|
|
|
|
my $tree = shift; |
69
|
|
|
|
|
|
|
if (is_hashref($tree)) { |
70
|
|
|
|
|
|
|
return collect_from_hash($tree); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
if (is_arrayref($tree)) { |
73
|
|
|
|
|
|
|
return collect_from_array($tree); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
return $tree; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub collect_from_array { |
79
|
|
|
|
|
|
|
my ($tree) = @_; |
80
|
|
|
|
|
|
|
my @new_array = (); |
81
|
|
|
|
|
|
|
foreach my $value (@$tree) { |
82
|
|
|
|
|
|
|
push(@new_array, scrubbed($value)); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
return \@new_array; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub collect_from_hash { |
88
|
|
|
|
|
|
|
my ($tree) = @_; |
89
|
|
|
|
|
|
|
my $new_hash = {}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my %types = ( |
92
|
|
|
|
|
|
|
'array' => \&array, |
93
|
|
|
|
|
|
|
'boolean' => \&boolean, |
94
|
|
|
|
|
|
|
'integer' => \&integer, |
95
|
|
|
|
|
|
|
'datetime' => \&datetime, |
96
|
|
|
|
|
|
|
'date' => \&date |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
foreach my $type (keys %types) { |
100
|
|
|
|
|
|
|
return $types{$type}->($tree) if is_of_type($type, $tree); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
return $tree->{'content'} if exists($tree->{'content'}); |
103
|
|
|
|
|
|
|
return undef if is_nil($tree); |
104
|
|
|
|
|
|
|
while (my ($key, $subtree) = each(%$tree)) { |
105
|
|
|
|
|
|
|
$new_hash->{sub_dashes($key)} = scrubbed($subtree); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
return $new_hash; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub is_of_type { |
111
|
|
|
|
|
|
|
my ($type, $tree) = @_; |
112
|
|
|
|
|
|
|
no warnings; |
113
|
|
|
|
|
|
|
return 0 unless $tree->{'type'}; |
114
|
|
|
|
|
|
|
return $tree->{'type'} eq $type; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub is_nil { |
118
|
|
|
|
|
|
|
my $tree = shift; |
119
|
|
|
|
|
|
|
return 0 unless $tree->{'nil'}; |
120
|
|
|
|
|
|
|
return $tree->{'nil'} eq "true"; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub boolean { |
124
|
|
|
|
|
|
|
return shift->{'content'} eq 'true' ? 1 : 0; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub integer { |
128
|
|
|
|
|
|
|
return shift->{'content'}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $f = DateTime::Format::Atom->new(); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub datetime { |
134
|
|
|
|
|
|
|
my $dt = $f->parse_datetime(shift->{'content'}); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub date { |
138
|
|
|
|
|
|
|
my $date = shift->{'content'}; |
139
|
|
|
|
|
|
|
$date .= 'T00:00:00Z'; |
140
|
|
|
|
|
|
|
my $dt = $f->parse_datetime($date); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub array { |
144
|
|
|
|
|
|
|
my $tree = shift; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
delete $tree->{type}; |
147
|
|
|
|
|
|
|
my $subtree = (values %$tree)[0]; |
148
|
|
|
|
|
|
|
if (ref $subtree eq 'HASH') { |
149
|
|
|
|
|
|
|
return [scrubbed($subtree)]; |
150
|
|
|
|
|
|
|
} elsif (ref $subtree eq 'ARRAY') { |
151
|
|
|
|
|
|
|
return scrubbed(force_array($subtree)); |
152
|
|
|
|
|
|
|
} elsif (defined($subtree)) { |
153
|
|
|
|
|
|
|
return [$subtree]; |
154
|
|
|
|
|
|
|
} else { |
155
|
|
|
|
|
|
|
return []; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub sub_dashes { |
162
|
|
|
|
|
|
|
my $string = shift; |
163
|
|
|
|
|
|
|
$string =~ s/-/_/g; |
164
|
|
|
|
|
|
|
return $string; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub force_array { |
168
|
|
|
|
|
|
|
my $subtree = shift; |
169
|
|
|
|
|
|
|
return $subtree if(is_arrayref($subtree)); |
170
|
|
|
|
|
|
|
return [$subtree]; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
1; |