line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
############################################################################# |
3
|
|
|
|
|
|
|
## $Id: Serializer.pm 6783 2006-08-11 17:43:28Z spadkins $ |
4
|
|
|
|
|
|
|
############################################################################# |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package App::Serializer; |
7
|
|
|
|
|
|
|
$VERSION = (q$Revision: 6783 $ =~ /(\d[\d\.]*)/)[0]; # VERSION numbers generated by svn |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
12
|
use App; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
52
|
|
10
|
2
|
|
|
2
|
|
1176
|
use App::Service; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
90
|
|
11
|
|
|
|
|
|
|
@ISA = ( "App::Service" ); |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
14
|
use Data::Dumper; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
85
|
|
14
|
|
|
|
|
|
|
# use Compress::Zlib; |
15
|
|
|
|
|
|
|
# use MIME::Base64; |
16
|
|
|
|
|
|
|
# use Digest::HMAC_MD5; |
17
|
|
|
|
|
|
|
# use Crypt::CBC; |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
21
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1196
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 NAME |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
App::Serializer - Interface for serialization and deserialization |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use App; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$context = App->context(); |
30
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
31
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
32
|
|
|
|
|
|
|
$data = { |
33
|
|
|
|
|
|
|
an => 'arbitrary', |
34
|
|
|
|
|
|
|
collection => [ 'of', 'data', ], |
35
|
|
|
|
|
|
|
of => { |
36
|
|
|
|
|
|
|
arbitrary => 'depth', |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
$serialized_data = $serializer->serialize($data); |
40
|
|
|
|
|
|
|
$data = $serializer->deserialize($serialized_data); |
41
|
|
|
|
|
|
|
print $serializer->dump($data), "\n"; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
A Serializer is a means by which a structure of data of arbitrary depth |
46
|
|
|
|
|
|
|
may be serialized or deserialized. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Serializers may be used for configuration files, data persistence, or |
49
|
|
|
|
|
|
|
transmission of data across a network. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Serializers include the ability to compress, encrypt, and/or MIME |
52
|
|
|
|
|
|
|
the serialized data. (These are all scalar-to-scalar transformations.) |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
############################################################################# |
57
|
|
|
|
|
|
|
# CLASS GROUP |
58
|
|
|
|
|
|
|
############################################################################# |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 Class Group: Serializer |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
The following classes might be a part of the Serializer Class Group. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=over |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=item * Class: App::Serializer |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item * Class: App::Serializer::Storable |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item * Class: App::Serializer::XMLSimple |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item * Class: App::Serializer::XML |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * Class: App::Serializer::Ini |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item * Class: App::Serializer::Properties |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=back |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
############################################################################# |
83
|
|
|
|
|
|
|
# CLASS |
84
|
|
|
|
|
|
|
############################################################################# |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 Class: App::Serializer |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
A Serializer serves to serialize and deserialize perl data structures |
89
|
|
|
|
|
|
|
of arbitrary depth. |
90
|
|
|
|
|
|
|
The base class serializes the data as Perl code using Data::Dumper. |
91
|
|
|
|
|
|
|
(This behavior is overridden with customized serialization techniques |
92
|
|
|
|
|
|
|
by the derived subclasses.) |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
95
|
|
|
|
|
|
|
* Since: 0.01 |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 Class Design |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
The class is entirely made up of static (class) methods. |
100
|
|
|
|
|
|
|
However, they are each intended to be |
101
|
|
|
|
|
|
|
called as methods on the instance itself. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
############################################################################# |
106
|
|
|
|
|
|
|
# CONSTRUCTOR METHODS |
107
|
|
|
|
|
|
|
############################################################################# |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 Constructor Methods: |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
############################################################################# |
114
|
|
|
|
|
|
|
# new() |
115
|
|
|
|
|
|
|
############################################################################# |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 new() |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
The constructor is inherited from |
120
|
|
|
|
|
|
|
L|App::Service/"new()">. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
############################################################################# |
125
|
|
|
|
|
|
|
# PUBLIC METHODS |
126
|
|
|
|
|
|
|
############################################################################# |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 Public Methods: |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
############################################################################# |
133
|
|
|
|
|
|
|
# serialize() |
134
|
|
|
|
|
|
|
############################################################################# |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 serialize() |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
* Signature: $serialized_data = $serializer->serialize($data); |
139
|
|
|
|
|
|
|
* Param: $data ref |
140
|
|
|
|
|
|
|
* Return: $serialized_data binary |
141
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
142
|
|
|
|
|
|
|
* Since: 0.01 |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Sample Usage: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
$context = App->context(); |
147
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
148
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
149
|
|
|
|
|
|
|
$data = { |
150
|
|
|
|
|
|
|
an => 'arbitrary', |
151
|
|
|
|
|
|
|
collection => [ 'of', 'data', ], |
152
|
|
|
|
|
|
|
of => { |
153
|
|
|
|
|
|
|
arbitrary => 'depth', |
154
|
|
|
|
|
|
|
}, |
155
|
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
$serialized_data = $serializer->serialize($data); |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub serialize { |
161
|
0
|
|
|
0
|
1
|
|
my ($self, $data) = @_; |
162
|
0
|
|
|
|
|
|
$self->dump($data); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
############################################################################# |
166
|
|
|
|
|
|
|
# deserialize() |
167
|
|
|
|
|
|
|
############################################################################# |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 deserialize() |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
* Signature: $serialized_data = $serializer->deserialize($data); |
172
|
|
|
|
|
|
|
* Signature: $serialized_data = App::Serializer->deserialize($data); |
173
|
|
|
|
|
|
|
* Param: $data ref |
174
|
|
|
|
|
|
|
* Return: $serialized_data binary |
175
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
176
|
|
|
|
|
|
|
* Since: 0.01 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Sample Usage: |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
$context = App->context(); |
181
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
182
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
183
|
|
|
|
|
|
|
$data = $serializer->deserialize($serialized_data); |
184
|
|
|
|
|
|
|
print $serializer->dump($data), "\n"; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub deserialize { |
189
|
0
|
|
|
0
|
1
|
|
my ($self, $serialized_data) = @_; |
190
|
0
|
|
|
|
|
|
my ($data, $serializer_class); |
191
|
0
|
|
|
|
|
|
$data = {}; |
192
|
0
|
|
|
|
|
|
$serializer_class = ""; |
193
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
if ($self eq "App::Serializer") { # static method call |
195
|
|
|
|
|
|
|
|
196
|
0
|
0
|
|
|
|
|
if ($serialized_data =~ s/#Serializer +([^ ]+) +\((.*)\)\n//) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
$serializer_class = $1; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
elsif ($serialized_data =~ /^
|
200
|
0
|
|
|
|
|
|
$serializer_class = "App::Serializer::XML"; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
elsif ($serialized_data =~ /^) { |
203
|
0
|
|
|
|
|
|
$serializer_class = "App::Serializer::XMLSimple"; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
0
|
0
|
|
|
|
|
if ($serializer_class) { |
208
|
0
|
|
|
|
|
|
eval "use $serializer_class;"; |
209
|
0
|
0
|
|
|
|
|
if ($@) { |
210
|
0
|
|
|
|
|
|
App::Exception::Serializer->throw( |
211
|
|
|
|
|
|
|
error => "create(): error loading $serializer_class serializer class\n" |
212
|
|
|
|
|
|
|
); |
213
|
|
|
|
|
|
|
} |
214
|
0
|
|
|
|
|
|
$data = $serializer_class->deserialize($serialized_data); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
else { |
217
|
0
|
0
|
|
|
|
|
if ($serialized_data =~ /^\$[a-zA-Z][a-zA-Z0-9_]* *= *(.*)$/s) { |
218
|
0
|
|
|
|
|
|
$serialized_data = "\$data = $1"; # untainted now |
219
|
0
|
|
|
|
|
|
eval($serialized_data); |
220
|
0
|
0
|
|
|
|
|
die "Deserialization Error: $@" if ($@); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
else { |
223
|
0
|
|
|
|
|
|
die "Deserialization Error: Data didn't have \"\$var = {...};\" or \"\$var = [ ... ];\" format."; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
$data; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
############################################################################# |
231
|
|
|
|
|
|
|
# serialized_content_type() |
232
|
|
|
|
|
|
|
############################################################################# |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 serialized_content_type() |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
* Signature: $serialized_content_type = $service->serialized_content_type(); |
237
|
|
|
|
|
|
|
* Param: void |
238
|
|
|
|
|
|
|
* Return: $serialized_content_type string |
239
|
|
|
|
|
|
|
* Throws: App::Exception |
240
|
|
|
|
|
|
|
* Since: 0.01 |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Sample Usage: |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
$serialized_content_type = $service->serialized_content_type(); |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub serialized_content_type { |
249
|
0
|
|
|
0
|
1
|
|
'text/plain'; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
############################################################################# |
253
|
|
|
|
|
|
|
# dump() |
254
|
|
|
|
|
|
|
############################################################################# |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 dump() |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
* Signature: $perl = $serializer->dump($data); |
259
|
|
|
|
|
|
|
* Param: $data ref |
260
|
|
|
|
|
|
|
* Return: $perl text |
261
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
262
|
|
|
|
|
|
|
* Since: 0.01 |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Sample Usage: |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
$context = App->context(); |
267
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
268
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
269
|
|
|
|
|
|
|
$data = $serializer->deserialize($serialized_data); |
270
|
|
|
|
|
|
|
print $serializer->dump($data), "\n"; |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=cut |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub dump { |
275
|
0
|
|
|
0
|
1
|
|
my ($self, $data) = @_; |
276
|
0
|
|
|
|
|
|
my $d = Data::Dumper->new([ $data ], [ "data" ]); |
277
|
0
|
|
|
|
|
|
$d->Indent(1); |
278
|
0
|
|
|
|
|
|
return $d->Dump(); |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
############################################################################# |
282
|
|
|
|
|
|
|
# PROTECTED METHODS |
283
|
|
|
|
|
|
|
############################################################################# |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head1 Protected Methods: |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=cut |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
############################################################################# |
290
|
|
|
|
|
|
|
# Method: service_type() |
291
|
|
|
|
|
|
|
############################################################################# |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head2 service_type() |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Returns 'Serializer'; |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
* Signature: $service_type = App::Serializer->service_type(); |
298
|
|
|
|
|
|
|
* Param: void |
299
|
|
|
|
|
|
|
* Return: $service_type string |
300
|
|
|
|
|
|
|
* Since: 0.01 |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
$service_type = $serializer->service_type(); |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=cut |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
sub service_type () { 'Serializer'; } |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
* Author: Stephen Adkins |
311
|
|
|
|
|
|
|
* License: This is free software. It is licensed under the same terms as Perl itself. |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=head1 SEE ALSO |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
L|App::Context>, |
316
|
|
|
|
|
|
|
L|App::Service> |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=cut |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
1; |
321
|
|
|
|
|
|
|
|