SVG/XML.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 66.6% 20 / 30 Conditioncond 66.6% 10 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 13 totaltotal 78.1% 129 / 165 SCAR 32.0
CC
23
Cov
86%
dtddecl
9.6
xmldecl
4.3
xmlescp
4.1
CRAP 24.5
« SVG.pm ↑ Summary
1 package SVG::XML;
2 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 our $VERSION = '2.86';
6  
7 =pod
8  
9 =head1 NAME
10  
11 SVG::XML - Handle the XML generation bits for SVG.pm
12  
13 =head1 AUTHOR
14  
15 Ronan Oger, cpan@roitsystems.com
16  
17 =head1 SEE ALSO
18  
19 L<SVG>,
20 L<SVG::DOM>,
21 L<SVG::Element>,
22 L<SVG::Parser>,
23 L<SVG::Extension>
24  
25 For Commercial Perl/SVG development, refer to the following sites:
26 L<ROASP.com: Serverside SVG server|http://www.roitsystems.com/>,
27 L<ROIT Systems: Commercial SVG perl solutions|http://www.roitsystems.com/>,
28 L<SVG at the W3C|http://www.w3c.org/Graphics/SVG/>
29  
30 =cut
31  
32 25 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 25 our @ISA = ('Exporter');
35  
36 25 our @EXPORT = qw(
37 xmlesc
38 xmlescape
39 xmlescp
40 cssstyle
41 xmlattrib
42 xmlcomment
43 xmlpi
44 xmltag
45 xmltagopen
46 xmltagclose
47 xmltag_ln
48 xmltagopen_ln
49 xmltagclose_ln
50 processtag
51 xmldecl
52 dtddecl
53 );
54  
55 sub xmlescp {
56 13 ▶ my ( $self, $s ) = @_;
Subroutine xmlescp: called CC=4 SCAR=14.1
Pod undocumented
57  
58 13 ▶ $s = '0' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 13 13
59 13 ▶ $s = join( ', ', @{$s} ) if ( ref($s) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $s eq "ARRAY" 0 13 13
60  
61 # Special XML entities are escaped
62 13 $s =~ s/&(?!#(x\w\w|\d+?);)/&amp;/g;
63 13 $s =~ s/>/&gt;/g;
64 13 $s =~ s/</&lt;/g;
65 13 $s =~ s/\"/&quot;/g;
66 13 $s =~ s/\'/&apos;/g;
67  
68 # Backtick is just a regular XML citizen
69 #$s=~s/\`/&apos;/g;
70  
71 # Invalid XML characters are removed, not just escaped: \x00-\x08\x0b\x1f
72 # Tabs (\x09) and newlines (\x0a) are valid.
73 13 ▶ while ( $s =~ s/([\x00-\x08\x0b\x1f])/''/e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/([\x00-\x08\x0b\x1f])/"";/e) 10 13 23
74 10 my $char = q{'\\x} . sprintf( '%02X', ord($1) ) . q{'};
75 10 $self->error( $char => 'This forbidden XML character was removed' );
76 }
77  
78 # Per suggestion from Adam Schneider
79 13 $s =~ s/([\200-\377])/'&#'.ord($1).';'/ge;
80  
81 13 return $s;
82 }
83  
84 25 *xmlesc = \&xmlescp;
85  
86 25 *xmlescape = \&xmlescp;
87  
88 sub cssstyle {
89 5 ▶ my %attrs = @_;
Subroutine cssstyle: called CC=1 SCAR=0.0
Pod undocumented
90 5 return ( join( '; ', map { qq($_: ) . $attrs{$_} } sort keys(%attrs) ) );
91 }
92  
93 # Per suggestion from Adam Schneider
94 sub xmlattrib {
95 85 ▶ my %attrs = @_;
Subroutine xmlattrib: called CC=2 SCAR=6.9
Pod undocumented
96 85 ▶ return '' unless ( scalar( keys %attrs ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %attrs 24 61 85
97 return (
98 ' '
99 . join( ' ',
100 61 map { qq($_=") . $attrs{$_} . q(") } sort keys(%attrs) )
101 );
102 }
103  
104 sub xmltag {
105 36 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltag: called CC=3 SCAR=11.0
Pod undocumented
106 36 ▶ $ns = $ns ? "$ns:" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 36 36
107 36 ▶ my $at = xmlattrib(%attrs) || '';
Condition100%2/2
xmlattrib(%attrs) || ''
l!l
297
MC/DC100%1/1
xmlattrib(%attrs) || ''
xmlattrib(%attrs)
Truth table100%2/2
A
A= xmlattrib(%attrs)
Aresult
11
00
108 36 return qq(<$ns$name$at />);
109 }
110  
111 sub xmltag_ln {
112 36 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltag_ln: called CC=1 SCAR=0.0
Pod undocumented
113 36 return xmltag( $name, $ns, %attrs );
114 }
115  
116 sub xmltagopen {
117 49 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltagopen: called CC=3 SCAR=11.0
Pod undocumented
118 49 ▶ $ns = $ns ? "$ns:" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 49 49
119 49 ▶ my $at = xmlattrib(%attrs) || '';
Condition100%2/2
xmlattrib(%attrs) || ''
l!l
3217
MC/DC100%1/1
xmlattrib(%attrs) || ''
xmlattrib(%attrs)
Truth table100%2/2
A
A= xmlattrib(%attrs)
Aresult
11
00
120 49 return qq(<$ns$name$at>);
121 }
122  
123 sub xmltagopen_ln {
124 49 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltagopen_ln: called CC=1 SCAR=0.0
Pod undocumented
125 49 return xmltagopen( $name, $ns, %attrs );
126 }
127  
128 sub xmlcomment {
129 22 ▶ my ( $self, $r_comment ) = @_;
Subroutine xmlcomment: called CC=3 SCAR=11.4
Pod undocumented
130 my $ind = $self->{-docref}->{-elsep}
131 22 . $self->{-docref}->{-indent} x $self->{-docref}->{-level};
132  
133 # If the comment starts with newline character then do not prefix
134 # with space (RT #123896).
135 return (
136 $ind . join(
137 $ind,
138 map {
139 22 ▶ ( (/^\n/) ? (q(<!--)) : (q(<!-- )) ) . qq($_)
Branch: 2 branches
BranchTrueFalseTotal
/^\n/ ? : 22 0 22
/\n[\t]?$/ ? : 22 0 22
140 . ( (/\n[\t]?$/) ? (q(-->)) : (q( -->)) );
141 } @$r_comment
142 )
143 );
144 }
145  
146 sub xmlpi {
147 2 ▶ my ( $self, $r_pi ) = @_;
Subroutine xmlpi: called CC=1 SCAR=0.0
Pod undocumented
148 my $ind = $self->{-docref}->{-elsep}
149 2 . $self->{-docref}->{-indent} x $self->{-docref}->{-level};
150 2 return ( join( $ind, map {qq(<?$_?>)} @$r_pi ) );
151 }
152  
153 25 *processinginstruction = \&xmlpi;
154  
155 sub xmltagclose {
156 49 ▶ my ( $name, $ns ) = @_;
Subroutine xmltagclose: called CC=2 SCAR=7.1
Pod undocumented
157 49 ▶ $ns = $ns ? "$ns:" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 49 49
158 49 return qq(</$ns$name>);
159 }
160  
161 sub xmltagclose_ln {
162 49 ▶ my ( $name, $ns ) = @_;
Subroutine xmltagclose_ln: called CC=1 SCAR=0.0
Pod undocumented
163 49 return xmltagclose( $name, $ns );
164 }
165  
166 sub dtddecl {
167 26 ▶ my $self = shift;
Subroutine dtddecl: called CC=9 SCAR=22.6
Pod undocumented
168 26 ▶ my $docroot = $self->{-docroot} || 'svg';
Condition50%1/2
$self->{'-docroot'} || 'svg'
l!l
260
MC/DC0%0/1
$self->{'-docroot'} || 'svg'
$self->{'-docroot'}
Truth table50%1/2
A
A= $self->{'-docroot'}
Aresult
11
00
169 26 my $id;
170  
171 26 ▶ if ( $self->{-pubid} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'-pubid'}) { } 25 1 26
elsif ($self->{'-sysid'}) { } 1 0 1
172 25 $id = 'PUBLIC "' . $self->{-pubid} . '"';
173 25 ▶ $id .= ' "' . $self->{-sysid} . '"' if ( $self->{-sysid} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-sysid"} 25 0 25
174 }
175 elsif ( $self->{-sysid} ) {
176 1 $id = 'SYSTEM "' . $self->{-sysid} . '"';
177 }
178 else {
179 $id
180 = 'PUBLIC "-//W3C//DTD SVG 1.0//EN"'
181 . $self->{-docref}->{-elsep}
182 0 . "\"$self->{-docref}->{-dtd}\"";
183 }
184  
185 26 my $at = join( ' ', ( $docroot, $id ) );
186  
187 #>>>TBD: add internal() method to return this
188 my $extension
189 = ( exists $self->{-internal} )
190 ? $self->{-internal}->render()
191 26 ▶ : q{};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-internal'} ? : 0 26 26
192 26 ▶ if ( exists $self->{-extension} and $self->{-extension} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-extension"} and $self->{"-extension"}) 1 25 26
Condition66%2/3
exists $self->{"-extension"} and $self->{"-extension"}
!ll&&!rl&&r
0251
MC/DC50%1/2
exists $self->{"-extension"} and $self->{"-extension"}
exists $self->{"-extension"} $self->{"-extension"}
Truth table66%2/3
A and B
A= exists $self->{"-extension"}B= $self->{"-extension"}
ABresult
0X0
100
111
193 $extension
194 .= $self->{-docref}{-elsep}
195 . $self->{-extension}
196 1 . $self->{-docref}{-elsep};
197 }
198 26 ▶ $extension = ' [' . $self->{-docref}{-elsep} . $extension . ']'
Branch: 1 branch
BranchTrueFalseTotal
if $extension 1 25 26
199 if $extension;
200  
201 26 return qq[$self->{-docref}{-elsep}<!DOCTYPE $at$extension>];
202 }
203  
204 sub xmldecl {
205 21 ▶ my $self = shift;
Subroutine xmldecl: called CC=4 SCAR=14.6
Pod undocumented
206  
207 21 ▶ my $version = $self->{-version} || '1.0';
Condition50%1/2
$self->{'-version'} || '1.0'
l!l
210
MC/DC0%0/1
$self->{'-version'} || '1.0'
$self->{'-version'}
Truth table50%1/2
A
A= $self->{'-version'}
Aresult
11
00
208 21 ▶ my $encoding = $self->{-encoding} || 'UTF-8';
Condition50%1/2
$self->{'-encoding'} || 'UTF-8'
l!l
210
MC/DC0%0/1
$self->{'-encoding'} || 'UTF-8'
$self->{'-encoding'}
Truth table50%1/2
A
A= $self->{'-encoding'}
Aresult
11
00
209 21 ▶ my $standalone = $self->{-standalone} || 'yes';
Condition50%1/2
$self->{'-standalone'} || 'yes'
l!l
210
MC/DC0%0/1
$self->{'-standalone'} || 'yes'
$self->{'-standalone'}
Truth table50%1/2
A
A= $self->{'-standalone'}
Aresult
11
00
210  
211 return
212 21 qq{<?xml version="$version" encoding="$encoding" standalone="$standalone"?>};
213 }
214  
215 #-------------------------------------------------------------------------------
216  
217 25 1;
« SVG.pm ↑ Summary