File Coverage

blib/lib/EBook/EPUB/Lite/Metadata.pm
Criterion Covered Total %
statement 54 94 57.4
branch 4 16 25.0
condition n/a
subroutine 15 26 57.6
pod 19 21 90.4
total 92 157 58.6


line stmt bran cond sub pod time code
1             # Copyright (c) 2009, 2010 Oleksandr Tymoshenko
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             package EBook::EPUB::Lite::Metadata;
25 4     4   24 use Carp;
  4         6  
  4         367  
26 4     4   23 use Moo;
  4         6  
  4         42  
27 4     4   1502 use Types::Standard qw/ArrayRef Int Ref Object/;
  4         9  
  4         31  
28 4     4   4818 use EBook::EPUB::Lite::Metadata::DCItem;
  4         16  
  4         201  
29 4     4   2914 use EBook::EPUB::Lite::Metadata::Item;
  4         11  
  4         5070  
30              
31             has items => (
32             is => 'ro',
33             isa => ArrayRef[Object],
34             default => sub { [] },
35             );
36              
37             has id_counter => (
38             is => 'rw',
39             isa => Int,
40             default => 0,
41             );
42              
43             has _book_id_item => (
44             is => 'rw',
45             isa => Ref,
46             );
47              
48             sub encode
49             {
50 2     2 1 5 my ($self, $writer) = @_;
51 2         10 $writer->startTag("metadata",
52             'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
53             'xmlns:opf' => 'http://www.idpf.org/2007/opf',
54             );
55            
56 2         140 foreach my $item (@{$self->items()}) {
  2         15  
57 6         24 $item->encode($writer);
58             }
59 2         55 my $id_item = $self->_book_id_item;
60 2 50       27 croak('No BookId specified by set_book_id') if (!defined($id_item));
61 2         10 $id_item->encode($writer);
62 2         7 $writer->endTag("metadata");
63             }
64              
65             sub add_title
66             {
67 1     1 1 2 my ($self, $title) = @_;
68 1         3 $self->add_meta_dcitem('title', $title);
69             }
70              
71             sub add_contributor
72             {
73 0     0 1 0 my ($self, $name, %opts) = @_;
74 0         0 my @args = ();
75 0         0 my $formal = $opts{'fileas'};
76 0         0 my $role = $opts{'role'};
77 0 0       0 if (defined($formal)) {
78 0         0 push @args, 'opf:file-as', $formal;
79             }
80 0 0       0 if (defined($role)) {
81 0         0 push @args, 'opf:role', $role;
82             }
83              
84 0         0 $self->add_meta_dcitem('contributor', $name, @args);
85             }
86              
87             sub add_creator
88             {
89 1     1 1 4 my ($self, $name, %opts) = @_;
90 1         2 my @args = ();
91 1         1 my $formal = $opts{'fileas'};
92 1         2 my $role = $opts{'role'};
93 1 50       4 if (defined($formal)) {
94 0         0 push @args, 'opf:file-as', $formal;
95             }
96 1 50       3 if (defined($role)) {
97 1         3 push @args, 'opf:role', $role;
98             }
99              
100 1         4 $self->add_meta_dcitem('creator', $name, @args);
101             }
102              
103             sub add_author
104             {
105 1     1 1 2 my ($self, $author, $formal) = @_;
106 1         4 $self->add_creator($author, fileas => $formal, role => 'aut');
107             }
108              
109             sub add_translator
110             {
111 0     0 1 0 my ($self, $name, $formal) = @_;
112 0         0 $self->add_creator($name, fileas => $formal, role => 'trl');
113             }
114              
115             sub add_subject
116             {
117 0     0 1 0 my ($self, $subject) = @_;
118 0         0 $self->add_meta_dcitem('subject', $subject);
119             }
120              
121             sub add_description
122             {
123 1     1 1 376 my ($self, $description) = @_;
124 1         3 $self->add_meta_dcitem('description', $description);
125             }
126              
127             sub add_date
128             {
129             # Todo: handle date format here
130             # http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html#Section2.2.7
131 1     1 1 322 my ($self, $date, $event) = @_;
132 1         1 my @attr;
133 1 50       4 if (defined($event)) {
134 0         0 push @attr, "opf:event", $event;
135             }
136 1         4 $self->add_meta_dcitem('date', $date, @attr);
137             }
138              
139             sub add_type
140             {
141 0     0 1 0 my ($self, $type) = @_;
142 0         0 $self->add_meta_dcitem('type', $type);
143             }
144              
145             sub add_format
146             {
147 0     0 1 0 my ($self, $format) = @_;
148 0         0 $self->add_meta_dcitem('format', $format);
149             }
150              
151             sub add_publisher
152             {
153 0     0 1 0 my ($self, $publisher) = @_;
154 0         0 $self->add_meta_dcitem('publisher', $publisher);
155             }
156              
157             sub add_coverage
158             {
159 0     0 1 0 my ($self, $coverage) = @_;
160 0         0 $self->add_meta_dcitem('coverage', $coverage);
161             }
162              
163             sub set_book_id
164             {
165 2     2 0 4 my ($self, $book_id) = @_;
166 2         7 my @attr = ('id', 'BookId');
167 2         19 my $dcitem = EBook::EPUB::Lite::Metadata::DCItem->new(
168             name => "dc:identifier",
169             value => $book_id,
170             attributes => \@attr);
171              
172 2         3349 $self->_book_id_item($dcitem);
173              
174             }
175              
176             sub add_identifier
177             {
178 0     0 1 0 my ($self, $ident, $scheme) = @_;
179 0         0 my $id = 'BookId';
180             # if it's urn:uuid - it should be BookId, otherwise include counter
181 0 0       0 if ($ident !~ /^urn:uuid/i) {
182 0         0 $id .= $self->id_counter;
183 0         0 $self->id_counter($self->id_counter + 1);
184             }
185              
186 0         0 my @attr = ('id', $id);
187 0 0       0 if (defined($scheme)) {
188 0         0 push @attr, "opf:scheme", $scheme;
189             }
190 0         0 $self->add_meta_dcitem('identifier', $ident, @attr);
191             }
192              
193             sub add_source
194             {
195 1     1 1 330 my ($self, $source) = @_;
196 1         3 $self->add_meta_dcitem('source', $source);
197             }
198              
199             sub add_language
200             {
201             # TODO: filter language?
202 1     1 1 854 my ($self, $lang) = @_;
203 1         3 $self->add_meta_dcitem('language', $lang);
204             }
205              
206             sub add_relation
207             {
208 0     0 1 0 my ($self, $relation) = @_;
209 0         0 $self->add_meta_dcitem('relation', $relation);
210             }
211              
212             sub add_rights
213             {
214 0     0 1 0 my ($self, $rights) = @_;
215 0         0 $self->add_meta_dcitem('rights', $rights);
216             }
217              
218             sub add_meta_dcitem
219             {
220 6     6 0 10 my ($self, $name, $value, @attributes) = @_;
221 6         121 my $dcitem = EBook::EPUB::Lite::Metadata::DCItem->new(
222             name => "dc:$name",
223             value => $value,
224             attributes => \@attributes);
225 6         317 push @{$self->items()}, $dcitem;
  6         25  
226             }
227              
228             sub add_meta_item
229             {
230 0     0 1   my ($self, $name, $value) = @_;
231 0           my $item = EBook::EPUB::Lite::Metadata::Item->new(
232             name => $name,
233             value => $value,
234             );
235 0           push @{$self->items()}, $item;
  0            
236             }
237              
238             1;
239              
240             __END__