line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package XML::LibXML::TreeDumper; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: print a tree of libxml elements |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
2231
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
144
|
|
6
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
104
|
|
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
19
|
use Carp; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
294
|
|
9
|
4
|
|
|
4
|
|
4209
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Moo; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.01; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has data => ( is => 'rw', trigger => \&_parse ); |
15
|
|
|
|
|
|
|
has node => ( is => 'rw', lazy => 1, builder => \&_parse); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub dump { |
18
|
|
|
|
|
|
|
my ($self, $xpath) = @_; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my @nodes = $xpath ? $self->node->findnodes( $xpath ) : $self->node; |
21
|
|
|
|
|
|
|
my $string = ''; |
22
|
|
|
|
|
|
|
for my $node ( @nodes ) { |
23
|
|
|
|
|
|
|
$self->_dump_node( $node, \$string ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
return $string; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _parse { |
30
|
|
|
|
|
|
|
my ($self) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $parser = XML::LibXML->new; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $doc; |
35
|
|
|
|
|
|
|
if ( ref $self->data ) { |
36
|
|
|
|
|
|
|
$doc = $parser->parse_string( ${ $self->data } ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
|
|
|
|
|
|
$doc = $parser->parse_file( $self->data ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$self->node( $doc->getDocumentElement ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _dump_node { |
46
|
|
|
|
|
|
|
my ($self, $node, $out, $indent) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$indent //= ''; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $name = $node->getName; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $text = $node->isa( 'XML::LibXML::Text' ) ? $node->getData : ''; |
53
|
|
|
|
|
|
|
if ( length $text ) { |
54
|
|
|
|
|
|
|
$text =~ s/\\/\\\\/g; |
55
|
|
|
|
|
|
|
$text =~ s/"/\\"/g; |
56
|
|
|
|
|
|
|
#$text =~ s/([[:cntrl:]])/quotemeta($1)/eg; |
57
|
|
|
|
|
|
|
$name = sprintf '"%s"', $text; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$$out .= $indent . ref( $node ) . ' ' . $name . "\n"; |
61
|
|
|
|
|
|
|
my @children = $node->childNodes; |
62
|
|
|
|
|
|
|
for my $child ( @children ) { |
63
|
|
|
|
|
|
|
$self->_dump_node( $child, $out, $indent . ( ' ' x 4 ) ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |