| 1 | package PICA::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | $PICA::Parser::VERSION = '0.585'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5 | #ABSTRACT: Parse PICA+ data | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 13 | our @EXPORT_OK = qw(parsefile parsedata); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 13 | our @CARP_NOT = qw(PICA::PlainParser PICA::XMLParser); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 13 | require PICA::PlainParser; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 13 | require PICA::XMLParser; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 83 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 83 | my $class = "PICA::Parser"; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 83 | ▶ | if (scalar(@_) % 2) { # odd | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 83 | $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 83 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 83 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 83 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 27 | defaultparams => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | xmlparser => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | plainparser => undef | ||||||||||||||||||||||||||||||||||||||||||
| 30 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 83 | ▶ | %{ $self->{defaultparams} } = %params if %params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 83 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 86 | ▶ | sub parsefile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parsefile: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 86 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 86 | my ($arg, $parser); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 86 | ▶ | if (ref($self) eq 'PICA::Parser') { # called as a method | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 49 | $arg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 49 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 49 | ▶ | if (ref(\$arg) eq 'SCALAR' and ($arg =~ /.xml$/i or $arg =~ /.xml.gz$/i)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
ref \$arg eq "SCALAR" and $arg =~ /.xml$/i || $arg =~ /.xml.gz$/i
$arg =~ /.xml$/i or $arg =~ /.xml.gz$/i
MC/DC66%2/3
ref \$arg eq "SCALAR" and $arg =~ /.xml$/i || $arg =~ /.xml.gz$/i
ref \$arg eq "SCALAR"
$arg =~ /.xml$/i
$arg =~ /.xml.gz$/i
Truth table75%3/4
A and B or C
A= ref \$arg eq "SCALAR"B= $arg =~ /.xml$/iC= $arg =~ /.xml.gz$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 15 | $params{Format} = "XML"; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 49 | $parser = $self->_getparser( %params ); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 49 | ▶ | croak("Missing argument to parsefile") unless defined $arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 49 | $parser->parsefile( $arg ); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 49 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } else { # called as a function | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 37 | ▶ | $arg = ($self eq 'PICA::Parser') ? shift : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 37 | ▶ | croak("Missing argument to parsefile") unless defined $arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 37 | $parser = PICA::Parser->new( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 37 | $parser->parsefile( $arg ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 37 | $parser; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 78 | ▶ | sub parsedata { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parsedata: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 78 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 78 | my ( $data, $parser ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 78 | ▶ | if (ref($self) eq 'PICA::Parser') { # called as a method | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 46 | $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 46 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 46 | $parser = $self->_getparser( %params ); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 46 | $parser->parsedata( $data ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 46 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } else { # called as a function | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 32 | ▶ | $data = ($self eq 'PICA::Parser') ? shift : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 32 | $parser = PICA::Parser->new( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 32 | $parser->parsedata( $data ); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 32 | $parser; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 26 | ▶ | sub records { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
records: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 26 | ▶ | return () unless ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 26 | ▶ | return $self->{plainparser}->records() if $self->{plainparser}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | ▶ | return $self->{xmlparser}->records() if $self->{xmlparser}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 22 | ▶ | sub counter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
counter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 22 | ▶ | return undef if !ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 22 | my $counter = 0; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 22 | ▶ | $counter += $self->{plainparser}->counter() if $self->{plainparser}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 22 | ▶ | $counter += $self->{xmlparser}->counter() if $self->{xmlparser}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 22 | return $counter; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 34 | ▶ | sub enable_binmode_encoding { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enable_binmode_encoding: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 34 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 34 | foreach my $layer ( PerlIO::get_layers( $fh ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 106 | 90 | ▶ | return if $layer =~ /^encoding|^utf8/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 12 | binmode ($fh, ':utf8'); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 95 | ▶ | sub _getparser { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_getparser: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 95 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 95 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 95 | ▶ | delete $params{Proceed} if defined $params{Proceed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 95 | my $parser; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | # join parameters | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 95 | my %unionparams = (); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 95 | my %defaultparams = %{ $self->{defaultparams} }; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 95 | my $key; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 95 | foreach $key (keys %defaultparams) { | |||||||||||||||||||||||||||||||||||||||||
| 124 | 93 | $unionparams{$key} = $defaultparams{$key} | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 95 | foreach $key (keys %params) { | |||||||||||||||||||||||||||||||||||||||||
| 127 | 24 | $unionparams{$key} = $params{$key} | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # remove format parameter | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 95 | ▶ | delete $params{Format} if defined $params{Format}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # XMLParser | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 95 | ▶ | if ( defined $unionparams{Format} and $unionparams{Format} =~ /^xml$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $unionparams{"Format"} and $unionparams{"Format"} =~ /^xml$/i
MC/DC50%1/2
defined $unionparams{"Format"} and $unionparams{"Format"} =~ /^xml$/i
defined $unionparams{"Format"}
$unionparams{"Format"} =~ /^xml$/i
Truth table66%2/3
A and B
A= defined $unionparams{"Format"}B= $unionparams{"Format"} =~ /^xml$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 24 | ▶ | if ( !$self->{xmlparser} or %params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"xmlparser"} or %params
MC/DC100%2/2
not $self->{"xmlparser"} or %params
not $self->{"xmlparser"}
%params
Truth table100%3/3
not(A) or B
A= not $self->{"xmlparser"}B= %params
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | #require PICA::XMLParser; | ||||||||||||||||||||||||||||||||||||||||||
| 136 | #if ($self->{xmlparser} && | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 22 | $self->{xmlparser} = PICA::XMLParser->new( %unionparams ); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 24 | $parser = $self->{xmlparser}; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } else { # PlainParser | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 71 | ▶ | if ( !$self->{plainparser} or %params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"plainparser"} or %params
MC/DC100%2/2
not $self->{"plainparser"} or %params
not $self->{"plainparser"}
%params
Truth table100%3/3
not(A) or B
A= not $self->{"plainparser"}B= %params
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | #require PICA::PlainParser; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 65 | $self->{plainparser} = PICA::PlainParser->new( %unionparams ); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 71 | $parser = $self->{plainparser}; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 95 | return $parser; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||||||||||||||||