blib/lib/oEdtk/XML/Default.pm

filter: Statementstmt 50.0% 25 / 50 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.6% 7 / 11 Podpod 66.6% 2 / 3 totaltotal 43.0% 34 / 79 CC 6 SCAR 28.1
CC
6
Cov
41%
cameleon_extract
42.0
my_encode
2.0
__ANON__
2.0
CRAP 16.6
« blib/lib/oEdtk/SignedField.pm ↑ Summary blib/lib/oEdtk/Net.pm »
1 package oEdtk::XML::Default;
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 Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl qw(:seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # This is so we can refer to Unicode characters by their name.
11 1 ▶use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 our $VERSION = 2.1129;
15 1 our @ISA = qw(Exporter);
16 1 our @EXPORT_OK = qw(cameleon_open cameleon_extract);
17  
18 # Wrapper for Encode::encode that warns about characters that cannot be
19 # converted, but doesn't insert any replacement text.
20 0 ▶sub my_encode {
Subroutine my_encode: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my ( $enc, $data ) = @_;
22 0 ▶ my $check = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 warn sprintf( "<U+%04X> does not map to $enc", shift );
24 0 return '';
25 0 };
26 0 encode( $enc, $data, $check );
27 }
28  
29 # Extract the key/value pairs from the Cameleon XML file and
30 # return them in a hash.
31 0 ▶sub cameleon_open {
Subroutine cameleon_open: not called CC=1 SCAR=6.9
Pod documented
32 0 my ( $fh, $path ) = @_;
33  
34 0 binmode($fh); # Make sure to drop any PerlIO layer.
35 0 my $xml = XML::LibXML->load_xml( IO => $fh );
36 0 my $xpc = XML::LibXML::XPathContext->new($xml);
37  
38 0 return $xpc;
39 }
40  
41 0 ▶sub cameleon_extract {
Subroutine cameleon_extract: not called CC=6 SCAR=37.4
Pod documented
42 0 my ( $xpc, $path ) = @_;
43 0 my $cond = "[\@cpe=\"$path\"]";
44 0 my @nodes =
45 $xpc->findnodes( '//conf:GenerativeProcess//'
46 . 'conf:ItemSalesBreakdownLine'
47 . $cond
48 . '/conf:Sig//conf:BusinessProperties' );
49  
50 0 my $data = {};
51 0 foreach my $line (@nodes) {
52 0 my ($knode) = $xpc->findnodes(
53 './conf:SingleBusinessProperty[@name="bpTypeDonnee"]/conf:Value/conf:textValue',
54 $line );
55 0 my ($vnode) = $xpc->findnodes(
56 './conf:SingleBusinessProperty[@name="bpValeur"]/conf:Value/conf:longTextValue',
57 $line );
58 0 ▶ if ( !defined($vnode)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $vnode && defined $knode or $vnode->nodeType != 1 or $knode->nodeType != 1) 0 0 0
Condition0%0/9
not defined $vnode && defined $knode or $vnode->nodeType != 1 or $knode->nodeType != 1
l!l&&r!l&&!r
000
not defined $vnode && defined $knode or $vnode->nodeType != 1
l!l&&r!l&&!r
000
defined $vnode && defined $knode
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $vnode && defined $knode or $vnode->nodeType != 1 or $knode->nodeType != 1
defined $vnode defined $knode $vnode->nodeType != 1 $knode->nodeType != 1
Truth table0%0/5
not(A && B) or C or D
A= defined $vnodeB= defined $knodeC= $vnode->nodeType != 1D= $knode->nodeType != 1
ABCDresult
0XXX1
10XX1
111X1
11011
11000
59 || !defined($knode)
60 || $vnode->nodeType != XML_ELEMENT_NODE
61 || $knode->nodeType != XML_ELEMENT_NODE )
62 {
63 0 return;
64 }
65 0 my $key = $knode->textContent();
66 0 my $val = $vnode->textContent();
67  
68 # We substitute the unicode character "RIGHT SINGLE
69 # QUOTATION MARK" by the common apostrophe character,
70 # because there is no other corresponding character
71 # when encoding in ISO-8859-15.
72 # http://ascii-table.com/unicode-index-e.php
73 0 $val =~ s/\N{RIGHT SINGLE QUOTATION MARK}/'/g;
74 0 $val =~ s/\N{HORIZONTAL ELLIPSIS}/.../g;
75  
76 0 $data->{$key} = my_encode( 'iso-8859-15', $val ); # alias = latin9
77 }
78 0 return $data;
79 }
80  
81 1 1;
82  
83 =head1 NAME
84  
85 oEdtk::XML::Default - the default Cameleon XML reader
86  
87 =head1 SYNOPSIS
88  
89 use oEdtk::XML::Default qw(cameleon_open cameleon_extract);
90  
91 my $xpc = cameleon_open($fh);
92 my $data = cameleon_extract($xpc, '/some/cpe/path');
93  
94 =head1 DESCRIPTION
95  
96 Read the key/value pairs a Cameleon generative process writes into its XML
97 output. Moved from C<oEUser::XML::Default> into the mainline at S21 of the
98 fork-merge plan (decision D23); the old name stays available as a delegate.
99  
100 =head1 FUNCTIONS
101  
102 =head2 cameleon_open
103  
104 my $xpc = cameleon_open($fh, $path);
105  
106 Load the Cameleon XML from the open filehandle C<$fh> and return an
107 L<XML::LibXML::XPathContext> over it.
108  
109 =head2 cameleon_extract
110  
111 my $data = cameleon_extract($xpc, $path);
112  
113 Return the C<bpTypeDonnee> / C<bpValeur> pairs of the C<ItemSalesBreakdownLine>
114 whose C<cpe> attribute is C<$path>, or C<undef> when a pair is incomplete. Values
115 are encoded to ISO-8859-15, with the typographic apostrophe and ellipsis folded to
116 their ASCII equivalents.
117  
118 The internal helper C<my_encode>, which encodes a value to ISO-8859-15 while
119 warning about any character it cannot map, is not part of the public API.
120  
121 =head1 AUTHOR
122  
123 The oEdtk team.
124  
125 =cut
« blib/lib/oEdtk/SignedField.pm ↑ Summary blib/lib/oEdtk/Net.pm »