Lite/Container.pm

filter: Statementstmt 88.3% 83 / 94 Branchbran 55.5% 10 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 37.5% 3 / 8 totaltotal 82.2% 111 / 135 CC 12 SCAR 8.1
CC
12
Cov
83%
add_path
3.5
__ANON__
3.1
write_container
3.0
CRAP 2.2
« Lite/Guide.pm ↑ Summary Lite/NCX/NavPoint.pm »
1 # Copyright (c) 2009, 2010 Oleksandr Tymoshenko <gonzo@bluezbox.com>
2 # All rights reserved.
3  
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # 1. Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12  
13 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 # SUCH DAMAGE.
24  
25 # OEPBS Container format implementation
26 # http://www.idpf.org/ocf/ocf1.0/download/ocf10.htm
27 package EBook::EPUB::Lite::Container;
28  
29 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub new
38 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 2 my ($class, %params) = @_;
40 2 my $self = {
41 root_files => [],
42 files => [],
43 encrypted_files => [],
44 };
45 2 return bless $self, $class;
46 }
47  
48 #
49 # Add root file (item in <rootfiles> element)
50 #
51 sub add_root_file
52 2 ▶{
Subroutine add_root_file: called CC=1 SCAR=0.0
Pod undocumented
53 2 my ($self, $path, $mediatype) = @_;
54 2 push @{$self->{root_files}}, {
55 path => $path,
56 mediatype => $mediatype,
57 };
58 }
59  
60 #
61 # Add content. Recurse if it's directory
62 #
63 sub add_path
64 2 ▶{
Subroutine add_path: called CC=3 SCAR=12.5
Pod documented
65 2 my ($self, $from_path, $container_path) = @_;
66  
67 # Closure to collect files recursively
68 15 ▶ my $file_cb = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
69 15 my $file = $File::Find::name;
70 15 my $dest = $file;
71 15 $dest =~ s/\Q$from_path\E/$container_path/;
72  
73 # XXX: UNIX only
74 15 ▶ if (-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 2 13 15
75 2 $dest .= "/";
76 }
77  
78 15 ▶ if (!is_valid_path($dest)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_valid_path($dest)) 0 15 15
79 0 croak("Bad container path: $dest");
80 0 return;
81 }
82  
83 15 push @{$self->{files}}, {
84 frompath => $file,
85 containerpath => $dest,
86 }
87 2 };
88 2 ▶ if (-d $from_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $from_path) { } 2 0 2
89 # XXX: UNIX only
90 # Strip unncessary slashes
91 2 $from_path =~ s/\/+$//g;
92 2 $container_path =~ s/\/+$//g;
93 2 find( { wanted => $file_cb, no_chdir => 1 }, $from_path);
94 }
95 else {
96 0 ▶ if (!is_valid_path($container_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_valid_path($container_path)) 0 0 0
97 0 croak("Bad container path: $container_path");
98 0 return;
99 }
100  
101 0 push @{$self->{files}}, {
102 frompath => $from_path,
103 containerpath => $container_path,
104 }
105 }
106
107 }
108  
109 #
110 # Add encrypted file, at the moment it means font "encrypted" with
111 # Adobe content protection algorithm
112 #
113 sub add_encrypted_path
114 1 ▶{
Subroutine add_encrypted_path: called CC=2 SCAR=7.9
Pod documented
115 1 my ($self, $path) = @_;
116  
117 1 ▶ if (!is_valid_path($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_valid_path($path)) 0 1 1
118 0 croak("Bad container path: $path");
119 0 return;
120 }
121  
122 1 push @{$self->{encrypted_files}}, $path;
123 }
124  
125 #
126 # Check if file name conforms specs
127 # TODO: make conformant to spec
128 sub is_valid_path
129 16 ▶{
Subroutine is_valid_path: called CC=2 SCAR=7.1
Pod undocumented
130 16 my $path = shift;
131 16 ▶ return if($path =~ /META-INF/);
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /META-INF/ 0 16 16
132  
133 16 return 1;
134 }
135  
136 #
137 # Generate container.xml for META-INF directory
138 #
139 sub write_container
140 2 ▶{
Subroutine write_container: called CC=3 SCAR=11.0
Pod undocumented
141 2 my ($self, $outname) = @_;
142 2 my $container = IO::File->new(">$outname");
143  
144 2 ▶ if (!defined($container)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $container) 0 2 2
145 0 return;
146 }
147  
148 2 my $writer = XML::Writer->new(
149 OUTPUT => $container,
150 DATA_MODE => 1,
151 DATA_INDENT => 2,
152 );
153 2 $writer->xmlDecl("utf-8");
154 2 $writer->startTag( "container",
155 "xmlns" => "urn:oasis:names:tc:opendocument:xmlns:container",
156 "version" => "1.0",
157 );
158 2 $writer->startTag("rootfiles");
159 2 foreach my $rf (@{$self->{root_files}}) {
160 $writer->emptyTag("rootfile",
161 "full-path", $rf->{path},
162 "media-type", $rf->{mediatype},
163 2 );
164 }
165 2 $writer->endTag("rootfiles");
166 2 $writer->endTag("container");
167 2 $writer->end();
168 2 $container->close();
169  
170 2 return 1;
171 }
172  
173 sub has_encrypted_files
174 2 ▶{
Subroutine has_encrypted_files: called CC=2 SCAR=6.9
Pod undocumented
175 2 my ($self) = @_;
176 2 ▶ return 1 if (@{$self->{encrypted_files}});
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"encrypted_files"};} 1 1 2
177  
178 # No encrypted data
179 1 return;
180 }
181  
182 #
183 # Generate encryption.xml for META-INF directory
184 #
185 sub write_encryption
186 1 ▶{
Subroutine write_encryption: called CC=3 SCAR=11.0
Pod undocumented
187 1 my ($self, $outname) = @_;
188 1 my $container = IO::File->new(">$outname");
189  
190 1 ▶ if (!defined($container)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $container) 0 1 1
191 0 return;
192 }
193  
194 1 my $writer = XML::Writer->new(
195 OUTPUT => $container,
196 DATA_MODE => 1,
197 DATA_INDENT => 2,
198 );
199 1 $writer->xmlDecl("utf-8");
200 1 $writer->startTag( "encryption",
201 "xmlns" => "urn:oasis:names:tc:opendocument:xmlns:container",
202 );
203 1 foreach my $rf (@{$self->{encrypted_files}}) {
204 1 $writer->startTag('EncryptedData',
205 'xmlns' => 'http://www.w3.org/2001/04/xmlenc#',
206 );
207 1 $writer->emptyTag('EncryptionMethod',
208 'Algorithm' => 'http://ns.adobe.com/pdf/enc#RC',
209 );
210 1 $writer->startTag('CipherData');
211 1 $writer->emptyTag('CipherReference',
212 'URI' => $rf,
213 );
214 1 $writer->endTag('CipherData');
215 1 $writer->endTag('EncryptedData');
216 }
217  
218 1 $writer->endTag("encryption");
219 1 $writer->end();
220 1 $container->close();
221  
222 1 return 1;
223 }
224  
225  
226  
227 4 1;
228  
229 __END__;
« Lite/Guide.pm ↑ Summary Lite/NCX/NavPoint.pm »