blib/lib/oEdtk/XML/Generic.pm

filter: Statementstmt 55.5% 35 / 63 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 84.6% 11 / 13 Podpod 100% 2 / 2 totaltotal 55.1% 48 / 87 CC 7 SCAR 23.4
CC
7
Cov
49%
xml_verif
30.0
xml_open
12.0
BEGIN
1.0
CRAP 10.4
« blib/lib/oEdtk/Dict.pm ↑ Summary blib/lib/oEdtk/Doc.pm »
1 package oEdtk::XML::Generic;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use feature qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 ## no critic qw(TestingAndDebugging::ProhibitNoWarnings)
10 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = 2.1126;
19  
20 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our @EXPORT_OK = qw(xml_open xml_verif);
22  
23 # Extract the key/value pairs from a Cameleon XML file and return them in a hash.
24 0 ▶sub xml_open ( $fh, $options_arg = undef ) {
Subroutine xml_open: not called CC=3 SCAR=24.8
Pod documented
25  
26 # The caller's options win over the default. The fork merged them with
27 # Hash::Merge::Simple, a dependency the fork never declared here; the default is a
28 # single scalar key, so a plain hash copy is equivalent and keeps the merged
29 # distribution free of that (dead) prerequisite.
30 0 ▶ my $options = { suppressempty => q{}, %{ $options_arg // {} } };
Condition0%0/2
$options_arg // {}
l!l
00
MC/DC0%0/1
$options_arg // {}
$options_arg
Truth table0%0/2
A
A= $options_arg
Aresult
11
00
31  
32 0 binmode($fh); # Make sure to drop any PerlIO layer.
33 0 my $xml = XML::Simple->new();
34 0 my $data = $xml->XMLin( $fh, %{$options} );
35  
36 0 return $data;
37 }
38  
39 0 ▶sub xml_verif ($namefile) {
Subroutine xml_verif: not called CC=5 SCAR=34.0
Pod documented
40 0 my ( $name, $path, $suffix ) = fileparse( $namefile, "\.[^\n+.]*" );
41  
42 # to be completed according to the future XML parsers
43 0 my @lib_ext = qw(.xsd .dtd);
44  
45 # The schema sits next to the document, under the same basename. The fork built
46 # the candidate by interpolating "$name.$lib_ext[$i]", which doubled the dot
47 # ("data..xsd") so the existence test never matched and xml_verif croaked on every
48 # call; the loader below used the concatenation form and so disagreed with the
49 # test. Build the candidate once and use it for both.
50 0 my $schema_file;
51 0 foreach my $ext (@lib_ext) {
52 0 my $candidate = $name . $ext;
53 0 ▶ if ( -e $candidate ) { $schema_file = $candidate; last; }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $candidate) 0 0 0
54 }
55  
56 0 ▶ if ($schema_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_file) { } 0 0 0
57 0 my $schema = XML::LibXML::Schema->new( location => $schema_file );
58 0 my $parser = XML::LibXML->new;
59 0 my $doc_parser = $parser->parse_file($namefile);
60  
61 # XML::LibXML::Schema::validate returns a false value when the document is
62 # valid and dies when it is not, so the check keys on the exception. The
63 # fork keyed it on the return value ("if (eval { ... })") and was inverted:
64 # it croaked on success and silently accepted an invalid document.
65 0 my $valid = eval { $schema->validate($doc_parser); 1 };
66 0 ▶ croak $@ unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 0 0 0
67 0 return ( print "\n>>>> validated successfully\n" );
68 }
69 else {
70 0 print "\n>>>>> can\'t locate schema_file\n";
71 0 croak;
72 }
73 0 return 0;
74 }
75  
76 1 1;
77  
78 =head1 NAME
79  
80 oEdtk::XML::Generic - generic XML reader and schema verifier
81  
82 =head1 SYNOPSIS
83  
84 use oEdtk::XML::Generic qw(xml_open xml_verif);
85  
86 my $data = xml_open($fh);
87 xml_verif($file); # dies when $file does not match its .xsd / .dtd
88  
89 =head1 DESCRIPTION
90  
91 Two generic helpers: C<xml_open> turns an XML filehandle into the plain Perl
92 data structure L<XML::Simple> builds, and C<xml_verif> validates a file against
93 the C<.xsd> or C<.dtd> sitting next to it. Moved from C<oEUser::XML::Generic>
94 into the mainline at S21 of the fork-merge plan (decision D23); the old name
95 stays available as a delegate.
96  
97 =head1 FUNCTIONS
98  
99 =head2 xml_open
100  
101 my $data = xml_open($fh, $options);
102  
103 Read C<$fh> with L<XML::Simple> and return the resulting structure. C<$options>
104 is merged over the default C<< { suppressempty => '' } >>.
105  
106 =head2 xml_verif
107  
108 xml_verif($file);
109  
110 Look for C<$file> with its extension replaced by C<.xsd> then C<.dtd>, validate
111 C<$file> against the first one found and report success. It croaks when the
112 document is invalid or when no schema file is found. Note that
113 C<XML::LibXML::Schema::validate> returns a false value on success and dies on
114 failure, which is what the check relies on; see F<t/int/42_xml_readers.t>.
115  
116 =head1 AUTHOR
117  
118 The oEdtk team.
119  
120 =cut
« blib/lib/oEdtk/Dict.pm ↑ Summary blib/lib/oEdtk/Doc.pm »