File Coverage

lib/Image/Info/SVG/XMLLibXMLReader.pm
Criterion Covered Total %
statement 41 46 89.1
branch 18 24 75.0
condition 5 5 100.0
subroutine 3 4 75.0
pod 0 1 0.0
total 67 80 83.7


line stmt bran cond sub pod time code
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     3   23 use strict;
  3         7  
  3         633  
18             our $VERSION = '1.06';
19              
20 3     3   722 use XML::LibXML::Reader;
  3         54394  
  3         2474  
21              
22             sub process_file {
23 5     5 0 15 my($info, $source) = @_;
24              
25 5         11 local $_;
26              
27 5         10 my(@comments, @warnings);
28             local $SIG{__WARN__} = sub {
29 0     0   0 push(@warnings, @_);
30 5         47 };
31              
32 5 50       46 my $reader = XML::LibXML::Reader->new(IO => $source, load_ext_dtd => 0, expand_entities => 0)
33             or die "Cannot read SVG from handle '$source'";
34 5         1181 while($reader->read) {
35 6 100       584 last if $reader->nodeType == XML_READER_TYPE_ELEMENT;
36             }
37              
38             # first XML element
39 5         55 my $root_name = $reader->name;
40 5 50       21 if ($root_name eq 'svg') {
41 5         121 $info->push_info(0, 'height', $reader->getAttribute('height'));
42 5         29 $info->push_info(0, 'width', $reader->getAttribute('width'));
43              
44 5   100     38 my $version = $reader->getAttribute('version') || 'unknown';
45 5         15 $info->push_info(0, 'SVG_Version', $version);
46              
47             } else {
48 0         0 return $info->push_info(0, "error", "Not a valid SVG image, got a <$root_name>");
49             }
50              
51 5         9 my $desc;
52 5         30 while($reader->read) {
53 164         99257 my $type = $reader->nodeType;
54 164 100       590 if ($type == XML_READER_TYPE_COMMENT) {
    100          
55 12         72 push @comments, $reader->value;
56             } elsif ($type == XML_READER_TYPE_ELEMENT) {
57 36         110 my $name = $reader->name;
58 36 100 100     363 if (!$desc && $name eq 'desc') {
    100          
    50          
59 4         30 require XML::Simple;
60             # Don't take any chances which XML::SAX is defaulted
61             # by the user. We know that we have XML::LibXML, so
62             # use this one.
63 4         11 local $XML::Simple::PREFERRED_PARSER = 'XML::LibXML::SAX::Parser';
64 4         30 my $xs = XML::Simple->new;
65 4         573 my $desc_xml = $reader->readOuterXml;
66 4         24 $desc = $xs->XMLin($desc_xml);
67             } elsif ($name eq 'title') {
68 1         54 my $title = $reader->copyCurrentNode(1)->textContent;
69 1 50       6 $info->push_info(0, 'SVG_Title', $title) if $title;
70             } elsif ($name eq 'image') {
71 0         0 my $href = $reader->getAttribute('xlink:href');
72 0 0       0 $info->push_info(0, 'SVG_Image', $href) if $href;
73             }
74             }
75             }
76              
77 5 100       72 $info->push_info(0, 'SVG_StandAlone', $reader->standalone == 1 ? "yes" : "no");
78              
79 5 100       27 $info->push_info(0, 'ImageDescription', $desc) if $desc;
80              
81 5         18 $info->push_info(0, "color_type" => "sRGB");
82 5         19 $info->push_info(0, "file_ext" => "svg");
83             # "image/svg+xml" is the official MIME type
84 5         15 $info->push_info(0, "file_media_type" => "image/svg+xml");
85              
86             # XXX how to determine images?
87 5         14 for (@comments) {
88 12         31 $info->push_info(0, "Comment", $_);
89             }
90            
91 5         40 for (@warnings) {
92 0           $info->push_info(0, "Warn", $_);
93             }
94              
95             }
96              
97             1;
98              
99             __END__