| 1 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | package DAIA; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | #ABSTRACT: Document Availability Information API | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | our $VERSION = '0.43'; #VERSION | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | # we do not want depend on the following modules | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | our ($TRINE_MODEL, $TRINE_SERIALIZER, $RDF_NS, $GRAPHVIZ); | |||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=15.6
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | # optionally use RDF::Trine::Serializer | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | $TRINE_MODEL = 'RDF::Trine::Model'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 11 | $TRINE_SERIALIZER = 'RDF::Trine::Serializer'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | eval "use $TRINE_MODEL; use $TRINE_SERIALIZER"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 11 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 11 | $TRINE_MODEL = undef; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 11 | $TRINE_SERIALIZER = undef; | |||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # optionally use RDF::NS | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | eval "use RDF::NS"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | ▶ | $RDF_NS = eval "RDF::NS->new('any')" unless $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | # optionally use RDF::Trine::Exporter::GraphViz | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | eval "use RDF::Trine::Exporter::GraphViz"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 11 | ▶ | $GRAPHVIZ = 'RDF::Trine::Exporter::GraphViz' unless $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 11 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 11 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||
| 28 | core => [qw(response document item available unavailable availability)], | ||||||||||||||||||||||||||||||||||||||||||
| 29 | entities => [qw(institution department storage limitation)], | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | our @EXPORT_OK = qw(is_uri parse guess); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 11 | Exporter::export_ok_tags; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | $EXPORT_TAGS{all} = [@EXPORT_OK, 'message']; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | Exporter::export_tags('all'); | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 11 | ▶ | use Carp; # use Carp::Clan; # qw(^DAIA::); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 11 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 11 | ▶ | use LWP::Simple (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 11 | ▶ | use XML::LibXML::Simple qw(XMLin); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 11 | ▶ | use DAIA::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 11 | ▶ | use DAIA::Document; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 11 | ▶ | use DAIA::Item; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 | ▶ | use DAIA::Availability; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 11 | ▶ | use DAIA::Available; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 11 | ▶ | use DAIA::Unavailable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 11 | ▶ | use DAIA::Message; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 11 | ▶ | use DAIA::Entity; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 11 | ▶ | use DAIA::Institution; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | ▶ | use DAIA::Department; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 11 | ▶ | use DAIA::Storage; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 11 | ▶ | use DAIA::Limitation; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 11 | ▶ | use Data::Validate::URI qw(is_uri); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | sub response { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Response->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | sub document { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Document->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
document: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 22 | ▶ | sub item { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Item->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 13 | ▶ | sub available { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Available->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
available: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 5 | ▶ | sub unavailable { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Unavailable->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unavailable: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | ▶ | sub availability { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Availability->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
availability: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 38 | ▶ | sub message { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Message->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | sub institution { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Institution->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
institution: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | ▶ | sub department { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Department->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
department: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 13 | ▶ | sub storage { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Storage->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
storage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | sub limitation { local $Carp::CarpLevel = $Carp::CarpLevel + 1; return DAIA::Limitation->new( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
limitation: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 19 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=48 SCAR=41.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 19 | ▶ | shift if UNIVERSAL::isa( $_[0], __PACKAGE__ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 19 | ▶ | my ($from, %param) = (@_ % 2) ? (@_) : (undef,@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 19 | ▶ | $from = $param{from} unless defined $from; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 19 | ▶ | $from = $param{data} unless defined $from; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 19 | ▶ | my $format = lc( $param{format} || '' ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{'format'} || ''
MC/DC100%1/1
$param{'format'} || ''
$param{'format'}
Truth table100%2/2
A
A= $param{'format'}
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 19 | my $file = $param{file}; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 19 | ▶ | $file = $from if defined $from and $from =~ /^http(s)?:\/\//; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $from and $from =~ m[^http(s)?://]
MC/DC0%0/2
defined $from and $from =~ m[^http(s)?://]
defined $from
$from =~ m[^http(s)?://]
Truth table66%2/3
A and B
A= defined $fromB= $from =~ m[^http(s)?://]
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 19 | ▶ | if (not defined $file and defined $from and not defined $param{data}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not defined $file and defined $from and not defined $param{"data"}
not defined $file and defined $from
MC/DC66%2/3
not defined $file and defined $from and not defined $param{"data"}
not defined $file
defined $from
not defined $param{"data"}
Truth table75%3/4
not(A) and B and not(C)
A= not defined $fileB= defined $fromC= not defined $param{"data"}
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 15 | ▶ | if( ref($from) eq 'GLOB' or UNIVERSAL::isa($from, 'IO::Handle')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
ref $from eq "GLOB" or UNIVERSAL::isa($from, "IO::Handle")
MC/DC100%2/2
ref $from eq "GLOB" or UNIVERSAL::isa($from, "IO::Handle")
ref $from eq "GLOB"
UNIVERSAL::isa($from, "IO::Handle")
Truth table100%3/3
A or B
A= ref $from eq "GLOB"B= UNIVERSAL::isa($from, "IO::Handle")
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | $file = $from; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } elsif( $from eq '-' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $file = \*STDIN; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } elsif( $from =~ /\.(xml|json)$/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | $file = $from ; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | $format = $1 unless $format; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 19 | ▶ | if ( $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 9 | ▶ | if ( $file =~ /^http(s)?:\/\// ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $from = LWP::Simple::get($file) or croak "Failed to fetch $file via HTTP"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | ▶ | if ( ! (ref($file) eq 'GLOB' or UNIVERSAL::isa( $file, 'IO::Handle') ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")
MC/DC100%2/2
ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")
ref $file eq "GLOB"
UNIVERSAL::isa($file, "IO::Handle")
Truth table100%3/3
A or B
A= ref $file eq "GLOB"B= UNIVERSAL::isa($file, "IO::Handle")
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 6 | ▶ | $file = do { IO::File->new($file, '<:encoding(UTF-8)') or croak("Failed to open file $file") }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
'IO::File'->new($file, '<:encoding(UTF-8)') || croak("Failed to open file $file")
MC/DC100%1/1
'IO::File'->new($file, '<:encoding(UTF-8)') || croak("Failed to open file $file")
'IO::File'->new($file, '<:encoding(UTF-8)')
Truth table100%2/2
A
A= 'IO::File'->new($file, '<:encoding(UTF-8)')
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # Enable :encoding(UTF-8) layer unless it or some other encoding has already been enabled | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # foreach my $layer ( PerlIO::get_layers( $file ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # return if $layer =~ /^encoding|^utf8/; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | #} | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 8 | binmode $file, ':encoding(UTF-8)'; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 8 | $from = do { local $/; <$file> }; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | croak "DAIA serialization is empty" unless $from; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 17 | ▶ | croak "Missing source to parse from " unless defined $from; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 17 | ▶ | $format = guess($from) unless $format; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 17 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 110 | my @objects; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 17 | my $root = 'Response'; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 17 | ▶ | if ( $format eq 'xml' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | # do not look for filename (security!) | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 13 | ▶ | if (defined $param{data} and guess($from) ne 'xml') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $param{"data"} and guess($from) ne "xml"
MC/DC50%1/2
defined $param{"data"} and guess($from) ne "xml"
defined $param{"data"}
guess($from) ne "xml"
Truth table66%2/3
A and B
A= defined $param{"data"}B= guess($from) ne "xml"
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | croak("XML is not well-formed (<...>)"); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 12 | ▶ | if (guess($from) eq 'xml') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 12 | utf8::encode($from);; | |||||||||||||||||||||||||||||||||||||||||
| 121 | #print "IS UTF8?". utf8::is_utf8($from) . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 12 | my $xml = _parse_daia_xml($from); | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 12 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | ▶ | croak "XML does not contain DAIA elements" unless $xml; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 12 | ▶ | while (my ($root,$value) = each(%$xml)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 12 | $root =~ s/{[^}]+}//; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 12 | $root = ucfirst($root); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 12 | ▶ | $root = 'Response' if $root eq 'Daia'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 12 | _filter_xml( $value ); # filter out all non DAIA elements and namespaces | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 12 | ▶ | $value = [ $value ] unless ref($value) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 12 | foreach my $v (@$value) { | |||||||||||||||||||||||||||||||||||||||||
| 139 | # TODO: croak of $root is not known! | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 12 | my $object = eval 'DAIA::'.$root.'->new( $v )'; ##no critic | |||||||||||||||||||||||||||||||||||||||||
| 141 | 12 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 11 | push @objects, $object; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | } elsif ( $format eq 'json' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | eval { $value = JSON->new->decode($from); }; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | if ( (keys %$value) == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | my ($k => $v) = %$value; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | ▶ | if (not $k =~ /^(timestamp|message|institution|document)$/ and ref($v) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $k =~ /^(timestamp|message|institution|document)$/ and ref $v eq "HASH"
MC/DC0%0/2
not $k =~ /^(timestamp|message|institution|document)$/ and ref $v eq "HASH"
not $k =~ /^(timestamp|message|institution|document)$/
ref $v eq "HASH"
Truth table33%1/3
not(A) and B
A= not $k =~ /^(timestamp|message|institution|document)$/B= ref $v eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | ($root, $value) = (ucfirst($k), $v); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # outdated variants | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 4 | ▶ | $root = "Response" if $root eq 'Daia'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | delete $value->{'xmlns:xsi'}; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | ▶ | delete $value->{schema} if $root eq 'Response'; # ignore schema attribute | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | ▶ | croak "JSON does not contain DAIA elements" unless $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | push @objects, eval('DAIA::'.$root.'->new( $value )'); ##no critic | |||||||||||||||||||||||||||||||||||||||||
| 165 | 4 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | croak "Unknown DAIA serialization format $format"; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 15 | ▶ | return if not wantarray and @objects > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not wantarray and @objects > 1
MC/DC0%0/2
not wantarray and @objects > 1
not wantarray
@objects > 1
Truth table33%1/3
not(A) and B
A= not wantarrayB= @objects > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 15 | ▶ | return wantarray ? @objects : $objects[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 8 | ▶ | sub parse_xml { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_xml: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 8 | ▶ | shift if UNIVERSAL::isa( $_[0], __PACKAGE__ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 8 | DAIA::parse( shift, format => 'xml', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub parse_json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_json: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | shift if UNIVERSAL::isa( $_[0], __PACKAGE__ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | DAIA::parse( shift, format => 'json' ); | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 21 | ▶ | sub guess { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guess: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 21 | ▶ | shift if UNIVERSAL::isa( $_[0], __PACKAGE__ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 21 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 21 | ▶ | return '' unless $data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 21 | ▶ | return 'xml' if $data =~ m{^\s*\<.*?\>\s*$}s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | ▶ | return 'json' if $data =~ m{^\s*\{.*?\}\s*$}s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | ▶ | sub formats { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
formats: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | shift if UNIVERSAL::isa( $_[0], __PACKAGE__ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | my %formats = ( | |||||||||||||||||||||||||||||||||||||||||
| 201 | xml => 'application/xml; charset=utf-8', | ||||||||||||||||||||||||||||||||||||||||||
| 202 | json => 'application/javascript; charset=utf-8', | ||||||||||||||||||||||||||||||||||||||||||
| 203 | rdfjson => 'application/javascript; charset=utf-8', | ||||||||||||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | ▶ | if ($TRINE_SERIALIZER) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $formats{'rdfxml'} = 'application/rdf+xml; charset=utf-8',; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | $formats{'turtle'} = 'text/turtle; charset=utf-8'; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $formats{'ntriples'} = 'text/plain'; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | ▶ | if ($GRAPHVIZ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | $formats{'svg'} = 'image/svg+xml'; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $formats{'dot'} = 'text/plain; charset=utf-8'; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | return %formats; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | #### internal methods (subject to be changed) | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 11 | my $NSEXPDAIA = qr/{http:\/\/(ws.gbv.de|purl.org\/ontology)\/daia\/}(.*)/; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 12 | ▶ | sub _parse_daia_xml { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_daia_xml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 12 | my ($from) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 12 | my $xml = eval { XMLin( $from, KeepRoot => 1, NSExpand => 1, KeyAttr => [ ], NormalizeSpace => 2 ); }; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 12 | daia_xml_roots($xml); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 12 | ▶ | sub daia_xml_roots { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
daia_xml_roots: called CC=15 SCAR=47.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 12 | my $xml = shift; # hash reference | |||||||||||||||||||||||||||||||||||||||||
| 232 | 12 | my $out = { }; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 12 | ▶ | return { } unless UNIVERSAL::isa($xml,'HASH'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 12 | foreach my $key (keys %$xml) { | |||||||||||||||||||||||||||||||||||||||||
| 237 | 12 | my $value = $xml->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 12 | ▶ | if ( $key =~ /^{([^}]*)}(.*)/ and !($key =~ $NSEXPDAIA) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key =~ /^{([^}]*)}(.*)/ and not $key =~ /$NSEXPDAIA/
MC/DC0%0/2
$key =~ /^{([^}]*)}(.*)/ and not $key =~ /$NSEXPDAIA/
$key =~ /^{([^}]*)}(.*)/
not $key =~ /$NSEXPDAIA/
Truth table33%1/3
A and not(B)
A= $key =~ /^{([^}]*)}(.*)/B= not $key =~ /$NSEXPDAIA/
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | # non DAIA element | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | my $children = UNIVERSAL::isa($value,'ARRAY') ? $value : [ $value ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | @$children = grep {defined $_} map { daia_xml_roots($_) } @$children; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | foreach my $n (@$children) { | |||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | while ( my ($k,$v) = each(%{$n}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | next if $k =~ /^xmlns/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | $v = [$v] unless UNIVERSAL::isa($v,'ARRAY'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ($out->{$k}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | push @$v, (UNIVERSAL::isa($out->{$k},'ARRAY') ? | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | @{$out->{$k}} : $out->{$k}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # filter out scalars | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | @$v = grep {ref($_)} @$v unless $k =~ $NSEXPDAIA; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | if (@$v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | $out->{$k} = (@$v > 1 ? $v : $v->[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | } else { # DAIA element or element without namespace | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 12 | $out->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 12 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | # filter out non DAIA XML elements, 'xmlns' attributes and empty values | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 57 | ▶ | sub _filter_xml { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_filter_xml: called CC=11 SCAR=24.1
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 57 | my $xml = shift; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | map { _filter_xml($_) } @$xml if ref($xml) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 57 | ▶ | return unless ref($xml) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 21 | my (@del,%add); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 21 | foreach my $key (keys %$xml) { | |||||||||||||||||||||||||||||||||||||||||
| 274 | 64 | my $value = $xml->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 64 | ▶ | if ($key =~ /^{([^}]*)}(.*)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 18 | my $local = $2; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 18 | ▶ | if ($1 =~ /^http:\/\/(ws.gbv.de|purl.org\/ontology)\/daia\/$/ and $value ne '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$1 =~ m[^http://(ws.gbv.de|purl.org/ontology)/daia/$] and $value ne ""
MC/DC50%1/2
$1 =~ m[^http://(ws.gbv.de|purl.org/ontology)/daia/$] and $value ne ""
$1 =~ m[^http://(ws.gbv.de|purl.org/ontology)/daia/$]
$value ne ""
Truth table66%2/3
A and B
A= $1 =~ m[^http://(ws.gbv.de|purl.org/ontology)/daia/$]B= $value ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 14 | $xml->{$local} = $xml->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 18 | push @del, $key; | |||||||||||||||||||||||||||||||||||||||||
| 281 | ▶ | } elsif ($key =~ /^xmlns/ or $key =~ /:/ or $value eq '') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$key =~ /^xmlns/ or $key =~ /:/ or $value eq ""
$key =~ /^xmlns/ or $key =~ /:/
MC/DC66%2/3
$key =~ /^xmlns/ or $key =~ /:/ or $value eq ""
$key =~ /^xmlns/
$key =~ /:/
$value eq ""
Truth table75%3/4
A or B or C
A= $key =~ /^xmlns/B= $key =~ /:/C= $value eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 15 | push @del, $key; | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | # remove non-daia elements | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 21 | foreach (@del) { delete $xml->{$_}; } | |||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | # recurse | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 21 | map { _filter_xml($xml->{$_}) } keys %$xml; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | __END__ | ||||||||||||||||||||||||||||||||||||||||||