File Coverage

lib/App/Serializer/Xml.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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