File Coverage

blib/lib/EBook/FB2.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
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             package EBook::FB2;
26              
27             our $VERSION = 0.2;
28              
29 1     1   31699 use Moose;
  0            
  0            
30             use XML::DOM;
31             use XML::DOM::XPath;
32             use Carp;
33              
34             use EBook::FB2::Description;
35             use EBook::FB2::Binary;
36             use EBook::FB2::Body;
37              
38             has description => (
39             isa => 'Object',
40             is => 'rw',
41             );
42              
43             has _bodies => (
44             isa => 'ArrayRef[Object]',
45             is => 'ro',
46             traits => ['Array'],
47             default => sub { [] },
48             handles => {
49             bodies => 'elements',
50             add_body => 'push',
51             },
52             );
53              
54             has _binaries => (
55             isa => 'ArrayRef[Object]',
56             is => 'ro',
57             traits => ['Array'],
58             default => sub { [] },
59             handles => {
60             binaries => 'elements',
61             add_binary => 'push',
62             },
63             );
64              
65             sub load
66             {
67             my ($self, $file) = @_;
68             my $parser = XML::DOM::Parser->new();
69             my $xp;
70             eval {
71             $xp = $parser->parsefile($file);
72             };
73              
74             if ($@) {
75             carp("Failed to parse $file");
76             return;
77             }
78              
79             my @nodes = $xp->findnodes('/FictionBook/description');
80             if (@nodes != 1) {
81             my $descriptions = @nodes;
82             warn "There should be only one <description> element";
83             return;
84             }
85              
86             my $desc = EBook::FB2::Description->new();
87             $desc->load($nodes[0]);
88             $self->description($desc);
89              
90             # load binaries
91             @nodes = $xp->findnodes('/FictionBook/binary');
92             foreach my $node (@nodes) {
93             my $bin = EBook::FB2::Binary->new();
94             $bin->load($node);
95             $self->add_binary($bin);
96             }
97              
98              
99             # Load bodies
100             @nodes = $xp->findnodes('/FictionBook/body');
101             foreach my $node (@nodes) {
102             my $bin = EBook::FB2::Body->new();
103             $bin->load($node);
104             $self->add_body($bin);
105             }
106              
107             # XXX: handle stylesheet?
108             return 1;
109             }
110              
111             1;
112              
113             __END__
114             =head1 NAME
115              
116             EBook::FB2
117              
118             =head1 VERSION
119              
120             Version 0.1
121              
122             =head1 SYNOPSIS
123              
124              
125             use EBook::FB2;
126              
127             my $fb2 = EBook::FB2->new;
128             $fb2->load("/path/to/file.fb2");
129              
130             =head1 SUBROUTINES/METHODS
131              
132             =over 4
133              
134             =item binaries()
135              
136             Returns array of references to L<EBook::FB2::Binary> objects
137              
138             =item bodies()
139              
140             Returns array of references to L<EBook::FB2::Body> objects
141              
142             =item description()
143              
144             Returns reference to L<EBook::FB2::Description> object that contains book metadata
145              
146             =item load($filename)
147              
148             Load FB2 document from $filename
149              
150              
151             =back
152              
153             =head1 AUTHOR
154              
155             Oleksandr Tymoshenko, E<lt>gonzo@bluezbox.comE<gt>
156              
157             =head1 BUGS
158              
159             Please report any bugs or feature requests to E<lt>gonzo@bluezbox.comE<gt>
160              
161             =head1 LICENSE AND COPYRIGHT
162              
163             Copyright 2009, 2010 Oleksandr Tymoshenko.
164              
165             L<http://bluezbox.com>
166              
167             This module is free software; you can redistribute it and/or
168             modify it under the terms of the BSD license. See the F<LICENSE> file
169             included with this distribution.