| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package XML::Loy::XRD; |
|
2
|
3
|
|
|
3
|
|
2673
|
use strict; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
94
|
|
|
3
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
96
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
1031
|
use Mojo::JSON qw/encode_json decode_json/; |
|
|
3
|
|
|
|
|
36640
|
|
|
|
3
|
|
|
|
|
195
|
|
|
6
|
3
|
|
|
3
|
|
24
|
use Mojo::Util 'quote'; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
125
|
|
|
7
|
3
|
|
|
3
|
|
17
|
use Carp qw/carp/; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
125
|
|
|
8
|
3
|
|
|
3
|
|
2124
|
use XML::Loy::Date::RFC3339; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
215
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use XML::Loy with => ( |
|
11
|
|
|
|
|
|
|
mime => 'application/xrd+xml', |
|
12
|
|
|
|
|
|
|
namespace => 'http://docs.oasis-open.org/ns/xri/xrd-1.0', |
|
13
|
|
|
|
|
|
|
prefix => 'xrd', |
|
14
|
|
|
|
|
|
|
on_init => sub { |
|
15
|
|
|
|
|
|
|
shift->namespace( |
|
16
|
530
|
|
|
530
|
|
1175
|
xsi => 'http://www.w3.org/2001/XMLSchema-instance' |
|
17
|
|
|
|
|
|
|
); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
3
|
|
|
3
|
|
620
|
); |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
28
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our @CARP_NOT; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Constructor |
|
24
|
|
|
|
|
|
|
sub new { |
|
25
|
527
|
|
|
527
|
1
|
67510
|
my $class = shift; |
|
26
|
|
|
|
|
|
|
|
|
27
|
527
|
|
|
|
|
638
|
my $xrd; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Empty |
|
30
|
527
|
100
|
|
|
|
886
|
unless ($_[0]) { |
|
|
|
100
|
|
|
|
|
|
|
31
|
516
|
|
|
|
|
1004
|
unshift(@_, 'XRD'); |
|
32
|
516
|
|
|
|
|
1178
|
$xrd = $class->SUPER::new(@_); |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# JRD |
|
36
|
0
|
|
|
|
|
0
|
elsif ($_[0] =~ /^\s*\{/) { |
|
37
|
2
|
|
|
|
|
10
|
$xrd = $class->SUPER::new('XRD'); |
|
38
|
2
|
|
|
|
|
9
|
$xrd->_to_xml($_[0]); |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Whatever |
|
42
|
|
|
|
|
|
|
else { |
|
43
|
9
|
|
|
|
|
36
|
$xrd = $class->SUPER::new(@_); |
|
44
|
|
|
|
|
|
|
}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
527
|
|
|
|
|
1293
|
return $xrd; |
|
47
|
|
|
|
|
|
|
}; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Set subject |
|
51
|
|
|
|
|
|
|
sub subject { |
|
52
|
9
|
50
|
|
9
|
1
|
3135
|
my $self = $_[0]->type eq 'root' ? |
|
53
|
|
|
|
|
|
|
shift : shift->root; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Return subject |
|
56
|
9
|
100
|
|
|
|
133
|
unless ($_[0]) { |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Subject found |
|
59
|
3
|
50
|
|
|
|
10
|
my $sub = $self->at('Subject') or return; |
|
60
|
3
|
|
|
|
|
77
|
return $sub->text; |
|
61
|
|
|
|
|
|
|
}; |
|
62
|
|
|
|
|
|
|
|
|
63
|
6
|
|
|
|
|
53
|
my $new_node = $self->set(Subject => @_); |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Set subject (only once) |
|
66
|
6
|
50
|
|
|
|
19
|
if (my $np = $self->at('*:root > *')) { |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Put in correct order - maybe not effective |
|
69
|
6
|
|
|
|
|
139
|
my $clone = $self->at('Subject'); |
|
70
|
|
|
|
|
|
|
|
|
71
|
6
|
|
|
|
|
115
|
$self->at('Subject')->remove; |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# return $np->prepend($clone); |
|
74
|
6
|
|
|
|
|
135
|
return $np->prepend($clone->to_string); |
|
75
|
|
|
|
|
|
|
}; |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Set subject |
|
78
|
0
|
|
|
|
|
0
|
return $new_node; |
|
79
|
|
|
|
|
|
|
}; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Add alias |
|
83
|
|
|
|
|
|
|
sub alias { |
|
84
|
11
|
50
|
|
11
|
1
|
1838
|
my $self = $_[0]->type eq 'root' ? |
|
85
|
|
|
|
|
|
|
shift : shift->root; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Return subject |
|
88
|
11
|
100
|
|
|
|
222
|
unless ($_[0]) { |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Subject found |
|
91
|
3
|
50
|
|
|
|
27
|
my $sub = $self->find('Alias') or return; |
|
92
|
3
|
|
|
|
|
91
|
return @{ $sub->map('text') }; |
|
|
3
|
|
|
|
|
10
|
|
|
93
|
|
|
|
|
|
|
}; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Add new alias |
|
96
|
8
|
|
|
|
|
37
|
$self->add(Alias => $_) foreach @_; |
|
97
|
|
|
|
|
|
|
|
|
98
|
8
|
|
|
|
|
37
|
return 1; |
|
99
|
|
|
|
|
|
|
}; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Add Property |
|
103
|
|
|
|
|
|
|
sub property { |
|
104
|
32
|
|
|
32
|
1
|
15243
|
my $self = shift; |
|
105
|
|
|
|
|
|
|
|
|
106
|
32
|
100
|
|
|
|
70
|
return unless $_[0]; |
|
107
|
|
|
|
|
|
|
|
|
108
|
31
|
|
|
|
|
59
|
my $type = shift; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Returns the first match |
|
111
|
31
|
100
|
|
|
|
110
|
return $self->at( qq{Property[type="$type"]} ) unless scalar @_ >= 1; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Get possible attributes |
|
114
|
11
|
100
|
66
|
|
|
57
|
my %hash = ($_[0] && ref $_[0] && ref $_[0] eq 'HASH') ? %{ shift(@_) } : (); |
|
|
1
|
|
|
|
|
6
|
|
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Set type |
|
117
|
11
|
|
|
|
|
28
|
$hash{type} = $type; |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Set xsi:nil unless there is content |
|
120
|
11
|
100
|
|
|
|
21
|
$hash{'xsi:nil'} = 'true' unless $_[0]; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Return element |
|
123
|
11
|
|
|
|
|
34
|
return $self->add(Property => \%hash => @_ ); |
|
124
|
|
|
|
|
|
|
}; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Add Link |
|
128
|
|
|
|
|
|
|
sub link { |
|
129
|
36
|
|
|
36
|
1
|
18751
|
my $self = shift; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# No rel given |
|
132
|
36
|
50
|
|
|
|
102
|
return unless $_[0]; |
|
133
|
|
|
|
|
|
|
|
|
134
|
36
|
|
|
|
|
48
|
my $rel = shift; |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Get link |
|
137
|
36
|
100
|
|
|
|
67
|
unless ($_[0]) { |
|
138
|
24
|
|
|
|
|
89
|
return $self->at( qq{Link[rel="$rel"]} ); |
|
139
|
|
|
|
|
|
|
}; |
|
140
|
|
|
|
|
|
|
|
|
141
|
12
|
|
|
|
|
13
|
my %hash; |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Accept hash reference |
|
144
|
12
|
100
|
66
|
|
|
50
|
if (ref $_[0] && ref $_[0] eq 'HASH') { |
|
145
|
8
|
|
|
|
|
9
|
%hash = %{ $_[0] }; |
|
|
8
|
|
|
|
|
35
|
|
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Accept string |
|
149
|
|
|
|
|
|
|
else { |
|
150
|
4
|
|
|
|
|
13
|
$hash{href} = shift; |
|
151
|
|
|
|
|
|
|
}; |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Set relation |
|
154
|
12
|
|
|
|
|
29
|
$hash{rel} = $rel; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# Return link object |
|
157
|
12
|
|
|
|
|
38
|
return $self->add(Link => \%hash); |
|
158
|
|
|
|
|
|
|
}; |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Set or get expiration date |
|
162
|
|
|
|
|
|
|
sub expires { |
|
163
|
10
|
|
|
10
|
1
|
6782
|
my $self = shift; |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# Return subject |
|
166
|
10
|
100
|
|
|
|
30
|
unless ($_[0]) { |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Subject found |
|
169
|
6
|
|
|
|
|
18
|
my $exp = $self->at('Expires'); |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# Return |
|
172
|
6
|
50
|
|
|
|
115
|
return unless $exp; |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# Return RFC3339 object |
|
175
|
6
|
|
|
|
|
38
|
return XML::Loy::Date::RFC3339->new($exp->text); |
|
176
|
|
|
|
|
|
|
}; |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# New RFC3339 object |
|
179
|
4
|
|
|
|
|
27
|
my $new_time = XML::Loy::Date::RFC3339->new($_[0])->to_string(0); |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# RFC3339 obect undefined |
|
182
|
4
|
50
|
|
|
|
22
|
return unless $new_time; |
|
183
|
|
|
|
|
|
|
|
|
184
|
4
|
|
|
|
|
18
|
my $new_node = $self->set(Expires => $new_time); |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Set subject (only once) |
|
187
|
4
|
50
|
|
|
|
15
|
if (my $np = $self->at('Link, Alias, Property')) { |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Put in correct order - maybe not effective |
|
190
|
4
|
|
|
|
|
98
|
my $clone = $self->at('Expires'); |
|
191
|
4
|
|
|
|
|
78
|
$self->at('Expires')->remove; |
|
192
|
4
|
|
|
|
|
87
|
return $np->prepend($clone->to_string); |
|
193
|
|
|
|
|
|
|
}; |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# Return new node |
|
196
|
0
|
|
|
|
|
0
|
return $new_node; |
|
197
|
|
|
|
|
|
|
}; |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# Check for expiration |
|
201
|
|
|
|
|
|
|
sub expired { |
|
202
|
2
|
50
|
|
2
|
1
|
556
|
my $self = $_[0]->type eq 'root' ? |
|
203
|
|
|
|
|
|
|
shift : shift->root; |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# No expiration date given |
|
206
|
2
|
50
|
|
|
|
42
|
my $exp = $self->expires or return; |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# Document is expired |
|
209
|
2
|
100
|
|
|
|
6
|
return 1 if $exp->epoch < time; |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# Document is still current |
|
212
|
1
|
|
|
|
|
8
|
return; |
|
213
|
|
|
|
|
|
|
}; |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Filter link relations |
|
217
|
|
|
|
|
|
|
sub filter_rel { |
|
218
|
4
|
|
|
4
|
1
|
2217
|
my $self = shift; |
|
219
|
4
|
|
|
|
|
13
|
my $xrd = $self->new( $self->to_string ); |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# No xrd |
|
222
|
4
|
50
|
|
|
|
13
|
return unless $xrd; |
|
223
|
|
|
|
|
|
|
|
|
224
|
4
|
|
|
|
|
22
|
my @rel; |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Push valid relations |
|
227
|
4
|
100
|
|
|
|
10
|
if (@_ == 1) { |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# Based on array reference |
|
230
|
2
|
100
|
66
|
|
|
13
|
if (ref $_[0] && ref $_[0] eq 'ARRAY') { |
|
231
|
1
|
|
|
|
|
3
|
@rel = @{ shift() }; |
|
|
1
|
|
|
|
|
4
|
|
|
232
|
|
|
|
|
|
|
} |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# Based on string |
|
235
|
|
|
|
|
|
|
else { |
|
236
|
1
|
|
|
|
|
7
|
@rel = split /\s+/, shift; |
|
237
|
|
|
|
|
|
|
} |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# As array |
|
241
|
|
|
|
|
|
|
else { |
|
242
|
2
|
|
|
|
|
5
|
@rel = @_; |
|
243
|
|
|
|
|
|
|
}; |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
# Create unwanted link relation query |
|
246
|
|
|
|
|
|
|
my $rel = scalar @rel ? 'Link:' . join(':', map { |
|
247
|
4
|
100
|
|
|
|
10
|
'not([rel=' . quote($_) . '])' |
|
|
6
|
|
|
|
|
37
|
|
|
248
|
|
|
|
|
|
|
} @rel) : 'Link'; |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
# Remove unwanted link relations |
|
251
|
4
|
|
|
|
|
35
|
$xrd->find($rel)->map('remove'); |
|
252
|
4
|
|
|
|
|
132
|
return $xrd; |
|
253
|
|
|
|
|
|
|
}; |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# Convert to xml |
|
257
|
|
|
|
|
|
|
sub _to_xml { |
|
258
|
2
|
|
|
2
|
|
4
|
my $xrd = shift; |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
# Parse json document |
|
261
|
2
|
|
|
|
|
3
|
my $jrd; |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
# There may be a parsing error |
|
264
|
2
|
50
|
|
|
|
4
|
eval { |
|
265
|
2
|
|
|
|
|
10
|
$jrd = decode_json $_[0]; |
|
266
|
|
|
|
|
|
|
} or carp $@; |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# Itterate over all XRD elements |
|
269
|
2
|
|
|
|
|
1404
|
foreach my $key (keys %$jrd) { |
|
270
|
9
|
|
|
|
|
21
|
$key = lc $key; |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
# Properties |
|
273
|
9
|
100
|
100
|
|
|
44
|
if ($key eq 'properties') { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
274
|
2
|
|
|
|
|
9
|
_to_xml_properties($xrd, $jrd->{$key}); |
|
275
|
|
|
|
|
|
|
} |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
# Links |
|
278
|
|
|
|
|
|
|
elsif ($key eq 'links') { |
|
279
|
2
|
|
|
|
|
9
|
_to_xml_links($xrd, $jrd->{$key}); |
|
280
|
|
|
|
|
|
|
} |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Subject or Expires |
|
283
|
|
|
|
|
|
|
elsif ($key eq 'subject' || $key eq 'expires') { |
|
284
|
3
|
|
|
|
|
61
|
$xrd->set(ucfirst($key), $jrd->{$key}); |
|
285
|
|
|
|
|
|
|
} |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# Aliases |
|
288
|
|
|
|
|
|
|
elsif ($key eq 'aliases') { |
|
289
|
2
|
|
|
|
|
3
|
$xrd->alias($_) foreach (@{$jrd->{$key}}); |
|
|
2
|
|
|
|
|
12
|
|
|
290
|
|
|
|
|
|
|
} |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
# Titles |
|
293
|
|
|
|
|
|
|
elsif ($key eq 'titles') { |
|
294
|
0
|
|
|
|
|
0
|
_to_xml_titles($xrd, $jrd->{$key}); |
|
295
|
|
|
|
|
|
|
}; |
|
296
|
|
|
|
|
|
|
}; |
|
297
|
|
|
|
|
|
|
}; |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
# Convert From JSON to XML |
|
301
|
|
|
|
|
|
|
sub _to_xml_titles { |
|
302
|
2
|
|
|
2
|
|
4
|
my ($node, $hash) = @_; |
|
303
|
2
|
|
|
|
|
8
|
foreach (keys %$hash) { |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# Default |
|
306
|
3
|
100
|
|
|
|
8
|
if ($_ eq 'default') { |
|
307
|
2
|
|
|
|
|
7
|
$node->add(Title => $hash->{$_}); |
|
308
|
|
|
|
|
|
|
} |
|
309
|
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# Language |
|
311
|
|
|
|
|
|
|
else { |
|
312
|
1
|
|
|
|
|
5
|
$node->add(Title => { 'xml:lang' => $_ } => $hash->{$_}); |
|
313
|
|
|
|
|
|
|
}; |
|
314
|
|
|
|
|
|
|
}; |
|
315
|
|
|
|
|
|
|
}; |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
# Convert from JSON to XML |
|
319
|
|
|
|
|
|
|
sub _to_xml_links { |
|
320
|
2
|
|
|
2
|
|
5
|
my ($node, $array) = @_; |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
# All link objects |
|
323
|
2
|
|
|
|
|
6
|
foreach (@$array) { |
|
324
|
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
# titles and properties |
|
326
|
4
|
|
|
|
|
9
|
my $titles = delete $_->{titles}; |
|
327
|
4
|
|
|
|
|
6
|
my $properties = delete $_->{properties}; |
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
# Add new link object |
|
330
|
4
|
|
|
|
|
13
|
my $link = $node->link(delete $_->{rel}, $_); |
|
331
|
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
# Add titles and properties |
|
333
|
4
|
100
|
|
|
|
21
|
_to_xml_titles($link, $titles) if $titles; |
|
334
|
4
|
100
|
|
|
|
26
|
_to_xml_properties($link, $properties) if $properties; |
|
335
|
|
|
|
|
|
|
}; |
|
336
|
|
|
|
|
|
|
}; |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
# Convert from JSON to XML |
|
340
|
|
|
|
|
|
|
sub _to_xml_properties { |
|
341
|
3
|
|
|
3
|
|
8
|
my ($node, $hash) = @_; |
|
342
|
|
|
|
|
|
|
|
|
343
|
3
|
|
|
|
|
17
|
$node->property($_ => $hash->{$_}) foreach keys %$hash; |
|
344
|
|
|
|
|
|
|
}; |
|
345
|
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
# Render JRD |
|
348
|
|
|
|
|
|
|
sub to_json { |
|
349
|
4
|
|
|
4
|
1
|
1843
|
my $self = shift; |
|
350
|
|
|
|
|
|
|
|
|
351
|
4
|
50
|
|
|
|
12
|
my $root = $self->type eq 'root' ? |
|
352
|
|
|
|
|
|
|
$self : $self->root; |
|
353
|
|
|
|
|
|
|
|
|
354
|
4
|
|
|
|
|
49
|
my %object; |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
# Serialize Subject and Expires |
|
357
|
4
|
|
|
|
|
9
|
foreach (qw/Subject Expires/) { |
|
358
|
8
|
|
|
|
|
133
|
my $obj = $root->at($_); |
|
359
|
8
|
100
|
|
|
|
836
|
$object{lc($_)} = $obj->text if $obj; |
|
360
|
|
|
|
|
|
|
}; |
|
361
|
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
# Serialize aliases |
|
363
|
4
|
|
|
|
|
74
|
my @aliases; |
|
364
|
|
|
|
|
|
|
$root->children('Alias')->each( |
|
365
|
|
|
|
|
|
|
sub { |
|
366
|
6
|
|
|
6
|
|
162
|
push(@aliases, shift->text ); |
|
367
|
4
|
|
|
|
|
13
|
}); |
|
368
|
4
|
100
|
|
|
|
124
|
$object{'aliases'} = \@aliases if @aliases; |
|
369
|
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# Serialize titles |
|
371
|
4
|
|
|
|
|
12
|
my $titles = _to_json_titles($root); |
|
372
|
4
|
50
|
|
|
|
16
|
$object{'titles'} = $titles if keys %$titles; |
|
373
|
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
# Serialize properties |
|
375
|
4
|
|
|
|
|
24
|
my $properties = _to_json_properties($root); |
|
376
|
4
|
50
|
|
|
|
18
|
$object{'properties'} = $properties if keys %$properties; |
|
377
|
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
# Serialize links |
|
379
|
4
|
|
|
|
|
8
|
my @links; |
|
380
|
|
|
|
|
|
|
$root->children('Link')->each( |
|
381
|
|
|
|
|
|
|
sub { |
|
382
|
8
|
|
|
8
|
|
117
|
my $link = shift; |
|
383
|
8
|
|
|
|
|
21
|
my $link_att = $link->attr; |
|
384
|
|
|
|
|
|
|
|
|
385
|
8
|
|
|
|
|
105
|
my %link_prop; |
|
386
|
8
|
|
|
|
|
16
|
foreach (qw/rel template href type/) { |
|
387
|
32
|
100
|
|
|
|
57
|
if (exists $link_att->{$_}) { |
|
388
|
17
|
|
|
|
|
37
|
$link_prop{$_} = $link_att->{$_}; |
|
389
|
|
|
|
|
|
|
}; |
|
390
|
|
|
|
|
|
|
}; |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
# Serialize link titles |
|
393
|
8
|
|
|
|
|
13
|
my $link_titles = _to_json_titles($link); |
|
394
|
8
|
100
|
|
|
|
25
|
$link_prop{'titles'} = $link_titles if keys %$link_titles; |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
# Serialize link properties |
|
397
|
8
|
|
|
|
|
18
|
my $link_properties = _to_json_properties($link); |
|
398
|
8
|
100
|
|
|
|
25
|
$link_prop{'properties'} = $link_properties |
|
399
|
|
|
|
|
|
|
if keys %$link_properties; |
|
400
|
|
|
|
|
|
|
|
|
401
|
8
|
|
|
|
|
30
|
push(@links, \%link_prop); |
|
402
|
4
|
|
|
|
|
14
|
}); |
|
403
|
4
|
50
|
|
|
|
36
|
$object{'links'} = \@links if @links; |
|
404
|
4
|
|
|
|
|
22
|
return encode_json(\%object); |
|
405
|
|
|
|
|
|
|
}; |
|
406
|
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
# Serialize node titles |
|
409
|
|
|
|
|
|
|
sub _to_json_titles { |
|
410
|
12
|
|
|
12
|
|
19
|
my $node = shift; |
|
411
|
12
|
|
|
|
|
14
|
my %titles; |
|
412
|
|
|
|
|
|
|
$node->children('Title')->each( |
|
413
|
|
|
|
|
|
|
sub { |
|
414
|
7
|
|
|
7
|
|
75
|
my $val = $_->text; |
|
415
|
7
|
|
100
|
|
|
189
|
my $lang = $_->attr->{'xml:lang'} || 'default'; |
|
416
|
7
|
|
|
|
|
108
|
$titles{$lang} = $val; |
|
417
|
12
|
|
|
|
|
28
|
}); |
|
418
|
12
|
|
|
|
|
215
|
return \%titles; |
|
419
|
|
|
|
|
|
|
}; |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
# Serialize node properties |
|
423
|
|
|
|
|
|
|
sub _to_json_properties { |
|
424
|
12
|
|
|
12
|
|
15
|
my $node = shift; |
|
425
|
12
|
|
|
|
|
18
|
my %property = (); |
|
426
|
|
|
|
|
|
|
$node->children('Property')->each( |
|
427
|
|
|
|
|
|
|
sub { |
|
428
|
10
|
|
|
10
|
|
118
|
my $p = shift; |
|
429
|
10
|
|
100
|
|
|
18
|
my $val = $p->text || undef; |
|
430
|
10
|
|
|
|
|
316
|
my $type = $p->attr->{'type'}; |
|
431
|
|
|
|
|
|
|
|
|
432
|
10
|
|
|
|
|
147
|
$property{$type} = $val; |
|
433
|
12
|
|
|
|
|
28
|
}); |
|
434
|
12
|
|
|
|
|
154
|
return \%property; |
|
435
|
|
|
|
|
|
|
}; |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
1; |
|
439
|
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
__END__ |