DAIA.pm

filter: Statementstmt 85.6% 203 / 237 Branchbran 60.9% 78 / 128 Conditioncond 72.5% 29 / 40 MC/DCmcdc 50.0% 12 / 24 Subroutinesub 97.6% 42 / 43 Podpod 41.1% 7 / 17 totaltotal 75.8% 371 / 489 CC 84 SCAR 36.8
CC
84
Cov
77%
daia_xml_roots
119.9
parse
61.2
_filter_xml
11.2
CRAP 39.5
« DAIA/Document.pm ↑ Summary DAIA/Object.pm »
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
BranchTrueFalseTotal
if ($@) 11 0 11
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
BranchTrueFalseTotal
unless $@ 0 11 11
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
BranchTrueFalseTotal
unless $@ 0 11 11
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
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 3 16 19
72 19 ▶ my ($from, %param) = (@_ % 2) ? (@_) : (undef,@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 15 4 19
73 19 ▶ $from = $param{from} unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 4 15 19
74 19 ▶ $from = $param{data} unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 4 15 19
75 19 ▶ my $format = lc( $param{format} || '' );
Condition100%2/2
$param{'format'} || ''
l!l
910
MC/DC100%1/1
$param{'format'} || ''
$param{'format'}
Truth table100%2/2
A
A= $param{'format'}
Aresult
11
00
76 19 my $file = $param{file};
77 19 ▶ $file = $from if defined $from and $from =~ /^http(s)?:\/\//;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from and $from =~ m[^http(s)?://] 0 19 19
Condition66%2/3
defined $from and $from =~ m[^http(s)?://]
!ll&&!rl&&r
3160
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)?://]
ABresult
0X0
100
111
78 19 ▶ if (not defined $file and defined $from and not defined $param{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file and defined $from and not defined $param{"data"}) 15 4 19
Condition83%5/6
not defined $file and defined $from and not defined $param{"data"}
!ll&&!rl&&r
3115
not defined $file and defined $from
!ll&&!rl&&r
3016
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"}
ABCresult
0XX0
10X0
1100
1111
79 15 ▶ if( ref($from) eq 'GLOB' or UNIVERSAL::isa($from, 'IO::Handle')) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $from eq 'GLOB' or UNIVERSAL::isa($from, 'IO::Handle')) { } 3 12 15
elsif ($from eq '-') { } 0 12 12
elsif ($from =~ /\.(xml|json)$/) { } 3 9 12
Condition100%3/3
ref $from eq "GLOB" or UNIVERSAL::isa($from, "IO::Handle")
l!l&&r!l&&!r
2112
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $format 3 0 3
86 }
87 }
88 19 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 9 10 19
89 9 ▶ if ( $file =~ /^http(s)?:\/\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^http(s)?://]) { } 0 9 9
90 0 ▶ $from = LWP::Simple::get($file) or croak "Failed to fetch $file via HTTP";
Branch: 1 branch
BranchTrueFalseTotal
$from = &LWP::Simple::get($file) or croak("Failed to fetch $file via HTTP") 0 0 0
91 } else {
92 9 ▶ if ( ! (ref($file) eq 'GLOB' or UNIVERSAL::isa( $file, 'IO::Handle') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")) 6 3 9
Condition100%3/3
ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")
l!l&&r!l&&!r
216
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")
ABresult
1X1
011
000
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")
l!l
51
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)')
Aresult
11
00
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
BranchTrueFalseTotal
unless $from 1 7 8
103 }
104  
105 17 ▶ croak "Missing source to parse from " unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 17 17
106  
107 17 ▶ $format = guess($from) unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 5 12 17
108  
109 17 my $value;
110 my @objects;
111 17 my $root = 'Response';
112  
113 17 ▶ if ( $format eq 'xml' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'xml') { } 13 4 17
elsif ($format eq 'json') { } 4 0 4
114 # do not look for filename (security!)
115 13 ▶ if (defined $param{data} and guess($from) ne 'xml') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"data"} and guess($from) ne "xml") 1 12 13
Condition66%2/3
defined $param{"data"} and guess($from) ne "xml"
!ll&&!rl&&r
1201
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"
ABresult
0X0
100
111
116 1 croak("XML is not well-formed (<...>)");
117 }
118  
119 12 ▶ if (guess($from) eq 'xml') {
Branch: 1 branch
BranchTrueFalseTotal
if (guess($from) eq "xml") 12 0 12
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
BranchTrueFalseTotal
if $@ 0 12 12
127 12 ▶ croak "XML does not contain DAIA elements" unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 12 12
128  
129 12 ▶ while (my ($root,$value) = each(%$xml)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$xml) 12 11 23
130 12 $root =~ s/{[^}]+}//;
131 12 $root = ucfirst($root);
132 12 ▶ $root = 'Response' if $root eq 'Daia';
Branch: 1 branch
BranchTrueFalseTotal
if $root eq "Daia" 4 8 12
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
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 12 0 12
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
BranchTrueFalseTotal
if $@ 1 11 12
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
BranchTrueFalseTotal
if $@ 0 4 4
149  
150 4 ▶ if ( (keys %$value) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$value == 1) 1 3 4
151 1 my ($k => $v) = %$value;
152 1 ▶ if (not $k =~ /^(timestamp|message|institution|document)$/ and ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $k =~ /^(timestamp|message|institution|document)$/ and ref $v eq "HASH") 1 0 1
Condition33%1/3
not $k =~ /^(timestamp|message|institution|document)$/ and ref $v eq "HASH"
!ll&&!rl&&r
001
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"
ABresult
0X0
100
111
153 1 ($root, $value) = (ucfirst($k), $v);
154 }
155 }
156  
157 # outdated variants
158 4 ▶ $root = "Response" if $root eq 'Daia';
Branch: 1 branch
BranchTrueFalseTotal
if $root eq "Daia" 0 4 4
159 4 delete $value->{'xmlns:xsi'};
160  
161 4 ▶ delete $value->{schema} if $root eq 'Response'; # ignore schema attribute
Branch: 1 branch
BranchTrueFalseTotal
if $root eq "Response" 3 1 4
162  
163 4 ▶ croak "JSON does not contain DAIA elements" unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 4 4
164 4 push @objects, eval('DAIA::'.$root.'->new( $value )'); ##no critic
165 4 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
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
BranchTrueFalseTotal
if not wantarray and @objects > 1 0 15 15
Condition33%1/3
not wantarray and @objects > 1
!ll&&!rl&&r
0150
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
ABresult
0X0
100
111
172 15 ▶ return wantarray ? @objects : $objects[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 15 15
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
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 6 2 8
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
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
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
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 1 20 21
190 21 my $data = shift;
191 21 ▶ return '' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 21 21
192 21 ▶ return 'xml' if $data =~ m{^\s*\<.*?\>\s*$}s;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /^\s*\<.*?\>\s*$/s 17 4 21
193 4 ▶ return 'json' if $data =~ m{^\s*\{.*?\}\s*$}s;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /^\s*\{.*?\}\s*$/s 4 0 4
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
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 1 0 1
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
BranchTrueFalseTotal
if ($DAIA::TRINE_SERIALIZER) 0 1 1
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
BranchTrueFalseTotal
if ($DAIA::GRAPHVIZ) 0 1 1
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
BranchTrueFalseTotal
unless UNIVERSAL::isa($xml, "HASH") 0 12 12
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
BranchTrueFalseTotal
if ($key =~ /^{([^}]*)}(.*)/ and not $key =~ /$NSEXPDAIA/) { } 0 12 12
Condition33%1/3
$key =~ /^{([^}]*)}(.*)/ and not $key =~ /$NSEXPDAIA/
!ll&&!rl&&r
1200
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/
ABresult
0X0
100
111
240 # non DAIA element
241 0 ▶ my $children = UNIVERSAL::isa($value,'ARRAY') ? $value : [ $value ];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($value, 'ARRAY') ? : 0 0 0
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
BranchTrueFalseTotal
if (() = each %{$n;}) 0 0 0
245 0 ▶ next if $k =~ /^xmlns/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^xmlns/ 0 0 0
246 0 ▶ $v = [$v] unless UNIVERSAL::isa($v,'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($v, "ARRAY") 0 0 0
247 0 ▶ if ($out->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out->{$k}) 0 0 0
248 push @$v, (UNIVERSAL::isa($out->{$k},'ARRAY') ?
249 0 ▶ @{$out->{$k}} : $out->{$k});
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($out->{$k}, 'ARRAY') ? : 0 0 0
250 }
251 # filter out scalars
252 0 ▶ @$v = grep {ref($_)} @$v unless $k =~ $NSEXPDAIA;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /$NSEXPDAIA/ 0 0 0
253 0 ▶ if (@$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$v) 0 0 0
254 0 ▶ $out->{$k} = (@$v > 1 ? $v : $v->[0]);
Branch: 1 branch
BranchTrueFalseTotal
@$v > 1 ? : 0 0 0
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
BranchTrueFalseTotal
if ref $xml eq "ARRAY" 0 57 57
270 57 ▶ return unless ref($xml) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $xml eq "HASH" 36 21 57
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
BranchTrueFalseTotal
if ($key =~ /^{([^}]*)}(.*)/) { } 18 46 64
elsif ($key =~ /^xmlns/ or $key =~ /:/ or $value eq '') { } 15 31 46
276 18 my $local = $2;
277 18 ▶ if ($1 =~ /^http:\/\/(ws.gbv.de|purl.org\/ontology)\/daia\/$/ and $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ m[^http://(ws.gbv.de|purl.org/ontology)/daia/$] and $value ne "") 14 4 18
Condition66%2/3
$1 =~ m[^http://(ws.gbv.de|purl.org/ontology)/daia/$] and $value ne ""
!ll&&!rl&&r
4014
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 ""
ABresult
0X0
100
111
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 ""
l!l&&r!l&&!r
11431
$key =~ /^xmlns/ or $key =~ /:/
l!l&&r!l&&!r
11035
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 ""
ABCresult
1XX1
01X1
0011
0000
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__
« DAIA/Document.pm ↑ Summary DAIA/Object.pm »