blib/lib/XML/Easy/Classify.pm

filter: Statementstmt 100% 108 / 108 Branchbran 100% 42 / 42 Conditioncond 61.1% 22 / 36 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 100% 37 / 37 Podpod 100% 16 / 16 totaltotal 89.3% 235 / 263 CC 36 SCAR 9.9
CC
36
Cov
92%
is_xml_content_twine
6.0
is_xml_attributes
5.0
check_xml_attributes
4.0
CRAP 2.7
« blib/lib/XML/Easy/Element.pm ↑ Summary blib/lib/XML/Easy/Content.pm »
1 =head1 NAME
2  
3 XML::Easy::Classify - classification of XML-related items
4  
5 =head1 SYNOPSIS
6  
7 use XML::Easy::Classify qw(
8 is_xml_name check_xml_name
9 is_xml_encname check_xml_encname
10 is_xml_chardata check_xml_chardata
11 is_xml_attributes check_xml_attributes
12 is_xml_content_object check_xml_content_object
13 is_xml_content_twine check_xml_content_twine
14 is_xml_content check_xml_content
15 is_xml_element check_xml_element);
16  
17 if(is_xml_name($arg)) { ...
18 check_xml_name($arg);
19 if(is_xml_encname($arg)) { ...
20 check_xml_encname($arg);
21 if(is_xml_chardata($arg)) { ...
22 check_xml_chardata($arg);
23  
24 if(is_xml_attributes($arg)) { ...
25 check_xml_attributes($arg);
26  
27 if(is_xml_content_object($arg)) { ...
28 check_xml_content_object($arg);
29 if(is_xml_content_twine($arg)) { ...
30 check_xml_content_twine($arg);
31 if(is_xml_content($arg)) { ...
32 check_xml_content($arg);
33  
34 if(is_xml_element($arg)) { ...
35 check_xml_element($arg);
36  
37 =head1 DESCRIPTION
38  
39 This module provides various type-testing functions, relating to data
40 types used in the L<XML::Easy> ensemble. These are mainly intended to be
41 used to enforce validity of data being processed by XML-related functions.
42  
43 There are two flavours of function in this module. Functions of the first
44 flavour only provide type classification, to allow code to discriminate
45 between argument types. Functions of the second flavour package up the
46 most common type of type discrimination: checking that an argument is
47 of an expected type. The functions come in matched pairs.
48  
49 =cut
50  
51 package XML::Easy::Classify;
52  
53 15 ▶{ use 5.008; }
Subroutine BEGIN: called CC=1 SCAR=0.0
54 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 15 ▶use Params::Classify 0.000 qw(is_string is_ref is_strictly_blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 use XML::Easy::Syntax 0.000
59 15 ▶ qw($xml10_char_rx $xml10_name_rx $xml10_encname_rx);
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 15 our $VERSION = "0.011";
62  
63 15 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
64 15 our @EXPORT_OK = qw(
65 is_xml_name check_xml_name
66 is_xml_encname check_xml_encname
67 is_xml_chardata check_xml_chardata
68 is_xml_attributes check_xml_attributes
69 is_xml_content_object check_xml_content_object
70 is_xml_content_twine check_xml_content_twine
71 is_xml_content_array
72 is_xml_content check_xml_content
73 is_xml_element check_xml_element
74 );
75  
76 sub _throw_data_error($) {
77 30712 ▶ my($msg) = @_;
Subroutine _throw_data_error: called CC=1 SCAR=0.0
78 30712 die "invalid XML data: $msg\n";
79 }
80  
81 =head1 FUNCTIONS
82  
83 Each of these functions takes one scalar argument (I<ARG>) to be tested.
84 Any scalar value is acceptable for the argument to be tested. Each C<is_>
85 function returns a simple truth value result, which is true iff I<ARG>
86 is of the type being checked for. Each C<check_> function will return
87 normally if the argument is of the type being checked for, or will C<die>
88 if it is not.
89  
90 =over
91  
92 =item is_xml_name(ARG)
93  
94 =item check_xml_name(ARG)
95  
96 Check whether I<ARG> is a plain string satisfying the XML name syntax.
97 (Such names are used to identify element types, attributes, entities,
98 and other things in XML.)
99  
100 =cut
101  
102 sub is_xml_name($) {
103 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
104 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001, qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
105 18084 ▶ return is_string($_[0]) && $_[0] =~ /\A$xml10_name_rx\z/o;
Subroutine is_xml_name: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
rand $_[0] && $_[0] =~ /\A$XML::Easy::Classify::xml10_name_rx\z/o
!ll&&!rl&&r
130260415350
MC/DC100%2/2
rand $_[0] && $_[0] =~ /\A$XML::Easy::Classify::xml10_name_rx\z/o
rand $_[0] $_[0] =~ /\A$XML::Easy::Classify::xml10_name_rx\z/o
Truth table100%3/3
A && B
A= rand $_[0]B= $_[0] =~ /\A$XML::Easy::Classify::xml10_name_rx\z/o
ABresult
0X0
100
111
106 }
107  
108 sub check_xml_name($) {
109 548 ▶ _throw_data_error("name isn't a string") unless is_string($_[0]);
Subroutine check_xml_name: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 26 522 548
110 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
111 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001, qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
112 522 ▶ _throw_data_error("illegal name")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A$XML::Easy::Classify::xml10_name_rx\z/o 434 88 522
113 unless $_[0] =~ /\A$xml10_name_rx\z/o;
114 }
115  
116 =item is_xml_encname(ARG)
117  
118 =item check_xml_encname(ARG)
119  
120 Check whether I<ARG> is a plain string satisfying the XML character
121 encoding name syntax.
122  
123 =cut
124  
125 sub is_xml_encname($) {
126 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
127 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001, qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
128 568 ▶ return is_string($_[0]) && $_[0] =~ /\A$xml10_encname_rx\z/o;
Subroutine is_xml_encname: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
rand $_[0] && $_[0] =~ /\A$XML::Easy::Classify::xml10_encname_rx\z/o
!ll&&!rl&&r
2648260
MC/DC100%2/2
rand $_[0] && $_[0] =~ /\A$XML::Easy::Classify::xml10_encname_rx\z/o
rand $_[0] $_[0] =~ /\A$XML::Easy::Classify::xml10_encname_rx\z/o
Truth table100%3/3
A && B
A= rand $_[0]B= $_[0] =~ /\A$XML::Easy::Classify::xml10_encname_rx\z/o
ABresult
0X0
100
111
129 }
130  
131 sub check_xml_encname($) {
132 568 ▶ _throw_data_error("encoding name isn't a string")
Subroutine check_xml_encname: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 26 542 568
133 unless is_string($_[0]);
134 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
135 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001, qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
136 542 ▶ _throw_data_error("illegal encoding name")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A$XML::Easy::Classify::xml10_encname_rx\z/o 482 60 542
137 unless $_[0] =~ /\A$xml10_encname_rx\z/o;
138 }
139  
140 =item is_xml_chardata(ARG)
141  
142 =item check_xml_chardata(ARG)
143  
144 Check whether I<ARG> is a plain string consisting of a sequence of
145 characters that are acceptable to XML. Such a string is valid as data
146 in an XML element (where it may be intermingled with subelements) or as
147 the value of an element attribute.
148  
149 =cut
150  
151 sub is_xml_chardata($) {
152 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
153 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001, qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
154 7076 ▶ return is_string($_[0]) && $_[0] =~ /\A$xml10_char_rx*\z/o;
Subroutine is_xml_chardata: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
rand $_[0] && $_[0] =~ /\A$XML::Easy::Classify::xml10_char_rx*\z/o
!ll&&!rl&&r
20817925076
MC/DC100%2/2
rand $_[0] && $_[0] =~ /\A$XML::Easy::Classify::xml10_char_rx*\z/o
rand $_[0] $_[0] =~ /\A$XML::Easy::Classify::xml10_char_rx*\z/o
Truth table100%3/3
A && B
A= rand $_[0]B= $_[0] =~ /\A$XML::Easy::Classify::xml10_char_rx*\z/o
ABresult
0X0
100
111
155 }
156  
157 sub check_xml_chardata($) {
158 704341 ▶ _throw_data_error("character data isn't a string")
Subroutine check_xml_chardata: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 1586 702755 704341
159 unless is_string($_[0]);
160 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
161 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001, qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
162 702755 ▶ _throw_data_error("character data contains illegal character")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A$XML::Easy::Classify::xml10_char_rx*\z/o 16352 686403 702755
163 unless $_[0] =~ /\A$xml10_char_rx*\z/o;
164 }
165  
166 =item is_xml_attributes(ARG)
167  
168 =item check_xml_attributes(ARG)
169  
170 Check whether I<ARG> is a reference to a hash that is well-formed as
171 an XML element attribute set. To be well-formed, each key in the hash
172 must be an XML name string, and each value must be an XML character
173 data string.
174  
175 =cut
176  
177 sub is_xml_attributes($) {
178 1160 ▶ return undef unless is_ref($_[0], "HASH");
Subroutine is_xml_attributes: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 32 1128 1160
179 1128 my $attrs = $_[0];
180 1128 foreach(keys %$attrs) {
181 return undef unless
182 1324 ▶ is_xml_name($_) && is_xml_chardata($attrs->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_name($_) and &is_xml_chardata($attrs->{$_}) 684 640 1324
Condition100%3/3
&is_xml_name($_) and &is_xml_chardata($attrs->{$_})
!ll&&!rl&&r
434250640
MC/DC100%2/2
&is_xml_name($_) and &is_xml_chardata($attrs->{$_})
&is_xml_name($_) &is_xml_chardata($attrs->{$_})
Truth table100%3/3
A and B
A= &is_xml_name($_)B= &is_xml_chardata($attrs->{$_})
ABresult
0X0
100
111
183 }
184 444 return 1;
185 }
186  
187 sub check_xml_attributes($) {
188 348336 ▶ _throw_data_error("attribute hash isn't a hash")
Subroutine check_xml_attributes: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 32 348304 348336
189 unless is_ref($_[0], "HASH");
190 348304 foreach(sort keys %{$_[0]}) {
191 15 ▶ no if "$]" < 5.017002, qw(warnings utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
192 15 ▶ no if "$]" >= 5.023006 && "$]" < 5.027001,
Condition33%1/3
"$]" >= 5.023006 && "$]" < 5.027001
!ll&&!rl&&r
0150
MC/DC0%0/2
"$]" >= 5.023006 && "$]" < 5.027001
"$]" >= 5.023006 "$]" < 5.027001
Truth table33%1/3
A && B
A= "$]" >= 5.023006B= "$]" < 5.027001
ABresult
0X0
100
111
193 15 ▶ qw(warnings deprecated);
Subroutine BEGIN: called CC=2 SCAR=6.9
194 18384 ▶ _throw_data_error("illegal attribute name")
Branch: 1 branch
BranchTrueFalseTotal
unless /\A$XML::Easy::Classify::xml10_name_rx\z/o 9456 8928 18384
195 unless /\A$xml10_name_rx\z/o;
196 8928 check_xml_chardata($_[0]->{$_});
197 }
198 }
199  
200 =item is_xml_content_object(ARG)
201  
202 =item check_xml_content_object(ARG)
203  
204 Check whether I<ARG> is a reference to an L<XML::Easy::Content>
205 object, and thus represents a chunk of XML content.
206  
207 =cut
208  
209 sub is_xml_content_object($) {
210 667828 ▶ return is_strictly_blessed($_[0], "XML::Easy::Content");
Subroutine is_xml_content_object: called CC=1 SCAR=0.0
Pod documented
211 }
212  
213 sub check_xml_content_object($) {
214 655622 ▶ _throw_data_error("content data isn't a content chunk")
Subroutine check_xml_content_object: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_content_object 352 655270 655622
215 unless &is_xml_content_object;
216 }
217  
218 =item is_xml_content_twine(ARG)
219  
220 =item check_xml_content_twine(ARG)
221  
222 Check whether I<ARG> is a reference to a twine array
223 (see L<XML::Easy::NodeBasics/Twine>),
224 and thus represents a chunk of XML content.
225  
226 =cut
227  
228 sub is_xml_element($);
229  
230 sub is_xml_content_twine($) {
231 3742 ▶ return undef unless is_ref($_[0], "ARRAY");
Subroutine is_xml_content_twine: called CC=6 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 58 3684 3742
232 3684 my $twine = $_[0];
233 3684 ▶ return undef unless @$twine % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$twine % 2 == 1 8 3676 3684
234 3676 for(my $i = $#$twine; ; $i--) {
235 5584 ▶ return undef unless is_xml_chardata($twine->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_chardata($twine->[$i]) 1500 4084 5584
236 4084 ▶ last if $i-- == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i-- == 0 2112 1972 4084
237 1972 ▶ return undef unless is_xml_element($twine->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_element($twine->[$i]) 64 1908 1972
238 }
239 2112 return 1;
240 }
241  
242 sub check_xml_element($);
243  
244 sub check_xml_content_twine($) {
245 352415 ▶ _throw_data_error("content array isn't an array")
Subroutine check_xml_content_twine: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 30 352385 352415
246 unless is_ref($_[0], "ARRAY");
247 352385 my $twine = $_[0];
248 352385 ▶ _throw_data_error("content array has even length")
Branch: 1 branch
BranchTrueFalseTotal
unless @$twine % 2 == 1 176 352209 352385
249 unless @$twine % 2 == 1;
250 352209 for(my $i = 0; ; $i++) {
251 683727 check_xml_chardata($twine->[$i]);
252 672727 ▶ last if ++$i == @$twine;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i == @$twine 339801 332926 672727
253 332926 check_xml_element($twine->[$i]);
254 }
255 }
256  
257 =item is_xml_content_array(ARG)
258  
259 Deprecated alias for L</is_xml_content_twine>.
260  
261 =cut
262  
263 15 *is_xml_content_array = \&is_xml_content_twine;
264  
265 =item is_xml_content(ARG)
266  
267 =item check_xml_content(ARG)
268  
269 Check whether I<ARG> is a reference to either an L<XML::Easy::Content>
270 object or a twine array (see L<XML::Easy::NodeBasics/Twine>),
271 and thus represents a chunk of XML content.
272  
273 =cut
274  
275 sub is_xml_content($) {
276 1872 ▶ return &is_xml_content_object || &is_xml_content_twine;
Subroutine is_xml_content: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
&is_xml_content_object || &is_xml_content_twine
l!l&&r!l&&!r
21056814
MC/DC100%2/2
&is_xml_content_object || &is_xml_content_twine
&is_xml_content_object &is_xml_content_twine
Truth table100%3/3
A || B
A= &is_xml_content_objectB= &is_xml_content_twine
ABresult
1X1
011
000
277 }
278  
279 sub check_xml_content($) {
280 1658 ▶ if(is_ref($_[0], "ARRAY")) {
Subroutine check_xml_content: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (rand $_[0]) { } 1628 30 1658
281 1628 &check_xml_content_twine;
282 } else {
283 30 &check_xml_content_object;
284 }
285 }
286  
287 =item is_xml_element(ARG)
288  
289 =item check_xml_element(ARG)
290  
291 Check whether I<ARG> is a reference to an L<XML::Easy::Element>
292 object, and thus represents an XML element.
293  
294 =cut
295  
296 348548 ▶sub is_xml_element($) { is_strictly_blessed($_[0], "XML::Easy::Element") }
Subroutine is_xml_element: called CC=1 SCAR=0.0
Pod documented
297  
298 sub check_xml_element($) {
299 334104 ▶ _throw_data_error("element data isn't an element")
Subroutine check_xml_element: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_element 1760 332344 334104
300 unless &is_xml_element;
301 }
302  
303 =back
304  
305 =head1 SEE ALSO
306  
307 L<Params::Classify>,
308 L<XML::Easy::NodeBasics>
309  
310 =head1 AUTHOR
311  
312 Andrew Main (Zefram) <zefram@fysh.org>
313  
314 =head1 COPYRIGHT
315  
316 Copyright (C) 2009, 2010, 2011, 2017
317 Andrew Main (Zefram) <zefram@fysh.org>
318  
319 =head1 LICENSE
320  
321 This module is free software; you can redistribute it and/or modify it
322 under the same terms as Perl itself.
323  
324 =cut
325  
326 15 1;
« blib/lib/XML/Easy/Element.pm ↑ Summary blib/lib/XML/Easy/Content.pm »