blib/lib/Uplug/Data.pm untested

filter: Statementstmt 0.0% 0 / 632 Branchbran 0.0% 0 / 306 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 58 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 1072 CC 154 SCAR 52.3
CC
154
Cov
0%
makeStep
992.0
splitContent
182.0
addParent
156.0
CRAP 186.8
1 #-*-perl-*-
2 #####################################################################
3 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # $Author$
20 # $Id$
21 #
22 ###########################################################################
23 #
24 ###########################################################################
25  
26  
27 package Uplug::Data;
28  
29 use strict;
30 use vars qw( @ISA @EXPORT $DEFAULTDELIMITER $DEFAULTROOTLABEL);
31 use Exporter;
32 use Data::Dumper;
33  
34 use Uplug::Data::Node;
35 use Uplug::Encoding;
36 use Encode;
37  
38  
39 @ISA=qw( Exporter);
40 @EXPORT = qw( $DEFAULTDELIMITER $DEFAULTROOTLABEL);
41  
42 $DEFAULTDELIMITER=' ';
43 $DEFAULTROOTLABEL='text';
44  
45  
46 sub DESTROY{
47 my $self=shift;
48 if (ref($self->{ROOT})){$self->{ROOT}->DESTROY();}
49 delete $self->{ROOT};
50 }
51  
52  
53 sub new{
54 my $class=shift;
55 my $self={};
56 bless $self,$class;
57 $self->init(@_);
58 return $self;
59 }
60  
61 #---------------------------------
62 # init(label,attributes,option => value, ...)
63 # label ..... data root label
64 # attr ...... root node attributes
65 # options:
66 #
67  
68  
69 sub init{
70 my $self=shift;
71 my ($label,$attr,%options)=@_;
72 if (defined $label){$self->setRoot($self->createNode($label,$attr));}
73 else{$self->setRoot(undef);}
74 foreach (keys %options){$self->{$_}=$options{$_};}
75 }
76  
77 #---------------------------------------------
78  
79 sub option{return $_[0]->{$_[1]};} # get object-property
80 sub setOption{$_[0]->{$_[1]}=$_[2];} # set object-property
81  
82 #---------------------------------------------
83  
84 sub root{return $_[0]->{ROOT};} # get root node
85 sub getRootNode{return $_[0]->root();} # get root node
86 sub setRoot{
87 if (ref($_[0]->{ROOT})){ # if there's a root node
88 $_[0]->{ROOT}->DESTROY(); # destroy it!
89 }
90 delete $_[0]->{ROOT};
91 $_[0]->{ROOT}=$_[1]; # set the new root node
92 }
93  
94 #----------------------------------------------
95  
96 sub header{return $_[0]->{HEADER};} # get data header (string)
97 sub setHeader{$_[0]->{HEADER}=$_[1];} # set data header
98 sub addHeader{$_[0]->{HEADER}.=$_[1];} # add string to data header
99  
100 sub tail{return $_[0]->{TAIL};} # data tail (string)
101 sub setTail{$_[0]->{TAIL}=$_[1];}
102 sub addTail{$_[0]->{TAIL}.=$_[1];}
103  
104 #--------------------------------------------------
105 # parent($NODE) ............... return parent of node $NODE
106 # setParent($NODE,$PARENT) .... set parent of node $NODE
107  
108 sub parent{if (ref($_[1])){return $_[1]->parent();}}
109 sub setParent{if (ref($_[1])){return $_[1]->setParent($_[2]);}}
110  
111 #--------------------------------------------------------------------
112 # createNode
113 # createNode(label,attr,content,parent)
114 # label ..... data label
115 # attr ...... optional reference to an attribute hash
116 # content ... optional content string
117 # parent .... optional reference to parent node
118 #
119  
120 sub createNode{
121 my ($self,$label,$attr,$content,$parent)=@_;
122 my $node=Uplug::Data::Node->new($label,$attr,$parent);
123 if (defined $content){$node->addTextNode($content);}
124 return $node;
125 }
126  
127 #--------------------------------------------------------------------
128 # createTextNode
129 #
130  
131 sub createTextNode{
132 my ($self,$content,$node)=@_;
133 if (not ref($node)){$node=Uplug::Data::Node->new();}
134 return $node->createTextNode($content);
135 }
136  
137 #--------------------------------------------------------------------
138 # delNode .... detach a node from its parent
139  
140 sub delNode{
141 my $self=shift;
142 my $node=shift;
143 if (not ref($node)){return undef;}
144 $node->detach();
145 }
146  
147  
148 #--------------------------------------------------------------------
149 # addNode
150 # 1) addNode(child) ................... add a child to the root node
151 # 2) addNode(parent,child) ............ add a child to parent
152 # 3) addNode(label,attr,content) ...... add a new child node to the root node
153 # 4) addNode(parent,label,attr,content) add a new child node to parent
154 #
155  
156 sub addNode{
157 my $self=shift;
158  
159 my $parent;
160 if (ref($_[0]) and (@_==1)){$parent=$self->root();} # 1) child to root
161 elsif (not ref($_[0])){$parent=$self->root();} # 3) get root
162 else{$parent=shift;} # 2) & 4)
163  
164 my @nodes=();
165 if (ref($_[0])){@nodes=@_;} # all arguments are children nodes
166 else{$nodes[0]=$self->createNode(@_);} # create a new child-node
167  
168 my @added=();
169 if (not ref($parent)){ # no parent? --> there is no root!
170 $self->setRoot(shift(@nodes)); # --> set first node as root
171 push (@added,$self->root());
172 }
173 foreach my $n (@nodes){ # add all nodes
174 push (@added,$parent->addContent($n));
175 $self->setParent($n,$parent); # set parent
176 }
177 return wantarray ? @added : $added[-1];
178 }
179  
180  
181  
182 #--------------------------------------------------------------------
183 # findNodes .... find nodes with certain labels and attributes
184 #
185 # 1) @nodes = $data->findNodes()
186 # 2) @nodes = $data->findNodes($labelRE)
187 # 3) @nodes = $data->findNodes($labelRE,\%attr)
188 #
189 #
190 # $labelRE: label pattern (regular expression)
191 # %attr: attribute value pairs for matching a node (hash)
192 #
193 # @nodes: array of nodes
194 #
195  
196  
197 sub findNodes{
198 my $self=shift;
199 my ($labelRE,$attr,$node,$match)=@_;
200  
201 if (not defined $labelRE){$labelRE='.*';} # default: match all!
202 if (not defined $node){$node=$self->root();} # start at root
203 if (not defined $node){return undef;} # no root node found!
204 if (not defined $match){$match=[];} # initialize return-array
205  
206 my $label=$node->label(); # get the node-label
207 if ($label=~/^$labelRE$/){ # and check it
208 if ((ref($attr) ne 'HASH') or # if the attribute-pattern
209 ($node->matchAttr($attr))){ # is set --> try to
210 push (@{$match},$node); # match it
211 }
212 }
213 foreach my $c ($node->children()){ # check all children nodes
214 if (not ref($c)){next;} # (only if not scalar)
215 $self->findNodes($labelRE,$attr,$c,$match);
216 }
217 return @{$match};
218 }
219  
220 #--------------------------------------------------------------------
221 # delNodes ..... delete nodes
222 #
223 # 1) delNodes(@NODES) .......... delete all nodes in @NODES
224 # 2) delNodes(labelRE,$attr).... delete all nodes which match labelRE & attr
225 # (uses findNodes)
226 #
227  
228 sub delNodes{
229 my $self=shift;
230  
231 my @nodes=();
232 if (ref($_[0])){@nodes=@_;}
233 else{@nodes=$self->findNodes(@_);}
234 foreach my $n (@nodes){$self->delNode($n);}
235 }
236  
237 #--------------------------------------------------------------------
238 # contentNodes .... return all content nodes
239 #
240 # @nodes = $data->contentNodes()
241 #
242 #
243  
244 sub contentNodes{
245 my $self=shift;
246 my ($node,$match)=@_;
247  
248 if (not ref($node)){$node=$self->root();} # no node --> root node
249 if (not ref($node)){return undef;} # no root node --> return
250 if (not defined $match){$match=[];} # initialize return-array
251 if ($node->isTextNode()){ # check if the current
252 push (@{$match},$node); # node is a text node
253 }
254 foreach my $c ($node->children()){ # check all children nodes
255 if (not ref($c)){next;} # (only if not scalar)
256 $self->contentNodes($c,$match);
257 }
258 return @{$match};
259 }
260  
261 #--------------------------------------------------------------------
262 # contentElements .... return all nodes that contain a text-node!
263 #
264 # @nodes = $data->contentElements()
265 #
266 #
267  
268 sub contentElements{
269 my $self=shift;
270  
271 my @nodes=$self->contentNodes(@_);
272 my @elements=();
273 foreach my $n (@nodes){
274 if (not ref($n)){next;}
275 my $parent=$n->parent();
276 if ($parent){push (@elements,$parent);}
277 }
278 return @elements;
279 }
280  
281  
282 sub contentNodesEncoded{
283 my $data = shift;
284 my $encoding = shift;
285 my $tokens = shift;
286  
287 my @nodes=$data->contentElements;
288 # my @nodes=$data->findNodes('w');
289  
290 unless ($encoding=~/utf-?8/i){
291 my @content = ();
292 $tokens = [] unless (ref($tokens) eq 'ARRAY');
293 foreach my $c (@nodes){
294 next unless (ref($c));
295 my $str = decode($encoding, encode($encoding, $c->content,sub{ return '' }) );
296 if ( $str=~/\S/ ){
297 push(@content,$c);
298 push(@{$tokens},$str);
299 }
300 }
301 return @content;
302 }
303 @{$tokens}=$data->content(\@nodes) if (ref($tokens) eq 'ARRAY');
304 return @nodes;
305 }
306  
307  
308  
309 #--------------------------------------------------------------------
310 # attribute .... return value of a node attribute
311 #
312 # 1) attribute(node,key) ..... return value of attribute 'key' in node
313 # 2) attribute(key) .......... return value of attribute 'key' in root-node
314 # 3) attribute(\@nodes,key) .. return values of attribute 'key' in all nodes
315 #
316 # if key is undefined ---> return reference to attribute-value hash!
317  
318  
319 sub attribute{
320 my $self=shift;
321  
322 if (ref($_[0]) eq 'ARRAY'){ # 3) first argument = node-array
323 my $nodes=shift;my @attr=(); # --> collect all attributes
324 foreach my $n (@{$nodes}){ # from all nodes
325 push (@attr,$self->attribute($n,@_));
326 }
327 return wantarray ? @attr : \@attr;
328 }
329 my $node;
330 if (ref($_[0])){$node=shift;} # 1) node is psecified
331 else{$node=$self->root();} # 2) node=root-node
332 if (not ref($node)){return undef;} # no node found -> return undef
333  
334 if (defined $_[0]){return $node->attribute($_[0]);} # return attr.-value
335 else{return $node->attributes();} # return attr.-hash
336
337 }
338  
339  
340 #--------------------------------------------------------------------
341 # setAttribute .... set attribute value in a node
342 #
343 # 1) setAttribute(node,key,value,...) ..... set attributes in node 'node'
344 # 2) setAttribute(key,value,...) .......... set attributes in root-node
345 # 3) setAttribute($node,\%hash) ........... set attribute 'key' in 'node'
346 # 4) setAttribute(\%hash) ................. set attribute 'key' in root-node
347  
348  
349 sub setAttribute{
350 my $self=shift;
351  
352 my $node;
353 if (ref($_[0]) and (ref($_[0]) ne 'HASH')){$node=shift;} # 1) node as arg.
354 else{$node=$self->root();} # 2) root-node
355  
356 if (not ref($node)){$node=$self->addNode();}# undefined node -> new root!
357 if (not ref($node)){return undef;} # still undefined? -> return
358 my %attr=();
359 if (ref($_[0]) eq 'HASH'){%attr=%{$_[0]};} # first argument=hash-reference
360 else{%attr=@_;} # arguments=attribute-hash
361 return $node->setAttribute(%attr); # set attributes of the node
362 }
363  
364 #--------------------------------------------------------------------
365 # setAttributes .... set attributes in several nodes
366 #
367 # setAttributes(\@nodes,@attr)
368 #
369 # @nodes ..... array of nodes
370 # @attr ...... array of attribute-value hashs
371  
372 sub setAttributes{
373 my $self=shift;
374  
375 my $nodes=shift;
376 my $attr=shift;
377  
378 if (ref($nodes) ne 'ARRAY'){return;}
379 if (ref($attr) ne 'ARRAY'){return;}
380  
381 foreach (0..$#{$nodes}){
382 if (ref($attr->[$_]) ne 'HASH'){next;}
383 $self->setAttribute($nodes->[$_],%{$attr->[$_]});
384 }
385 }
386  
387 #--------------------------------------------------------------------
388 # setContentAttribute .... set attribute value in content nodes
389 #
390 # setContentAttribute($attr,@values)
391 # setContentAttribute($attr,\@values)
392 #
393  
394  
395 sub setContentAttribute{
396 my $self=shift;
397 my $attr=shift;
398  
399 my @val=@_;
400 if (ref($_[0]) eq 'ARRAY'){@val=@{$_[0]};}
401 my @nodes=$self->contentElements();
402 foreach my $n (@nodes){
403 $self->setAttribute($n,$attr,shift(@val));
404 if (not @val){last;}
405 }
406 }
407  
408 #--------------------------------------------------------------------
409 # delAttribute .... delete an attribute in a node
410 #
411 # 1) delAttribute(node,key) ..... delete attribute 'key' in node 'node'
412 # 2) delAttribute(key) .......... delete attribute 'key' in root-node
413  
414  
415 sub delAttribute{
416 my $self=shift;
417  
418 my $node;
419 if (ref($_[0])){$node=shift;} # 1) node is psecified
420 else{$node=$self->root();} # 2) node=root-node
421 if (not ref($node)){return undef;} # 3) no node found -> return undef
422  
423 return $node->delAttribute(@_); # set attribute-value of the node
424 }
425  
426  
427 #--------------------------------------------------------------------
428 # content ...... return content-string
429 #
430 # 1) content(@NODES) ........ return content of all nodes @NODES
431 # 2) content(\@NODES) ....... return content of all nodes @NODES
432 #
433 # if @NODES is undefined --> return content of root-node
434 #
435 # return value:
436 # @content ..... 'array of content strings' in array context
437 # $content ..... content-string for all nodes joined together
438 # (using CONTENTDELIMITER string)
439 #
440  
441 sub content{
442 my $self=shift;
443  
444 my @nodes=();
445 if (ref($_[0]) eq 'ARRAY'){@nodes=@{$_[0]};}
446 elsif (ref($_[0])){@nodes=@_;}
447 else{$nodes[0]=$self->root();}
448  
449 my $del=$DEFAULTDELIMITER;
450 my @content=();
451 foreach my $n (@nodes){
452 if (not ref($n)){next;}
453 my @children=$n->content($del);
454 push (@content,@children);
455 }
456 return wantarray ? @content : join ($del,@content);
457 }
458  
459  
460 #--------------------------------------------------------------------
461 # setContent
462 #
463 # 1) setContent($string,$label) ........ set content in root node
464 # 2) setContent($node,$string,$label) ... set content in specific $node
465 #
466 # string ...... content string (create a text node as one and only child-node)
467 # label ....... optional! change label of the node
468  
469  
470 sub setContent{
471 my $self=shift;
472  
473 my $node;
474 if (ref($_[0])){$node=shift;} # 1st argument = reference -> node-reference
475 else{$node=$self->root();} # else -> get root-node
476 my $string=shift;
477  
478 if (not ref($node)){
479 $node=$self->createNode($DEFAULTROOTLABEL);
480 $self->setRoot($node);
481 }
482  
483 # create a new text-node as the one and only content
484 if (defined $string){$node->setContent($node->createTextNode($string));}
485  
486 # or delete all content
487 else{$node->delContent();}
488  
489 # set a label if specified
490 if (defined $_[0]){$node->setLabel($_[0]);}
491 }
492  
493 #-------------------------------------------
494 # addContent
495  
496 sub addContent{
497 my $self=shift;
498 my $node;
499 if (ref($_[0])){$node=shift;}
500 else{$node=$self->root();}
501 if (not ref($node)){$node->addNode(@_);} # no root node --> create new one
502 if (defined $_[0]){$node->addTextNode($_[0]);} # add a new text node
503 }
504  
505  
506  
507 #--------------------------------------------------
508 # label ...... return data label of a node
509 #
510 # 1) label(node) ........ label of node
511 # 2) label() ............ label of root node
512 #
513  
514 sub label{
515 my $self=shift;
516 my $node;
517 if (ref($_[0])){$node=shift;}
518 else{$node=$self->root;}
519 if (ref($node)){$node->label($_[0]);}
520 }
521  
522  
523 #--------------------------------------------------
524 # setLabel ...... set label of a node
525 #
526 # 1) setLabel(node,label) ....... set label of node
527 # 2) setLabel(label) ............ set label of root node
528 #
529  
530 sub setLabel{
531 my $self=shift;
532 my $node;
533 if (ref($_[0])){$node=shift;}
534 else{$node=$self->root;}
535 if (ref($node)){$node->setLabel($_[0]);}
536 }
537  
538  
539  
540  
541  
542 #--------------------------------------------------------------------
543 # taken from Object::Clone
544 # http://usr.bin.dk/~jonasbn/perl/ObjectClone.html
545 #
546 # use Data::Dumper;
547 # sub clone {
548 # my $self = shift;
549 #
550 # my $VAR1;
551 # my $copy_dump = Dumper $self;
552 # eval $copy_dump; warn $@ if $@;
553 # return $VAR1;
554 # }
555  
556 sub clone{return Uplug::Data->new();}
557 # return $_[0]->new();
558  
559 #--------------------------------------------------------------------
560 # subData .... return a sub-data object
561 #
562 # 1) $subdata=subData($subData,$labelRE,\%attr)
563 # 2) $subdata=subData($labelRE,\%attr)
564 #
565 # subdata is a clone of this object
566 # the root of subdata is the first node that matches labelRE and attr
567 # (using findNodes)
568 #
569  
570  
571 sub subData{
572 my $self=shift;
573 my $data;
574 if (ref($_[0])){$data=shift;}
575 else{$data=$self->clone();}
576 if (not ref($data)){return undef;} # could not clone myself
577 my ($node)=$self->findNodes(@_); # find first matching node
578 $data->setRoot($node); # set root-node
579 return $data; # return subdata object
580 }
581  
582  
583  
584  
585  
586  
587  
588  
589  
590  
591 #--------------------------------------------------------------------
592 # splitContent
593 #
594 # 1) @NewNodes = $object->splitContent($node,$label,\@segments)
595 # 2) @NewNodes = $object->splitContent($labeRE,\%NodeAttr,$label,\@segments)
596 #
597 # $node: parent node
598 # $label: label for new children nodes (string)
599 # @segments: array of strings to replace the former content (array)
600 # %NodeAttr: attribute-value pairs for finding a node (hash)
601 #
602 # @NewNodes: array of new nodes
603 #
604 # Note:
605 # if splitContent finds more than one node that matches $labelRE and
606 # %NodeAttr ---> splitContent splits only the first node!
607 #
608  
609  
610 sub splitContent{
611 my $self=shift;
612  
613 my $node=shift;
614 if (not ref($node)){
615 my $labelRE=$node;
616 my $attr=shift;
617 ($node)=$self->findNodes($labelRE,$attr);
618 }
619 if (not ref($node)){return ();}
620 my $label=shift;
621 my $segments=shift;
622 if (ref($segments) ne 'ARRAY'){return ();}
623  
624 #------------------------------------------------------------------------
625  
626 my @contentNodes=$self->contentNodes(); # get all content nodes
627 my @content=$self->content(@contentNodes); # and their content
628 my @children=();
629  
630 #------------------------------------------------------------------------
631  
632 foreach my $s (@{$segments}){ # foreach text-segment:
633 my @chunk=(shift @contentNodes); # - take the next text-node
634 my @chunkContent=(shift @content); # - save its content
635 my $text=$chunkContent[-1]; # - the text
636 my $pat=quotemeta($s); # - pattern of the segment
637 while ($text!~/$pat/s){ # - add more text nodes
638 if (not @contentNodes){last;} # as long as the segment
639 push(@chunkContent,shift @content); # is not completely
640 push(@chunk,shift @contentNodes); # included in the text
641 $text.=$DEFAULTDELIMITER; # (text-node deilimiter!)
642 $text.=$chunkContent[-1]; # (append new text)
643 }
644 if (not @chunk){next;} # - no nodes -> next segment
645 #------------------------------------------------------------------------
646 if ($text=~/($pat)(.*)$/s){ # - there's something behind
647 my $str1=$1; # the text-segment which
648 my $str2=$2; # has to be removed from
649 my $pat2=quotemeta($str2); # the last text-node
650 my $this=$chunkContent[-1]; # * this is the last text-node
651 # if ($this=~/^(.*)$pat2$/s){ # which has to be split
652 # $str1=$1; # between $str1 and $str2
653 # }
654 if ($this=~/(.*)$pat2$/s){ # which has to be split
655 $str1=$1; # between $str1 and $str2
656 }
657 #------------------------------------------------------------------------
658 my ($before,$after)= # split the last text-node
659 $self->splitContentNode($chunk[-1], # between str1 and str2
660 $str1, # before and after are the
661 $str2); # new text-nodes!
662 $chunk[-1]=$before; # last node in the chunk
663 $chunkContent[-1]=
664 $self->content($chunk[-1]); # and its content
665 #------------------------------------------------------------------------
666 if (defined $after){ # if there is something
667 my $afterStr=$self->content($after); # behind it
668 unshift(@contentNodes,$after); # push it into the
669 unshift(@content,$afterStr); # text-node array
670 }
671 #------------------------------------------------------------------------
672 $text=join ($DEFAULTDELIMITER,@chunkContent); # check the text of
673 my $this=quotemeta($chunkContent[0]); # the current chunk!
674 while ($text=~/$this$pat$/){ # - remove all
675 shift(@chunk); # initial nodes
676 shift(@chunkContent); # before the text-
677 if (not @chunk){last;} # segment
678 $text=join ($DEFAULTDELIMITER,@chunkContent);
679 $this=quotemeta($chunkContent[0]);
680 }
681 #------------------------------------------------------------------------
682 if (not @chunk){next;} # no node left -> next segment
683 if ($text=~/^(.+)$pat$/s){ # there's something in front
684 my $str1=$1; # of the text segment
685 my $pat=quotemeta($str1); # --> split the first text-
686 my $this=$chunkContent[0]; # node into two nodes
687 if ($this=~/^($pat)(.*)$/s){ # and remove the prefix-
688 my $str2=$2; # node from the chunk
689 my ($before,$after)=
690 $self->splitContentNode($chunk[0],$str1,$str2);
691 $chunk[0]=$after;
692 }
693 }
694  
695 #------------------------------------------------------------------------
696 # finally: add the parent-label around all nodes in the chunk
697 # if possible (check addParent)
698 #
699 push(@children,$self->addParent(\@chunk,$label));
700 }
701 }
702 return @children;
703 }
704  
705 #------------------------------------------------------------------------
706  
707 sub splitContentNode{
708 my $self=shift;
709 my ($node,$before,$after)=@_;
710 if (not ref($node)){return $node;}
711 my $parent=$node->parent();
712 if (not ref($parent)){return $node;}
713 if ((defined $before) and (defined $after)){
714 my $child=$parent->createTextNode($before);
715 my $child=$self->createTextNode($before);
716 $child=$parent->insertBefore($child,$node);
717 $node->setNodeValue($after);
718 return ($child,$node);
719 }
720 return $node;
721 }
722  
723  
724  
725 #--------------------------------------------------------------------
726 # addParent
727 #
728 # 1) insertParent(\@children,$label,\%attr)
729 # 2) insertParent($label,\%attr,$childLabelRE,$childrenAttr,$label,\%attr)
730 #
731 # $label ........ label for the new parent
732 # %attr ......... attribute-value-hash for the new parent
733 # @children ..... list of nodes to be the children of the new parent
734 # $childLabelRE . regular expression to match labels of nodes (=children)
735 # $childrenAttr . attributes to be matched with nodes (=>children)
736 #
737  
738 sub addParent{
739 my $self=shift;
740  
741 my $children;
742 my $parentLabel; # label of the new parent
743 my $parentAttr; # attribute-value hash for the parent
744 if (not ref($_[0])){
745 $parentLabel=shift;
746 $parentAttr=shift;
747  
748 #---------------------------------------
749 # childLabelRE:
750 # label of the node (reg exp)
751 # childrenAttr:
752 # array of attribute-value hashs OR
753 # hash of attribute-value pairs
754 # (for matching children-nodes)
755 # - optional!
756 #---------------------------------------
757 my ($childLabelRE,$childrenAttr)=@_;
758  
759 $children=[];
760 @{$children}=$self->findNodes($childLabelRE,$childrenAttr);
761 }
762 else{
763 $children=shift;
764 $parentLabel=shift;
765 $parentAttr=shift;
766 }
767
768 if (not @{$children}){return undef;}
769 if (not defined $children->[0]){return undef;}
770  
771 my $text=$self->content($children);
772  
773 my $root=$children->[0]->parent();
774 foreach my $i (1..$#{$children}){
775 if (not defined $children->[$i]){next;}
776 if (not defined $children->[$i-1]){next;}
777  
778 my $child1=$children->[$i-1];
779 my $child2=$children->[$i];
780  
781 ($root,$children->[$i-1],$children->[$i])=
782 $self->findCommonParent($children->[$i-1],$children->[$i]);
783 if (($child1 ne $children->[$i-1]) or
784 ($child2 ne $children->[$i])){
785  
786 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
787 # here should be some test if the common parent
788 # does not take to much content
789 # (content before <=> after ...)
790 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
791 # something like this: ...
792  
793 my ($before,$after);
794 if ($child1 ne $children->[$i-1]){
795 $before=$self->content($child1);
796 $after=$self->content($children->[$i-1]);
797 }
798 else{
799 $before=$self->content($child2);
800 $after=$self->content($children->[$i]);
801 }
802 if ($before ne $after){return undef;}
803  
804 # (addParent fails)
805 # could maybe split the parent-node ....
806 #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
807  
808 return $self->addParent($children,$parentLabel,$parentAttr);
809 }
810 if (not defined $root){last;}
811 }
812 my @childNodes=();
813 foreach my $i (0..$#{$children}){
814 if (ref($children->[$i])){
815 push(@childNodes,$children->[$i]);
816 }
817 }
818 if (defined $root){
819 return $self->insertParent($root,\@childNodes,
820 $parentLabel,$parentAttr);
821 # return $self->insertParent($root,$children,$parentLabel,$parentAttr);
822 }
823 }
824  
825 sub findCommonParent{
826 my $self=shift;
827 my ($node1,$node2)=@_;
828  
829 my $parent1=$node1->parent();
830 if (not defined $parent1){return undef;}
831 my $parent2=$node2->parent();
832 if (not defined $parent2){return undef;}
833  
834 if ($parent1 eq $parent2){
835 return ($parent1,$node1,$node2);
836 }
837 if ($parent2->isAncestor($parent1)){ # this is NOT (!!!) DOM
838 my $child=$parent1;
839 my $parent=$child->parent();
840 while ($parent ne $parent2){
841 $child=$parent;
842 if (not ref($child)){last;}
843 $parent=$child->parent();
844 }
845 return ($parent,$child,$node2);
846 }
847 if ($parent1->isAncestor($parent2)){ # this is NOT (!!!) DOM
848 my $child=$parent2;
849 my $parent=$child->parent();
850 while ($parent ne $parent1){
851 $child=$parent;
852 if (not ref($child)){last;}
853 $parent=$child->parent();
854 }
855 return ($parent,$node1,$child);
856 }
857 return $self->findCommonParent($parent1,$parent2);
858 return undef;
859 }
860  
861 #--------------------------------------------------------------------
862 # insertParent
863 #
864  
865 sub insertParent{
866 my $self=shift;
867 my ($oldParent,$children,$parentLabel,$parentAttr)=@_;
868  
869 my $newParent=$self->createNode($parentLabel,$parentAttr);
870 # $self->createNode(\$newParent,$parentLabel,$parentAttr);
871  
872 if (ref($children) eq 'ARRAY'){
873 if ($children->[0]->parent() eq $oldParent){
874 $oldParent->insertBefore($newParent,$children->[0]);
875 }
876 else{
877 $oldParent->insertBefore($newParent);
878 }
879 foreach (0..$#{$children}){
880 # if (not ref($children->[$_]->getParentNode())){next;}
881 if (not ref($children->[$_])){next;}
882 if ($children->[$_]->parent eq $oldParent){
883 $children->[$_]=$oldParent->removeChild($children->[$_]);
884 $newParent->appendChild($children->[$_]);
885 }
886 }
887 }
888 else{
889 $children=$oldParent->replaceChild($newParent,$children);
890 $newParent->appendChild($children);
891 }
892 return $newParent;
893 }
894  
895 #....... end of insertParent!!!!!!!!!!!!!!!!!!!!!!!!
896  
897  
898  
899 #--------------------------
900 # walk the tree ...
901 # make all steps in a path!
902 # a path is a sequence of steps which makeStep understands
903 # (all steps have to be joined with ':' as delimiter symbol!)
904 #
905  
906 sub moveTo{
907 my $self=shift;
908 my $node=shift;
909 my $path=shift;
910  
911 my @steps=split(/\:/,$path);
912 foreach (@steps){
913 $node=$self->makeStep($node,$_);
914 }
915 return $node;
916 }
917  
918  
919 #--------------------------
920 # walk the tree ...
921 #
922 # up: one step up
923 # down: one step down (left-most = first child)
924 # downright: one step down (right-most = last child)
925 # top: go to the top (root node)
926 # bottom: do 'down' as often as possible (go the first leaf)
927 # bottomright: do 'downright' as often as possible (last leaf)
928 # $tag: go up until a node with the tag $tag is reached
929 # left: go the left neighbour
930 # if no neighbour exists: do 'up-left-downright'
931 # left($tag): go the left neighbour with the tag $tag
932 # right: go the right neighbour
933 # if no neighbour exists: do 'up-right-down'
934 # right($tag): go the left neighbour with the tag $tag
935 #
936 # makeSteps returns the start-node if no appropriate node is found!
937 # (exception: left($tag) & right($tag): returns undef if nothing found)
938 #
939  
940  
941 sub makeStep{
942 my $self=shift;
943 my $node=shift;
944 my $step=shift;
945 # print STDERR "$step:";
946  
947 if (not ref($node)){return $node;}
948  
949 if ($step=~/up/i){
950 return $node->getParentNode();
951 }
952 elsif ($step=~/top/i){
953 return $self->getRootNode();
954 }
955 elsif ($step=~/downright/i){
956 my $down=$node->getLastChild();
957 if (not ref($down)){return $down;} # this return undef!!!
958 if ($self->isEmptyNode($down)){
959 # print STDERR "empty:";
960 return $self->makeStep($down,'left');
961 }
962 return $down;
963 }
964 elsif ($step=~/down/i){
965 my $down=$node->getFirstChild();
966 if (not ref($down)){return $down;} # this return undef!!!
967 if ($self->isEmptyNode($down)){
968 # print STDERR "empty:";
969 return $self->makeStep($down,'right');
970 }
971 return $down;
972 }
973 elsif ($step=~/bottomright/i){
974 while ($node->hasChildNodes()){
975 my $down=$self->makeStep($node,'downright');
976 if (ref($down)){$node=$down;}
977 }
978 return $node;
979 }
980 elsif ($step=~/bottom/i){
981 while ($node->hasChildNodes()){
982 my $down=$self->makeStep($node,'down');
983 if (ref($down)){$node=$down;}
984 }
985 return $node;
986 }
987 elsif ($step=~/left$/i){
988 my $left=$node->getPreviousSibling();
989 if (not ref($left)){
990 return $self->moveTo($node,'up:left:downright');
991 }
992 if ($self->isEmptyNode($left)){
993 # print STDERR "empty:";
994 return $self->makeStep($left,$step);
995 }
996 return $left;
997 }
998 elsif ($step=~/left\((.*)\)/i){ # left neighbour with tag $tag!
999 my $tag=$1;
1000 my $left=$node->getPreviousSibling();
1001 if (ref($left)){
1002 if ($left->getNodeName() eq $tag){
1003 return $left;
1004 }
1005 }
1006 my @nodes=$self->findNodes($tag);
1007 foreach (0..$#nodes){
1008 if ($node==$nodes[$_]){
1009 if ($_){return $nodes[$_-1];}
1010 else{return undef;}
1011 }
1012 }
1013 }
1014 elsif ($step=~/right$/i){
1015 my $right=$node->getNextSibling();
1016 if (not ref($right)){
1017 return $self->moveTo($node,'up:right:down');
1018 }
1019 if ($self->isEmptyNode($right)){
1020 # print STDERR "empty:";
1021 return $self->makeStep($right,$step);
1022 }
1023 return $right;
1024 }
1025 elsif ($step=~/right\((.*)\)/i){ # right neighbour with tag $tag!
1026 my $tag=$1;
1027 my $left=$node->getNextSibling();
1028 if (ref($left)){
1029 if ($left->getNodeName() eq $tag){
1030 return $left;
1031 }
1032 }
1033 my @nodes=$self->findNodes($tag);
1034 foreach (0..$#nodes){
1035 if ($node==$nodes[$_]){
1036 return $nodes[$_+1];
1037 }
1038 }
1039 }
1040  
1041 my $current=$node;
1042 while (ref($current)){ # $step is a tag name!
1043 $current=$current->getParentNode(); # go up the tree until we find
1044 if (not ref($current)){last;}
1045 my $name=$current->getNodeName();
1046 if ($name=~/$step/){ # the element or we reach
1047 return $current; # the root node
1048 }
1049 }
1050  
1051 return $node # nothing happened! return the $node
1052 }
1053  
1054 sub isEmptyNode{
1055 my $self=shift;
1056 my $node=shift;
1057 if (not ref($node)){return undef;}
1058 if ($node->getNodeName eq '#text'){
1059 my $text=$node->getNodeValue();
1060 if ($text!~/\S/s){return 1;}
1061 }
1062 return 0;
1063 }
1064  
1065  
1066  
1067  
1068 ######################################################################
1069 ######################################################################
1070 ######################################################################
1071 # ..............
1072 #
1073 # should look through these functions some day
1074 #
1075 ######################################################################
1076  
1077 #----------------------------------------------
1078 # return a complex data structure
1079  
1080 sub data{
1081 my $self=shift;
1082 my $root=$self->root();
1083 if (not ref($root)){return undef;}
1084 return $root->data();
1085 # return $self->attribute(); # get all root attributes
1086 }
1087  
1088 #----------------------------------------------
1089 # read a complex data structure into a Data-object
1090  
1091 sub setData{
1092 my $self=shift;
1093 my $root=$self->root();
1094 if (not ref($root)){$root=$self->addNode();}
1095 if (not ref($root)){return undef;}
1096 return $root->setData(@_);
1097 # return $self->setAttribute(@_);
1098 }
1099  
1100  
1101 sub keepAttributes{
1102 my $self=shift;
1103 my $names=shift;
1104 if (ref($names) ne 'ARRAY'){return;}
1105  
1106 my $attr=$self->attribute(); # get all root attributes
1107 if (ref($attr) ne 'HASH'){return;}
1108 foreach my $a (keys %{$attr}){
1109 if (not grep (/^$a$/,@{$names})){ # delete matching attributes
1110 $self->delAttribute($a);
1111 }
1112 }
1113 }
1114  
1115 sub delEmptyFields{ # delete empty fields ('')
1116 my $self=shift;
1117  
1118 # get attributes ... delete empty attributes... !!!!!!!
1119 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1120 # ... this is just for right now ...
1121  
1122 my $data=$self->data;
1123 foreach (keys %{$data}){
1124 if (not defined $$data{$_}){delete $$data{$_};}
1125 if ($$data{$_} eq ''){delete $$data{$_};}
1126 }
1127 }
1128  
1129  
1130 #--------------------------------------------------
1131 # matchData: does not really match all data in this object
1132 # matches only the root attributes with the select pattern
1133  
1134 sub matchData{
1135 my $self=shift;
1136 my $pattern=shift;
1137 my $root=$self->root();
1138 if (not ref($root)){return 0;}
1139 return $root->matchAttr($pattern);
1140 }
1141  
1142  
1143 #-----------------------------------------
1144 # experimental: convert to HTML-tables
1145
1146 sub toHTML{
1147 my $self=shift;
1148 my $dom=shift;
1149 my $indent=shift;
1150  
1151 my %colors=('default' => '#FFFFFF',
1152 's' => '#EEEEEE',
1153 'chunk' => '#E4EEFF',
1154 'c' => '#E4EEFF',
1155 'source' => '#FFFFEE',
1156 'target' => '#FFFFEE',
1157 'w' => '#FFEEDD');
1158  
1159 if (not defined $dom){
1160 $dom=$self->root();
1161 }
1162  
1163 my $html="$indent<table>";
1164 $html.="$indent<tr>";
1165 foreach my $n ($dom->getChildNodes()){
1166 my $name=$n->getNodeName;
1167  
1168 if (defined $colors{$name}){
1169 $html.="$indent<td bgcolor='$colors{$name}'>";
1170 }
1171 else{
1172 $html.="$indent<td bgcolor='$colors{default}'>";
1173 if ($name!~/^\#/){
1174 $html.="<b>$name</b><br>";
1175 }
1176 }
1177 if ($name eq '#text'){
1178 my $str=$n->getNodeValue;
1179 if ($str=~/\S/){
1180 $html.="<i>$str</i>";
1181 }
1182 }
1183 elsif ($name eq '#comment'){
1184 my $str=$n->getNodeValue;
1185 if ($str=~/\S/){
1186 $html.='<!-- '.$str.' -->';
1187 }
1188 }
1189 else{
1190 my $attr=$n->attributes();
1191 # $html.="<b>$name</b>";
1192 if (ref($attr) eq 'HASH'){
1193 # $html.='<font size="-3">';
1194 delete $$attr{id}; # skip ID and byte-spans!
1195 delete $$attr{span};
1196 $html.=join '<br>',values %{$attr};
1197 # foreach (sort keys %{$attr}){
1198 # $html.="<br>$_='$attr->{$_}'";
1199 # }
1200 # $html.='</font>';
1201 }
1202 }
1203 $html.=$indent.'</td>';
1204 }
1205  
1206 $html.="$indent</tr>$indent<tr>";
1207  
1208 #--------------------
1209 # process children
1210  
1211 foreach my $n ($dom->getChildNodes()){
1212 $html.="$indent<td>";
1213 # $indent.=' ';
1214 $indent='';
1215 $html.=$self->toHTML($n,$indent);
1216 $html.="$indent</td>";
1217 }
1218  
1219 $html.="$indent</tr>$indent</table>\n";
1220 return $html;
1221 }
1222  
1223  
1224 ######################################################################
1225 ######################################################################
1226 ######################################################################
1227 ######################################################################
1228  
1229  
1230  
1231  
1232  
1233  
1234 #--------------------------------------------------------------------
1235 # finally: return a true value
1236 #
1237  
1238 1;