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