blib/lib/XML/Pastor/Util.pm

filter: Statementstmt 51.7% 58 / 112 Branchbran 21.0% 8 / 38 Conditioncond 9.3% 3 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 58.8% 10 / 17 Podpod 0.0% 0 / 9 totaltotal 34.9% 79 / 226 CC 37 SCAR 33.5
CC
37
Cov
38%
validate_time
90.0
sprint_xml_element
42.0
validate_date
42.0
CRAP 28.6
« blib/lib/XML/Pastor/Schema/Model.pm ↑ Summary blib/lib/XML/Pastor/Builtin/List.pm »
1 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6
7 #================================================================
8 package XML::Pastor::Util;
9
10 10 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 10 our @ISA = qw (Exporter);
14 10 our @EXPORT = qw();
15 10 our @EXPORT_OK = qw( mergeHash
16 getAttributeHash
17 getChildrenHashDOM
18
19 module_path
20
21 slurp_file
22 sprint_xml_doc
23 sprint_xml_element
24
25 validate_date
26 validate_time
27 );
28
29 #------------------------------------------------------------------
30 # merge h2 into h1;
31 #------------------------------------------------------------------
32 30 ▶sub mergeHash {
Subroutine mergeHash: called CC=2 SCAR=6.9
Pod undocumented
33 30 my $h1 = shift;
34 30 my $h2 = shift;
35
36 30 for my $key (keys %$h2) {
37 50 $h1->{$key}=$h2->{$key};
38 }
39
40 30 return $h1;
41 }
42
43
44 #------------------------------------------------------------
45 # This is a utility function (not a method) that will get all the attributes
46 # of an DOM element into a hash and return that hash.
47 #-------------------------------------------------------------
48 603 ▶sub getAttributeHash {
Subroutine getAttributeHash: called CC=4 SCAR=14.3
Pod undocumented
49 603 my $node = shift;
50 603 my $attribs = {};
51
52 603 ▶ unless (UNIVERSAL::isa($node, "XML::LibXML::Element")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($node, "XML::LibXML::Element")) 0 603 603
53 0 return undef;
54 }
55
56 603 my @attributes=$node->attributes();
57 603 foreach my $attribute (@attributes) {
58 873 ▶ next unless defined $attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 873 873
59 873 $attribs->{ $attribute->nodeName()} = $attribute->value();
60 }
61 603 return $attribs;
62 }
63
64
65 #------------------------------------------------------------
66 # This is a utility function (not a method) that will get all the children
67 # of an DOM element into a hash of arrays and return that hash.
68 #-------------------------------------------------------------
69 68 ▶sub getChildrenHashDOM {
Subroutine getChildrenHashDOM: called CC=5 SCAR=16.3
Pod undocumented
70 68 my $node = shift;
71 68 my $result = {};
72
73 68 ▶ unless (UNIVERSAL::isa($node, "XML::LibXML::Element")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($node, "XML::LibXML::Element")) 0 68 68
74 0 return undef;
75 }
76
77 68 my @children = grep {UNIVERSAL::isa($_, "XML::LibXML::Element")} $node->childNodes();
78 68 foreach my $child (@children) {
79 101 ▶ next unless defined $child;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 0 101 101
80 101 my $name = $child->localName();
81 101 ▶ unless (defined($result->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result->{$name}) 73 28 101
82 73 $result->{$name} = [];
83 }
84 101 push @{$result->{$name}}, $child;
85 }
86
87 68 return $result;
88 }
89
90
91
92 #------------------------------------------------------------
93 # A DEBUG routine that prints out an XMLish document given a DOM document.
94 #-------------------------------------------------------------
95 0 ▶sub sprint_xml_doc() {
Subroutine sprint_xml_doc: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my $dom = shift;
97 0 my $s = "";
98
99 0 $s .= "\n<xml version=\"1.0\">\n";
100 0 my $root=$dom->documentElement();
101 0 $s .= sprint_xml_element($root);
102 0 $s .= "\n</xml>\n";
103 0 return $s;
104 }
105
106
107 #------------------------------------------------------------
108 # A DEBUG routine that returns an XMLish element string given a DOM element.
109 #-------------------------------------------------------------
110 sub sprint_xml_element($;$); # warning if the compiler doesn't see a prototype in a recursive call.
111 0 ▶sub sprint_xml_element($;$) {
Subroutine sprint_xml_element: not called CC=6 SCAR=37.4
Pod undocumented
112 0 my $elem = shift;
113 0 ▶ my $indent=shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
114 0 my $s = "";
115 0 my $name = $elem->localName(); #$elem->nodeName();
116 0 my @attributes=$elem->attributes();
117 0 $s .= _sprint_indent($indent);
118 0 $s .= "<$name ";
119 0 foreach my $attribute (@attributes) {
120 0 ▶ next unless defined $attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 0 0
121 0 $s .= $attribute->nodeName() . "=\"" .$attribute->value() . "\" ";
122 }
123 0 my @children = grep {UNIVERSAL::isa($_, "XML::LibXML::Element")} $elem->childNodes();
124 0 ▶ unless (scalar(@children)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @children) { } 0 0 0
125 0 $s .= "/>";
126 }else {
127 0 $s .= ">";
128 0 foreach my $child (@children) {
129 0 $s .= sprint_xml_element($child, $indent+1);
130 }
131 0 $s .= _sprint_indent($indent);
132 0 $s .= "</$name>";
133 }
134 0 return $s;
135 }
136
137
138 #------------------------------------------------------------
139 # A DEBUG routine that helps the print_xml_element family
140 #-------------------------------------------------------------
141 0 ▶sub _sprint_indent(){
Subroutine _sprint_indent: not called CC=2 SCAR=17.9
142 0 my $indent = shift;
143 0 my $s = "";
144 0 $s .= "\n";
145 0 ▶ for(my $i=0; $i<$indent;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $indent) 0 0 0
146 0 $s.= " ";
147 }
148 0 return $s;
149 }
150
151 #------------------------------------------------------------
152 # Slurps an entire file into memory and returns the resulting string
153 #-------------------------------------------------------------
154 0 ▶sub slurp_file {
Subroutine slurp_file: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my $file = shift;
156
157 0 local( $/ ) ;
158 0 ▶ open( my $fh, $file ) or die "XML::Pastor::Util : slurp_file : Can't open file $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "XML::Pastor::Util : slurp_file : Can't open file $file\n" 0 0 0
159 0 return <$fh>;
160 }
161
162 #------------------------------------------------------------
163 # module_path
164 # Given a module name (without the .pm) and a destination directory,
165 # Compute the file path where the module would be written to.
166 #-------------------------------------------------------------
167 17 ▶sub module_path {
Subroutine module_path: called CC=6 SCAR=19.9
Pod undocumented
168 17 my $args = {@_};
169 17 ▶ my $module = $args->{module} || die "Need a module name!";
Condition50%1/2
$args->{'module'} || die('Need a module name!')
l!l
170
MC/DC0%0/1
$args->{'module'} || die('Need a module name!')
$args->{'module'}
Truth table50%1/2
A
A= $args->{'module'}
Aresult
11
00
170 17 ▶ my $destination = $args->{destination} || "/tmp/lib/perl/";
Condition50%1/2
$args->{'destination'} || '/tmp/lib/perl/'
l!l
170
MC/DC0%0/1
$args->{'destination'} || '/tmp/lib/perl/'
$args->{'destination'}
Truth table50%1/2
A
A= $args->{'destination'}
Aresult
11
00
171
172 # Add a trailing slash if necessary.
173 17 ▶ $destination .= '/' if ($destination && ($destination !~ /\/$/));
Branch: 1 branch
BranchTrueFalseTotal
if $destination and not $destination =~ m[/$] 0 17 17
Condition33%1/3
$destination and not $destination =~ m[/$]
!ll&&!rl&&r
0170
MC/DC0%0/2
$destination and not $destination =~ m[/$]
$destination not $destination =~ m[/$]
Truth table33%1/3
A and not(B)
A= $destinationB= not $destination =~ m[/$]
ABresult
0X0
100
111
174
175 # get ride of any trailing columns.
176 17 ▶ while ($module =~ /:$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /:$/) 0 17 17
177 0 $module =~ s/:$//;
178 }
179
180 17 my $file = $module;
181 17 $file = $destination . $file . '.pm';
182 17 $file =~ s/::/\//g;
183
184 17 return $file;
185 }
186
187 #------------------------------------------------
188 # code taken from XML::Schema::Validate who took it from Date::Simple
189 #--------------------------------------------------
190 10 our @days_in_month = ([0,31,28,31,30,31,30,31,31,30,31,30,31],
191 [0,31,29,31,30,31,30,31,31,30,31,30,31]);
192
193 0 ▶sub validate_date ($$$) {
Subroutine validate_date: not called CC=6 SCAR=37.4
Pod undocumented
194 0 my ($y, $m, $d)= @_;
195
196 # any +ve integral year is valid
197 0 ▶ return 0 if $y != abs int $y;
Branch: 1 branch
BranchTrueFalseTotal
if $y != abs int $y 0 0 0
198 0 ▶ return 0 unless 1 <= $m and $m <= 12;
Branch: 1 branch
BranchTrueFalseTotal
unless 1 <= $m and $m <= 12 0 0 0
Condition0%0/3
1 <= $m and $m <= 12
!ll&&!rl&&r
000
MC/DC0%0/2
1 <= $m and $m <= 12
1 <= $m $m <= 12
Truth table0%0/3
A and B
A= 1 <= $mB= $m <= 12
ABresult
0X0
100
111
199 0 ▶ return 0 unless 1 <= $d and $d <=$days_in_month[_leap_year($y)][$m];
Branch: 1 branch
BranchTrueFalseTotal
unless 1 <= $d and $d <= $XML::Pastor::Util::days_in_month[_leap_year($y)][$m] 0 0 0
Condition0%0/3
1 <= $d and $d <= $XML::Pastor::Util::days_in_month[_leap_year($y)][$m]
!ll&&!rl&&r
000
MC/DC0%0/2
1 <= $d and $d <= $XML::Pastor::Util::days_in_month[_leap_year($y)][$m]
1 <= $d $d <= $XML::Pastor::Util::days_in_month[_leap_year($y)][$m]
Truth table0%0/3
A and B
A= 1 <= $dB= $d <= $XML::Pastor::Util::days_in_month[_leap_year($y)][$m]
ABresult
0X0
100
111
200 0 return 1;
201 }
202
203 0 ▶sub _leap_year {
Subroutine _leap_year: not called CC=4 SCAR=30.0
204 0 my $y = shift;
205 0 ▶ return (($y%4==0) and ($y%400==0 or $y%100!=0)) || 0;
Condition0%0/8
$y % 4 == 0 && ($y % 400 == 0 || $y % 100 != 0) || 0
l!l
00
$y % 4 == 0 and $y % 400 == 0 || $y % 100 != 0
!ll&&!rl&&r
000
$y % 400 == 0 or $y % 100 != 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$y % 4 == 0 && ($y % 400 == 0 || $y % 100 != 0) || 0
$y % 4 == 0 $y % 400 == 0 $y % 100 != 0
Truth table0%0/4
A and B or C
A= $y % 4 == 0B= $y % 400 == 0C= $y % 100 != 0
ABCresult
11X1
1011
0XX0
1000
206 }
207
208 #------------------------------------------------
209 0 ▶sub validate_time ($$$) {
Subroutine validate_time: not called CC=9 SCAR=45.0
Pod undocumented
210 0 my ($h, $m, $s) = @_;
211
212 0 ▶ return 0 if $h != abs int $h;
Branch: 1 branch
BranchTrueFalseTotal
if $h != abs int $h 0 0 0
213 0 ▶ return 0 if $m != abs int $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m != abs int $m 0 0 0
214 0 ▶ return 0 unless 0 <= $h and $h <= 23;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 <= $h and $h <= 23 0 0 0
Condition0%0/3
0 <= $h and $h <= 23
!ll&&!rl&&r
000
MC/DC0%0/2
0 <= $h and $h <= 23
0 <= $h $h <= 23
Truth table0%0/3
A and B
A= 0 <= $hB= $h <= 23
ABresult
0X0
100
111
215 0 ▶ return 0 unless 0 <= $m and $m <= 59;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 <= $m and $m <= 59 0 0 0
Condition0%0/3
0 <= $m and $m <= 59
!ll&&!rl&&r
000
MC/DC0%0/2
0 <= $m and $m <= 59
0 <= $m $m <= 59
Truth table0%0/3
A and B
A= 0 <= $mB= $m <= 59
ABresult
0X0
100
111
216 0 ▶ return 0 unless 0 <= $s and $s <= 61; # Leap seconds can be upto 61!!!
Branch: 1 branch
BranchTrueFalseTotal
unless 0 <= $s and $s <= 61 0 0 0
Condition0%0/3
0 <= $s and $s <= 61
!ll&&!rl&&r
000
MC/DC0%0/2
0 <= $s and $s <= 61
0 <= $s $s <= 61
Truth table0%0/3
A and B
A= 0 <= $sB= $s <= 61
ABresult
0X0
100
111
217 0 return 1;
218 }
219
220
221 #
222
223 10 1;
« blib/lib/XML/Pastor/Schema/Model.pm ↑ Summary blib/lib/XML/Pastor/Builtin/List.pm »