blib/lib/Uplug/Data/Node.pm untested

filter: Statementstmt 0.0% 0 / 312 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 54 Podpod 0.0% 0 / 52 totaltotal 0.0% 0 / 527 CC 48 SCAR 25.9
CC
48
Cov
0%
setData
42.0
data
42.0
content
30.0
CRAP 13.3
1 ###########################################################################
2 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #
18 # $Author$
19 # $Id$
20 #
21 #
22  
23 package Uplug::Data::Node;
24  
25 use strict;
26  
27 use vars qw($DEBUG %CREATED %DESTROYED %CHILDREN %ALIVE);
28  
29 $DEBUG=0;
30 %CREATED;
31 %DESTROYED;
32 %CHILDREN;
33 %ALIVE;
34  
35 sub new{
36 my $class=shift;
37 my $self={};
38 bless $self,$class;
39 $self->init(@_);
40 if ($DEBUG){
41 $CREATED{$self}=1;
42 $ALIVE{$self}=1;
43 if (not ((scalar keys %CREATED) % 100)){
44 print STDERR scalar keys %DESTROYED;
45 print STDERR '/';
46 print STDERR scalar keys %CREATED;
47 print STDERR ' - ';
48 print STDERR scalar keys %CHILDREN;
49 print STDERR ' - ';
50 print STDERR scalar keys %ALIVE;
51 print STDERR "\n";
52 }
53 }
54 return $self;
55 }
56  
57 sub init{
58 my $self=shift;
59 my $label=shift;
60 my $attr=shift;
61 my $parent=shift;
62  
63 $self->{LABEL}=$label;
64 if (ref($attr) eq 'HASH'){
65 %{$self->{ATTR}}=%{$attr};
66 }
67 else{
68 $self->{ATTR}={};
69 }
70 $self->{PARENT}=$parent;
71 $self->delContent();
72 @{$self->{CONTENT}}=@_; # text or children-nodes
73 }
74  
75 sub root{
76 return $_[0];
77 }
78  
79 sub parent{
80 my $self=shift;
81 return $self->{PARENT};
82 }
83  
84 sub setParent{
85 my $self=shift;
86 $self->{PARENT}=$_[0];
87 }
88  
89 sub label{
90 my $self=shift;
91 return $self->{LABEL};
92 }
93  
94 sub setLabel{
95 my $self=shift;
96 $self->{LABEL}=$_[0];
97 }
98  
99 sub attributes{
100 my $self=shift;
101 return $self->{ATTR};
102 }
103  
104 sub attribute{
105 my $self=shift;
106 return $self->{ATTR}->{$_[0]};
107 }
108  
109 sub setAttribute{
110 my $self=shift;
111 while(@_){
112 my $key=shift;
113 $self->{ATTR}->{$key}=shift;
114 }
115 }
116  
117 sub delAttribute{
118 my $self=shift;
119 delete $self->{ATTR}->{$_[0]};
120 }
121  
122 sub children{
123 my $self=shift;
124 if (ref($self->{CONTENT}) ne 'ARRAY'){$self->{CONTENT}=[];}
125 return wantarray ? @{$self->{CONTENT}} : $self->{CONTENT};
126 }
127  
128 sub nrChildren{
129 my $self=shift;
130 return $#{$self->{CONTENT}};
131 }
132  
133 sub child{
134 my $self=shift;
135 my $nr=shift;
136 if (not defined $nr){$nr=0;} # no nr --> first child!
137 if ($nr>$#{$self->{CONTENT}}){return undef;} # nr > number of children
138 return $self->{CONTENT}->[$nr];
139 }
140  
141 sub hasChildren{
142 my $self=shift;
143 return scalar @{$self->{CONTENT}};
144 }
145  
146  
147 #----------------------------------------------------------
148 # setData ..... read a complex data structure (produced by self->data)
149 # and build a node-tree
150 #----------------------------------------------------------
151  
152 sub setData{
153 my $self=shift;
154 my $data=shift;
155 if (ref($data) ne 'HASH'){return;}
156 my %attr=();
157 foreach (keys %{$data}){
158 if ($_!~/^\#/){$attr{$_}=$data->{$_};}
159 }
160 $self->delContent(); # delete old children
161 $self->setLabel($data->{'#label'}) if ($data->{'#label'}); # set label
162 $self->setAttribute(%attr); # set attributes
163 $self->addTextNode($data->{'#text'}) if ($data->{'#text'});# add text
164 if (ref($data->{'#children'}) eq 'ARRAY'){ # add children
165 foreach my $c (@{$data->{'#children'}}){
166 my $n=$self->addContent($self->createNode());
167 $n->setData($c);
168 }
169 }
170 }
171  
172 #----------------------------------------------------------
173 # data ..... put all data in one complex data-structure
174 #----------------------------------------------------------
175 #
176 # for example:
177 #
178 # %returndata = (
179 # '#label' => 'align', # root label
180 # 'id' => '1', # attribute of root
181 # '#children' => [ # children array
182 # {'#label' => 'source', # label of first child
183 # '#children' => [ # children of first child
184 # {'#label' => 's', # label
185 # 'id' => 's1.1', # attribute
186 # 'lang' => 'swedish' # attribute
187 # '#children' => [
188 # {
189 # '#label' => 'w', # label
190 # '#text' => 'REGERINGSF?RKLARING', # text-children only!
191 # 'id' => 'w1.1.1', # attribute
192 # },
193 # {
194 # '#label' => 'w',
195 # '#text' => '.',
196 # 'id' => 'w1.1.2',
197 # },
198 # ],
199 # }
200 # ] # end of children of first child
201 # } # end of first child
202 # ] # end of children array
203 # } # end of root
204  
205  
206 sub data{
207 my $self=shift;
208 my $attr=$self->attributes();
209 my $label=$self->label();
210 my %hash=%{$attr};
211 if ($label){$hash{'#label'}=$label;}
212 if ($self->hasChildren()){
213 $hash{'#children'}=[];
214 my $idx=0; # children index
215 my $textonly=1; # text-only flag
216 foreach my $c ($self->children()){
217 if ($c->isTextNode()){$hash{'#children'}[$idx]=$c->text();}
218 else{$textonly=0;%{$hash{'#children'}[$idx]}=$c->data();}
219 $idx++;
220 }
221 if ($textonly){
222 $hash{'#text'}=join '',@{$hash{'#children'}};
223 delete $hash{'#children'};
224 }
225 }
226 return wantarray ? %hash : \%hash;
227 }
228  
229 # end of data
230 #----------------------------------------------------------
231  
232  
233 sub index{
234 my $self=shift;
235 my $child=shift;
236 my $idx=0;
237 foreach ($self->children){
238 if ($_ eq $child){return $idx;}
239 $idx++;
240 }
241 return -1;
242 }
243  
244  
245 sub setContent{
246 my $self=shift;
247 $self->delContent();
248 return $self->addContent(@_);
249 }
250  
251 sub delContent{
252 my $self=shift;
253 # my $level=shift;
254 foreach my $c (@{$self->{CONTENT}}){
255 if (not ref($c)){next;}
256 # $c->setParent(undef);
257 # $level++;
258 # $c->DESTROY($level);
259 $c->DESTROY();
260 if ($DEBUG){$CHILDREN{$c}=1;}
261 }
262 $self->{CONTENT}=[];
263 }
264  
265 sub lastNode{
266 my $self=shift;
267 if (ref($self->{CONTENT}) ne 'ARRAY'){return undef;}
268 return $self->{CONTENT}->[-1];
269 }
270  
271 sub addTextNode{
272 my $self=shift;
273 my $text=shift;
274 my $last=$self->lastNode();
275 if (ref($last) and $last->isTextNode){ # this is quite a hack!
276 $last->setText($last->text().' '.$text); # (adds spaces!!!!)
277 return $last;
278 }
279 my $child=new Uplug::Data::Node('#text',{'#text' => $text},$self);
280 return $self->addContent($child);
281 }
282  
283 sub createTextNode{
284 my $self=shift;
285 my $text=shift;
286 return new Uplug::Data::Node('#text',{'#text' => $text},$self);
287 }
288  
289 sub isTextNode{
290 my $self=shift;
291 return ($self->label eq '#text');
292 }
293  
294 sub text{
295 my $self=shift;
296 return $self->attribute('#text');
297 }
298  
299 sub setNodeValue{
300 my $self=shift;
301 if ($self->isTextNode){
302 return $self->setAttribute('#text',$_[0]);
303 }
304 }
305  
306 sub setText{
307 my $self=shift;
308 return $self->setAttribute('#text',$_[0]);
309 }
310  
311  
312 sub addElement{
313 my $self=shift;
314 my $label=shift;
315 my $attr=shift;
316  
317 my $child=new Uplug::Data::Node($label,$attr,$self);
318 return $self->addContent($child);
319 }
320  
321 sub createElement{
322 my $self=shift;
323 return $self->createNode(@_);
324 }
325  
326 sub createNode{
327 my $self=shift;
328 my $label=shift;
329 my $attr=shift;
330  
331 return new Uplug::Data::Node($label,$attr,$self);
332 }
333  
334  
335 sub addContent{
336 my $self=shift;
337  
338 foreach my $c (@_){
339 if (not ref($c)){next;}
340 push (@{$self->{CONTENT}},$c); # --> push a new content element
341 $c->setParent($self);
342 }
343 return $self->{CONTENT}->[-1]; # return last content-element!
344 }
345  
346 sub appendChild{
347 my $self=shift;
348 return $self->addContent(@_);
349 }
350  
351 sub insertBefore{
352 my $self=shift;
353 my $child=shift;
354 my $node=shift;
355 my @tmp=@{$self->{CONTENT}};
356 my @new=();
357 foreach (@tmp){
358 if ($_ eq $node){
359 push (@new,$child);
360 }
361 push (@new,$_);
362 }
363 @{$self->{CONTENT}}=@new;
364 $child->setParent($self);
365 return $child;
366 }
367  
368  
369 sub content{
370 my $self=shift;
371 my $del=shift;
372 if ($self->isTextNode){return $self->text;}
373 my @content=();
374 foreach my $c ($self->children){
375 if (not ref($c)){next;}
376 if ($c->isTextNode){push(@content,$c->text());}
377 else{
378 my @children=$c->content($del);
379 push(@content,@children);
380 }
381 }
382 return wantarray ? @content : join ($del,@content);
383 # return join $del,@content;
384 }
385  
386  
387 sub normalize{
388 my $self=shift;
389 # check children nodes and put contiguous text nodes together
390 }
391  
392 sub isAncestor{
393 my $self=shift;
394 my $node=shift;
395  
396 my $parent=$self->parent;
397 while (defined $parent){
398 if ($parent eq $node){return 1;}
399 $parent=$parent->parent;
400 }
401 return 0;
402 }
403  
404  
405 sub detach{
406 my $self=shift;
407 my $parent=$self->parent;
408 if (ref($parent)){
409 return $parent->removeChild($self);
410 }
411 return undef;
412 }
413  
414 sub removeChild{
415 my $self=shift;
416 my $child=shift;
417 if (ref($child) and (ref($self->{CONTENT}) eq 'ARRAY')){
418 my $idx=$self->index($child);
419 if ($idx>=0){
420 splice(@{$self->{CONTENT}},$self->index($child),1);
421 # print STDERR "remove ...\n";
422 $child->setParent(undef);
423 return $child;
424 }
425 }
426 return undef;
427 }
428  
429  
430 sub replaceChild{
431 my $self=shift;
432 my $new=shift;
433 my $old=shift;
434  
435 if (ref($old)){
436 $self->{CONTENT}->[$self->index($old)]=$new;
437 # print STDERR "replace ...\n";
438 $new->setParent($self);
439 $old->setParent(undef);
440 return 1;
441 }
442 return 0;
443 }
444  
445  
446  
447  
448 sub matchAttr{
449 my $self=shift;
450 my $pattern=shift;
451  
452 my $attr=$self->attributes;
453 if ((ref($attr) eq 'HASH') and (ref($pattern) eq 'HASH')){
454 foreach my $k (keys %{$pattern}){
455 if (not defined $attr->{$k}){
456 return 0;
457 }
458 if (defined $pattern->{$k}){
459 my $p=quotemeta($pattern->{$k});
460 if ($attr->{$k}!~/^$p$/){
461 return 0;
462 }
463 }
464 }
465 return 1;
466 }
467 return 0;
468 }
469  
470  
471  
472  
473  
474  
475  
476  
477 sub DESTROY{
478 my $self=shift;
479 # my $level=shift;
480 # if ($level){print STDERR "$level\n";}
481 if ($DEBUG){
482 $DESTROYED{$self}=1;
483 delete $ALIVE{$self};
484 }
485 # $self->delContent($level);
486 $self->delContent();
487 %{$self->{ATTR}}=();
488 delete $self->{CONTENT};
489 delete $self->{ATTR};
490 delete $self->{PARENT};
491 }
492  
493  
494  
495 ########################################################################
496 ########################################################################
497 ########################################################################
498 #
499 # DOM-like methods
500 #
501  
502 # getElementsByTagName
503 sub getElementsByTagName{
504 my $self=shift;
505 my ($label,$nodes)=@_;
506 if (not ref($nodes)){$nodes=[];}
507 if ($label eq '*'){push (@{$nodes},$self);}
508 elsif ($self->label() eq $label){push (@{$nodes},$self);}
509 foreach my $c ($self->children()){
510 $c->getElementsByTagName($label,$nodes);
511 }
512 return @{$nodes};
513 }
514  
515 sub getNodeValue{ # ... not really shure if this is correct ...
516 my $self=shift;
517 if ($self->isTextNode){return $self->text();}
518 }
519  
520 sub getNodeName{return $_[0]->label();}
521 sub setTagName{return $_[0]->setLabel($_[1]);} # not standard-DOM!
522 sub getParentNode{return $_[0]->parent();}
523 sub hasChildNodes{return $_[0]->hasChildren();}
524 sub getFirstChild{return $_[0]->child(0);}
525 sub getLastChild{return $_[0]->child(-1);}
526  
527  
528 sub getChildNodes{ # is that DOM?
529 my $self=shift;
530 return $self->children(@_);
531 }
532  
533 sub getPreviousSibling{
534 my $self=shift;
535 my $parent=$self->parent();
536 if (ref($parent)){
537 my $idx=$parent->index($self);
538 if ($idx>0){
539 return $parent->child($idx-1);
540 }
541 }
542 return undef;
543 }
544  
545 sub getNextSibling{
546 my $self=shift;
547 my $parent=$self->parent();
548 if (ref($parent)){
549 my $idx=$parent->index($self);
550 if ($idx>=0){
551 return $parent->child($idx+1);
552 }
553 }
554 return undef;
555 }
556  
557  
558  
559  
560  
561 1;