/usr/share/perl5/MDOM/Dumper.pm is in libmakefile-dom-perl 0.008-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | package MDOM::Dumper;
=pod
=head1 NAME
MDOM::Dumper - Dumping of MDOM trees
=head1 SYNOPSIS
# Load a document
my $Module = MDOM::Document->new( 'MyMakefile' );
# Create the dumper
my $Dumper = MDOM::Dumper->new( $Module );
# Dump the document
$Dumper->print;
=head1 DESCRIPTION
The MDOM trees in MDOM are quite complex, and getting a dump of their
structure for development and debugging purposes is important.
This module provides that functionality.
The process is relatively simple. Create a dumper object with a
particular set of options, and then call one of the dump methods to
generate the dump content itself.
=head1 METHODS
=cut
use strict;
use Params::Util '_INSTANCE';
use vars qw{$VERSION};
BEGIN {
$VERSION = '0.008';
}
#####################################################################
# Constructor
=pod
=head2 new $Element, param => value, ...
The C<new> constructor creates a dumper, and takes as argument a single
L<MDOM::Element> object of any type to serve as the root of the tree to
be dumped, and a number of key-E<gt>value parameters to control the output
format of the Dumper. Details of the parameters are listed below.
Returns a new C<MDOM::Dumper> object, or C<undef> if the constructor
is not passed a correct L<MDOM::Element> root object.
=over
=item memaddr
Should the dumper print the memory addresses of each MDOM element.
True/false value, off by default.
=item indent
Should the structures being dumped be indented. This value is numeric,
with the number representing the number of spaces to use when indenting
the dumper output. Set to '2' by default.
=item class
Should the dumper print the full class for each element.
True/false value, on by default.
=item content
Should the dumper show the content of each element. True/false value,
on by default.
=item whitespace
Should the dumper show whitespace tokens. By not showing the copious
numbers of whitespace tokens the structure of the code can often be
made much clearer. True/false value, on by default.
=item comments
Should the dumper show comment tokens. In situations where you have
a lot of comments, the code can often be made clearer by ignoring
comment tokens. True/value value, on by default.
=item locations
Should the dumper show the location of each token. The values shown are
[ line, rowchar, column ]. See L<MDOM::Element/"location"> for a description of
what these values really are. True/false value, off by default.
=back
=cut
sub new {
my $class = shift;
my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef;
# Create the object
my $self = bless {
root => $Element,
display => {
memaddr => '', # Show the refaddr of the item
indent => 2, # Indent the structures
class => 1, # Show the object class
content => 1, # Show the object contents
whitespace => 1, # Show whitespace tokens
comments => 1, # Show comment tokens
locations => 0, # Show token locations
},
}, $class;
# Handle the options
my %options = map { lc $_ } @_;
foreach ( keys %{$self->{display}} ) {
if ( exists $options{$_} ) {
if ( $_ eq 'indent' ) {
$self->{display}->{indent} = $options{$_};
} else {
$self->{display}->{$_} = !! $options{$_};
}
}
}
$self->{indent_string} = join '', (' ' x $self->{display}->{indent});
# Try to auto-call index_locations. If it failes, turn of locations display
if ( $self->{display}->{locations} ) {
my $Document = $Element->isa('MDOM::Document') ? $Element : $Element->top;
if ( $Document->isa('MDOM::Document') ) {
$Document->index_locations();
} else {
$self->{display}->{locations} = 0;
}
}
$self;
}
#####################################################################
# Main Interface Methods
=pod
=head2 print
The C<print> method generates the dump and prints it to STDOUT.
Returns as for the internal print function.
=cut
sub print {
CORE::print(shift->string);
}
=pod
=head2 string
The C<string> method generates the dump and provides it as a
single string.
Returns a string or undef if there is an error while generating the dump.
=cut
sub string {
my $array_ref = shift->_dump or return undef;
join '', map { "$_\n" } @$array_ref;
}
=pod
=head2 list
The C<list> method generates the dump and provides it as a raw
list, without trailing newlines.
Returns a list or the null list if there is an error while generation
the dump.
=cut
sub list {
my $array_ref = shift->_dump or return ();
@$array_ref;
}
#####################################################################
# Generation Support Methods
sub _dump {
my $self = ref $_[0] ? shift : shift->new(shift);
my $Element = _INSTANCE($_[0], 'MDOM::Element') ? shift : $self->{root};
my $indent = shift || '';
my $output = shift || [];
# Print the element if needed
my $show = 1;
if ( $Element->isa('MDOM::Token::Whitespace') ) {
$show = 0 unless $self->{display}->{whitespace};
} elsif ( $Element->isa('MDOM::Token::Comment') ) {
$show = 0 unless $self->{display}->{comments};
}
push @$output, $self->_element_string( $Element, $indent ) if $show;
# Recurse into our children
if ( $Element->isa('MDOM::Node') ) {
my $child_indent = $indent . $self->{indent_string};
foreach my $child ( @{$Element->{children}} ) {
$self->_dump( $child, $child_indent, $output );
}
}
$output;
}
sub _element_string {
my $self = ref $_[0] ? shift : shift->new(shift);
my $Element = _INSTANCE($_[0], 'MDOM::Element') ? shift : $self->{root};
my $indent = shift || '';
my $string = '';
# Add the memory location
if ( $self->{display}->{memaddr} ) {
$string .= $Element->refaddr . ' ';
}
# Add the location if such exists
if ( $self->{display}->{locations} ) {
my $loc_string;
if ( $Element->isa('MDOM::Token') ) {
my $location = $Element->location;
if ($location) {
$loc_string = sprintf("[ % 4d, % 3d, % 3d ] ", @$location);
}
}
# Output location or pad with 20 spaces
$string .= $loc_string || " " x 20;
}
# Add the indent
if ( $self->{display}->{indent} ) {
$string .= $indent;
}
# Add the class name
if ( $self->{display}->{class} ) {
$string .= ref $Element;
}
if ( $Element->isa('MDOM::Token') ) {
# Add the content
if ( $self->{display}->{content} ) {
my $content = $Element->content;
$content =~ s/\n/\\n/g;
$content =~ s/\t/\\t/g;
$content =~ s/'/\\'/g;
$content =~ s/\r/\\r/g;
$string .= " \t'$content'";
}
} elsif ( $Element->isa('MDOM::Structure') ) {
# Add the content
if ( $self->{display}->{content} ) {
my $start = $Element->start
? $Element->start->content
: '???';
my $finish = $Element->finish
? $Element->finish->content
: '???';
$string .= " \t$start ... $finish";
}
}
$string;
}
1;
=pod
=head1 SUPPORT
See the L<support section|MDOM/SUPPORT> in the main module.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
Yichun "agentzh" Zhang C<< <agentzh@gmail.com> >>
=head1 COPYRIGHT
Copyright 2001 - 2006 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|