File Coverage

lib/XML/Compile/C14N.pm
Criterion Covered Total %
statement 24 64 37.5
branch 0 20 0.0
condition 0 10 0.0
subroutine 8 14 57.1
pod 5 6 83.3
total 37 114 32.4


line stmt bran cond sub pod time code
1             # Copyrights 2011-2020 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution XML-Compile-C14N. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package XML::Compile::C14N;
10 1     1   277850 use vars '$VERSION';
  1         3  
  1         51  
11             $VERSION = '0.95';
12              
13              
14 1     1   6 use warnings;
  1         2  
  1         25  
15 1     1   5 use strict;
  1         1  
  1         22  
16              
17 1     1   5 use Log::Report 'xml-compile-c14n';
  1         1  
  1         6  
18              
19 1     1   699 use XML::Compile::C14N::Util qw/:c14n :paths/;
  1         2  
  1         144  
20 1     1   7 use XML::LibXML ();
  1         2  
  1         25  
21 1     1   4 use Scalar::Util qw/weaken/;
  1         2  
  1         44  
22 1     1   5 use Encode qw/_utf8_off/;
  1         1  
  1         715  
23              
24             my %versions =
25             ( '1.0' => {}
26             , '1.1' => {}
27             );
28              
29             my %prefixes =
30             ( c14n => C14N_EXC_NS
31             );
32              
33             my %features = #comment excl
34             ( &C14N_v10_NO_COMM => [ 0, 0 ]
35             , &C14N_v10_COMMENTS => [ 1, 0 ]
36             , &C14N_v11_NO_COMM => [ 0, 0 ]
37             , &C14N_v11_COMMENTS => [ 1, 0 ]
38             , &C14N_EXC_NO_COMM => [ 0, 1 ]
39             , &C14N_EXC_COMMENTS => [ 1, 1 ]
40             );
41              
42              
43 0     0 1   sub new(@) { my $class = shift; (bless {}, $class)->init( {@_} ) }
  0            
44             sub init($)
45 0     0 0   { my ($self, $args) = @_;
46              
47 0           my $version = $args->{version};
48 0 0         if(my $c = $args->{for})
49 0 0 0       { $version ||= index($c, C14N10 )==0 ? '1.0'
    0          
    0          
50             : index($c, C14N11 )==0 ? '1.1'
51             : index($c, C14NEXC)==0 ? '1.1'
52             : undef;
53             }
54 0   0       $version ||= '1.1';
55 0           trace "initializing v14n $version";
56              
57 0 0         $versions{$version}
58             or error __x"unknown c14n version {v}, pick from {vs}"
59             , v => $version, vs => [keys %versions];
60 0           $self->{XCC_version} = $version;
61              
62             $self->loadSchemas($args->{schema})
63 0 0         if $args->{schema};
64              
65 0           $self;
66             }
67              
68             #-----------
69              
70              
71 0     0 1   sub version() {shift->{XCC_version}}
72 0     0 1   sub schema() {shift->{XCC_schema}}
73              
74             #-----------
75              
76             sub normalize($$%)
77 0     0 1   { my ($self, $type, $node, %args) = @_;
78 0   0       my $prefixes = $args{prefix_list} || [];
79              
80 0 0         my $features = $features{$type}
81             or error __x"unsupported canonicalization method {name}", name => $type;
82            
83 0           my ($with_comments, $with_exc) = @$features;
84 0 0         my $serialize = $with_exc ? 'toStringEC14N' : 'toStringC14N';
85              
86 0           my $xpath = $args{xpath};
87 0   0       my $context = $args{context} || XML::LibXML::XPathContext->new($node);
88              
89             my $canon =
90 0           eval { $node->$serialize($with_comments, $xpath, $context, $prefixes) };
  0            
91             #warn "--> $canon#\n";
92              
93             # The cannonicalization (XML::LibXML <2.0110) sets the utf8 flag. Later,
94             # Digest::SHA >5.74 downgrades that string, changing some bytes... So,
95             # enforce this output to be interpreted as bytes!
96 0           _utf8_off $canon;
97              
98 0 0         if(my $err = $@)
99             { # $err =~ s/ at .*//s;
100 0           panic $err;
101             }
102 0           $canon;
103             }
104              
105             #-----------
106              
107             sub loadSchemas($)
108 0     0 1   { my ($self, $schema) = @_;
109              
110 0 0         $schema->isa('XML::Compile::Cache')
111             or error __x"loadSchemas() requires a XML::Compile::Cache object";
112 0           $self->{XCC_schema} = $schema;
113 0           weaken $self->{XCC_schema};
114              
115 0           my $version = $self->version;
116 0           my $def = $versions{$version};
117              
118 0           $schema->addPrefixes(\%prefixes);
119 0           my $rewrite = join ',', keys %prefixes;
120 0           $schema->addKeyRewrite("PREFIXED($rewrite)");
121              
122 0           (my $xsd = __FILE__) =~ s! \.pm$ !/exc-c14n.xsd!x;
123 0           trace "loading c14n for $version";
124              
125 0           $schema->importDefinitions($xsd);
126 0           $self;
127             }
128              
129             #-----------------
130              
131             1;