| 1 | package XML::Struct::Simple; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Scalar::Util qw(reftype blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | our $VERSION = '0.25'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | has root => ( | ||||||||||||||||||||||||||||||||||||||||||
| 10 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | has attributes => ( | ||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 35 | ▶ | coerce => sub { !defined $_[0] or ($_[0] and $_[0] ne 'remove') }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
Branch: 1 branch
Condition83%5/6
!defined($_[0]) || $_[0] && $_[0] ne 'remove'
$_[0] and $_[0] ne "remove"
MC/DC66%2/3
!defined($_[0]) || $_[0] && $_[0] ne 'remove'
!defined($_[0])
$_[0]
$_[0] ne "remove"
Truth table75%3/4
not(A) || B and C
A= !defined($_[0])B= $_[0]C= $_[0] ne "remove"
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | has content => ( | ||||||||||||||||||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | ▶ | default => sub { 'content' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | has depth => ( | ||||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 26 | ▶ | coerce => sub { (defined $_[0] and $_[0] >= 0) ? $_[0] : undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
defined $_[0] and $_[0] >= 0
MC/DC50%1/2
defined $_[0] and $_[0] >= 0
defined $_[0]
$_[0] >= 0
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] >= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | sub transform { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 35 | ▶ | my ($self, $element) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
transform: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 35 | my $simple = $self->transform_content($element,0); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # enforce root for special case <root>text</root> | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 35 | ▶ | if ($self->root or !ref $simple) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->root or not ref $simple
MC/DC100%2/2
$self->root or not ref $simple
$self->root
not ref $simple
Truth table100%3/3
A or not(B)
A= $self->rootB= not ref $simple
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 20 | ▶ | my $root = $self->root !~ /^[+-]?[0-9]+$/ ? $self->root : $element->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 20 | return { $root => $simple }; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 15 | return $simple; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # returns a (possibly empty) hash or a scalar | ||||||||||||||||||||||||||||||||||||||||||
| 45 | sub transform_content { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 113 | ▶ | my ($self, $element, $depth) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
transform_content: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 113 | ▶ | $depth = 0 if !defined $depth; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 113 | ▶ | if (defined $self->depth and $depth >= $self->depth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $self->depth and $depth >= $self->depth
MC/DC100%2/2
defined $self->depth and $depth >= $self->depth
defined $self->depth
$depth >= $self->depth
Truth table100%3/3
A and B
A= defined $self->depthB= $depth >= $self->depth
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 14 | return $element; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } elsif ( @$element == 1 ) { # empty tag | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return { }; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 99 | my $attributes = {}; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 99 | my $children; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 99 | ▶ | if ( reftype $element->[1] eq 'HASH' ) { # [ $tag, \%attributes, \@children ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 93 | ▶ | $attributes = $element->[1] if $self->attributes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 93 | $children = $element->[2]; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } else { # [ $tag, \@children ] | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | $children = $element->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # no element children | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 99 | ▶ | unless ( first { ref $_ } @$children ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 54 | my $content = join "", @$children; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 54 | ▶ | if (!%$attributes) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 39 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } elsif ($content eq '') { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | return { %$attributes }; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | return { %$attributes, $self->content => $content }; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 45 | my $simple = { map {$_ => [$attributes->{$_}] } keys %$attributes }; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 45 | foreach my $child ( @$children ) { | |||||||||||||||||||||||||||||||||||||||||
| 80 | 86 | ▶ | next unless ref $child; # skip mixed content text | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 78 | my $name = $child->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 78 | my $content = $self->transform_content($child, $depth+1); | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 78 | ▶ | if ( $simple->{$name} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 14 | push @{$simple->{$name}}, $content; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 64 | $simple->{$name} = [$content]; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 45 | foreach my $name (keys %$simple) { | |||||||||||||||||||||||||||||||||||||||||
| 93 | 70 | ▶ | next if @{$simple->{$name}} != 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 57 | my $c = $simple->{$name}->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 57 | ▶ | if (!ref $c or (!blessed $c and reftype $c eq 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not ref $c or not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
MC/DC66%2/3
not ref $c or not builtin::blessed($c) and builtin::reftype($c) eq "HASH"
not ref $c
not builtin::blessed($c)
builtin::reftype($c) eq "HASH"
Truth table75%3/4
not(A) or not(B) and C
A= not ref $cB= not builtin::blessed($c)C= builtin::reftype($c) eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 49 | $simple->{$name} = $c; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 45 | return $simple; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | sub removeXMLAttr { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | ▶ | my $node = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
removeXMLAttr: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | ref $node | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ? ( $node->[2] | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | ▶ | ? [ $node->[0], [ map { removeXMLAttr($_) } @{$node->[2]} ] ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | : [ $node->[0] ] ) # empty element | ||||||||||||||||||||||||||||||||||||||||||
| 109 | : $node; # text node | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||||||||||