Lite/Element.pm

filter: Statementstmt 51.2% 61 / 119 Branchbran 25.0% 5 / 20 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 44.0% 11 / 25 Podpod 25.0% 1 / 4 totaltotal 41.9% 81 / 193 CC 20 SCAR 25.2
CC
20
Cov
45%
STORE
56.0
UNSHIFT
12.0
appendChildren
8.5
CRAP 12.4
« Lite/Document.pm ↑ Summary Lite/Header.pm »
1 package TEI::Lite::Element;
2  
3 ##==================================================================##
4 ## Libraries and Variables ##
5 ##==================================================================##
6  
7 6 require 5.006;
8 6 require Exporter;
9  
10 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use I18N::LangTags qw( is_language_tag );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $VERSION = "0.60";
17  
18 6 our @EXPORT = qw( %TEI_ELEMENT );
19  
20 6 our @ISA = qw( Exporter XML::LibXML::Element );
21  
22 ## Global array containing all of the global attributes of TEI Lite elements.
23 6 our @G_ATTR = qw( ana corresp id lang n next prev rend );
24  
25 ## Global hash that contains all of the TEI Lite elements and their
26 ## associated attributes.
27 6 our %TEI_ELEMENT = (
28 'abbr' => [ 'type', 'expan' ],
29 'add' => [ 'place' ],
30 'address' => [],
31 'addrLine' => [],
32 'anchor' => [],
33 'argument' => [],
34 'author' => [],
35 'authority' => [],
36 'availability' => [ 'status' ],
37 'back' => [],
38 'bibl' => [],
39 'biblFull' => [],
40 'biblScope' => [],
41 'body' => [],
42 'byline' => [],
43 'catDesc' => [],
44 'category' => [],
45 'catRef' => [ 'target' ],
46 'cell' => [ 'role', 'cols', 'rows' ],
47 'change' => [],
48 'cit' => [],
49 'classCode' => [ 'scheme' ],
50 'classDecl' => [],
51 'closer' => [],
52 'code' => [],
53 'corr' => [ 'sic', 'resp', 'cert' ],
54 'creation' => [],
55 'date' => [ 'calendar', 'value' ],
56 'dateline' => [],
57 'del' => [ 'type', 'status', 'hand' ],
58 'distributer' => [],
59 'div' => [ 'type' ],
60 'div0' => [ 'type' ],
61 'div1' => [ 'type' ],
62 'div2' => [ 'type' ],
63 'div3' => [ 'type' ],
64 'div4' => [ 'type' ],
65 'div5' => [ 'type' ],
66 'div6' => [ 'type' ],
67 'div7' => [ 'type' ],
68 'divGen' => [ 'type' ],
69 'docAuthor' => [],
70 'docDate' => [],
71 'docEdition' => [],
72 'docImprint' => [],
73 'docTitle' => [],
74 'edition' => [],
75 'editionStmt' => [],
76 'editor' => [ 'role' ],
77 'editorialDecl' => [],
78 'eg' => [],
79 'emph' => [],
80 'encodingDesc' => [],
81 'epigraph' => [],
82 'extent' => [],
83 'figDesc' => [],
84 'figure' => [ 'entity', 'url' ],
85 'fileDesc' => [],
86 'foreign' => [],
87 'formula' => [ 'notation' ],
88 'front' => [],
89 'funder' => [],
90 'gap' => [ 'desc', 'resp' ],
91 'gi' => [],
92 'gloss' => [ 'target' ],
93 'group' => [],
94 'head' => [],
95 'hi' => [],
96 'ident' => [],
97 'idno' => [ 'type' ],
98 'imprint' => [],
99 'index' => [ 'level1', 'level2', 'level3',
100 'level4', 'index' ],
101 'interp' => [ 'type', 'value', 'resp', 'inst' ],
102 'interpGrp' => [],
103 'item' => [],
104 'keywords' => [ 'scheme' ],
105 'kw' => [],
106 'l' => [ 'part' ],
107 'label' => [],
108 'langUsage' => [],
109 'lb' => [ 'ed' ],
110 'lg' => [],
111 'list' => [ 'type' ],
112 'listBibl' => [],
113 'mentioned' => [],
114 'milestone' => [ 'ed', 'unit' ],
115 'name' => [ 'type', 'key', 'reg' ],
116 'note' => [ 'type', 'resp', 'place',
117 'target', 'targetEnd', 'anchored' ],
118 'noteStmt' => [],
119 'num' => [ 'type', 'value' ],
120 'opener' => [],
121 'orig' => [ 'reg', 'resp' ],
122 'p' => [ 'type' ],
123 'pb' => [],
124 'principal' => [],
125 'profileDesc' => [],
126 'projectDesc' => [],
127 'ptr' => [ 'type', 'target', 'targType',
128 'crDate', 'resp' ],
129 'publicationStmt' => [],
130 'publisher' => [],
131 'pubPlace' => [],
132 'q' => [ 'type', 'who' ],
133 'ref' => [ 'type', 'target', 'targType',
134 'crDate', 'resp' ],
135 'refsDecl' => [],
136 'reg' => [ 'orig', 'resp' ],
137 'rendition' => [],
138 'resp' => [],
139 'respStmt' => [],
140 'revisionDesc' => [],
141 'row' => [ 'role' ],
142 'rs' => [ 'type', 'key', 'reg' ],
143 's' => [ 'type' ],
144 'salute' => [],
145 'samplingDecl' => [],
146 'seg' => [ 'type' ],
147 'series' => [],
148 'seriesStmt' => [],
149 'sic' => [ 'corr', 'resp', 'cert' ],
150 'signed' => [],
151 'soCalled' => [],
152 'sourceDesc' => [],
153 'sp' => [ 'who' ],
154 'speaker' => [],
155 'sponsor' => [],
156 'stage' => [ 'type' ],
157 'table' => [ 'rows', 'cols' ],
158 'tagsDecl' => [],
159 'tagUsage' => [ 'gi', 'occurs' ],
160 'taxonomy' => [],
161 'teiHeader' => [],
162 'term' => [],
163 'text' => [],
164 'textClass' => [],
165 'time' => [ 'value' ],
166 'title' => [ 'type', 'level' ],
167 'titlePage' => [],
168 'titlePart' => [ 'title' ],
169 'titleStmt' => [],
170 'trailer' => [],
171 'unclear' => [ 'reason', 'resp' ],
172 'xptr' => [ 'target', 'type', 'targType', 'crDate',
173 'resp', 'doc', 'from', 'to', 'url' ],
174 'xref' => [ 'target', 'type', 'targType', 'crDate',
175 'resp', 'doc', 'from', 'to', 'url' ]
176 );
177  
178 6 ▶no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 ## Loop through each entry in our element hash and build a
181 ## closure for that element.
182 6 foreach my $element ( keys( %TEI_ELEMENT ) )
183 {
184 ## Add each of these elements to the default export list.
185 ## I can't use the below function because of the warnings it
186 ## will generate. What good is it?
187 #Exporter::export_tags( "tei_$element" );
188 852 push( @EXPORT, "tei_$element" );
189
190 852 ▶ *{ "tei_$element" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
191 14 my $attributes = shift;
192 14 my @children = @_;
193  
194 ## Need to set the type argument so the constructor knows
195 ## what to create.
196 14 $$attributes{ '__type__' } = $element;
197
198 ## Call the default constructor
199 14 my $node = TEI::Lite::Element->new( $attributes, @children );
200
201 14 return( $node );
202 }
203 852 }
204  
205 6 ▶use strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
206
207 ##==================================================================##
208 ## Constructor(s)/Deconstructor(s) ##
209 ##==================================================================##
210  
211 ##----------------------------------------------##
212 ## new ##
213 ##----------------------------------------------##
214 sub new
215 14 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
216 ## Pull in what type of an object we will be.
217 14 my $type = shift;
218 ## Pull in the parameters, $attributes should be an array ref
219 ## and the rest of it should be children of the element.
220 14 my $attributes = shift;
221 14 my @children = @_;
222 ## We will use an XML::LibXML::Element object as the basis for our object.
223 14 my $self = XML::LibXML::Element->new( $$attributes{__type__} );
224 ## Determine what exact class we will be blessing this instance into.
225 14 ▶ my $class = ref( $type ) || $type;
Condition33%1/3
ref $type || $type
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table33%1/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
226 ## Bless the class for it is good [tm].
227 14 bless( $self, $class );
228 ## Set the attributes of the element.
229 14 $self->setAttributes( $attributes );
230 14 $self->appendChildren( @children );
231 ## Send it back to the caller all happy like.
232 14 return( $self );
233 }
234  
235 ##----------------------------------------------##
236 ## TIEARRAY ##
237 ##----------------------------------------------##
238 sub TIEARRAY
239 0 ▶{
Subroutine TIEARRAY: not called CC=1 SCAR=6.9
240 0 my( $class, $self ) = @_;
241  
242 0 bless( $self, $class );
243  
244 0 return( $self );
245 }
246  
247 ##----------------------------------------------##
248 ## DESTROY ##
249 ##----------------------------------------------##
250 sub DESTROY
251 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
252 ## This is mainly a placeholder to keep things like mod_perl happy.
253 0 return;
254 }
255  
256 ##----------------------------------------------##
257 ## UNTIE ##
258 ##----------------------------------------------##
259 sub UNTIE
260 0 ▶{
Subroutine UNTIE: not called CC=1 SCAR=6.9
261 ## This is mainly a placeholder to keep things like mod_perl happy.
262 0 return;
263 }
264  
265 ##==================================================================##
266 ## Method(s) ##
267 ##==================================================================##
268  
269 ##----------------------------------------------##
270 ## appendChildren ##
271 ##----------------------------------------------##
272 sub appendChildren
273 14 ▶{
Subroutine appendChildren: called CC=6 SCAR=21.4
Pod undocumented
274 14 my( $self, @children ) = @_;
275  
276 ## Loop through each of the children and determine what type of
277 ## data element they are ...
278 14 foreach( @children )
279 {
280 1 ▶ if( ( defined( $_ ) ) && ( $_ ne "" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and $_ ne "") 1 0 1
Condition33%1/3
defined $_ and $_ ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_ and $_ ne ""
defined $_ $_ ne ""
Truth table33%1/3
A and B
A= defined $_B= $_ ne ""
ABresult
0X0
100
111
281 {
282 1 ▶ if( ( ref ) && ( $_->isa( "XML::LibXML::Node" ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and $_->isa('XML::LibXML::Node')) { } 0 1 1
Condition33%1/3
ref $_ and $_->isa("XML::LibXML::Node")
!ll&&!rl&&r
100
MC/DC0%0/2
ref $_ and $_->isa("XML::LibXML::Node")
ref $_ $_->isa("XML::LibXML::Node")
Truth table33%1/3
A and B
A= ref $_B= $_->isa("XML::LibXML::Node")
ABresult
0X0
100
111
283 {
284 ## If it is one of the items above, we should be able to
285 ## safely append it to our DOM tree.
286 0 $self->appendChild( $_ );
287 }
288 else
289 {
290 ## If it isn't one of the items above, assume that it is
291 ## text data.
292 1 $self->appendText( $_ );
293 }
294 }
295 }
296  
297 14 return;
298 }
299  
300 ##----------------------------------------------##
301 ## CLEAR ##
302 ##----------------------------------------------##
303 sub CLEAR
304 0 ▶{
Subroutine CLEAR: not called CC=2 SCAR=17.9
305 0 my $self = shift;
306  
307 ## Grab all the nodes of our element ...
308 0 my @childnodes = $self->childNodes;
309  
310 ## Run through all of the nodes and remove each one.
311 0 foreach( @childnodes )
312 {
313 0 $self->removeChild( $_ );
314 }
315  
316 0 return( $self );
317 }
318  
319 ##----------------------------------------------##
320 ## DELETE ##
321 ##----------------------------------------------##
322 sub DELETE
323 0 ▶{
Subroutine DELETE: not called CC=1 SCAR=6.9
324 0 my( $self, $index ) = @_;
325  
326 ## Grab all of the nodes of our element ...
327 0 my @childnodes = $self->childNodes;
328  
329 ## Remove the requested node at $index.
330 0 $self->removeChild( $childnodes[ $index ] );
331  
332 0 return;
333 }
334  
335 ##----------------------------------------------##
336 ## EXISTS ##
337 ##----------------------------------------------##
338 sub EXISTS
339 0 ▶{
Subroutine EXISTS: not called CC=2 SCAR=17.9
340 0 my( $self, $index ) = @_;
341  
342 ## Grab all of the nodes of our element ...
343 0 my @childnodes = $self->childNodes;
344  
345 ## Check to see if we have a node at the $index.
346 0 ▶ if( defined( $childnodes[ $index ] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $childnodes[$index]) { } 0 0 0
347 {
348 0 return( 1 );
349 }
350 else
351 {
352 0 return( 0 );
353 }
354 }
355  
356 ##----------------------------------------------##
357 ## EXTEND ##
358 ##----------------------------------------------##
359 sub EXTEND
360 0 ▶{
Subroutine EXTEND: not called CC=1 SCAR=6.9
361 ## We don't do anything with this in our implementataion.
362 0 return;
363 }
364  
365 ##----------------------------------------------##
366 ## FETCH ##
367 ##----------------------------------------------##
368 sub FETCH
369 0 ▶{
Subroutine FETCH: not called CC=1 SCAR=6.9
370 0 my( $self, $index ) = @_;
371  
372 ## Grab all of the nodes of our element ...
373 0 my @childnodes = $self->childNodes;
374  
375 0 return( $childnodes[ $index ] );
376 }
377  
378 ##----------------------------------------------##
379 ## FETCHSIZE ##
380 ##----------------------------------------------##
381 sub FETCHSIZE
382 0 ▶{
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
383 0 my $self = shift;
384  
385 ## Grab the number of elements attached to our element.
386 0 my @childnodes = $self->childNodes;
387 0 my $size = scalar( @childnodes );
388  
389 0 return( $size );
390 }
391  
392 ##----------------------------------------------##
393 ## POP ##
394 ##----------------------------------------------##
395 sub POP
396 0 ▶{
Subroutine POP: not called CC=1 SCAR=6.9
397 0 my $self = shift;
398  
399 ## Remove the last child and return it.
400 0 return( $self->removeChild( $self->lastChild ) );
401 }
402  
403 ##----------------------------------------------##
404 ## PUSH ##
405 ##----------------------------------------------##
406 sub PUSH
407 0 ▶{
Subroutine PUSH: not called CC=1 SCAR=6.9
408 0 my( $self, @elements ) = @_;
409  
410 ## We will just call our trusty function to do the
411 ## pushing.
412 0 $self->appendChildren( @elements );
413  
414 0 return( $self->FETCHSIZE() );
415 }
416  
417 ##----------------------------------------------##
418 ## setAttributes ##
419 ##----------------------------------------------##
420 sub setAttributes
421 14 ▶{
Subroutine setAttributes: called CC=3 SCAR=11.0
Pod undocumented
422 14 my( $self, $attributes ) = @_;
423  
424 ## Grab the type of element.
425 14 my $element = $self->nodeName;
426  
427 ## Loop through the global attributes and the element specific
428 ## attributes.
429 14 foreach( @G_ATTR, @{ $TEI_ELEMENT{ $element } } )
430 {
431 ## If it is defined in our attribute hash, then go ahead and
432 ## set it.
433 129 ▶ if( defined( $$attributes{ $_ } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes->{$_}) 4 125 129
434 {
435 4 $self->setAttribute( $_, $$attributes{ $_ } );
436 }
437 }
438  
439 14 return;
440 }
441  
442 ##----------------------------------------------##
443 ## setLang ##
444 ##----------------------------------------------##
445 sub setLang
446 1 ▶{
Subroutine setLang: called CC=2 SCAR=7.4
Pod undocumented
447 1 my( $self, $lang ) = @_;
448  
449 1 ▶ if( is_language_tag( $lang ) )
Branch: 1 branch
BranchTrueFalseTotal
if (is_language_tag($lang)) 1 0 1
450 {
451 1 $self->setAttribute( "lang", $lang );
452 1 return( 1 );
453 }
454  
455 0 return( 0 );
456 }
457  
458 ##----------------------------------------------##
459 ## SHIFT ##
460 ##----------------------------------------------##
461 sub SHIFT
462 0 ▶{
Subroutine SHIFT: not called CC=1 SCAR=6.9
463 0 my $self = shift;
464  
465 ## Grab the first element, remove it and then
466 ## send it back to the caller.
467 0 return( $self->removeChild( $self->firstChild ) );
468 }
469  
470 ##----------------------------------------------##
471 ## STORE ##
472 ##----------------------------------------------##
473 sub STORE
474 0 ▶{
Subroutine STORE: not called CC=7 SCAR=40.3
475 0 my( $self, $index, $value ) = @_;
476
477 ## Grab the number of elements attached to our element.
478 0 my @childnodes = $self->childNodes;
479 0 my $size = scalar( @childnodes );
480
481 ## Check to see if our $index is greater then our current size.
482 0 ▶ if( $index >= ( $size - 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= $size - 1) { } 0 0 0
483 {
484 ## Determine if we need to add "buffer" space to make
485 ## the insertion at the correct index.
486 0 my $blanknodes = $index - $size;
487  
488 0 ▶ for( my $i = 0; $i < $blanknodes; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $blanknodes) 0 0 0
489 {
490 0 $self->appendTextNode( " " );
491 }
492
493 ## We shall call our convience function to determine if
494 ## the data is text or another element.
495 0 $self->appendChildren( $value );
496 }
497 else
498 {
499 ## Determine what type of node we have and take the
500 ## appropriate function.
501 0 ▶ if( ( defined( $value ) ) && ( $value ne "" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "") 0 0 0
Condition0%0/3
defined $value and $value ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table0%0/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
502 {
503 0 ▶ if( ( ref( $value ) ) && ( $value->isa( "XML::LibXML::Node" ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->isa('XML::LibXML::Node')) { } 0 0 0
Condition0%0/3
ref $value and $value->isa("XML::LibXML::Node")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and $value->isa("XML::LibXML::Node")
ref $value $value->isa("XML::LibXML::Node")
Truth table0%0/3
A and B
A= ref $valueB= $value->isa("XML::LibXML::Node")
ABresult
0X0
100
111
504 {
505 0 $childnodes[$index]->replaceNode( $value );
506 }
507 else
508 {
509 0 my $node = XML::LibXML::Text->new( $value );
510 0 $childnodes[$index]->replaceNode( $node );
511 }
512 }
513 }
514  
515 0 return;
516 }
517  
518 ##----------------------------------------------##
519 ## UNSHIFT ##
520 ##----------------------------------------------##
521 sub UNSHIFT
522 0 ▶{
Subroutine UNSHIFT: not called CC=3 SCAR=24.8
523 0 my( $self, @list ) = @_;
524  
525 0 foreach( @list )
526 {
527 0 my $first = $self->firstChild;
528
529 0 ▶ if( $_->isa( "XML::LibXML::Node" ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa('XML::LibXML::Node')) { } 0 0 0
530 {
531 0 $self->insertBefore( $_, $first );
532 }
533 else
534 {
535 0 my $node = XML::LibXML::Text->new( $_ );
536 0 $self->insertBefore( $node, $first );
537 }
538 }
539  
540 0 return;
541 }
542  
543 ##==================================================================##
544 ## Internal Functions ##
545 ##==================================================================##
546  
547 ##==================================================================##
548 ## End of Code ##
549 ##==================================================================##
550 6 1;
551  
552 ##==================================================================##
553 ## Plain Old Documentation (POD) ##
554 ##==================================================================##
555  
556 __END__
« Lite/Document.pm ↑ Summary Lite/Header.pm »