blib/lib/XML/Compile/C14N.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 129 SCAR 42.8
CC
8
Cov
0%
init
42.0
normalize
12.0
new
2.0
CRAP 72.0
1 # Copyrights 2011-2014 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.01.
5 use warnings;
6 use strict;
7  
8 package XML::Compile::C14N;
9 our $VERSION = '0.94';
10  
11  
12 use Log::Report 'xml-compile-c14n';
13  
14 use XML::Compile::C14N::Util qw/:c14n :paths/;
15 use XML::LibXML ();
16 use Scalar::Util qw/weaken/;
17 use Encode qw/_utf8_off/;
18  
19 my %versions =
20 ( '1.0' => {}
21 , '1.1' => {}
22 );
23  
24 my %prefixes =
25 ( c14n => C14N_EXC_NS
26 );
27  
28 my %features = #comment excl
29 ( &C14N_v10_NO_COMM => [ 0, 0 ]
30 , &C14N_v10_COMMENTS => [ 1, 0 ]
31 , &C14N_v11_NO_COMM => [ 0, 0 ]
32 , &C14N_v11_COMMENTS => [ 1, 0 ]
33 , &C14N_EXC_NO_COMM => [ 0, 1 ]
34 , &C14N_EXC_COMMENTS => [ 1, 1 ]
35 );
36  
37  
38 sub new(@) { my $class = shift; (bless {}, $class)->init( {@_} ) }
39 sub init($)
40 { my ($self, $args) = @_;
41  
42 my $version = $args->{version};
43 if(my $c = $args->{for})
44 { $version ||= index($c, C14N10 )==0 ? '1.0'
45 : index($c, C14N11 )==0 ? '1.1'
46 : index($c, C14NEXC)==0 ? '1.1'
47 : undef;
48 }
49 $version ||= '1.1';
50 trace "initializing v14n $version";
51  
52 $versions{$version}
53 or error __x"unknown c14n version {v}, pick from {vs}"
54 , v => $version, vs => [keys %versions];
55 $self->{XCC_version} = $version;
56  
57 $self->loadSchemas($args->{schema})
58 if $args->{schema};
59  
60 $self;
61 }
62  
63 #-----------
64  
65  
66 sub version() {shift->{XCC_version}}
67 sub schema() {shift->{XCC_schema}}
68  
69 #-----------
70  
71 sub normalize($$%)
72 { my ($self, $type, $node, %args) = @_;
73 my $prefixes = $args{prefix_list} || [];
74  
75 my $features = $features{$type}
76 or error __x"unsupported canonicalization method {name}", name => $type;
77
78 my ($with_comments, $with_exc) = @$features;
79 my $serialize = $with_exc ? 'toStringEC14N' : 'toStringC14N';
80  
81 my $xpath = $args{xpath};
82 my $context = $args{context} || XML::LibXML::XPathContext->new($node);
83  
84 my $canon =
85 eval { $node->$serialize($with_comments, $xpath, $context, $prefixes) };
86 #warn "--> $canon#\n";
87 _utf8_off $canon;
88  
89 if(my $err = $@)
90 { # $err =~ s/ at .*//s;
91 panic $err;
92 }
93 $canon;
94 }
95  
96 #-----------
97  
98 sub loadSchemas($)
99 { my ($self, $schema) = @_;
100  
101 $schema->isa('XML::Compile::Cache')
102 or error __x"loadSchemas() requires a XML::Compile::Cache object";
103 $self->{XCC_schema} = $schema;
104 weaken $self->{XCC_schema};
105  
106 my $version = $self->version;
107 my $def = $versions{$version};
108  
109 $schema->addPrefixes(\%prefixes);
110 my $rewrite = join ',', keys %prefixes;
111 $schema->addKeyRewrite("PREFIXED($rewrite)");
112  
113 (my $xsd = __FILE__) =~ s! \.pm$ !/exc-c14n.xsd!x;
114 trace "loading c14n for $version";
115  
116 $schema->importDefinitions($xsd);
117 $self;
118 }
119  
120 #-----------------
121  
122 1;