File Coverage

blib/lib/EBook/FB2/Body.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::Body;
26 1     1   1906 use Moose;
  0            
  0            
27             use EBook::FB2::Body::Section;
28              
29             has name => ( isa => 'Str', is => 'rw' );
30             has title => ( isa => 'Ref', is => 'rw' );
31             has _epigraphs => (
32             isa => 'ArrayRef',
33             is => 'ro',
34             traits => ['Array'],
35             default => sub { [] },
36             handles => {
37             epigraphs => 'elements',
38             add_epigraph => 'push',
39             }
40             );
41             has image => ( isa => 'Str', is => 'rw' );
42             has _sections => (
43             isa => 'ArrayRef',
44             is => 'ro',
45             traits => ['Array'],
46             default => sub { [] },
47             handles => {
48             sections => 'elements',
49             add_section => 'push',
50             }
51             );
52              
53             sub load
54             {
55             my ($self, $node) = @_;
56              
57             my $anode = $node->getAttribute("name");
58             if (defined($anode)) {
59             $self->name($anode);
60             }
61              
62             my @nodes = $node->findnodes("title");
63             if (@nodes) {
64             $self->title($nodes[0]);
65             }
66             @nodes = $node->findnodes("epigraph");
67             foreach my $n (@nodes) {
68             $self->add_epigraph($n);
69             }
70              
71             @nodes = $node->findnodes("image");
72             if (@nodes) {
73             my $map = $nodes[0]->getAttributes;
74             # find href attribute, a litle bit hackerish
75             my $i = 0;
76             while ($i < $map->getLength) {
77             my $item = $map->item($i);
78             if ($item->getName =~ /:href/i) {
79             my $id = $item->getValue;
80             $id =~ s/^#//;
81             $self->image($id);
82             last;
83             }
84             $i++;
85             }
86             }
87              
88             @nodes = $node->findnodes("section");
89             foreach my $n (@nodes) {
90             my $s = EBook::FB2::Body::Section->new();
91             $s->load($n);
92             $self->add_section($s);
93             }
94             }
95              
96             1;
97              
98             __END__
99             =head1 NAME
100              
101             EBook::FB2::Body
102              
103             =head1 SYNOPSIS
104              
105             EBook::FB2::Body - class that represents <body> element
106              
107             =head1 SUBROUTINES/METHODS
108              
109             =over 4
110              
111             =item epigraphs()
112              
113             Returns array of references to XML::DOM::Node objects, parsed epigraphs
114             of body element
115              
116             =item sections()
117              
118             Returns array of references to L<EBook::FB2::Body::Section> objects,
119             sections of body element
120              
121             =item name()
122              
123             Returns name of body element. Ususally it's either empty or "notes"
124              
125             =item image()
126              
127             Returns id of image associated with body element
128              
129             =item title()
130              
131             Returns title of body element
132              
133             =back
134              
135             =head1 AUTHOR
136              
137             Oleksandr Tymoshenko, E<lt>gonzo@bluezbox.comE<gt>
138              
139             =head1 BUGS
140              
141             Please report any bugs or feature requests to E<lt>gonzo@bluezbox.comE<gt>
142              
143             =head1 LICENSE AND COPYRIGHT
144              
145             Copyright 2009, 2010 Oleksandr Tymoshenko.
146              
147             L<http://bluezbox.com>
148              
149             This module is free software; you can redistribute it and/or
150             modify it under the terms of the BSD license. See the F<LICENSE> file
151             included with this distribution.