| 1 | package MDOM::Dumper; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =pod | ||||||||||||||
| 4 | |||||||||||||||
| 5 | =head1 NAME | ||||||||||||||
| 6 | |||||||||||||||
| 7 | MDOM::Dumper - Dumping of MDOM trees | ||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||
| 10 | |||||||||||||||
| 11 | # Load a document | ||||||||||||||
| 12 | my $Module = MDOM::Document->new( 'MyMakefile' ); | ||||||||||||||
| 13 | |||||||||||||||
| 14 | # Create the dumper | ||||||||||||||
| 15 | my $Dumper = MDOM::Dumper->new( $Module ); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | # Dump the document | ||||||||||||||
| 18 | $Dumper->print; | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||
| 21 | |||||||||||||||
| 22 | The MDOM trees in MDOM are quite complex, and getting a dump of their | ||||||||||||||
| 23 | structure for development and debugging purposes is important. | ||||||||||||||
| 24 | |||||||||||||||
| 25 | This module provides that functionality. | ||||||||||||||
| 26 | |||||||||||||||
| 27 | The process is relatively simple. Create a dumper object with a | ||||||||||||||
| 28 | particular set of options, and then call one of the dump methods to | ||||||||||||||
| 29 | generate the dump content itself. | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =cut | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 14 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 14 | ▶ | use Params::Util '_INSTANCE'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | 14 | ▶ | use vars qw{$VERSION}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | BEGIN { | ||||||||||||||
| 40 | 14 | ▶ | $VERSION = '0.007'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | |||||||||||||||
| 44 | |||||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | ##################################################################### | ||||||||||||||
| 48 | # Constructor | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =pod | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =head2 new $Element, param => value, ... | ||||||||||||||
| 53 | |||||||||||||||
| 54 | The C<new> constructor creates a dumper, and takes as argument a single | ||||||||||||||
| 55 | L<MDOM::Element> object of any type to serve as the root of the tree to | ||||||||||||||
| 56 | be dumped, and a number of key-E<gt>value parameters to control the output | ||||||||||||||
| 57 | format of the Dumper. Details of the parameters are listed below. | ||||||||||||||
| 58 | |||||||||||||||
| 59 | Returns a new C<MDOM::Dumper> object, or C<undef> if the constructor | ||||||||||||||
| 60 | is not passed a correct L<MDOM::Element> root object. | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =over | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =item memaddr | ||||||||||||||
| 65 | |||||||||||||||
| 66 | Should the dumper print the memory addresses of each MDOM element. | ||||||||||||||
| 67 | True/false value, off by default. | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =item indent | ||||||||||||||
| 70 | |||||||||||||||
| 71 | Should the structures being dumped be indented. This value is numeric, | ||||||||||||||
| 72 | with the number representing the number of spaces to use when indenting | ||||||||||||||
| 73 | the dumper output. Set to '2' by default. | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =item class | ||||||||||||||
| 76 | |||||||||||||||
| 77 | Should the dumper print the full class for each element. | ||||||||||||||
| 78 | True/false value, on by default. | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =item content | ||||||||||||||
| 81 | |||||||||||||||
| 82 | Should the dumper show the content of each element. True/false value, | ||||||||||||||
| 83 | on by default. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =item whitespace | ||||||||||||||
| 86 | |||||||||||||||
| 87 | Should the dumper show whitespace tokens. By not showing the copious | ||||||||||||||
| 88 | numbers of whitespace tokens the structure of the code can often be | ||||||||||||||
| 89 | made much clearer. True/false value, on by default. | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =item comments | ||||||||||||||
| 92 | |||||||||||||||
| 93 | Should the dumper show comment tokens. In situations where you have | ||||||||||||||
| 94 | a lot of comments, the code can often be made clearer by ignoring | ||||||||||||||
| 95 | comment tokens. True/value value, on by default. | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =item locations | ||||||||||||||
| 98 | |||||||||||||||
| 99 | Should the dumper show the location of each token. The values shown are | ||||||||||||||
| 100 | [ line, rowchar, column ]. See L<MDOM::Element/"location"> for a description of | ||||||||||||||
| 101 | what these values really are. True/false value, off by default. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =back | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =cut | ||||||||||||||
| 106 | |||||||||||||||
| 107 | sub new { | ||||||||||||||
| 108 | 69 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: called CC=8 SCAR=29.8
Pod
documented
| |||||||||||||||
| 109 | 69 | ▶ | my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | |||||||||||||||
| 111 | # Create the object | ||||||||||||||
| 112 | 69 | my $self = bless { | |||||||||||||
| 113 | root => $Element, | ||||||||||||||
| 114 | display => { | ||||||||||||||
| 115 | memaddr => '', # Show the refaddr of the item | ||||||||||||||
| 116 | indent => 2, # Indent the structures | ||||||||||||||
| 117 | class => 1, # Show the object class | ||||||||||||||
| 118 | content => 1, # Show the object contents | ||||||||||||||
| 119 | whitespace => 1, # Show whitespace tokens | ||||||||||||||
| 120 | comments => 1, # Show comment tokens | ||||||||||||||
| 121 | locations => 0, # Show token locations | ||||||||||||||
| 122 | }, | ||||||||||||||
| 123 | }, $class; | ||||||||||||||
| 124 | |||||||||||||||
| 125 | # Handle the options | ||||||||||||||
| 126 | 69 | my %options = map { lc $_ } @_; | |||||||||||||
| 127 | 69 | foreach ( keys %{$self->{display}} ) { | |||||||||||||
| 128 | 483 | ▶ | if ( exists $options{$_} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 0 | ▶ | if ( $_ eq 'indent' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | $self->{display}->{indent} = $options{$_}; | |||||||||||||
| 131 | } else { | ||||||||||||||
| 132 | 0 | $self->{display}->{$_} = !! $options{$_}; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 69 | $self->{indent_string} = join '', (' ' x $self->{display}->{indent}); | |||||||||||||
| 138 | |||||||||||||||
| 139 | # Try to auto-call index_locations. If it failes, turn of locations display | ||||||||||||||
| 140 | 69 | ▶ | if ( $self->{display}->{locations} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 0 | ▶ | my $Document = $Element->isa('MDOM::Document') ? $Element : $Element->top; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | 0 | ▶ | if ( $Document->isa('MDOM::Document') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 0 | $Document->index_locations(); | |||||||||||||
| 144 | } else { | ||||||||||||||
| 145 | 0 | $self->{display}->{locations} = 0; | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 69 | $self; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | |||||||||||||||
| 153 | |||||||||||||||
| 154 | |||||||||||||||
| 155 | |||||||||||||||
| 156 | ##################################################################### | ||||||||||||||
| 157 | # Main Interface Methods | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =pod | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =head2 print | ||||||||||||||
| 162 | |||||||||||||||
| 163 | The C<print> method generates the dump and prints it to STDOUT. | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Returns as for the internal print function. | ||||||||||||||
| 166 | |||||||||||||||
| 167 | =cut | ||||||||||||||
| 168 | |||||||||||||||
| 169 | sub print { | ||||||||||||||
| 170 | 0 | ▶ | CORE::print(shift->string); | ||||||||||||
|
Subroutine
print: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 171 | } | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =pod | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =head2 string | ||||||||||||||
| 176 | |||||||||||||||
| 177 | The C<string> method generates the dump and provides it as a | ||||||||||||||
| 178 | single string. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | Returns a string or undef if there is an error while generating the dump. | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =cut | ||||||||||||||
| 183 | |||||||||||||||
| 184 | sub string { | ||||||||||||||
| 185 | 69 | ▶ | my $array_ref = shift->_dump or return undef; | ||||||||||||
|
Subroutine
string: called CC=2 SCAR=7.1
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 186 | 69 | join '', map { "$_\n" } @$array_ref; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =pod | ||||||||||||||
| 190 | |||||||||||||||
| 191 | =head2 list | ||||||||||||||
| 192 | |||||||||||||||
| 193 | The C<list> method generates the dump and provides it as a raw | ||||||||||||||
| 194 | list, without trailing newlines. | ||||||||||||||
| 195 | |||||||||||||||
| 196 | Returns a list or the null list if there is an error while generation | ||||||||||||||
| 197 | the dump. | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =cut | ||||||||||||||
| 200 | |||||||||||||||
| 201 | sub list { | ||||||||||||||
| 202 | 0 | ▶ | my $array_ref = shift->_dump or return (); | ||||||||||||
|
Subroutine
list: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 203 | 0 | @$array_ref; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | |||||||||||||||
| 207 | |||||||||||||||
| 208 | |||||||||||||||
| 209 | |||||||||||||||
| 210 | ##################################################################### | ||||||||||||||
| 211 | # Generation Support Methods | ||||||||||||||
| 212 | |||||||||||||||
| 213 | sub _dump { | ||||||||||||||
| 214 | 861 | ▶ | my $self = ref $_[0] ? shift : shift->new(shift); | ||||||||||||
|
Subroutine
_dump: called CC=12 SCAR=25.0
Branch: 1 branch
| |||||||||||||||
| 215 | 861 | ▶ | my $Element = _INSTANCE($_[0], 'MDOM::Element') ? shift : $self->{root}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 216 | 861 | ▶ | my $indent = shift || ''; | ||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 217 | 861 | ▶ | my $output = shift || []; | ||||||||||||
|
Condition100%2/2
shift() || []
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 218 | |||||||||||||||
| 219 | # Print the element if needed | ||||||||||||||
| 220 | 861 | my $show = 1; | |||||||||||||
| 221 | 861 | ▶ | if ( $Element->isa('MDOM::Token::Whitespace') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 222 | 279 | ▶ | $show = 0 unless $self->{display}->{whitespace}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 223 | } elsif ( $Element->isa('MDOM::Token::Comment') ) { | ||||||||||||||
| 224 | 16 | ▶ | $show = 0 unless $self->{display}->{comments}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 225 | } | ||||||||||||||
| 226 | 861 | ▶ | push @$output, $self->_element_string( $Element, $indent ) if $show; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 227 | |||||||||||||||
| 228 | # Recurse into our children | ||||||||||||||
| 229 | 861 | ▶ | if ( $Element->isa('MDOM::Node') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 230 | 187 | my $child_indent = $indent . $self->{indent_string}; | |||||||||||||
| 231 | 187 | foreach my $child ( @{$Element->{children}} ) { | |||||||||||||
| 232 | 792 | $self->_dump( $child, $child_indent, $output ); | |||||||||||||
| 233 | } | ||||||||||||||
| 234 | } | ||||||||||||||
| 235 | |||||||||||||||
| 236 | 861 | $output; | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | sub _element_string { | ||||||||||||||
| 240 | 861 | ▶ | my $self = ref $_[0] ? shift : shift->new(shift); | ||||||||||||
|
Subroutine
_element_string: called CC=17 SCAR=39.7
Branch: 1 branch
| |||||||||||||||
| 241 | 861 | ▶ | my $Element = _INSTANCE($_[0], 'MDOM::Element') ? shift : $self->{root}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 242 | 861 | ▶ | my $indent = shift || ''; | ||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 243 | 861 | my $string = ''; | |||||||||||||
| 244 | |||||||||||||||
| 245 | # Add the memory location | ||||||||||||||
| 246 | 861 | ▶ | if ( $self->{display}->{memaddr} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 247 | 0 | $string .= $Element->refaddr . ' '; | |||||||||||||
| 248 | } | ||||||||||||||
| 249 | |||||||||||||||
| 250 | # Add the location if such exists | ||||||||||||||
| 251 | 861 | ▶ | if ( $self->{display}->{locations} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 252 | 0 | my $loc_string; | |||||||||||||
| 253 | 0 | ▶ | if ( $Element->isa('MDOM::Token') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 254 | 0 | my $location = $Element->location; | |||||||||||||
| 255 | 0 | ▶ | if ($location) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 256 | 0 | $loc_string = sprintf("[ % 4d, % 3d, % 3d ] ", @$location); | |||||||||||||
| 257 | } | ||||||||||||||
| 258 | } | ||||||||||||||
| 259 | # Output location or pad with 20 spaces | ||||||||||||||
| 260 | 0 | ▶ | $string .= $loc_string || " " x 20; | ||||||||||||
|
Condition0%0/2
$loc_string || ' '
MC/DC0%0/1
$loc_string || ' '
$loc_string
Truth table0%0/2
A
A= $loc_string
| |||||||||||||||
| 261 | } | ||||||||||||||
| 262 | |||||||||||||||
| 263 | # Add the indent | ||||||||||||||
| 264 | 861 | ▶ | if ( $self->{display}->{indent} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 265 | 861 | $string .= $indent; | |||||||||||||
| 266 | } | ||||||||||||||
| 267 | |||||||||||||||
| 268 | # Add the class name | ||||||||||||||
| 269 | 861 | ▶ | if ( $self->{display}->{class} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 270 | 861 | $string .= ref $Element; | |||||||||||||
| 271 | } | ||||||||||||||
| 272 | |||||||||||||||
| 273 | 861 | ▶ | if ( $Element->isa('MDOM::Token') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 274 | # Add the content | ||||||||||||||
| 275 | 674 | ▶ | if ( $self->{display}->{content} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 276 | 674 | my $content = $Element->content; | |||||||||||||
| 277 | 674 | $content =~ s/\n/\\n/g; | |||||||||||||
| 278 | 674 | $content =~ s/\t/\\t/g; | |||||||||||||
| 279 | 674 | $content =~ s/'/\\'/g; | |||||||||||||
| 280 | 674 | $content =~ s/\r/\\r/g; | |||||||||||||
| 281 | 674 | $string .= " \t'$content'"; | |||||||||||||
| 282 | } | ||||||||||||||
| 283 | } elsif ( $Element->isa('MDOM::Structure') ) { | ||||||||||||||
| 284 | # Add the content | ||||||||||||||
| 285 | 0 | ▶ | if ( $self->{display}->{content} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 286 | 0 | ▶ | my $start = $Element->start | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 287 | ? $Element->start->content | ||||||||||||||
| 288 | : '???'; | ||||||||||||||
| 289 | 0 | ▶ | my $finish = $Element->finish | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 290 | ? $Element->finish->content | ||||||||||||||
| 291 | : '???'; | ||||||||||||||
| 292 | 0 | $string .= " \t$start ... $finish"; | |||||||||||||
| 293 | } | ||||||||||||||
| 294 | } | ||||||||||||||
| 295 | |||||||||||||||
| 296 | 861 | $string; | |||||||||||||
| 297 | } | ||||||||||||||
| 298 | |||||||||||||||
| 299 | 14 | 1; | |||||||||||||
| 300 | |||||||||||||||
| 301 | =pod | ||||||||||||||
| 302 | |||||||||||||||
| 303 | =head1 SUPPORT | ||||||||||||||
| 304 | |||||||||||||||
| 305 | See the L<support section|MDOM/SUPPORT> in the main module. | ||||||||||||||
| 306 | |||||||||||||||
| 307 | =head1 AUTHOR | ||||||||||||||
| 308 | |||||||||||||||
| 309 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||
| 310 | |||||||||||||||
| 311 | Yichun "agentzh" Zhang C<< <agentzh@gmail.com> >> | ||||||||||||||
| 312 | |||||||||||||||
| 313 | =head1 COPYRIGHT | ||||||||||||||
| 314 | |||||||||||||||
| 315 | Copyright 2001 - 2006 Adam Kennedy. | ||||||||||||||
| 316 | |||||||||||||||
| 317 | This program is free software; you can redistribute | ||||||||||||||
| 318 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 319 | |||||||||||||||
| 320 | The full text of the license can be found in the | ||||||||||||||
| 321 | LICENSE file included with this module. | ||||||||||||||
| 322 | |||||||||||||||
| 323 | =cut | ||||||||||||||