File: TreePrinter.pm

package info (click to toggle)
libmkdoc-xml-perl 0.75-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 404 kB
  • sloc: perl: 2,629; xml: 17; makefile: 2
file content (123 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -------------------------------------------------------------------------------------
# MKDoc::XML::TreePrinter
# -------------------------------------------------------------------------------------
# Author : Jean-Michel Hiver.
# Copyright : (c) MKDoc Holdings Ltd, 2003
#
# This module is the counterpart of MKDoc::XML::TreePrinter. It turns an XML
# tree back into a string.
#
# This module is distributed under the same license as Perl itself.
# -------------------------------------------------------------------------------------
package MKDoc::XML::TreePrinter;
use warnings;
use strict;


##
# $class->process (@nodes);
# ----------------------------
# Does the stuff.
##
sub process
{
    my $class  = shift;
    my @nodes  = @_;
    my @res = ();
    
    foreach my $node (@nodes)
    {
        ref $node or do {
            push @res, $node;
            next;
        };

        $node->{_tag} =~ /\~pi/ and do {
            push @res, "<?$node->{text}?>";
            next;
        };

        $node->{_tag} =~ /\~declaration/ and do {
            push @res, "<!$node->{text}>";
            next;
        };

        $node->{_tag} =~ /\~comment/ and do {
            push @res, "<!--" . $node->{text} . "-->";
            next;
        };

        my $tag   = $node->{_tag};
        my %att   = map { $_ => _encode_quot ($node->{$_}) } grep !/^_/, keys %{$node};
        my $attr  = join " ", map { "$_=\"$att{$_}\"" } sort keys %att;
        my $open  = $node->{_open};
        my $close = $node->{_close};
        
        $open && $close && do {
            if ($attr) { push @res, "<$tag $attr />" }
            else       { push @res, "<$tag />"       }
            next;
        };
        
        my $open_tag  = $attr ? "<$tag $attr>" : "<$tag>";
        my $close_tag = "</$tag>";
        my @desc      = $node->{_content} ? @{$node->{_content}} : ();
        
        my $res = $open_tag . $class->process (@desc) . $close_tag;
        push @res, $res;
        next;
    };

    return join '', @res;
}


sub _encode_quot
{
    my $res = shift;
    return '' unless (defined $res);

    $res =~ s/\"/\&quot\;/g;
    return $res;
}


1;


__END__


=head1 NAME

MKDoc::XML::TreePrinter - Builds XML data from a parsed tree


=head1 SYNOPSIS

  my $xml_data = MKDoc::XML::TreePrinter->process_data (@top_nodes);


=head1 SUMMARY

L<MKDoc::XML::TreePrinter> takes trees which are produced by
L<MKDoc::XML::TreeBuilder> to turn a parsed tree back into XML data. This means
you can parse some stuff using L<MKDoc::TreeBuilder>, fiddle around with the
tree, and then get the result back as XML data. 


=head1 AUTHOR

Copyright 2003 - MKDoc Holdings Ltd.

Author: Jean-Michel Hiver

This module is free software and is distributed under the same license as Perl
itself. Use it at your own risk.


=head1 SEE ALSO

L<MKDoc::XML::TreeBuilder>

=cut