| 1 | package XML::MyXML::Object; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use XML::MyXML::Util 'trim', 'strip_ns'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Encode; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 6 | our $VERSION = "1.11"; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 15 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 16 | 0 | my $xml = shift; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 0 | return bless XML::MyXML::xml_to_object($xml), $class; | |||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 6 | my $ch0 = chr(0); | |||||||||||||||||||||||||||||||
| 22 | 50 | ▶ | sub _string_unescape { | ||||||||||||||||||||||||||||||
|
Subroutine
_string_unescape: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 23 | 50 | my $string = shift; | |||||||||||||||||||||||||||||||
| 24 | 50 | ▶ | defined $string or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 33 | $string =~ s/\\(.)/$1/gs; | |||||||||||||||||||||||||||||||
| 26 | 33 | return $string; | |||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 77 | ▶ | sub _parse_description { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_description: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 30 | 77 | my ($desc) = @_; | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 77 | my ($el_name, $el_ns, $attrs_str) = $desc =~ / | |||||||||||||||||||||||||||||||
| 33 | # start anchor | ||||||||||||||||||||||||||||||||
| 34 | ^ | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | # element name | ||||||||||||||||||||||||||||||||
| 37 | ( | ||||||||||||||||||||||||||||||||
| 38 | (?: | ||||||||||||||||||||||||||||||||
| 39 | \\ \[ | ||||||||||||||||||||||||||||||||
| 40 | | | ||||||||||||||||||||||||||||||||
| 41 | \\ \{ | ||||||||||||||||||||||||||||||||
| 42 | | | ||||||||||||||||||||||||||||||||
| 43 | [^\[\{] | ||||||||||||||||||||||||||||||||
| 44 | )* | ||||||||||||||||||||||||||||||||
| 45 | ) | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | # element namespace | ||||||||||||||||||||||||||||||||
| 48 | (?: | ||||||||||||||||||||||||||||||||
| 49 | # opening curly bracket | ||||||||||||||||||||||||||||||||
| 50 | \{ | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | # namespace name | ||||||||||||||||||||||||||||||||
| 53 | ((?: \\ \} | [^\}] )*) | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | # closing curly bracket | ||||||||||||||||||||||||||||||||
| 56 | \} | ||||||||||||||||||||||||||||||||
| 57 | )? | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | # attributes string | ||||||||||||||||||||||||||||||||
| 60 | (.*) | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # end anchor | ||||||||||||||||||||||||||||||||
| 63 | \z | ||||||||||||||||||||||||||||||||
| 64 | /x; | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 77 | my @attrs = $attrs_str =~ / | |||||||||||||||||||||||||||||||
| 67 | # opening square bracket | ||||||||||||||||||||||||||||||||
| 68 | \[ | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | # attribute name | ||||||||||||||||||||||||||||||||
| 71 | ( | ||||||||||||||||||||||||||||||||
| 72 | # attribute characters | ||||||||||||||||||||||||||||||||
| 73 | (?: \\ \] | \\ \= | \\ \{ | [^\]\=\{] )+ | ||||||||||||||||||||||||||||||||
| 74 | ) | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # optional namespace | ||||||||||||||||||||||||||||||||
| 77 | (?: | ||||||||||||||||||||||||||||||||
| 78 | # opening curly bracket | ||||||||||||||||||||||||||||||||
| 79 | \{ | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | # namespace name | ||||||||||||||||||||||||||||||||
| 82 | ((?: \\ \} | [^\}] )*) | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | # closing curly bracket | ||||||||||||||||||||||||||||||||
| 85 | \} | ||||||||||||||||||||||||||||||||
| 86 | )? | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # value option | ||||||||||||||||||||||||||||||||
| 89 | (?: | ||||||||||||||||||||||||||||||||
| 90 | # equals sign | ||||||||||||||||||||||||||||||||
| 91 | \= | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | # value | ||||||||||||||||||||||||||||||||
| 94 | ( | ||||||||||||||||||||||||||||||||
| 95 | (?: \\ \] | [^\]] )* | ||||||||||||||||||||||||||||||||
| 96 | ) | ||||||||||||||||||||||||||||||||
| 97 | )? | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | # closing square bracket | ||||||||||||||||||||||||||||||||
| 100 | \] | ||||||||||||||||||||||||||||||||
| 101 | /gx; | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 77 | my %attrs; | |||||||||||||||||||||||||||||||
| 104 | 77 | ▶ | while (@attrs) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 15 | my ($attr_name, $attr_ns, $attr_value) = splice @attrs, 0, 3; | |||||||||||||||||||||||||||||||
| 106 | # $attr_value =~ s/^\"|\"\z//g if defined $attr_value; | ||||||||||||||||||||||||||||||||
| 107 | 15 | $attrs{_string_unescape $attr_name} = { | |||||||||||||||||||||||||||||||
| 108 | ns => _string_unescape($attr_ns), | ||||||||||||||||||||||||||||||||
| 109 | value => _string_unescape($attr_value), | ||||||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 77 | return ($el_name, $el_ns, \%attrs); | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 155 | ▶ | sub cmp_element { | ||||||||||||||||||||||||||||||
|
Subroutine
cmp_element: called CC=19 SCAR=30.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 117 | 155 | my ($self, $desc) = @_; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | my ($el_name, $el_ns, $attrs) = ref $desc | ||||||||||||||||||||||||||||||||
| 120 | 155 | ▶ | ? @$desc{qw/ el_name el_ns attrs /} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | : _parse_description($desc); | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | # check element name | ||||||||||||||||||||||||||||||||
| 124 | 155 | ▶ | if (length $el_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 147 | ▶ | if (! defined $el_ns) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 126 | 145 | ▶ | $self->{el_name} eq $el_name or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | } elsif (length $el_ns) { | ||||||||||||||||||||||||||||||||
| 128 | 2 | ▶ | $el_name !~ /\:/ or croak 'You can either have a ns requirement, or a ":" in your path segment'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | exists $self->{ns_data}{"$el_ns:"} or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | strip_ns($self->{el_name}) eq $el_name or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||||||||||||||
| 132 | # ! grep /\:\z/, keys %{ $self->{ns_data} } or return 0; | ||||||||||||||||||||||||||||||||
| 133 | # $self->{el_name} eq $el_name or return 0; | ||||||||||||||||||||||||||||||||
| 134 | 0 | croak 'empty ns in path segment'; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | # check attributes | ||||||||||||||||||||||||||||||||
| 139 | 115 | foreach my $attr_name (keys %$attrs) { | |||||||||||||||||||||||||||||||
| 140 | 50 | my ($attr_ns, $attr_value) = @{ $attrs->{$attr_name} }{qw/ ns value /}; | |||||||||||||||||||||||||||||||
| 141 | 50 | ▶ | if (! defined $attr_ns) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 142 | 48 | my $actual_attr_value = $self->attr($attr_name); | |||||||||||||||||||||||||||||||
| 143 | 48 | ▶ | defined $actual_attr_value or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 35 | ▶ | ! defined $attr_value or $attr_value eq $actual_attr_value or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $attr_value or $attr_value eq $actual_attr_value
MC/DC100%2/2
not defined $attr_value or $attr_value eq $actual_attr_value
not defined $attr_value
$attr_value eq $actual_attr_value
Truth table100%3/3
not(A) or B
A= not defined $attr_valueB= $attr_value eq $actual_attr_value
| |||||||||||||||||||||||||||||||||
| 145 | } elsif (length $attr_ns) { | ||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | $attr_name !~ /\:/ or croak 'You can either have a ns requirement, or a ":" in your path segment'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 2 | my $actual_attr_value = $self->{ns_data}{"$attr_ns:$attr_name"}; | |||||||||||||||||||||||||||||||
| 148 | 2 | ▶ | defined $actual_attr_value or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | ! defined $attr_value or $attr_value eq $actual_attr_value or return 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $attr_value or $attr_value eq $actual_attr_value
MC/DC0%0/2
not defined $attr_value or $attr_value eq $actual_attr_value
not defined $attr_value
$attr_value eq $actual_attr_value
Truth table33%1/3
not(A) or B
A= not defined $attr_valueB= $attr_value eq $actual_attr_value
| |||||||||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||||||
| 151 | # my $actual_attr_value = $self->attr($attr_name); | ||||||||||||||||||||||||||||||||
| 152 | # defined $actual_attr_value or return 0; | ||||||||||||||||||||||||||||||||
| 153 | # ! exists $self->{ns_data}{} | ||||||||||||||||||||||||||||||||
| 154 | 0 | croak 'empty ns in path segment'; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 82 | return 1; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 202 | ▶ | sub children { | ||||||||||||||||||||||||||||||
|
Subroutine
children: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 162 | 202 | my $self = shift; | |||||||||||||||||||||||||||||||
| 163 | 202 | my $path_segment = shift; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 202 | ▶ | $path_segment = '' if ! defined $path_segment; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 202 | my @all_children = grep { defined $_->{el_name} } @{$self->{content}}; | |||||||||||||||||||||||||||||||
| 168 | 202 | ▶ | length $path_segment or return @all_children; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 65 | my ($el_name, $el_ns, $attrs) = _parse_description($path_segment); | |||||||||||||||||||||||||||||||
| 171 | 65 | my $desc = { el_name => $el_name, el_ns => $el_ns, attrs => $attrs }; | |||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 65 | return grep $_->cmp_element($desc), @all_children; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 41 | ▶ | sub path { | ||||||||||||||||||||||||||||||
|
Subroutine
path: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 177 | 41 | my $self = shift; | |||||||||||||||||||||||||||||||
| 178 | 41 | my $path = shift; | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 41 | my $original_path = $path; | |||||||||||||||||||||||||||||||
| 181 | 41 | my $path_starts_with_root = $path =~ m|^/|; | |||||||||||||||||||||||||||||||
| 182 | 41 | ▶ | $path = "/$path" unless $path_starts_with_root; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | 41 | my @path_segments = $path =~ m! | |||||||||||||||||||||||||||||||
| 184 | # slash | ||||||||||||||||||||||||||||||||
| 185 | \/ | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | ( | ||||||||||||||||||||||||||||||||
| 188 | # allowed strings | ||||||||||||||||||||||||||||||||
| 189 | (?: | ||||||||||||||||||||||||||||||||
| 190 | # escaped "/" | ||||||||||||||||||||||||||||||||
| 191 | \\ \/ | ||||||||||||||||||||||||||||||||
| 192 | | | ||||||||||||||||||||||||||||||||
| 193 | # escaped "[" | ||||||||||||||||||||||||||||||||
| 194 | \\ \[ | ||||||||||||||||||||||||||||||||
| 195 | | | ||||||||||||||||||||||||||||||||
| 196 | # escaped "{" | ||||||||||||||||||||||||||||||||
| 197 | \\ \{ | ||||||||||||||||||||||||||||||||
| 198 | | | ||||||||||||||||||||||||||||||||
| 199 | # non- "/", "[", "]" | ||||||||||||||||||||||||||||||||
| 200 | [^\/\[\{] | ||||||||||||||||||||||||||||||||
| 201 | | | ||||||||||||||||||||||||||||||||
| 202 | # attribute | ||||||||||||||||||||||||||||||||
| 203 | \[ | ||||||||||||||||||||||||||||||||
| 204 | (?: \\ \] | [^\]] )* | ||||||||||||||||||||||||||||||||
| 205 | \] | ||||||||||||||||||||||||||||||||
| 206 | | | ||||||||||||||||||||||||||||||||
| 207 | # namespace | ||||||||||||||||||||||||||||||||
| 208 | \{ | ||||||||||||||||||||||||||||||||
| 209 | (?: \\ \} | [^\}] )* | ||||||||||||||||||||||||||||||||
| 210 | \} | ||||||||||||||||||||||||||||||||
| 211 | )* | ||||||||||||||||||||||||||||||||
| 212 | ) | ||||||||||||||||||||||||||||||||
| 213 | !gx; | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | 41 | my @result = ($self); | |||||||||||||||||||||||||||||||
| 216 | 41 | ▶ | $self->cmp_element(shift @path_segments) or return if $path_starts_with_root; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$path_starts_with_root && ($self->cmp_element(shift @path_segments) || (return))
$self->cmp_element(shift @path_segments) or return
MC/DC0%0/2
$path_starts_with_root && ($self->cmp_element(shift @path_segments) || (return))
$path_starts_with_root
$self->cmp_element(shift @path_segments)
Truth table66%2/3
A && B
A= $path_starts_with_rootB= $self->cmp_element(shift @path_segments)
| |||||||||||||||||||||||||||||||||
| 217 | 40 | foreach my $path_segment (@path_segments) { | |||||||||||||||||||||||||||||||
| 218 | 58 | ▶ | @result = map $_->children($path_segment), @result or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | 40 | ▶ | return wantarray ? @result : $result[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | 55 | ▶ | sub text { | ||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 224 | 55 | my $self = shift; | |||||||||||||||||||||||||||||||
| 225 | 55 | ▶ | my $flags = (@_ and ref $_[-1]) ? pop : {}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and ref $_[-1]
MC/DC100%2/2
@_ and ref $_[-1]
@_
ref $_[-1]
Truth table100%3/3
A and B
A= @_B= ref $_[-1]
| |||||||||||||||||||||||||||||||||
| 226 | 55 | ▶ | my $set_value = @_ ? (defined $_[0] ? shift : '') : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | 55 | ▶ | if (! defined $set_value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 229 | 52 | my $value = ''; | |||||||||||||||||||||||||||||||
| 230 | 52 | ▶ | if ($self->{content}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | 29 | $value .= $_->text($flags) foreach @{ $self->{content} }; | |||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | 52 | ▶ | if (defined $self->{text} and length $self->{text}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"text"} and length $self->{"text"}
MC/DC50%1/2
defined $self->{"text"} and length $self->{"text"}
defined $self->{"text"}
length $self->{"text"}
Truth table66%2/3
A and B
A= defined $self->{"text"}B= length $self->{"text"}
| |||||||||||||||||||||||||||||||||
| 234 | 23 | my $temp_value = $self->{text}; | |||||||||||||||||||||||||||||||
| 235 | 23 | ▶ | $temp_value = trim $temp_value if $flags->{strip}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | 23 | $value .= $temp_value; | |||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||
| 238 | 52 | return $value; | |||||||||||||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||||||||||||||
| 240 | 3 | ▶ | if (length $set_value) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | 1 | my $entry = bless { | |||||||||||||||||||||||||||||||
| 242 | text => $set_value, | ||||||||||||||||||||||||||||||||
| 243 | parent => $self | ||||||||||||||||||||||||||||||||
| 244 | }, 'XML::MyXML::Object'; | ||||||||||||||||||||||||||||||||
| 245 | 1 | weaken $entry->{parent}; | |||||||||||||||||||||||||||||||
| 246 | 1 | $self->{content} = [ $entry ]; | |||||||||||||||||||||||||||||||
| 247 | } else { | ||||||||||||||||||||||||||||||||
| 248 | 2 | $self->{content} = []; | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | 6 | *value = \&text; | |||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | 7 | ▶ | sub inner_xml { | ||||||||||||||||||||||||||||||
|
Subroutine
inner_xml: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 256 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||
| 257 | 7 | ▶ | my $flags = (@_ and ref $_[-1]) ? pop : {}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and ref $_[-1]
MC/DC100%2/2
@_ and ref $_[-1]
@_
ref $_[-1]
Truth table100%3/3
A and B
A= @_B= ref $_[-1]
| |||||||||||||||||||||||||||||||||
| 258 | 7 | ▶ | my $set_xml = @_ ? defined $_[0] ? shift : '' : undef; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 7 | ▶ | if (! defined $set_xml) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | 3 | my $xml = $self->to_xml($flags); | |||||||||||||||||||||||||||||||
| 262 | 3 | $xml =~ s/^\<.*?\>//s; | |||||||||||||||||||||||||||||||
| 263 | 3 | $xml =~ s/\<\/[^\>]*\>\z//s; # nothing to remove if empty element | |||||||||||||||||||||||||||||||
| 264 | 3 | return $xml; | |||||||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||||||
| 266 | 4 | my $xml = "<div>$set_xml</div>"; | |||||||||||||||||||||||||||||||
| 267 | 4 | my $obj = XML::MyXML::xml_to_object($xml, $flags); | |||||||||||||||||||||||||||||||
| 268 | 4 | $self->{content} = []; | |||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | foreach my $child (@{ $obj->{content} || [] }) { | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$obj->{'content'} || []
MC/DC100%1/1
$obj->{'content'} || []
$obj->{'content'}
Truth table100%2/2
A
A= $obj->{'content'}
| |||||||||||||||||||||||||||||||||
| 270 | 5 | $child->{parent} = $self; | |||||||||||||||||||||||||||||||
| 271 | 5 | weaken $child->{parent}; | |||||||||||||||||||||||||||||||
| 272 | 5 | push @{ $self->{content} }, $child; | |||||||||||||||||||||||||||||||
| 273 | 5 | ▶ | $child->_apply_namespace_declarations if $child->{el_name}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 209 | ▶ | sub attr { | ||||||||||||||||||||||||||||||
|
Subroutine
attr: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 279 | 209 | my $self = shift; | |||||||||||||||||||||||||||||||
| 280 | 209 | my $attr_name = shift; | |||||||||||||||||||||||||||||||
| 281 | 209 | ▶ | my $flags = ref $_[-1] ? pop : {}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 282 | 209 | my ($set_to, $must_set); | |||||||||||||||||||||||||||||||
| 283 | 209 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 284 | 4 | $set_to = shift; | |||||||||||||||||||||||||||||||
| 285 | 4 | $must_set = 1; | |||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 209 | ▶ | if (defined $attr_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 289 | 72 | ▶ | if ($must_set) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 290 | 4 | ▶ | if (defined ($set_to)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 291 | 3 | $self->{attrs}{$attr_name} = $set_to; | |||||||||||||||||||||||||||||||
| 292 | } else { | ||||||||||||||||||||||||||||||||
| 293 | 1 | delete $self->{attrs}{$attr_name}; | |||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | 4 | ▶ | if ($attr_name =~ /^xmlns(\:|\z)/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 296 | 2 | $self->_apply_namespace_declarations; | |||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | 4 | return $set_to; | |||||||||||||||||||||||||||||||
| 299 | } else { | ||||||||||||||||||||||||||||||||
| 300 | 68 | return $self->{attrs}->{$attr_name}; | |||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||
| 302 | } else { | ||||||||||||||||||||||||||||||||
| 303 | 137 | return %{$self->{attrs}}; | |||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 5 | ▶ | sub tag { | ||||||||||||||||||||||||||||||
|
Subroutine
tag: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 308 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||
| 309 | 5 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 5 | my $el_name = $self->{el_name}; | |||||||||||||||||||||||||||||||
| 312 | 5 | ▶ | if (defined $el_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 313 | 5 | ▶ | $el_name =~ s/^.*\:// if $flags->{strip_ns}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 314 | 5 | return $el_name; | |||||||||||||||||||||||||||||||
| 315 | } else { | ||||||||||||||||||||||||||||||||
| 316 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||
| 320 | 6 | *name = \&tag; | |||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | 2 | ▶ | sub parent { | ||||||||||||||||||||||||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 323 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | 2 | return $self->{parent}; | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | 13 | ▶ | sub simplify { | ||||||||||||||||||||||||||||||
|
Subroutine
simplify: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 329 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||
| 330 | 13 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | 13 | my $simple = XML::MyXML::_objectarray_to_simple([$self], $flags); | |||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 13 | ▶ | if ($flags->{internal}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 335 | 5 | ▶ | $simple = | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 336 | ref $simple eq 'HASH' ? (values %$simple)[0] | ||||||||||||||||||||||||||||||||
| 337 | : ref $simple eq 'ARRAY' ? $simple->[1] | ||||||||||||||||||||||||||||||||
| 338 | : croak; | ||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | 13 | return $simple; | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 21 | ▶ | sub to_xml { | ||||||||||||||||||||||||||||||
|
Subroutine
to_xml: called CC=7 SCAR=21.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 345 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||
| 346 | 21 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 21 | my $decl = ''; | |||||||||||||||||||||||||||||||
| 349 | 21 | ▶ | $decl .= qq'<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>\n' if $flags->{complete}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 350 | 21 | my $xml = XML::MyXML::_objectarray_to_xml([$self]); | |||||||||||||||||||||||||||||||
| 351 | $xml = XML::MyXML::tidy_xml($xml, { | ||||||||||||||||||||||||||||||||
| 352 | %$flags, | ||||||||||||||||||||||||||||||||
| 353 | bytes => 0, | ||||||||||||||||||||||||||||||||
| 354 | complete => 0, | ||||||||||||||||||||||||||||||||
| 355 | save => undef | ||||||||||||||||||||||||||||||||
| 356 | 21 | ▶ | }) if $flags->{tidy}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 357 | 21 | $xml = $decl . $xml; | |||||||||||||||||||||||||||||||
| 358 | 21 | ▶ | if (defined $flags->{save}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | open my $fh, '>', $flags->{save} or croak "Error: Couldn't open file '$flags->{save}' for writing: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 360 | 0 | binmode $fh, ':encoding(UTF-8)'; | |||||||||||||||||||||||||||||||
| 361 | 0 | print $fh $xml; | |||||||||||||||||||||||||||||||
| 362 | 0 | close $fh; | |||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||
| 364 | 21 | ▶ | $xml = encode_utf8 $xml if $flags->{bytes}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 365 | 21 | return $xml; | |||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | 1 | ▶ | sub to_tidy_xml { | ||||||||||||||||||||||||||||||
|
Subroutine
to_tidy_xml: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 369 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | my $flags = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||
| 372 | 1 | return $self->to_xml({ %$flags, tidy => 1 }); | |||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||
| 375 | 137 | ▶ | sub _apply_namespace_declarations { | ||||||||||||||||||||||||||||||
|
Subroutine
_apply_namespace_declarations: called CC=17 SCAR=28.5
| |||||||||||||||||||||||||||||||||
| 376 | 137 | my $self = shift; | |||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | # only elements | ||||||||||||||||||||||||||||||||
| 379 | 137 | ▶ | $self->{el_name} or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||
| 381 | 137 | my %attr = $self->attr; | |||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | # parse namespace declarations | ||||||||||||||||||||||||||||||||
| 384 | 137 | my ($ns_info, @cancel_declarations) = ({}); | |||||||||||||||||||||||||||||||
| 385 | 137 | foreach my $ns_decl_attr_name (grep /^xmlns(\:|\z)/, keys %attr) { | |||||||||||||||||||||||||||||||
| 386 | 7 | my ($ns_prefix) = $ns_decl_attr_name =~ /^xmlns(?:\:(.+))?\z/; | |||||||||||||||||||||||||||||||
| 387 | 7 | ▶ | $ns_prefix = '' if ! defined $ns_prefix; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 388 | 7 | ▶ | if (length $attr{$ns_decl_attr_name}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 389 | 5 | $ns_info->{$ns_prefix} = $attr{$ns_decl_attr_name}; | |||||||||||||||||||||||||||||||
| 390 | } else { | ||||||||||||||||||||||||||||||||
| 391 | 2 | push @cancel_declarations, $ns_prefix; | |||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | # insert these declarations into the full_ns_info hashref | ||||||||||||||||||||||||||||||||
| 396 | $self->{full_ns_info} = (%$ns_info or @cancel_declarations) ? { | ||||||||||||||||||||||||||||||||
| 397 | 6 | %{ $self->{parent}{full_ns_info} }, | |||||||||||||||||||||||||||||||
| 398 | %$ns_info, | ||||||||||||||||||||||||||||||||
| 399 | 137 | ▶ | } : $self->{parent}{full_ns_info}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
%$ns_info or @cancel_declarations
MC/DC100%2/2
%$ns_info or @cancel_declarations
%$ns_info
@cancel_declarations
Truth table100%3/3
A or B
A= %$ns_infoB= @cancel_declarations
| |||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | # remove cancelled declarations (can cancel with ns name = "") | ||||||||||||||||||||||||||||||||
| 402 | 137 | delete @{ $self->{full_ns_info} }{@cancel_declarations}; | |||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||
| 404 | # ns_data is... | ||||||||||||||||||||||||||||||||
| 405 | # $ns_name: => undef for element name | ||||||||||||||||||||||||||||||||
| 406 | # $ns_name:$attr_localpart => $attr_value for attributes | ||||||||||||||||||||||||||||||||
| 407 | 137 | $self->{ns_data} = {}; | |||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | # apply all active declarations to element | ||||||||||||||||||||||||||||||||
| 410 | 137 | my $el_name = $self->{el_name}; | |||||||||||||||||||||||||||||||
| 411 | 137 | my $num_colons = () = $el_name =~ /(\:)/g; | |||||||||||||||||||||||||||||||
| 412 | 137 | my $ns_name = do { | |||||||||||||||||||||||||||||||
| 413 | 137 | ▶ | if ($num_colons == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 414 | 133 | $self->{full_ns_info}{''}; | |||||||||||||||||||||||||||||||
| 415 | } elsif ($num_colons == 1) { | ||||||||||||||||||||||||||||||||
| 416 | 4 | my ($prefix) = $el_name =~ /^(.+)?\:./; # colon must not be at start or end | |||||||||||||||||||||||||||||||
| 417 | 4 | ▶ | defined $prefix ? $self->{full_ns_info}{$prefix} : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 418 | } else { | ||||||||||||||||||||||||||||||||
| 419 | 0 | undef; | |||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||
| 421 | }; | ||||||||||||||||||||||||||||||||
| 422 | 137 | ▶ | $self->{ns_data}{"$ns_name:"} = undef if defined $ns_name and length $ns_name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ns_name and length $ns_name
MC/DC50%1/2
defined $ns_name and length $ns_name
defined $ns_name
length $ns_name
Truth table66%2/3
A and B
A= defined $ns_nameB= length $ns_name
| |||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||
| 424 | # apply all active declarations to attributes | ||||||||||||||||||||||||||||||||
| 425 | 137 | foreach my $attr_name (keys %attr) { | |||||||||||||||||||||||||||||||
| 426 | 64 | ▶ | if ($attr_name =~ /^([^\:]+)\:([^\:]+)\z/) { # if has one colon, not at the edges | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 427 | 7 | my ($prefix, $localpart) = ($1, $2); | |||||||||||||||||||||||||||||||
| 428 | 7 | my $ns_name = $self->{full_ns_info}{$prefix}; | |||||||||||||||||||||||||||||||
| 429 | 7 | ▶ | $self->{ns_data}{"$ns_name:$localpart"} = $attr{$attr_name} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ns_name and length $ns_name
MC/DC50%1/2
defined $ns_name and length $ns_name
defined $ns_name
length $ns_name
Truth table66%2/3
A and B
A= defined $ns_nameB= length $ns_name
| |||||||||||||||||||||||||||||||||
| 430 | if defined $ns_name and length $ns_name; | ||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||
| 434 | # continue by applying to all children (and further ancestors) | ||||||||||||||||||||||||||||||||
| 435 | 137 | $_->_apply_namespace_declarations foreach $self->children; | |||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||
| 438 | 6 | 1; | |||||||||||||||||||||||||||||||