line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
############################################################################# |
3
|
|
|
|
|
|
|
## $Id: Properties.pm 6783 2006-08-11 17:43:28Z spadkins $ |
4
|
|
|
|
|
|
|
############################################################################# |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package App::Serializer::Properties; |
7
|
|
|
|
|
|
|
$VERSION = (q$Revision: 6783 $ =~ /(\d[\d\.]*)/)[0]; # VERSION numbers generated by svn |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use App; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
10
|
1
|
|
|
1
|
|
5
|
use App::Serializer; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
@ISA = ( "App::Serializer" ); |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
5
|
use App::Reference; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
608
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
App::Serializer::Properties - Interface for serialization and deserialization |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use App; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$context = App->context(); |
27
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
28
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
29
|
|
|
|
|
|
|
$data = { |
30
|
|
|
|
|
|
|
an => 'arbitrary', |
31
|
|
|
|
|
|
|
collection => [ 'of', 'data', ], |
32
|
|
|
|
|
|
|
of => { |
33
|
|
|
|
|
|
|
arbitrary => 'depth', |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
$propdata = $serializer->serialize($data); |
37
|
|
|
|
|
|
|
$data = $serializer->deserialize($propdata); |
38
|
|
|
|
|
|
|
print $serializer->dump($data), "\n"; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 DESCRIPTION |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
A Serializer allows you to serialize a structure of data |
43
|
|
|
|
|
|
|
of arbitrary depth to a scalar and deserialize it back to the |
44
|
|
|
|
|
|
|
structure. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
The Properties serializer reads and writes data which conforms to |
47
|
|
|
|
|
|
|
the standards of Java properties files. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
############################################################################# |
52
|
|
|
|
|
|
|
# CLASS |
53
|
|
|
|
|
|
|
############################################################################# |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 Class: App::Serializer::Properties |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
58
|
|
|
|
|
|
|
* Since: 0.01 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 Design |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
The class is entirely made up of static (class) methods. |
63
|
|
|
|
|
|
|
However, they are each intended to be |
64
|
|
|
|
|
|
|
called as methods on the instance itself. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
############################################################################# |
69
|
|
|
|
|
|
|
# CONSTRUCTOR METHODS |
70
|
|
|
|
|
|
|
############################################################################# |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 Constructor Methods: |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
############################################################################# |
77
|
|
|
|
|
|
|
# new() |
78
|
|
|
|
|
|
|
############################################################################# |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 new() |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
The constructor is inherited from |
83
|
|
|
|
|
|
|
L|App::Service/"new()">. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
############################################################################# |
88
|
|
|
|
|
|
|
# PUBLIC METHODS |
89
|
|
|
|
|
|
|
############################################################################# |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 Public Methods: |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
############################################################################# |
96
|
|
|
|
|
|
|
# serialize() |
97
|
|
|
|
|
|
|
############################################################################# |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 serialize() |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
* Signature: $propdata = $serializer->serialize($data); |
102
|
|
|
|
|
|
|
* Param: $data ref |
103
|
|
|
|
|
|
|
* Return: $propdata text |
104
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
105
|
|
|
|
|
|
|
* Since: 0.01 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Sample Usage: |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$context = App->context(); |
110
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
111
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
112
|
|
|
|
|
|
|
$data = { |
113
|
|
|
|
|
|
|
an => 'arbitrary', |
114
|
|
|
|
|
|
|
collection => [ 'of', 'data', ], |
115
|
|
|
|
|
|
|
of => { |
116
|
|
|
|
|
|
|
arbitrary => 'depth', |
117
|
|
|
|
|
|
|
}, |
118
|
|
|
|
|
|
|
}; |
119
|
|
|
|
|
|
|
$propdata = $serializer->serialize($data); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub serialize { |
124
|
1
|
|
|
1
|
1
|
4790
|
my ($self, $data) = @_; |
125
|
1
|
|
|
|
|
7
|
$self->_serialize($data, ""); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _serialize { |
129
|
18
|
|
|
18
|
|
30
|
my ($self, $data, $section) = @_; |
130
|
18
|
|
|
|
|
23
|
my ($section_data, $idx, $key, $elem, $prefix); |
131
|
18
|
|
|
|
|
25
|
$prefix = ""; |
132
|
18
|
100
|
66
|
|
|
94
|
$prefix = "$section." if (defined $section && $section ne ""); |
133
|
18
|
50
|
|
|
|
61
|
if (ref($data) eq "ARRAY") { |
|
|
50
|
|
|
|
|
|
134
|
0
|
|
|
|
|
0
|
for ($idx = 0; $idx <= $#$data; $idx++) { |
135
|
0
|
|
|
|
|
0
|
$elem = $data->[$idx]; |
136
|
0
|
0
|
|
|
|
0
|
if (!ref($elem)) { |
137
|
0
|
|
|
|
|
0
|
$section_data .= "$prefix$idx = $elem\n"; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
else { |
140
|
0
|
|
|
|
|
0
|
$section_data .= $self->_serialize($elem, "$prefix$idx"); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
elsif (ref($data)) { |
145
|
18
|
|
|
|
|
58
|
foreach $key (sort keys %$data) { |
146
|
38
|
|
|
|
|
88
|
$elem = $data->{$key}; |
147
|
38
|
100
|
|
|
|
73
|
if (!ref($elem)) { |
148
|
21
|
|
|
|
|
69
|
$section_data .= "$prefix$key = $elem\n"; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
else { |
151
|
17
|
|
|
|
|
66
|
$section_data .= $self->_serialize($elem, "$prefix$key"); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
18
|
|
|
|
|
69
|
return $section_data; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
############################################################################# |
160
|
|
|
|
|
|
|
# deserialize() |
161
|
|
|
|
|
|
|
############################################################################# |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 deserialize() |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
* Signature: $data = $serializer->deserialize($propdata); |
166
|
|
|
|
|
|
|
* Signature: $data = App::Serializer->deserialize($propdata); |
167
|
|
|
|
|
|
|
* Param: $data ref |
168
|
|
|
|
|
|
|
* Return: $propdata text |
169
|
|
|
|
|
|
|
* Throws: App::Exception::Serializer |
170
|
|
|
|
|
|
|
* Since: 0.01 |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Sample Usage: |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$context = App->context(); |
175
|
|
|
|
|
|
|
$serializer = $context->service("Serializer"); # or ... |
176
|
|
|
|
|
|
|
$serializer = $context->serializer(); |
177
|
|
|
|
|
|
|
$data = $serializer->deserialize($propdata); |
178
|
|
|
|
|
|
|
print $serializer->dump($data), "\n"; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub deserialize { |
183
|
2
|
|
|
2
|
1
|
939
|
my ($self, $propdata) = @_; |
184
|
2
|
|
|
|
|
4
|
my ($data, $r, $line, $attrib, $value); |
185
|
|
|
|
|
|
|
|
186
|
2
|
|
|
|
|
17
|
$r = App::Reference->new(); # dummy ref (shorthand for static calls) |
187
|
2
|
|
|
|
|
5
|
$data = {}; |
188
|
|
|
|
|
|
|
|
189
|
2
|
|
|
|
|
23
|
foreach $line (split(/\n/, $propdata)) { |
190
|
55
|
100
|
|
|
|
139
|
next if ($line =~ /^#/); # ignore comments |
191
|
49
|
100
|
|
|
|
282
|
if ($line =~ /^ *([^ =]+) *= *(.*)$/) { |
192
|
42
|
|
|
|
|
85
|
$attrib = $1; |
193
|
42
|
|
|
|
|
69
|
$value = $2; |
194
|
42
|
|
|
|
|
120
|
$r->set($attrib, $value, $data); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
2
|
|
|
|
|
20
|
return $data; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
############################################################################# |
202
|
|
|
|
|
|
|
# dump() |
203
|
|
|
|
|
|
|
############################################################################# |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 dump() |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
The constructor is inherited from |
208
|
|
|
|
|
|
|
L|App::Serializer/"dump()">. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
* Author: Stephen Adkins |
213
|
|
|
|
|
|
|
* License: This is free software. It is licensed under the same terms as Perl itself. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 SEE ALSO |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L|App::Context>, |
218
|
|
|
|
|
|
|
L|App::Service> |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
1; |
223
|
|
|
|
|
|
|
|