blib/lib/XML/Easy/Classify.pm

filter: Statementstmt 100% 87 / 87 Branchbran 100% 42 / 42 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 30 / 30 Podpod 100% 16 / 16 totaltotal 100% 200 / 200 SCAR 33.7
CC
29
Cov
100%
is_xml_content_twine
6.0
is_xml_attributes
5.0
check_xml_attributes
4.0
CRAP 29.0
« blib/lib/XML/Easy/Text.pm ↑ Summary blib/lib/XML/Easy/NodeBasics.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  
18 if(is_xml_name($arg)) { ...
19 check_xml_name($arg);
20 if(is_xml_encname($arg)) { ...
21 check_xml_encname($arg);
22 if(is_xml_chardata($arg)) { ...
23 check_xml_chardata($arg);
24  
25 if(is_xml_attributes($arg)) { ...
26 check_xml_attributes($arg);
27  
28 if(is_xml_content_object($arg)) { ...
29 check_xml_content_object($arg);
30 if(is_xml_content_twine($arg)) { ...
31 check_xml_content_twine($arg);
32 if(is_xml_content($arg)) { ...
33 check_xml_content($arg);
34  
35 if(is_xml_element($arg)) { ...
36 check_xml_element($arg);
37  
38 =head1 DESCRIPTION
39  
40 This module provides various type-testing functions, relating to data
41 types used in the L<XML::Easy> ensemble. These are mainly intended to be
42 used to enforce validity of data being processed by XML-related functions.
43  
44 There are two flavours of function in this module. Functions of the first
45 flavour only provide type classification, to allow code to discriminate
46 between argument types. Functions of the second flavour package up the
47 most common type of type discrimination: checking that an argument is
48 of an expected type. The functions come in matched pairs.
49  
50 =cut
51  
52 package XML::Easy::Classify;
53  
54 15 ▶{ use 5.008; }
Subroutine BEGIN: called CC=1 SCAR=0.0
55 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 15 ▶use Params::Classify 0.000 qw(is_string is_ref is_strictly_blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 use XML::Easy::Syntax 0.000
60 15 ▶ qw($xml10_char_rx $xml10_name_rx $xml10_encname_rx);
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 15 our $VERSION = "0.010";
63  
64 15 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
65 15 our @EXPORT_OK = qw(
66 is_xml_name check_xml_name
67 is_xml_encname check_xml_encname
68 is_xml_chardata check_xml_chardata
69 is_xml_attributes check_xml_attributes
70 is_xml_content_object check_xml_content_object
71 is_xml_content_twine check_xml_content_twine
72 is_xml_content_array
73 is_xml_content check_xml_content
74 is_xml_element check_xml_element
75 );
76  
77 sub _throw_data_error($) {
78 32092 ▶ my($msg) = @_;
Subroutine _throw_data_error: called CC=1 SCAR=0.0
79 32092 die "invalid XML data: $msg\n";
80 }
81  
82 =head1 FUNCTIONS
83  
84 Each of these functions takes one scalar argument (I<ARG>) to be tested.
85 Any scalar value is acceptable for the argument to be tested. Each C<is_>
86 function returns a simple truth value result, which is true iff I<ARG>
87 is of the type being checked for. Each C<check_> function will return
88 normally if the argument is of the type being checked for, or will C<die>
89 if it is not.
90  
91 =over
92  
93 =item is_xml_name(ARG)
94  
95 =item check_xml_name(ARG)
96  
97 Check whether I<ARG> is a plain string satisfying the XML name syntax.
98 (Such names are used to identify element types, attributes, entities,
99 and other things in XML.)
100  
101 =cut
102  
103 sub is_xml_name($) {
104 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
105 18632 ▶ 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
130266415838
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 558 ▶ _throw_data_error("name isn't a string") unless is_string($_[0]);
Subroutine check_xml_name: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 26 532 558
110 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
111 532 ▶ _throw_data_error("illegal name")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A$XML::Easy::Classify::xml10_name_rx\z/o 444 88 532
112 unless $_[0] =~ /\A$xml10_name_rx\z/o;
113 }
114  
115 =item is_xml_encname(ARG)
116  
117 =item check_xml_encname(ARG)
118  
119 Check whether I<ARG> is a plain string satisfying the XML character
120 encoding name syntax.
121  
122 =cut
123  
124 sub is_xml_encname($) {
125 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
126 578 ▶ 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
2649260
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
127 }
128  
129 sub check_xml_encname($) {
130 578 ▶ _throw_data_error("encoding name isn't a string")
Subroutine check_xml_encname: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 26 552 578
131 unless is_string($_[0]);
132 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
133 552 ▶ _throw_data_error("illegal encoding name")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A$XML::Easy::Classify::xml10_encname_rx\z/o 492 60 552
134 unless $_[0] =~ /\A$xml10_encname_rx\z/o;
135 }
136  
137 =item is_xml_chardata(ARG)
138  
139 =item check_xml_chardata(ARG)
140  
141 Check whether I<ARG> is a plain string consisting of a sequence of
142 characters that are acceptable to XML. Such a string is valid as data
143 in an XML element (where it may be intermingled with subelements) or as
144 the value of an element attribute.
145  
146 =cut
147  
148 sub is_xml_chardata($) {
149 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
150 7236 ▶ 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
20819205108
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
151 }
152  
153 sub check_xml_chardata($) {
154 705719 ▶ _throw_data_error("character data isn't a string")
Subroutine check_xml_chardata: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless rand $_[0] 1586 704133 705719
155 unless is_string($_[0]);
156 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
157 704133 ▶ _throw_data_error("character data contains illegal character")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A$XML::Easy::Classify::xml10_char_rx*\z/o 17520 686613 704133
158 unless $_[0] =~ /\A$xml10_char_rx*\z/o;
159 }
160  
161 =item is_xml_attributes(ARG)
162  
163 =item check_xml_attributes(ARG)
164  
165 Check whether I<ARG> is a reference to a hash that is well-formed as
166 an XML element attribute set. To be well-formed, each key in the hash
167 must be an XML name string, and each value must be an XML character
168 data string.
169  
170 =cut
171  
172 sub is_xml_attributes($) {
173 1186 ▶ 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 1154 1186
174 1154 my $attrs = $_[0];
175 1154 foreach(keys %$attrs) {
176 return undef unless
177 1350 ▶ is_xml_name($_) && is_xml_chardata($attrs->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_name($_) and &is_xml_chardata($attrs->{$_}) 710 640 1350
Condition100%3/3
&is_xml_name($_) and &is_xml_chardata($attrs->{$_})
!ll&&!rl&&r
444266640
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
178 }
179 444 return 1;
180 }
181  
182 sub check_xml_attributes($) {
183 349018 ▶ _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 348986 349018
184 unless is_ref($_[0], "HASH");
185 348986 foreach(sort keys %{$_[0]}) {
186 15 ▶ no warnings "utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
187 18888 ▶ _throw_data_error("illegal attribute name")
Branch: 1 branch
BranchTrueFalseTotal
unless /\A$XML::Easy::Classify::xml10_name_rx\z/o 9648 9240 18888
188 unless /\A$xml10_name_rx\z/o;
189 9240 check_xml_chardata($_[0]->{$_});
190 }
191 }
192  
193 =item is_xml_content_object(ARG)
194  
195 =item check_xml_content_object(ARG)
196  
197 Check whether I<ARG> is a reference to an L<XML::Easy::Content>
198 object, and thus represents a chunk of XML content.
199  
200 =cut
201  
202 sub is_xml_content_object($) {
203 668324 ▶ return is_strictly_blessed($_[0], "XML::Easy::Content");
Subroutine is_xml_content_object: called CC=1 SCAR=0.0
Pod documented
204 }
205  
206 sub check_xml_content_object($) {
207 655686 ▶ _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 655334 655686
208 unless &is_xml_content_object;
209 }
210  
211 =item is_xml_content_twine(ARG)
212  
213 =item check_xml_content_twine(ARG)
214  
215 Check whether I<ARG> is a reference to a twine array
216 (see L<XML::Easy::NodeBasics/Twine>),
217 and thus represents a chunk of XML content.
218  
219 =cut
220  
221 sub is_xml_element($);
222  
223 sub is_xml_content_twine($) {
224 3838 ▶ 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 3780 3838
225 3780 my $twine = $_[0];
226 3780 ▶ return undef unless @$twine % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$twine % 2 == 1 8 3772 3780
227 3772 for(my $i = $#$twine; ; $i--) {
228 5712 ▶ return undef unless is_xml_chardata($twine->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_chardata($twine->[$i]) 1596 4116 5712
229 4116 ▶ last if $i-- == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i-- == 0 2112 2004 4116
230 2004 ▶ return undef unless is_xml_element($twine->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_xml_element($twine->[$i]) 64 1940 2004
231 }
232 2112 return 1;
233 }
234  
235 sub check_xml_element($);
236  
237 sub check_xml_content_twine($) {
238 353129 ▶ _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 353099 353129
239 unless is_ref($_[0], "ARRAY");
240 353099 my $twine = $_[0];
241 353099 ▶ _throw_data_error("content array has even length")
Branch: 1 branch
BranchTrueFalseTotal
unless @$twine % 2 == 1 176 352923 353099
242 unless @$twine % 2 == 1;
243 352923 for(my $i = 0; ; $i++) {
244 684441 check_xml_chardata($twine->[$i]);
245 672737 ▶ last if ++$i == @$twine;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i == @$twine 339811 332926 672737
246 332926 check_xml_element($twine->[$i]);
247 }
248 }
249  
250 =item is_xml_content_array(ARG)
251  
252 Deprecated alias for L</is_xml_content_twine>.
253  
254 =cut
255  
256 15 *is_xml_content_array = \&is_xml_content_twine;
257  
258 =item is_xml_content(ARG)
259  
260 =item check_xml_content(ARG)
261  
262 Check whether I<ARG> is a reference to either an L<XML::Easy::Content>
263 object or a twine array (see L<XML::Easy::NodeBasics/Twine>),
264 and thus represents a chunk of XML content.
265  
266 =cut
267  
268 sub is_xml_content($) {
269 1920 ▶ 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
21056862
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
270 }
271  
272 sub check_xml_content($) {
273 1690 ▶ if(is_ref($_[0], "ARRAY")) {
Subroutine check_xml_content: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (rand $_[0]) { } 1660 30 1690
274 1660 &check_xml_content_twine;
275 } else {
276 30 &check_xml_content_object;
277 }
278 }
279  
280 =item is_xml_element(ARG)
281  
282 =item check_xml_element(ARG)
283  
284 Check whether I<ARG> is a reference to an L<XML::Easy::Element>
285 object, and thus represents an XML element.
286  
287 =cut
288  
289 348974 ▶sub is_xml_element($) { is_strictly_blessed($_[0], "XML::Easy::Element") }
Subroutine is_xml_element: called CC=1 SCAR=0.0
Pod documented
290  
291 sub check_xml_element($) {
292 334114 ▶ _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 332354 334114
293 unless &is_xml_element;
294 }
295  
296 =back
297  
298 =head1 SEE ALSO
299  
300 L<Params::Classify>,
301 L<XML::Easy::NodeBasics>
302  
303 =head1 AUTHOR
304  
305 Andrew Main (Zefram) <zefram@fysh.org>
306  
307 =head1 COPYRIGHT
308  
309 Copyright (C) 2009, 2010, 2011, 2017
310 Andrew Main (Zefram) <zefram@fysh.org>
311  
312 =head1 LICENSE
313  
314 This module is free software; you can redistribute it and/or modify it
315 under the same terms as Perl itself.
316  
317 =cut
318  
319 15 1;
« blib/lib/XML/Easy/Text.pm ↑ Summary blib/lib/XML/Easy/NodeBasics.pm »