blib/lib/oEdtk/XML/DTD.pm

filter: Statementstmt 33.0% 34 / 103 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 47.6% 10 / 21 Podpod 100% 11 / 11 totaltotal 26.5% 55 / 207 CC 33 SCAR 33.4
CC
33
Cov
21%
list_children_nodes
72.0
validate_xml
56.0
parse_xml
30.0
CRAP 28.3
« lib/oEdtk/Tracking.pm ↑ Summary blib/lib/oEdtk/Utils.pm »
1 package oEdtk::XML::DTD;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use oEdtk::Config qw(config_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny qw(try catch);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = 2.1131;
15  
16 2 Readonly my $TYPE_FILE => 'file';
17 2 Readonly my $TYPE_STRING => 'string';
18  
19 0 ▶sub found_in_list {
Subroutine found_in_list: not called CC=3 SCAR=24.8
Pod documented
20 0 my ( $elt, $l ) = @_;
21 0 foreach my $v ( @{$l} ) {
22 0 ▶ return 1 if $elt eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $elt eq $v 0 0 0
23 }
24 0 return;
25 }
26  
27 0 ▶sub load_xml_catalogs {
Subroutine load_xml_catalogs: not called CC=4 SCAR=30.0
Pod documented
28 0 my ($parser) = @_;
29 0 my $cfg = config_read('MAIL');
30 0 ▶ if ( ( exists $cfg->{'EDTK_XML_CATALOG_FILES'} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cfg->{'EDTK_XML_CATALOG_FILES'} and defined $cfg->{'EDTK_XML_CATALOG_FILES'}) { } 0 0 0
Condition0%0/3
exists $cfg->{"EDTK_XML_CATALOG_FILES"} and defined $cfg->{"EDTK_XML_CATALOG_FILES"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $cfg->{"EDTK_XML_CATALOG_FILES"} and defined $cfg->{"EDTK_XML_CATALOG_FILES"}
exists $cfg->{"EDTK_XML_CATALOG_FILES"} defined $cfg->{"EDTK_XML_CATALOG_FILES"}
Truth table0%0/3
A and B
A= exists $cfg->{"EDTK_XML_CATALOG_FILES"}B= defined $cfg->{"EDTK_XML_CATALOG_FILES"}
ABresult
0X0
100
111
31 && ( defined $cfg->{'EDTK_XML_CATALOG_FILES'} ) )
32 {
33 0 my $catalog_files = $cfg->{'EDTK_XML_CATALOG_FILES'};
34 0 warn "Using XML_CATALOG_FILES=$catalog_files";
35 0 $parser->load_catalog($_) foreach split m/:/, $catalog_files;
36 }
37 else {
38 0 warn "No XML_CATALOG_FILES found";
39 }
40 0 return;
41 }
42  
43 0 ▶sub list_children_nodes {
Subroutine list_children_nodes: not called CC=8 SCAR=42.8
Pod documented
44 0 my ( $doc, @names ) = @_;
45 0 my %names = map { $_ => 1 } @names;
46 0 my @res = ();
47 0 foreach my $node ( $doc->childNodes() ) {
48 0 my $type = $node->nodeType();
49 0 ▶ if ( $type eq XML_TEXT_NODE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 3) { } 0 0 0
elsif ($type eq 1) { } 0 0 0
elsif ($type eq 8) { } 0 0 0
50 0 ▶ croak "Non-empty text in node: " . $node
Branch: 1 branch
BranchTrueFalseTotal
if $node->to_literal =~ /\S/sx 0 0 0
51 if $node->to_literal =~ m{\S}sx;
52 }
53 elsif ( $type eq XML_ELEMENT_NODE ) {
54 0 my $name = $node->nodeName();
55 croak "Unexpected node name '$name'. Authorized: " . join( ' ', @names )
56 0 ▶ if ( scalar @names ) && !exists $names{$name};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @names and not exists $names{$name} 0 0 0
Condition0%0/3
scalar @names and not exists $names{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @names and not exists $names{$name}
scalar @names not exists $names{$name}
Truth table0%0/3
A and not(B)
A= scalar @namesB= not exists $names{$name}
ABresult
0X0
100
111
57 0 push @res, $node;
58 }
59 elsif ( $type eq XML_COMMENT_NODE ) { } # ignore
60 0 else { croak "Unexpected node type '$type'" }
61 }
62 0 return @res;
63 }
64  
65 0 ▶sub validate_xml {
Subroutine validate_xml: not called CC=7 SCAR=40.3
Pod documented
66 0 my ( $dom, $dtds, $msg, $flag_fatal ) = @_;
67  
68 # The DTD node comes back from internalSubset: this XML::LibXML build returns the
69 # XML::LibXML::Dtd (publicId included) when the document declares a DOCTYPE and
70 # undef otherwise, so a document without a DOCTYPE reports an empty public id
71 # rather than dying. Probing externalSubset instead is wrong twice over: it always
72 # returns undef here, and calling it corrupts the DTD node ownership, so a later
73 # is_valid on the same DOM double-frees (see t/int/35_xml_dtd_xsd.t).
74 0 my $dtd = $dom->internalSubset;
75 0 ▶ my $publicId = ( $dtd ? $dtd->publicId : undef ) // q{};
Branch: 1 branch
BranchTrueFalseTotal
$dtd ? : 0 0 0
Condition0%0/2
($dtd ? $dtd->publicId : undef) // ""
l!l
00
MC/DC0%0/1
($dtd ? $dtd->publicId : undef) // ""
($dtd ? $dtd->publicId : undef)
Truth table0%0/2
A
A= ($dtd ? $dtd->publicId : undef)
Aresult
11
00
76 0 ▶ if ( !found_in_list( $publicId, $dtds ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (found_in_list($publicId, $dtds)) 0 0 0
77 0 my $str = "Unexpected DTD public id '$publicId':" . Dumper($dtds);
78 0 ▶ return $flag_fatal ? ( croak $str ) : ( carp $str );
Branch: 1 branch
BranchTrueFalseTotal
$flag_fatal ? : 0 0 0
79 }
80 0 ▶ if ( !$dom->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dom->is_valid) 0 0 0
81 0 my $str = "$msg does not validate against its DTD: " . XML::LibXML->get_last_error();
82 0 ▶ return $flag_fatal ? ( croak "ERROR: $str" ) : ( carp "WARNING: $str" );
Branch: 1 branch
BranchTrueFalseTotal
$flag_fatal ? : 0 0 0
83 }
84 0 say STDERR "$msg validates against its DTD";
85 0 return ( $dom, $publicId );
86 }
87  
88 0 ▶sub parse_xml {
Subroutine parse_xml: not called CC=5 SCAR=34.0
Pod documented
89 0 my ( $input, $type, $dtds, $msg, $flag_fatal ) = @_;
90 0 ▶ $flag_fatal //= 1;
Condition0%0/2
$flag_fatal //= 1
l!l
00
MC/DC0%0/1
$flag_fatal //= 1
$flag_fatal
Truth table0%0/2
A
A= $flag_fatal
Aresult
11
00
91 0 ▶ my $sub = ( $type eq $TYPE_FILE ) ? 'parse_file' : 'parse_string';
Branch: 1 branch
BranchTrueFalseTotal
$type eq $TYPE_FILE ? : 0 0 0
92 0 my $parser = XML::LibXML->new( validation => 1 );
93 0 load_xml_catalogs($parser);
94 0 my $dom;
95 0 ▶ if ($flag_fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag_fatal) { } 0 0 0
96 0 $dom = $parser->$sub($input);
97 }
98 else {
99 0 eval { $dom = $parser->$sub($input); };
100 0 ▶ return carp $EVAL_ERROR if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $oEdtk::XML::DTD::EVAL_ERROR 0 0 0
101 }
102 0 return validate_xml( $dom, $dtds, $msg, $flag_fatal );
103 }
104  
105 0 ▶sub parse_and_validate_xml_file {
Subroutine parse_and_validate_xml_file: not called CC=1 SCAR=6.9
Pod documented
106 0 my ( $file, $dtds, $flag_fatal ) = @_;
107 0 return parse_xml( $file, $TYPE_FILE, $dtds, "File '$file'", $flag_fatal );
108 }
109  
110 0 ▶sub parse_and_validate_xml_string {
Subroutine parse_and_validate_xml_string: not called CC=2 SCAR=17.9
Pod documented
111 0 my ( $str, $dtds, $flag_fatal ) = @_;
112 0 ▶ my $s =
Branch: 1 branch
BranchTrueFalseTotal
length $str < 20 ? : 0 0 0
113 ( length $str < 20 )
114 ? $str
115 : substr( $str, 0, 8 ) . '...' . substr( $str, -8 );
116 0 return parse_xml( $str, $TYPE_STRING, $dtds, "String '$s'", $flag_fatal );
117 }
118  
119 # XSLT with output method="text" should not include the <?xml decl; see
120 # https://github.com/sparklemotion/nokogiri/issues/1750 and the lab/seb notes.
121 0 ▶sub xml_to_txt {
Subroutine xml_to_txt: not called CC=4 SCAR=30.0
Pod documented
122 0 my ($str) = @_;
123 0 ▶ if ( $str =~ s{^(<\?xml\b[^<>]*\?>\n?)}{}sx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(<\?xml\b[^<>]*\?>\n?)//sx) 0 0 0
124 0 my $n = $str =~ s{\&\#x(\w+);}{chr(hex($1))}gsex;
125 0 ▶ croak "Remaining numerical entities: $1" if $str =~ m{(\&\#\w+;)}sx;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /(\&\#\w+;)/sx 0 0 0
126 0 ▶ croak "Remaining entities: $1" if $str =~ m{(\&\w+;)}sx;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /(\&\w+;)/sx 0 0 0
127 }
128 0 return $str;
129 }
130  
131 # Kept for the fork's callers: the XSD variant is the single source in
132 # oEdtk::XML::XSD.
133 0 ▶sub parse_and_validate_xml_file_xsd {
Subroutine parse_and_validate_xml_file_xsd: not called CC=1 SCAR=6.9
Pod documented
134 0 my ( $file, $cfg ) = @_;
135 0 require oEdtk::XML::XSD;
136 0 return oEdtk::XML::XSD::parse_and_validate_xml_file_xsd( $file, $cfg );
137 }
138  
139 # Returns the first valid id for the document (in the shared DTD);
140 # as a business rule one of these should exist.
141 0 ▶sub first_id {
Subroutine first_id: not called CC=4 SCAR=30.0
Pod documented
142 0 my ($root) = @_;
143 return
144 0 ▶ xpath( $root, 'id/@numaba', 0 )
Condition0%0/8
xpath($root, 'id/@numaba', 0) || xpath($root, 'id/@external', 0) || xpath($root, 'id/@phoenix', 0) || croak('No id found in ' . $root->toString)
l!l
00
xpath($root, "id/\@numaba", 0) or xpath($root, "id/\@external", 0) or xpath($root, "id/\@phoenix", 0)
l!l&&r!l&&!r
000
xpath($root, "id/\@numaba", 0) or xpath($root, "id/\@external", 0)
l!l&&r!l&&!r
000
MC/DC0%0/3
xpath($root, 'id/@numaba', 0) || xpath($root, 'id/@external', 0) || xpath($root, 'id/@phoenix', 0) || croak('No id found in ' . $root->toString)
xpath($root, "id/\@numaba", 0) xpath($root, "id/\@external", 0) xpath($root, "id/\@phoenix", 0)
Truth table0%0/4
A or B or C
A= xpath($root, "id/\@numaba", 0)B= xpath($root, "id/\@external", 0)C= xpath($root, "id/\@phoenix", 0)
ABCresult
1XX1
01X1
0011
0000
145 || xpath( $root, 'id/@external', 0 )
146 || xpath( $root, 'id/@phoenix', 0 )
147 || croak "No id found in " . $root->toString;
148 }
149  
150 # Returns the value of an xpath attribute; fatal if not found and was required.
151 0 ▶sub xpath {
Subroutine xpath: not called CC=4 SCAR=30.0
Pod documented
152 0 my ( $node, $path, $required ) = @_;
153 0 ▶ $required //= 1;
Condition0%0/2
$required //= 1
l!l
00
MC/DC0%0/1
$required //= 1
$required
Truth table0%0/2
A
A= $required
Aresult
11
00
154 return
155 0 ▶ $node->exists($path) ? $node->findvalue($path)
Branch: 2 branches
BranchTrueFalseTotal
$node->exists($path) ? : 0 0 0
$required ? : 0 0 0
156 : $required ? croak join "\n", "XPath '$path' does not exist for node:",
157 $node->toString, q{}
158 : q{};
159 }
160  
161 2 1;
162  
163 =head1 NAME
164  
165 oEdtk::XML::DTD - parse and validate XML documents against their DTD
166  
167 =head1 SYNOPSIS
168  
169 use oEdtk::XML::DTD;
170  
171 my ($dom, $public_id) =
172 oEdtk::XML::DTD::parse_and_validate_xml_file($file, \@accepted_ids);
173  
174 =head1 DESCRIPTION
175  
176 Parse an XML document and check its DTD public identifier against a list of
177 accepted ones before checking that the document validates. The module was formed
178 at S21 of the fork-merge plan by merging the 0.8032 fork's F<oEdtk::XML::DTD> and
179 F<oEdtk::XML::DTD::Utils> (about 90% identical); the XSD variant it used to carry
180 now lives in L<oEdtk::XML::XSD>, and L<oEdtk::XML::DTD::Utils> remains loadable as a
181 delegate.
182  
183 =head1 FUNCTIONS
184  
185 =head2 parse_and_validate_xml_file
186  
187 parse_and_validate_xml_file($file, \@accepted_public_ids, $flag_fatal);
188  
189 Parse C<$file> and validate it. When C<$flag_fatal> is false the failures are
190 reported and the function returns instead of dying. Returns C<($dom, $public_id)>.
191  
192 =head2 parse_and_validate_xml_string
193  
194 parse_and_validate_xml_string($xml, \@accepted_public_ids, $flag_fatal);
195  
196 The same, from a string.
197  
198 =head2 parse_xml
199  
200 parse_xml($input, $type, \@accepted_public_ids, $msg, $flag_fatal);
201  
202 The shared step behind the two wrappers above; C<$type> is C<'file'> or
203 C<'string'>.
204  
205 =head2 validate_xml
206  
207 my ($dom, $public_id) = validate_xml($dom, \@accepted_public_ids, $msg, $flag_fatal);
208  
209 Check the public identifier of an already parsed document and that it validates.
210 The public identifier is read through C<internalSubset>, which returns the DTD
211 node when the document declares a DOCTYPE and undef otherwise, so a document
212 without a DOCTYPE reports an empty public id rather than crashing.
213  
214 =head2 parse_and_validate_xml_file_xsd
215  
216 parse_and_validate_xml_file_xsd($file, $xsd_schema);
217  
218 Forward to L<oEdtk::XML::XSD>, kept so the fork's callers keep working.
219  
220 =head2 load_xml_catalogs
221  
222 load_xml_catalogs($parser);
223  
224 Load the XML catalogs named by the C<EDTK_XML_CATALOG_FILES> configuration key.
225  
226 =head2 list_children_nodes
227  
228 my @nodes = list_children_nodes($node, @authorized_names);
229  
230 Return the element children of C<$node>, refusing unexpected names, non-empty
231 text and unexpected node types.
232  
233 =head2 xml_to_txt
234  
235 my $text = xml_to_txt($string);
236  
237 Drop a leading C<< <?xml ... ?> >> declaration and decode the hexadecimal
238 numeric entities that follow it; croak on any entity left undecoded.
239  
240 =head2 first_id
241  
242 my $id = first_id($root);
243  
244 Return the first of the C<id/@numaba>, C<id/@external> and C<id/@phoenix>
245 attributes, and croak when none of them exists.
246  
247 =head2 xpath
248  
249 my $value = xpath($node, $path, $required);
250  
251 Return the value of the C<$path> attribute of C<$node>, croaking when it is
252 missing and C<$required> is true.
253  
254 =head2 found_in_list
255  
256 my $ok = found_in_list($value, \@list);
257  
258 True when C<$value> equals one element of C<\@list>.
259  
260 =head1 AUTHOR
261  
262 The oEdtk team.
263  
264 =cut
« lib/oEdtk/Tracking.pm ↑ Summary blib/lib/oEdtk/Utils.pm »