blib/lib/XML/Simple/Sorted.pm

filter: Statementstmt 50.0% 29 / 58 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 100% 2 / 2 totaltotal 46.4% 39 / 84 CC 12 SCAR 31.7
CC
12
Cov
40%
sorted_keys
42.0
new
34.4
BEGIN
1.0
CRAP 23.8
↑ Summary
1 =head1 NAME
2  
3 XML::Simple::Sorted - Version of XML::Simple with enforced tag and attribute sort order.
4 This module was born out of the need to interface with some legacy systems that could not be
5 changed and that expected a certain tag order, otherwise they would crash and burn... just kidding ;-)
6  
7 =head1 SYNOPSIS
8  
9 use XML::Simple::Sorted;
10  
11 my $xml = XML::Simple::Sorted->new( [OPT] ) ;
12  
13 =cut
14  
15 package XML::Simple::Sorted;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 BEGIN
24 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶ use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
27 1 $VERSION = '1.00';
28 1 @ISA = qw(XML::Simple);
29 1 @EXPORT = ();
30 1 %EXPORT_TAGS = ();
31 1 @EXPORT_OK = qw();
32 }
33 1 our @EXPORT_OK;
34  
35 =head1 CLASS METHODS
36  
37 =head2 B<new() >
38  
39 Creates a new instance of C<XML::Simple::Sorted>.
40 Here, we simply override the constructor of L<XML::Simple> and check if the user has given
41 an *.ini file or hash containing the desired tag and attribute order.
42  
43 =over 4
44  
45 =item B<SortOrder>
46  
47 This new option can either specify a hashref containing the desired tag and attribute sort
48 order or the path to an ini file (see L<Config::IniFiles>) containing the desired tag and
49 attribute sort order.
50 This method will croak() if one specifies a non-existing config file.
51  
52 =back
53  
54 =head3 Example
55  
56 my $xml = XML::Simple::Sorted->new(SortOrder => 'sort_config.ini');
57  
58 my $sort_hashref = { ... };
59 my $xml = XML::Simple::Sorted->new(SortOrder => $sort_hashref);
60  
61 =head3 Example sort_config.ini file
62  
63 [message]
64 tags = [ 'tradeid', 'leg' ]
65 attr = [ 'system', 'sender', 'receiver', 'timestamp' ]
66 [leg]
67 tags = [ 'legid', 'payment', 'book', 'product' ]
68 [payment]
69 tags = [ 'start', 'end', 'nominal', 'currency' ]
70 ...
71  
72 =head3 Example sort_hashref
73  
74 my $order = {
75 message => {
76 tags => [ 'tradeid', 'leg' ],
77 attr => [ 'system', 'sender', 'receiver', 'timestamp' ]
78 },
79 leg => {
80 tags => [ 'legid', 'payment', 'book', 'product' ]
81 },
82 payment => {
83 tags => [ 'start', 'end', 'nominal', 'currency' ]
84 },
85 };
86 Please note that the 'tags' und 'attr' parameters are semantically identical, i.e. one
87 could specify the attribute order also using the 'tags' parameter and vice versa.
88 Two parameters only serve to hopefully improve the readability of the *.ini file / hashref.
89  
90 =cut
91 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=35.4
Pod documented
92 0 my ($class, %args) = @_;
93 0 my @params = ('tags', 'attr');
94 0 my $sort;
95 0 ▶ if (exists($args{SortOrder})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"SortOrder"}) 0 0 0
96 0 ▶ if (UNIVERSAL::isa($args{SortOrder}, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'SortOrder'}, 'HASH')) { } 0 0 0
97 0 $sort = $args{SortOrder};
98 } else {
99 1 ▶ use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶ use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 0 my %ini;
102 0 ▶ croak($args{SortOrder}, ' not found') if (! -f $args{SortOrder});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $args{"SortOrder"} 0 0 0
103 0 tie %ini, 'Config::IniFiles', ( -file => $args{SortOrder} );
104 0 my %sorted;
105 0 for my $k (keys %ini) {
106 0 for my $p (@params) {
107 0 ▶ $sorted{$k}{$p} = eval($ini{$k}{$p}) if ($ini{$k}{$p});
Branch: 1 branch
BranchTrueFalseTotal
if $ini{$k}{$p} 0 0 0
108 }
109 }
110 0 $sort = \%sorted;
111 }
112 0 delete($args{SortOrder});
113 }
114 0 my $self = $class->SUPER::new(%args);
115 0 $self->{params} = \@params;
116 0 $self->{sort} = $sort;
117 0 bless($self, $class);
118 0 return $self;
119 } # new()
120  
121 =head2 B<sorted_keys()>
122  
123 Override method C<sorted_keys()> of L<XML::Simple> to perform the desired ordering of XML
124 tags and attributes.
125  
126 =cut
127 0 ▶sub sorted_keys {
Subroutine sorted_keys: not called CC=6 SCAR=37.4
Pod documented
128 0 my ($self, $name, $hashref) = @_;
129 0 my @sorted_tags;
130 0 for my $p (@{$self->{params}}) {
131 0 ▶ if (my $val = $self->{sort}->{$name}{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->{"sort"}{$name}{$p}) 0 0 0
132 0 for my $k (@$val) {
133 # Only return tags and attributes that also exist in the given XML hashref to
134 # prevent the creation of empty attributes.
135 0 ▶ push(@sorted_tags, $k) if (exists($hashref->{$k}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{$k} 0 0 0
136 }
137 }
138 }
139 0 ▶ return @sorted_tags if (@sorted_tags);
Branch: 1 branch
BranchTrueFalseTotal
if @sorted_tags 0 0 0
140 0 return $self->SUPER::sorted_keys($name, $hashref);
141 } # sorted_keys()
142  
143 1 1;
144  
145 =head1 SEE ALSO
146  
147 L<XML::Simple>, L<Config::IniFiles>
148  
149 =head1 AUTHOR
150  
151 Sinisa Susnjar <sini@cpan.org>
152  
153 =head1 COPYRIGHT AND LICENSE
154  
155 Copyright 2012 Sinisa Susnjar
156  
157 This library is free software; you can redistribute it and/or modify it
158 under the same terms as Perl itself.
159  
160 =cut
↑ Summary