SVG/Extension.pm

filter: Statementstmt 23.5% 36 / 153 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 17.9% 7 / 39 Podpod 25.0% 8 / 32 totaltotal 17.2% 51 / 295 CC 40 SCAR 51.1
CC
40
Cov
16%
xmlify
552.0
getElementDeclByName
12.0
getAttributeDeclsByName
12.0
CRAP 166.4
↑ Summary SVG/Element.pm »
1 package SVG::Extension;
2 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 30 our $VERSION = '2.89';
6  
7 =head1 NAME
8  
9 SVG::Extension - additional methods
10  
11 =cut
12  
13 # although DTD declarations are not elements, we use the same API so we can
14 # manipulate the internal DTD subset using the same methods available for
15 # elements. At this state, all extensions are the same object class, but
16 # may be subclassed in the future to e.g. SVG::Extension::ELEMENT. Use
17 # e.g. isElementDecl() to determine types; this API will be retained
18 # irrespective.
19  
20 30 ▶use parent qw/SVG::Element/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # DTD declarations handled in this module
23 30 ▶use constant ELEMENT => 'ELEMENT';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 30 ▶use constant ATTLIST => 'ATTLIST';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 30 ▶use constant NOTATION => 'NOTATION';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 30 ▶use constant ENTITY => 'ENTITY';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 30 our @TYPES = ( ELEMENT, ATTLIST, NOTATION, ENTITY );
29 30 our %TYPES = map { $_ => 1 } @TYPES;
30  
31 #-----------------
32  
33 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
34 0 return shift->SUPER::new(@_);
35 }
36  
37 0 ▶sub internal_subset {
Subroutine internal_subset: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my $self = shift;
39  
40 0 my $document = $self->{-docref};
41 0 ▶ unless ( exists $document->{-internal} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $document->{"-internal"}) 0 0 0
42 0 $document->{-internal} = new SVG::Extension('internal');
43 0 $document->{-internal}{-docref} = $document;
44 }
45  
46 0 return $document->{-internal};
47 }
48  
49 =head2 extension
50  
51 return the element object
52  
53 =cut
54  
55 0 ▶sub extension {
Subroutine extension: not called CC=2 SCAR=17.9
Pod documented
56 0 my $self = shift;
57 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
58  
59 0 return bless $self->SUPER::element(@_), $class;
60 }
61  
62 #-----------------
63  
64 =head2 element_decl
65  
66 generate an element declaration in the DTD
67  
68 =cut
69  
70 0 ▶sub element_decl {
Subroutine element_decl: not called CC=1 SCAR=6.9
Pod documented
71 0 my ( $self, %attrs ) = @_;
72 0 my $subset = $self->internal_subset();
73  
74 0 return $subset->extension( 'ELEMENT', %attrs );
75 }
76  
77 =head2 attribute_decl
78  
79 return generate an attribute list for an element
80  
81 =cut
82  
83 0 ▶sub attribute_decl {
Subroutine attribute_decl: not called CC=2 SCAR=17.9
Pod documented
84 0 my ( $element_decl, %attrs ) = @_;
85  
86 0 ▶ unless ( $element_decl->getElementType eq 'ELEMENT' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($element_decl->getElementType eq "ELEMENT") 0 0 0
87 0 $element_decl->error(
88 $element_decl => 'is not an ELEMENT declaration' );
89 0 return;
90 }
91  
92 0 return $element_decl->extension( 'ATTLIST', %attrs );
93 }
94  
95 =head2 attlist_decl
96  
97 =cut
98  
99 0 ▶sub attlist_decl {
Subroutine attlist_decl: not called CC=2 SCAR=17.9
Pod documented
100 0 my ( $self, %attrs ) = @_;
101 0 my $subset = $self->internal_subset();
102  
103 0 my $element_decl = $subset->getElementDeclByName( $attrs{name} );
104 0 ▶ unless ($element_decl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($element_decl) 0 0 0
105 0 $subset->error( "ATTLIST declaration '$attrs{attr}'" =>
106 "ELEMENT declaration '$attrs{name}' does not exist" );
107 0 return;
108 }
109  
110 0 return $element_decl->attribute_decl(%attrs);
111 }
112  
113 =head2 notation_decl(%attrs)
114  
115 return an extension object of type NOTATION
116  
117 =cut
118  
119 0 ▶sub notation_decl {
Subroutine notation_decl: not called CC=1 SCAR=6.9
Pod documented
120 0 my ( $self, %attrs ) = @_;
121 0 my $subset = $self->internal_subset();
122  
123 0 return $subset->extension( 'NOTATION', %attrs );
124 }
125  
126 =head2 entity_decl(%attrs)
127  
128 return an extension object of type 'ENTITY'
129  
130 =cut
131  
132 0 ▶sub entity_decl {
Subroutine entity_decl: not called CC=1 SCAR=6.9
Pod documented
133 0 my ( $self, %attrs ) = @_;
134 0 my $subset = $self->internal_subset();
135  
136 0 return $subset->extension( 'ENTITY', %attrs );
137 }
138  
139 #-----------------
140  
141 # this interim version of xmlify handles the vanilla extension
142 # format of one parent 'internal' element containing a list of
143 # extension elements. A hierarchical model will follow in time
144 # with the same render API.
145  
146 =head2 xmilfy
147  
148 =cut
149  
150 0 ▶sub xmlify {
Subroutine xmlify: not called CC=23 SCAR=63.1
Pod undocumented
151 0 my $self = shift;
152 0 my $decl = q{};
153  
154 0 ▶ if ( $self->{-name} ne 'internal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-name"} ne "internal") 0 0 0
155 0 $decl = '<!';
156 0 SWITCH: foreach ( $self->{-name} ) {
157 0 ▶ /^ELEMENT$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^ELEMENT$/ and do { $decl .= qq[ELEMENT $self->{"name"}]; $decl .= ' ' . $self->{"model"} if exists $self->{"model"}; last SWITCH } 0 0 0
158 0 $decl .= "ELEMENT $self->{name}";
159  
160 0 ▶ $decl .= q{ } . $self->{model} if exists $self->{model};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"model"} 0 0 0
161  
162 0 last SWITCH;
163 };
164 0 ▶ /^ATTLIST$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^ATTLIST$/ and do { $decl .= qq[ATTLIST $self->{"name"} $self->{"attr"}]; $decl .= qq[ $self->{"type"} ] . ($self->{"fixed"} ? "#FIXED " : "") . $self->{"default"}; last SWITCH } 0 0 0
165 0 $decl .= "ATTLIST $self->{name} $self->{attr}";
166  
167 $decl
168 .= " $self->{type} "
169 . ( $self->{fixed} ? '#FIXED ' : q{} )
170 0 ▶ . $self->{default};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'fixed'} ? : 0 0 0
171  
172 0 last SWITCH;
173 };
174 0 ▶ /^NOTATION$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^NOTATION$/ and do { $decl .= qq[NOTATION $self->{"name"}]; $decl .= ' ' . $self->{"base"} if exists $self->{"base"}; if (exists $self->{"pubid"}) { $decl .= qq[PUBLIC $self->{"pubid"} ]; $decl .= ' ' . $self->{"sysid"} if exists $self->{"sysid"}; } elsif (exists $self->{"sysid"}) { $decl .= ' SYSTEM ' . $self->{"sysid"} if exists $self->{"sysid"}; } ; last SWITCH } 0 0 0
175 0 $decl .= "NOTATION $self->{name}";
176  
177 0 ▶ $decl .= q{ } . $self->{base} if exists $self->{base};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"base"} 0 0 0
178 0 ▶ if ( exists $self->{pubid} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'pubid'}) { } 0 0 0
elsif (exists $self->{'sysid'}) { } 0 0 0
179 0 $decl .= "PUBLIC $self->{pubid} ";
180 0 ▶ $decl .= q{ } . $self->{sysid} if exists $self->{sysid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sysid"} 0 0 0
181 }
182 elsif ( exists $self->{sysid} ) {
183 $decl .= ' SYSTEM ' . $self->{sysid}
184 0 ▶ if exists $self->{sysid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sysid"} 0 0 0
185 }
186  
187 0 last SWITCH;
188 };
189 0 ▶ /^ENTITY$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^ENTITY$/ and do { $decl .= 'ENTITY ' . ($self->{"isp"} ? "% " : "") . $self->{"name"}; if (exists $self->{"value"}) { $decl .= ' "' . $self->{"value"} . '"'; } elsif (exists $self->{"pubid"}) { $decl .= qq[PUBLIC $self->{"pubid"} ]; $decl .= ' ' . $self->{"sysid"} if exists $self->{"sysid"}; $decl .= ' ' . $self->{"ndata"} if $self->{"ndata"}; } else { $decl .= ' SYSTEM ' . $self->{"sysid"} if exists $self->{"sysid"}; $decl .= ' ' . $self->{"ndata"} if $self->{"ndata"}; } ; last SWITCH; DEFAULT: $decl .= qq[$self->{"-name"} $self->{"name"}] } 0 0 0
190 $decl
191 .= 'ENTITY '
192 . ( $self->{isp} ? '% ' : q{} )
193 0 ▶ . $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'isp'} ? : 0 0 0
194  
195 0 ▶ if ( exists $self->{value} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'value'}) { } 0 0 0
elsif (exists $self->{'pubid'}) { } 0 0 0
196 0 $decl .= ' "' . $self->{value} . '"';
197 }
198 elsif ( exists $self->{pubid} ) {
199 0 $decl .= "PUBLIC $self->{pubid} ";
200 0 ▶ $decl .= q{ } . $self->{sysid} if exists $self->{sysid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sysid"} 0 0 0
201 0 ▶ $decl .= q{ } . $self->{ndata} if $self->{ndata};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ndata"} 0 0 0
202 }
203 else {
204 $decl .= ' SYSTEM ' . $self->{sysid}
205 0 ▶ if exists $self->{sysid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"sysid"} 0 0 0
206 0 ▶ $decl .= q{ } . $self->{ndata} if $self->{ndata};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ndata"} 0 0 0
207 }
208  
209 0 last SWITCH;
210 0 DEFAULT:
211  
212 # we don't know what this is, but the underlying parser allowed it
213 $decl .= "$self->{-name} $self->{name}";
214 };
215 }
216 0 $decl .= '>' . $self->{-docref}{-elsep};
217 }
218  
219 0 my $result = q{};
220 0 ▶ if ( $self->hasChildren ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasChildren) 0 0 0
221 0 $self->{-docref}->{-level}++;
222 0 foreach my $child ( $self->getChildren ) {
223 $result
224 .= ( $self->{-docref}{-indent} x $self->{-docref}->{-level} )
225 0 . $child->render();
226 }
227 0 $self->{-docref}->{-level}--;
228 }
229  
230 0 return $decl . $result;
231 }
232  
233 #some aliases for xmilfy
234  
235 =head2 render
236  
237 alias for xmlify
238  
239 =head2 to_xml
240  
241 alias for xmlify
242  
243 =head2 serialise
244  
245 alias for xmlify
246  
247 =head2 serialise
248  
249 alias for xmlify
250  
251 =cut
252  
253 30 *render = \&xmlify;
254 30 *to_xml = \&xmlify;
255 30 *serialise = \&xmlify;
256 30 *serialize = \&xmlify;
257  
258 #-----------------
259  
260 =head2 getDeclName
261  
262 Simply an alias for the general method for SVG::Extension objects
263  
264 =head2 getExtensionName
265  
266 alias to getDeclName
267  
268 =cut
269  
270 # simply an alias for the general method for SVG::Extension objects
271 0 ▶sub getDeclName {
Subroutine getDeclName: not called CC=1 SCAR=6.9
Pod documented
272 0 return shift->SUPER::getElementName();
273 }
274 30 *getExtensionName = \&getDeclName;
275  
276 =head2 getDeclNames
277  
278 return list of existing decl types by extracting it from the overall list
279 of existing element types
280  
281 sub getDeclNames {
282  
283 =head2 getExtensionNames
284  
285 alias to getDeclNames
286  
287 =cut
288  
289 # return list of existing decl types by extracting it from the overall list
290 # of existing element types
291 0 ▶sub getDeclNames {
Subroutine getDeclNames: not called CC=1 SCAR=6.9
Pod documented
292 0 my $self = shift;
293  
294 0 return grep { exists $TYPES{$_} } $self->SUPER::getElementNames();
295 }
296 30 *getExtensionNames = \&getDeclNames;
297  
298 #-----------------
299  
300 # we can have only one element decl of a given name...
301 0 ▶sub getElementDeclByName {
Subroutine getElementDeclByName: not called CC=3 SCAR=24.8
Pod undocumented
302 0 my ( $self, $name ) = @_;
303 0 my $subset = $self->internal_subset();
304  
305 0 my @element_decls = $subset->getElementsByName('ELEMENT');
306 0 foreach my $element_decl (@element_decls) {
307 0 ▶ return $element_decl if $element_decl->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $element_decl->{"name"} eq $name 0 0 0
308 }
309  
310 0 return;
311 }
312  
313 # ...but we can have multiple attributes. Note that this searches the master list
314 # which is not what you are likely to want in most cases. See getAttributeDeclByName
315 # (no 's') below, to search for an attribute decl on a particular element decl.
316 # You can use the result of this method along with getParent to find the list of
317 # all element decls that define a given attribute.
318 0 ▶sub getAttributeDeclsByName {
Subroutine getAttributeDeclsByName: not called CC=3 SCAR=24.8
Pod undocumented
319 0 my ( $self, $name ) = @_;
320 0 my $subset = $self->internal_subset();
321  
322 0 my @element_decls = $subset->getElementsByName('ELEMENT');
323 0 foreach my $element_decl (@element_decls) {
324 0 ▶ return $element_decl if $element_decl->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $element_decl->{"name"} eq $name 0 0 0
325 }
326  
327 0 return;
328 }
329  
330 #-----------------
331  
332 0 ▶sub getElementDecls {
Subroutine getElementDecls: not called CC=1 SCAR=6.9
Pod undocumented
333 0 return shift->SUPER::getElements('ELEMENT');
334 }
335  
336 0 ▶sub getNotations {
Subroutine getNotations: not called CC=1 SCAR=6.9
Pod undocumented
337 0 return shift->SUPER::getElements('NOTATION');
338 }
339 30 *getNotationDecls = \&getNotations;
340  
341 0 ▶sub getEntities {
Subroutine getEntities: not called CC=1 SCAR=6.9
Pod undocumented
342 0 return shift->SUPER::getElements('ENTITY');
343 }
344 30 *getEntityDecls = \&getEntities;
345  
346 0 ▶sub getAttributeDecls {
Subroutine getAttributeDecls: not called CC=1 SCAR=6.9
Pod undocumented
347 0 return shift->SUPER::getElements('ATTLIST');
348 }
349  
350 #-----------------
351 # until/unless we subclass these, use the name. After (if) we
352 # subclass, will use the object class.
353  
354 0 ▶sub isElementDecl {
Subroutine isElementDecl: not called CC=2 SCAR=17.9
Pod undocumented
355 0 ▶ return ( shift->getElementName eq ELEMENT ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->getElementName eq 'ELEMENT' ? : 0 0 0
356 }
357  
358 0 ▶sub isNotation {
Subroutine isNotation: not called CC=2 SCAR=17.9
Pod undocumented
359 0 ▶ return ( shift->getElementName eq NOTATION ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->getElementName eq 'NOTATION' ? : 0 0 0
360 }
361  
362 0 ▶sub isEntity {
Subroutine isEntity: not called CC=2 SCAR=17.9
Pod undocumented
363 0 ▶ return ( shift->getElementName eq ENTITY ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->getElementName eq 'ENTITY' ? : 0 0 0
364 }
365  
366 0 ▶sub isAttributeDecl {
Subroutine isAttributeDecl: not called CC=2 SCAR=17.9
Pod undocumented
367 0 ▶ return ( shift->getElementName eq ATTLIST ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->getElementName eq 'ATTLIST' ? : 0 0 0
368 }
369  
370 #-----------------
371  
372 # the Decl 'name' is an attribute, the name is e.g. 'ELEMENT'
373 # use getElementName if you want the actual decl type
374 0 ▶sub getElementDeclName {
Subroutine getElementDeclName: not called CC=2 SCAR=17.9
Pod undocumented
375 0 my $self = shift;
376  
377 0 ▶ if ( exists $self->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"name"}) 0 0 0
378 0 return $self->{name};
379 }
380  
381 0 return;
382 }
383  
384 # identical to the above; will be smarter as and when we subclass
385 # as above, the name is ATTLIST, the 'name' is a property of the decl
386 0 ▶sub getAttributeDeclName {
Subroutine getAttributeDeclName: not called CC=2 SCAR=17.9
Pod undocumented
387 0 my $self = shift;
388  
389 0 ▶ if ( exists $self->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"name"}) 0 0 0
390 0 return $self->{name};
391 }
392  
393 0 return;
394 }
395  
396 # unlike other 'By' methods, attribute searches work from their parent element
397 # del only. Multiple element decls with the same attribute name is more than
398 # likely, so searching the master ATTLIST is not very useful. If you really want
399 # to do that, use getAttributeDeclsByName (with an 's') above.
400 0 ▶sub getAttributeDeclByName {
Subroutine getAttributeDeclByName: not called CC=3 SCAR=24.8
Pod undocumented
401 0 my ( $self, $name ) = @_;
402  
403 0 my @attribute_decls = $self->getElementAttributeDecls();
404 0 foreach my $attribute_decl (@attribute_decls) {
405 0 ▶ return $attribute_decl if $attribute_decl->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $attribute_decl->{"name"} eq $name 0 0 0
406 }
407  
408 0 return;
409 }
410  
411 # as this is element specific, we allow a 'ElementAttribute' name too,
412 # for those that like consistency at the price of brevity. Not that
413 # the shorter name is all that brief to start with...
414 30 *getElementAttributeDeclByName = \&getAttributeDeclByName;
415  
416 # ...and for those who live their brevity:
417 30 *getAttributeDecl = \&getAttributeDeclByName;
418  
419 0 ▶sub hasAttributeDecl {
Subroutine hasAttributeDecl: not called CC=2 SCAR=17.9
Pod undocumented
420 0 ▶ return ( shift->getElementDeclByName(shift) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->getElementDeclByName(shift()) ? : 0 0 0
421 }
422  
423 #-----------------
424 # directly map to Child/Siblings: we presume this is being called from an
425 # element decl. You can use 'getChildIndex', 'getChildAtIndex' etc. as well
426  
427 0 ▶sub getElementAttributeAtIndex {
Subroutine getElementAttributeAtIndex: not called CC=1 SCAR=6.9
Pod undocumented
428 0 my ( $self, $index, @children ) = @_;
429  
430 0 return $self->SUPER::getChildAtIndex( $index, @children );
431 }
432  
433 0 ▶sub getElementAttributeIndex {
Subroutine getElementAttributeIndex: not called CC=1 SCAR=6.9
Pod undocumented
434 0 return shift->SUPER::getChildIndex(@_);
435 }
436  
437 0 ▶sub getFirstAttributeDecl {
Subroutine getFirstAttributeDecl: not called CC=1 SCAR=6.9
Pod undocumented
438 0 return shift->SUPER::getFirstChild();
439 }
440  
441 0 ▶sub getNextAttributeDecl {
Subroutine getNextAttributeDecl: not called CC=1 SCAR=6.9
Pod undocumented
442 0 return shift->SUPER::getNextSibling();
443 }
444  
445 0 ▶sub getLastAttributeDecl {
Subroutine getLastAttributeDecl: not called CC=1 SCAR=6.9
Pod undocumented
446 0 return shift->SUPER::getLastChild();
447 }
448  
449 0 ▶sub getPreviousAttributeDecl {
Subroutine getPreviousAttributeDecl: not called CC=1 SCAR=6.9
Pod undocumented
450 0 return shift->SUPER::getPreviousSibling();
451 }
452  
453 0 ▶sub getElementAttributeDecls {
Subroutine getElementAttributeDecls: not called CC=1 SCAR=6.9
Pod undocumented
454 0 return shift->SUPER::getChildren();
455 }
456  
457 #-------------------------------------------------------------------------------
458  
459 # These methods are slated for inclusion in a future release of SVG.pm. They
460 # will allow programmatic advance determination of the validity of various DOM
461 # manipulations. If you are in a hurry for this feature, get in touch!
462 #
463 # example:
464 # if ($svg_object->allowsElement("symbol")) { ... }
465 #
466 #package SVG::Element;
467 #
468 #sub allowedElements {}
469 #sub allowedAttributes {}
470 #
471 #sub allowsElement {}
472 #sub allowsAttribute {}
473 #
474  
475 #-------------------------------------------------------------------------------
476  
477 30 1;
↑ Summary SVG/Element.pm »