summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl.straw/lib/PPM/XML/Element.pm
blob: 055d9b15b82e889ddc7bc4e7ac96ea439353372b (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package PPM::XML::Element;
use vars qw( $VERSION );
$VERSION = '0.01_01';

#
# PPM::XML::Element
#
# Base class for XML Elements.  Provides the ability to output the XML document
# once it's been parsed using the XML::Parser module.
#
###############################################################################

###############################################################################
# Required inclusions.
###############################################################################
use HTML::Entities;                     # Needed for escaping char entities

###############################################################################
# Allow for creation via 'new'.
###############################################################################
sub new
{
    my ($class, %args) = @_;
    bless \%args, $class;
}

###############################################################################
# Subroutine:   output
###############################################################################
# Outputs the entire XML document on the currently selected filehandle.
###############################################################################
sub output
{
    my $self = shift;
    print $self->as_text();
}

###############################################################################
# Subroutine:   content
###############################################################################
# Returns a string containing all of the content of this element.
###############################################################################
sub content
{
    my $self = shift;
    my $kids = $self->{'Kids'};
    return unless (defined $kids and ref($kids) eq 'ARRAY');
    my @kids = @{ $kids };
    my $text;

    if (@kids > 0)
    {
        foreach (@kids)
        {
            # Allow for outputting of char data
            if ((ref $_) =~ /::Characters$/o)
                { $text .= encode_entities( $_->{'Text'} ); }
            else
                { $text .= $_->as_text(); }
        }
    }

    return $text;
}

###############################################################################
# Subroutine:   add_child ($elemref)
###############################################################################
# Adds a new child element to ourselves.
###############################################################################
sub add_child (\$)
{
    my $self    = shift;
    my $elemref = shift;
    push( @{$self->{'Kids'}}, $elemref );
}

###############################################################################
# Subroutine:   remove_child ($elemref)
###############################################################################
# Removes a child element from ourselves.  Returns non-zero if it was able to
# remove the child element, and zero if it was unable to do so.
###############################################################################
sub remove_child
{
    my $self    = shift;
    my $elemref = shift;

    foreach my $idx (0 .. @{$self->{'Kids'}})
    {
        if ($self->{'Kids'}[$idx] == $elemref)
        {
            splice( @{$self->{'Kids'}}, $idx, 1 );
            return 1;
        }
    }

    return 0;
}

###############################################################################
# Subroutine:   add_text ($text)
###############################################################################
# Adds character data to the given element.  Returns undef if unable to add the
# text to this element, and returns a reference to the character data element
# if successful.
###############################################################################
sub add_text
{
    my $self = shift;
    my $text = shift;

    return if (!defined $text);

    my $type = ref $self;                   # Do package name magic
    $type =~ s/::[^:]+?$/::Characters/o;

    my $elem = new $type;
    $elem->{'Text'} = $text;
    $self->add_child( $elem );
    return $elem;
}

###############################################################################
# Subroutine:   as_text
###############################################################################
# Returns a string containing the entire XML document.
###############################################################################
sub as_text
{
    my $self = shift;
    my $text;

    my $type = ref $self;
    $type =~ s/.*:://;

    $text = "\n<" . $type;
    foreach (sort keys %{$self})
    {
        if ($_ !~ /Text|Kids/) { 
	  if (defined $self->{$_} ) {
	      $text .= " $_=\"" . $self->{$_} . '"'; }
	  }
    }

    my $cont = $self->content();
    if (defined $cont)
        { $text .= '>' . $cont . '</' . $type . '>'; }
    else
        { $text .= ' />'; }

    $text =~ s/\n\n/\n/g;
    return $text;
}

__END__

###############################################################################
# PPD Documentation
###############################################################################

=head1 NAME

PPM::XML::Element - Base element class for XML elements

=head1 SYNOPSIS

 use PPM::XML::Element;
 @ISA = qw( PPM::XML::Element );

=head1 DESCRIPTION

Base element class for XML elements.  To be derived from to create your own
elements for use with the XML::Parser module.  Supports output of empty
elements using <.... />.

It is recommended that you use a version of the XML::Parser module which 
includes support for Styles; by deriving your own elements from 
PPM::XML::Element and using the 'Objects' style it becomes B<much> easier 
to create your own parser.

=head1 METHODS

=over 4

=item add_text ($text)

Adds character data to the end of the element.  The element created is placed
within the same package space as the element it was created under (e.g. adding
text to a XML::Foobar::Stuff element would put the character data into an
XML::Foobar::Characters element).  If successful, this method returns a
reference to the newly created element.

=item as_text

Returns a string value containing the entire XML document from this element on
down.

=item content

Returns a string value containing the entire content of this XML element.  Note
that this is quite similar to the C<as_text()> method except that it does not
include any information about this element in particular.

=item output

Recursively outputs the structure of the XML document from this element on
down.

=item add_child ($elemref)

Adds the child element to the list of children for this element.  Note that
the element given must be a reference to an object derived from 
C<PPM::XML::Element>.

=item remove_child ($elemref)

Removes the given child element from the list of children for this element.
This method returns non-zero if it is able to locate and remove the child
element, returning zero if it is unable to do so.

=back

=head1 LIMITATIONS

The C<PPM::XML::Element> module has no provisions for outputting processor
directives or external entities.  It only outputs child elements and any
character data which the elements may contain.

=head1 AUTHORS

Graham TerMarsch <gtermars@activestate.com>

=head1 SEE ALSO

L<XML::Parser>

=cut