lib/Image/Info/SVG/XMLLibXMLReader.pm

filter: Statementstmt 90.2% 46 / 51 Branchbran 75.0% 21 / 28 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 1 totaltotal 84.9% 79 / 93 CC 19 SCAR 28.5
CC
19
Cov
86%
process_file
20.6
__ANON__
2.0
BEGIN
1.0
CRAP 17.2
« lib/Image/Info/BMP.pm ↑ Summary lib/Image/Info/PPM.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Image_Info_SVG_LibXML.pm,v 1.2 2008/11/22 14:34:16 eserte Exp eserte $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2008,2009,2016 Slaven Rezic. All rights reserved.
8 # This package is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 package Image::Info::SVG::XMLLibXMLReader;
16  
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 $VERSION = '1.05';
20  
21 3 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub process_file {
24 5 ▶ my($info, $source) = @_;
Subroutine process_file: called CC=19 SCAR=30.3
Pod undocumented
25  
26 5 local $_;
27  
28 5 my(@comments, @warnings);
29 local $SIG{__WARN__} = sub {
30 0 ▶ push(@warnings, @_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 5 };
32  
33 5 ▶ my $reader = XML::LibXML::Reader->new(IO => $source, load_ext_dtd => 0, expand_entities => 0)
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "XML::LibXML::Reader"->new("IO", $source, "load_ext_dtd", 0, "expand_entities", 0) or die "Cannot read SVG from handle '${source}'" 0 5 5
34 or die "Cannot read SVG from handle '$source'";
35 5 ▶ while($reader->read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 6 0 6
36 6 ▶ last if $reader->nodeType == XML_READER_TYPE_ELEMENT;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodeType == 1 5 1 6
37 }
38  
39 # first XML element
40 5 my $root_name = $reader->name;
41 5 ▶ if ($root_name eq 'svg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($root_name eq 'svg') { } 5 0 5
42 5 $info->push_info(0, 'height', $reader->getAttribute('height'));
43 5 $info->push_info(0, 'width', $reader->getAttribute('width'));
44  
45 5 ▶ my $version = $reader->getAttribute('version') || 'unknown';
Condition100%2/2
$reader->getAttribute('version') || 'unknown'
l!l
14
MC/DC100%1/1
$reader->getAttribute('version') || 'unknown'
$reader->getAttribute('version')
Truth table100%2/2
A
A= $reader->getAttribute('version')
Aresult
11
00
46 5 $info->push_info(0, 'SVG_Version', $version);
47  
48 } else {
49 0 return $info->push_info(0, "error", "Not a valid SVG image, got a <$root_name>");
50 }
51  
52 5 my $desc;
53 5 ▶ while($reader->read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 164 5 169
54 164 my $type = $reader->nodeType;
55 164 ▶ if ($type == XML_READER_TYPE_COMMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 8) { } 12 152 164
elsif ($type == 1) { } 36 116 152
56 12 push @comments, $reader->value;
57 } elsif ($type == XML_READER_TYPE_ELEMENT) {
58 36 my $name = $reader->name;
59 36 ▶ if (!$desc && $name eq 'desc') {
Branch: 3 branches
BranchTrueFalseTotal
if (not $desc and $name eq 'desc') { } 4 32 36
elsif ($name eq 'title') { } 1 31 32
elsif ($name eq 'image') { } 0 31 31
Condition100%3/3
not $desc and $name eq "desc"
!ll&&!rl&&r
16164
MC/DC100%2/2
not $desc and $name eq "desc"
not $desc $name eq "desc"
Truth table100%3/3
not(A) and B
A= not $descB= $name eq "desc"
ABresult
0X0
100
111
60 4 require XML::Simple;
61 # Don't take any chances which XML::SAX is defaulted
62 # by the user. We know that we have XML::LibXML, so
63 # use this one.
64 4 local $XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX::Parser';
65 4 my $xs = XML::Simple->new;
66 4 my $desc_xml = $reader->readOuterXml;
67 4 $desc = $xs->XMLin($desc_xml);
68 } elsif ($name eq 'title') {
69 1 my $title = $reader->copyCurrentNode(1)->textContent;
70 1 ▶ $info->push_info(0, 'SVG_Title', $title) if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 1 0 1
71 } elsif ($name eq 'image') {
72 0 my $href = $reader->getAttribute('xlink:href');
73 0 ▶ $info->push_info(0, 'SVG_Image', $href) if $href;
Branch: 1 branch
BranchTrueFalseTotal
if $href 0 0 0
74 }
75 }
76 }
77  
78 5 ▶ $info->push_info(0, 'SVG_StandAlone', $reader->standalone == 1 ? "yes" : "no");
Branch: 1 branch
BranchTrueFalseTotal
$reader->standalone == 1 ? : 4 1 5
79  
80 5 ▶ $info->push_info(0, 'ImageDescription', $desc) if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 4 1 5
81  
82 5 $info->push_info(0, "color_type" => "sRGB");
83 5 $info->push_info(0, "file_ext" => "svg");
84 # "image/svg+xml" is the official MIME type
85 5 $info->push_info(0, "file_media_type" => "image/svg+xml");
86  
87 # XXX how to determine images?
88 5 for (@comments) {
89 12 $info->push_info(0, "Comment", $_);
90 }
91
92 5 for (@warnings) {
93 0 $info->push_info(0, "Warn", $_);
94 }
95  
96 }
97  
98 3 1;
99  
100 __END__
« lib/Image/Info/BMP.pm ↑ Summary lib/Image/Info/PPM.pm »