blib/lib/XML/Simple/Sugar.pm

filter: Statementstmt 100% 49 / 49 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 100% 62 / 62 CC 1 SCAR 0.0
CC
1
Cov
100%
__ANON__
1.0
__ANON__
1.0
__ANON__
1.0
CRAP 1.0
↑ Summary
1 1 ▶use 5.18.2;
Subroutine __ANON__: called CC=1 SCAR=0.0
2 1 ▶use Modern::Perl;
Subroutine __ANON__: called CC=1 SCAR=0.0
3 1 ▶use Moops;
Subroutine __ANON__: called CC=1 SCAR=0.0
4  
5 1 ▶class XML::Simple::Sugar 1.0.5 {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 our $AUTOLOAD;
7 use XML::Simple;
8 use UNIVERSAL::isa;
9 use Data::Dumper;
10  
11 has 'xml_index' => ( 'is' => 'ro', 'isa' => 'Int', default => 0 );
12 has 'xml_node' => ( 'is' => 'ro', 'isa' => Maybe[Str] );
13 has 'xml_xs' => (
14 'is' => 'rw',
15 'isa' => 'XML::Simple',
16 'default' => sub { XML::Simple->new( XMLDecl => '<?xml version="1.0"?>' ); }
17 );
18 has 'xml_data' => (
19 'is' => 'rw',
20 'isa' => Maybe[HashRef|ArrayRef],
21 'default' => method { $self->xml_data ? $self->xml_data : {}; }
22 );
23 has 'xml_parent' => ( 'is' => 'ro', 'isa' => InstanceOf['XML::Simple::Sugar'] );
24 has 'xml_autovivify' => ( 'is' => 'rw', 'isa' => Bool, default => 1 );
25 has 'xml' => (
26 'is' => 'rw',
27 'isa' => Str,
28 'trigger' => method {
29 $self->xml_data(
30 XMLin(
31 $self->xml,
32 ForceContent => 1,
33 KeepRoot => 1,
34 ForceArray => 1,
35 ContentKey => 'xml_content',
36 )
37 );
38 }
39 );
40  
41 method xml_write {
42 return $self->xml_xs->XMLout(
43 $self->xml_root->xml_data,
44 KeepRoot => 1,
45 ContentKey => 'xml_content',
46 );
47 }
48  
49 method xml_read (Str $xml) {
50 $self->xml_data(
51 $self->xml_xs->XMLin(
52 $xml,
53 ForceContent => 1,
54 KeepRoot => 1,
55 ForceArray => 1,
56 ContentKey => 'xml_content',
57 )
58 );
59 return $self;
60 }
61  
62 method xml_root {
63 if ( defined( $self->xml_parent ) ) {
64 return $self->xml_parent->xml_root;
65 }
66 else {
67 return $self;
68 }
69 }
70  
71 multi method xml_attr (HashRef $attr) {
72 foreach my $attribute (keys %$attr) {
73 if (
74 $self->xml_autovivify
75 || grep( /^$attribute$/,
76 keys %{
77 $self->xml_parent->xml_data->{ $self->xml_node }
78 ->[ $self->xml_index ]
79 } )
80 )
81 {
82 $self->xml_parent->xml_data->{ $self->xml_node }
83 ->[ $self->xml_index ]->{$attribute} =
84 $attr->{$attribute};
85 }
86 else {
87 die qq|$attribute is not an attribute of | . $self->xml_node;
88 }
89 }
90 return $self;
91 }
92  
93 multi method xml_attr () {
94 my %attr;
95 foreach ( keys %{ $self->xml_data } ) {
96 $attr{$_} = $self->xml_data->{$_}
97 if ( !( UNIVERSAL::isa( $self->xml_data->{$_}, 'ARRAY' ) ) );
98 }
99 return \%attr;
100 }
101  
102 method xml_rmattr (Str $attr) {
103 delete $self->xml_parent->xml_data->{ $self->xml_node }
104 ->[ $self->xml_index ]->{$attr};
105 return $self;
106 }
107  
108 method xml_content (Str $content?) {
109 if ($content) {
110 $self->xml_data->{xml_content} = $content;
111 return $self;
112 }
113 else {
114 $self->xml_data->{xml_content};
115 }
116 }
117  
118 method xml_nest (InstanceOf['XML::Simple::Sugar'] $xs) {
119 $self->xml_parent->xml_data->{ $self->xml_node }->[ $self->xml_index ]
120 = $xs->xml_data;
121 return $self;
122 }
123  
124 multi method xml_subnode (Str $node, InstanceOf['XML::Simple::Sugar'] $content) {
125 $self->xml_data->{$node}->[ $self->xml_index ] = $content->xml_data;
126 }
127  
128 multi method xml_subnode (Str $node, HashRef $content) {
129 foreach my $attribute (keys %$content) {
130 if ( UNIVERSAL::isa( $self->xml_data->{$node}, 'ARRAY' ) ) {
131 if (
132 $self->xml_autovivify
133 || grep( /^$attribute$/,
134 keys %{
135 $self->xml_data->{$node}->[ $self->xml_index ]
136 } )
137 )
138 {
139 $self->xml_data->{$node}->[ $self->xml_index ]
140 ->{$attribute} = $content->{$attribute};
141 }
142 else {
143 die qq|$attribute is not an attribute of $node|;
144 }
145 }
146 else {
147 if (
148 $self->xml_autovivify
149 || grep( /^$attribute$/,
150 keys %{ $self->xml_data->{$node} } )
151 )
152 {
153 $self->xml_data->{$node}->{$attribute} =
154 { 'value' => $content->{$attribute} };
155 }
156 else {
157 die qq|$attribute is not an attribute of $node|;
158 }
159 }
160 }
161 return $self;
162 }
163  
164 multi method xml_subnode (Str $node, ArrayRef $content) {
165 if ( $content->[0] =~ m/^[0-9]+$/ )
166 {
167 if ( $self->xml_autovivify ) {
168 unless ( $self->xml_data->{$node} ) {
169 $self->xml_data->{$node} = [];
170 }
171 unless (
172 UNIVERSAL::isa(
173 $self->xml_data->{$node}->[ $content->[0] ], 'HASH'
174 )
175 )
176 {
177 $self->xml_data->{$node}->[ $content->[0] ] = {};
178 }
179 }
180 else {
181 unless ( $self->xml_data->{$node} ) {
182 die qq|$node not in |
183 . Data::Dumper::Dumper( $self->xml_data );
184 }
185 unless (
186 UNIVERSAL::isa(
187 $self->xml_data->{$node}->[ $content->[0] ], 'HASH'
188 )
189 )
190 {
191 die qq|Element $content->[0] not in |
192 . Data::Dumper::Dumper( $self->xml_data->{$node} );
193 }
194 }
195 my $xs = XML::Simple::Sugar->new(
196 {
197 xml_node => $node,
198 xml_data => $self->xml_data->{$node}->[ $content->[0] ],
199 xml_parent => $self,
200 xml_autovivify => $self->xml_autovivify,
201 xml_index => $content->[0]
202 }
203 );
204 if ( defined( $content->[1] )
205 && UNIVERSAL::isa( $content->[1], InstanceOf['XML::Simple::Sugar'] ) )
206 {
207 $xs->xml_nest( $content->[1] );
208 }
209 elsif ( defined( $content->[1] ) ) {
210 $xs->xml_content( $content->[1] );
211 }
212 if ( defined( $content->[2] )
213 && UNIVERSAL::isa( $content->[2], 'HASH' ) )
214 {
215 $xs->xml_attr( $content->[2] );
216 }
217 return $xs;
218 }
219 elsif ( $content->[0] =~ m/^all$/i )
220 {
221 if ( UNIVERSAL::isa( $self->xml_data->{$node}, 'ARRAY' ) ) {
222 return map {
223 XML::Simple::Sugar->new(
224 {
225 xml_node => $node,
226 xml_data => $self->xml_data->{$node}->[$_],
227 xml_parent => $self,
228 xml_autovivify => $self->xml_autovivify,
229 xml_index => $_
230 }
231 );
232 } 0 .. scalar @{ $self->xml_data->{$node} } - 1;
233 }
234 }
235 }
236
237 multi method xml_subnode (Str $node, Str $content) {
238 $self->xml_data->{$node}->[0]->{xml_content} = $content;
239 return $self;
240 }
241  
242 multi method xml_subnode (Str $node) {
243 unless ( $self->xml_data->{$node} ) {
244 if ( $self->xml_autovivify == 1 ) {
245 $self->xml_data->{$node}->[0] = {};
246 }
247 else {
248 die qq|$node not in |
249 . Data::Dumper::Dumper( $self->xml_data );
250 }
251 }
252  
253 if ( UNIVERSAL::isa( $self->xml_data->{$node}, 'ARRAY' ) ) {
254 return XML::Simple::Sugar->new(
255 {
256 xml_node => $node,
257 xml_data => $self->xml_data->{$node}->[0],
258 xml_parent => $self,
259 xml_autovivify => $self->xml_autovivify,
260 xml_index => $self->xml_index
261 }
262 );
263 }
264 else {
265 return XML::Simple::Sugar->new(
266 {
267 xml_node => $node,
268 xml_data => $self->xml_data->{$node},
269 xml_parent => $self,
270 xml_autovivify => $self->xml_autovivify,
271 xml_index => $self->xml_index
272 }
273 );
274 }
275 }
276  
277 method AUTOLOAD ($content?) {
278 my ($node) = $AUTOLOAD =~ m/([^:]*)$/;
279 $content ? $self->xml_subnode($node, $content) : $self->xml_subnode($node);
280 }
281 }
282  
283 1;
284  
285 # ABSTRACT: Sugar sprinkled on XML::Simple
286 # PODNAME: XML::Simple::Sugar
287  
288 __END__
289  
290 =pod
291  
292 =encoding UTF-8
293  
294 =head1 NAME
295  
296 XML::Simple::Sugar - Sugar sprinkled on XML::Simple
297  
298 =head1 VERSION
299  
300 version v1.0.5
301  
302 =head1 SYNOPSIS
303  
304 The basics...
305  
306 use Modern::Perl;
307 use Data::Dumper;
308 use XML::Simple::Sugar;
309
310 my $xs = XML::Simple::Sugar->new();
311
312 #Autovivify some elements nested within each other, and set the content of the salary element
313 $xs->company->departments->department([0])->person([0])->salary(60000);
314
315 my $departments = $xs->company->departments;
316 $departments->department([0])->xml_attr({ name => 'IT Department', manager => 'John Smith' });
317  
318 say $xs->xml_write;
319  
320 # Or from a child element... (returns a string reflecting the root element's XML)
321 say $departments->xml_write;
322  
323 Working with existing XML
324  
325 my $xs = XML::Simple::Sugar->new({ xml => '<your_xml>here</your_xml>' });
326  
327 Setting and retrieving content/attributes
328  
329 # Return all person elements in the first department element and say their first names
330 map{ say $_->first_name->xml_content; } $xs->company->departments->department([0])->person(['all']);
331
332 # Setting the content of elements
333 my $person = $xs->company->departments->department([0])->person([0]);
334 $person->first_name('John')->last_name('Smith')->email('jsmith@example.com');
335 # Or, using xml_content...
336 $person->first_name->xml_content('John');
337 say $person->first_name->xml_content;
338  
339 # Setting attributes of an element
340 $xs->company->departments->department([0])->person([0])->salary({ 'jobgrade' => 10, 'exempt' => 1 });
341 # Or, using xml_attr...
342 $person->xml_attr({ skill => 'Perl', skill_level => 'intermediate' });
343 say Data::Dumper::Dumper($person->xml_attr);
344
345 # Or setting the string contents of an element and attribute values all at once...
346 $person->first_name([0,'John',{'is_nice' => 'true'}]);
347  
348 Composing larger documents from other XML::Simple::Sugar objects
349  
350 my $xs=XML::Simple::Sugar->new();
351 my $xs2=XML::Simple::Sugar->new();
352 $xs2->table->tr->th([0,'First Name',{ style => 'text-align:left' }]);
353 $xs2->table->tr->th([1,'Last Name']);
354 $xs->html->body->div->h1('Page Title')->xml_attr({ style => 'font-weight:bold' });
355 $xs->html->body->div([1,$xs2]);
356  
357 =head1 DESCRIPTION
358  
359 This module is a wrapper around L<XML::Simple> to provide AUTOLOADed accessors to XML nodes.
360  
361 =head1 WHY ANOTHER XML MODULE?
362  
363 I wanted to write/manipulate simple XML payloads with more DWIMmery than what I found in modules available on CPAN (admittedly I didn't look very hard). If the above syntax doesn't accomplish that for you, you should probably use a different module.
364  
365 Additionally, this package depends on L<XML::Simple>, which currently has a "do not use this module in new code" notice. If you are cool with that, then so am I. :)
366  
367 =head1 PLEASE BE ADVISED
368  
369 Most of the automagic happens with AUTOLOAD. Accessors/mutators and method names in this package cannot be used as element names in the XML document. XML naming rules prohibit the use of elements starting with the string "xml", so I've used this string as a prefix to all accessors/mutators/methods to avoid potential conflicts with AUTOLOAD. Sorry for the extra characters. :/
370  
371 =head1 ATTRIBUTES
372  
373 =head2 xml_autovivify (bool)
374  
375 This attribute determines on a per element basis whether new attributes or elements may be introduced. Child elements inherit this setting from their parent. Setting autovivify to false is useful when working with templates with a strict predefined XML structure. This attribute is true by default.
376  
377 =head2 xml_data (XML::Simple compliant Perl representation of an XML document)
378  
379 This is the Perl representation of the XML. This is ugly to work with directly (hence this module), but in lieu of methods yet unwritten there may be a use case for having direct access to this structure.
380  
381 =head2 xml_index
382  
383 The index number of an element in a collection
384  
385 =head2 xml_node
386  
387 The name of the current node
388  
389 =head2 xml_parent
390  
391 The parent XML::Simple::Sugar object to the current element
392  
393 =head2 xml
394  
395 This readonly attribute is only useful during instantiation (XML::Simple::Sugar->new({ xml => $xml_string })). This is primarily intended for working with XML templates, or XML service responses.
396  
397 =head2 xml_xs
398  
399 This is underlying XML::Simple object. If you need to adjust the XML declaration, you can do that by passing an an XML::Simple object with your preferred options to the constructor. Be wary of setting other XML::Simple options as this module will happily overwrite anything that conflicts with its assumptions.
400  
401 =head2 xml_root
402  
403 Returns the root element XML::Simple::Sugar object
404  
405 =head2 xml_content (String)
406  
407 Returns the content of the current element
408  
409 =head1 METHODS
410  
411 =head2 xml_read (XML String)
412  
413 Parses an XML string and sets the data attribute
414  
415 =head2 xml_write
416  
417 Writes out an XML string
418  
419 =head2 xml_attr (HashRef)
420  
421 If passed a hash reference, this method will set the attributes of the current element. Otherwise, this method returns a hash reference representing the current element's attributes.
422  
423 =head2 xml_rmattr (String)
424  
425 This method removes the passed scalar argument from the element's list of attributes.
426  
427 =head2 xml_nest (XML::Simple::Sugar)
428  
429 Merges another XML::Simple::Sugar object as a child of the current node.
430  
431 =head1 REPOSITORY
432  
433 L<https://github.com/Camspi/XML-Simple-Sugar>
434  
435 =head1 MINIMUM PERL VERSION SUPPORTED
436  
437 Perl 5.18.2 or later is required by this module. Lesser Perl versions struggle with deep recursion. Patches welcome.
438  
439 =head1 VERSIONING
440  
441 Semantic versioning is adopted by this module. See L<http://semver.org/>.
442  
443 =head1 SEE ALSO
444  
445 =over 4
446  
447 =item
448 * L<XML::Simple>
449  
450 =back
451  
452 =head1 CREDITS
453  
454 =over 4
455  
456 =item
457 * Jonathan Cast for excellent critique.
458  
459 =item
460 * Kyle Bolton for peeking over my shoulder and giving me pro tips.
461  
462 =item
463 * eMortgage Logic, LLC., for allowing me to publish this module to CPAN
464  
465 =back
466  
467 =head1 AUTHOR
468  
469 Chris Tijerina
470  
471 =head1 COPYRIGHT AND LICENSE
472  
473 This software is copyright (c) 2014 by eMortgage Logic LLC.
474  
475 This is free software; you can redistribute it and/or modify it under
476 the same terms as the Perl 5 programming language system itself.
477  
478 =cut
↑ Summary