| 1 | package EPUB::Parser::File::Parser; | ||||||||||||||||||||
| 2 | 14 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 14 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 14 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 14 | ▶ | use Smart::Args; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 14 | ▶ | use XML::LibXML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 14 | ▶ | use XML::LibXML::XPathContext; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 14 | ▶ | use constant CONTEXT_XPATH => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | metadata => '/pkg:package/pkg:metadata', | ||||||||||||||||||||
| 11 | manifest => '/pkg:package/pkg:manifest', | ||||||||||||||||||||
| 12 | spine => '/pkg:package/pkg:spine', | ||||||||||||||||||||
| 13 | guide => '/pkg:package/pkg:guide', | ||||||||||||||||||||
| 14 | toc => '/xhtml:html/xhtml:body/xhtml:nav[@id="toc"]', | ||||||||||||||||||||
| 15 | 14 | }; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 32 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 18 | 32 | args( | |||||||||||||||||||
| 19 | my $class => 'ClassName', | ||||||||||||||||||||
| 20 | my $data, | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 32 | my $xml = XML::LibXML->new({ no_network => 1, recover => 1 }); | |||||||||||||||||||
| 24 | 32 | my $xml_document = $xml->parse_string($data); | |||||||||||||||||||
| 25 | 32 | my $xpath = XML::LibXML::XPathContext->new($xml_document); | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 32 | my $self = bless { | |||||||||||||||||||
| 28 | parser => $xpath, | ||||||||||||||||||||
| 29 | doc => $xml_document, | ||||||||||||||||||||
| 30 | current_context_key => '', | ||||||||||||||||||||
| 31 | } => $class; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 32 | return $self; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 120 | ▶ | sub context_node { | ||||||||||||||||||
|
Subroutine
context_node: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 120 | my $self = shift; | |||||||||||||||||||
| 38 | 120 | my $key = shift; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 120 | ▶ | return $self->{parser}->getContextNode unless $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 46 | ▶ | if ( $self->{current_context_key} ne $key ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 21 | ▶ | croak "did not match key($key) in CONTEXT_XPATH()" unless my $xpath = CONTEXT_XPATH()->{$key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 21 | my $nodes = $self->{parser}->findnodes($xpath); | |||||||||||||||||||
| 47 | 21 | my $context_node; | |||||||||||||||||||
| 48 | 21 | ▶ | if ( $nodes->size >= 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 21 | $context_node = $nodes->get_node(1); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 21 | ▶ | croak '$context_node not found' unless $context_node; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 21 | $self->{parser}->setContextNode($context_node); | |||||||||||||||||||
| 55 | 21 | $self->{current_context_key} = $key; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 46 | return $self; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 73 | ▶ | sub _find { | ||||||||||||||||||
|
Subroutine
_find: called CC=4 SCAR=15.6
| |||||||||||||||||||||
| 62 | 73 | my $self = shift; | |||||||||||||||||||
| 63 | 73 | ▶ | my $xpath = shift or croak 'please input xpath'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 73 | ▶ | my $node = shift || $self->context_node; | ||||||||||||||||||
|
Condition33%1/3
shift() || $self->context_node
MC/DC0%0/2
shift() || $self->context_node
shift()
$self->context_node
Truth table33%1/3
A || B
A= shift()B= $self->context_node
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | $node ? $self->{parser}->findnodes($xpath, $node) | ||||||||||||||||||||
| 67 | 73 | ▶ | : $self->{parser}->findnodes($xpath); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 44 | ▶ | sub find { | ||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 71 | 44 | my $self = shift; | |||||||||||||||||||
| 72 | 44 | $self->_find(@_); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 29 | ▶ | sub single { | ||||||||||||||||||
|
Subroutine
single: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 76 | 29 | my $self = shift; | |||||||||||||||||||
| 77 | 29 | my $node_list = $self->_find(@_); | |||||||||||||||||||
| 78 | 29 | ▶ | if ( $node_list->size >= 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 29 | $node_list->get_node(1); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 14 | 1; | |||||||||||||||||||