File Coverage

lib/MKDoc/XML/TreePrinter.pm
Criterion Covered Total %
statement 46 47 97.8
branch 15 18 83.3
condition 2 3 66.6
subroutine 4 4 100.0
pod 0 1 0.0
total 67 73 91.7


line stmt bran cond sub pod time code
1             # -------------------------------------------------------------------------------------
2             # MKDoc::XML::TreePrinter
3             # -------------------------------------------------------------------------------------
4             # Author : Jean-Michel Hiver.
5             # Copyright : (c) MKDoc Holdings Ltd, 2003
6             #
7             # This module is the counterpart of MKDoc::XML::TreePrinter. It turns an XML
8             # tree back into a string.
9             #
10             # This module is distributed under the same license as Perl itself.
11             # -------------------------------------------------------------------------------------
12             package MKDoc::XML::TreePrinter;
13 1     1   1356 use warnings;
  1         13  
  1         48  
14 1     1   6 use strict;
  1         3  
  1         764  
15              
16              
17             ##
18             # $class->process (@nodes);
19             # ----------------------------
20             # Does the stuff.
21             ##
22             sub process
23             {
24 7     7 0 20 my $class = shift;
25 7         16 my @nodes = @_;
26 7         8 my @res = ();
27            
28 7         12 foreach my $node (@nodes)
29             {
30 25 100       52 ref $node or do {
31 15         23 push @res, $node;
32 15         21 next;
33             };
34              
35 10 100       25 $node->{_tag} =~ /\~pi/ and do {
36 1         4 push @res, "{text}?>";
37 1         2 next;
38             };
39              
40 9 100       22 $node->{_tag} =~ /\~declaration/ and do {
41 1         4 push @res, "{text}>";
42 1         2 next;
43             };
44              
45 8 100       27 $node->{_tag} =~ /\~comment/ and do {
46 1         4 push @res, "";
47 1         2 next;
48             };
49              
50 7         8 my $tag = $node->{_tag};
51 7         10 my %att = map { $_ => _encode_quot ($node->{$_}) } grep !/^_/, keys %{$node};
  1         4  
  7         46  
52 7         21 my $attr = join " ", map { "$_=\"$att{$_}\"" } keys %att;
  1         5  
53 7         10 my $open = $node->{_open};
54 7         8 my $close = $node->{_close};
55            
56 7 100 66     30 $open && $close && do {
57 1 50       3 if ($attr) { push @res, "<$tag $attr />" }
  0         0  
58 1         4 else { push @res, "<$tag />" }
59 1         3 next;
60             };
61            
62 6 100       18 my $open_tag = $attr ? "<$tag $attr>" : "<$tag>";
63 6         9 my $close_tag = "";
64 6 50       20 my @desc = $node->{_content} ? @{$node->{_content}} : ();
  6         23  
65            
66 6         35 my $res = $open_tag . $class->process (@desc) . $close_tag;
67 6         10 push @res, $res;
68 6         15 next;
69             };
70              
71 7         48 return join '', @res;
72             }
73              
74              
75             sub _encode_quot
76             {
77 1     1   3 my $res = shift;
78 1 50       3 return '' unless (defined $res);
79              
80 1         3 $res =~ s/\"/\"\;/g;
81 1         5 return $res;
82             }
83              
84              
85             1;
86              
87              
88             __END__