PML/Schema/XMLNode.pm

filter: Statementstmt 92.8% 116 / 125 Branchbran 60.0% 36 / 60 Conditioncond 48.9% 24 / 49 MC/DCmcdc 17.8% 5 / 28 Subroutinesub 100% 17 / 17 Podpod 28.5% 2 / 7 totaltotal 69.9% 200 / 286 CC 52 SCAR 27.8
CC
52
Cov
75%
write
38.6
serialize
23.0
serialize_children
7.3
CRAP 16.1
« PML/Backend/NTRED.pm ↑ Summary PML/FSFormat.pm »
1 package Treex::PML::Schema::XMLNode;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 $VERSION='2.29'; # version template
9 }
10 9 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Scalar::Util qw(weaken isweak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 82 ▶sub copy_decl {
Subroutine copy_decl: called CC=3 SCAR=11.0
Pod documented
17 82 my ($self,$t)=@_;
18 82 my $copy;
19 82 ▶ if (ref $t->{-schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t->{'-schema'}) { } 63 19 82
20 63 $copy = Treex::PML::CloneValue($t,[$t->{-parent},$t->{-schema}], [$self,$self->{-schema}]);
21 } else {
22 19 $copy = Treex::PML::CloneValue($t,[$t->{-parent}], [$self]);
23 }
24 82 ▶ if (exists $self->{'-##'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-##"}) 82 0 82
25 82 $copy->{'-#'}=$self->{'-##'}++;
26 }
27 # we must do this here, otherwise any operation
28 # that rewrites this value will create an unaccessible crircular reference
29 Treex::PML::Schema::_traverse_data(
30 370 ▶ $copy => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
31 370 my ($val,$is_hash) = @_;
32 370 ▶ weaken($val->{-parent}) if ref($val->{-parent}) and not isweak($val->{-parent});
Branch: 1 branch
BranchTrueFalseTotal
if ref $val->{"-parent"} and not builtin::is_weak($val->{"-parent"}) 325 45 370
Condition66%2/3
ref $val->{"-parent"} and not builtin::is_weak($val->{"-parent"})
!ll&&!rl&&r
450325
MC/DC50%1/2
ref $val->{"-parent"} and not builtin::is_weak($val->{"-parent"})
ref $val->{"-parent"} not builtin::is_weak($val->{"-parent"})
Truth table66%2/3
A and not(B)
A= ref $val->{"-parent"}B= not builtin::is_weak($val->{"-parent"})
ABresult
0X0
100
111
33 370 ▶ weaken($val->{-schema}) if ref($val->{-schema}) and not isweak($val->{-schema});
Branch: 1 branch
BranchTrueFalseTotal
if ref $val->{"-schema"} and not builtin::is_weak($val->{"-schema"}) 282 88 370
Condition66%2/3
ref $val->{"-schema"} and not builtin::is_weak($val->{"-schema"})
!ll&&!rl&&r
880282
MC/DC50%1/2
ref $val->{"-schema"} and not builtin::is_weak($val->{"-schema"})
ref $val->{"-schema"} not builtin::is_weak($val->{"-schema"})
Truth table66%2/3
A and not(B)
A= ref $val->{"-schema"}B= not builtin::is_weak($val->{"-schema"})
ABresult
0X0
100
111
34 },
35 {
36 82 $self->{-schema}=>1, $self=> 1 # do not recurse into these
37 },
38 1, # only hashes
39 );
40 82 return $copy;
41 }
42  
43 270 ▶sub serialize_attributes {
Subroutine serialize_attributes: called CC=7 SCAR=19.6
Pod undocumented
44 270 my ($self,$opts)=@_;
45 270 ▶ my $attributes = $self->{-attributes}||[];
Condition50%1/2
$self->{'-attributes'} || []
l!l
2700
MC/DC0%0/1
$self->{'-attributes'} || []
$self->{'-attributes'}
Truth table50%1/2
A
A= $self->{'-attributes'}
Aresult
11
00
46 270 my @ret;
47 270 for my $attr (@$attributes) {
48 366 ▶ next if $attr=~/^xmlns/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^xmlns/ 11 355 366
49 355 my $value = $self->{$attr};
50 355 ▶ if (!defined($value) and $attr eq 'name') { # FIXME: THIS IS A HACK
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $attr eq "name") 121 234 355
Condition66%2/3
not defined $value and $attr eq "name"
!ll&&!rl&&r
2340121
MC/DC50%1/2
not defined $value and $attr eq "name"
not defined $value $attr eq "name"
Truth table66%2/3
not(A) and B
A= not defined $valueB= $attr eq "name"
ABresult
0X0
100
111
51 121 $value = $self->{'-'.$attr};
52 }
53 355 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 355 0 355
54 355 push @ret, $attr, $value;
55 }
56 }
57 270 return \@ret;
58 }
59  
60 253 ▶sub serialize_exclude_keys {}
Subroutine serialize_exclude_keys: called CC=1 SCAR=0.0
Pod undocumented
61 264 ▶sub serialize_get_children {
Subroutine serialize_get_children: called CC=7 SCAR=19.6
Pod undocumented
62 264 my ($self,$opts)=@_;
63 264 my %exclude;
64 @exclude{
65 264 ▶ @{$self->{-attributes}||[]},
Condition50%1/2
$self->{'-attributes'} || []
l!l
2640
MC/DC0%0/1
$self->{'-attributes'} || []
$self->{'-attributes'}
Truth table50%1/2
A
A= $self->{'-attributes'}
Aresult
11
00
66 $self->serialize_exclude_keys($opts)
67 }=();
68 my @children = map {
69 192 my $name = $_;
70 192 my $val = $self->{$_};
71 121 ▶ (ref($val) eq 'HASH') ? ( map { [$name,$_] } grep { UNIVERSAL::DOES::does($_,'Treex::PML::Schema::XMLNode') } values(%{$val})) :
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 8 133 141
72 192 ▶ (ref($val) eq 'ARRAY') ? ( map { [$name,$_] } grep { UNIVERSAL::DOES::does($_,'Treex::PML::Schema::XMLNode') } @{$val}) :
Branch: 2 branches
BranchTrueFalseTotal
ref $val eq 'HASH' ? : 51 141 192
UNIVERSAL::DOES::does($val, 'Treex::PML::Schema::XMLNode') || !ref($val) ? : 133 0 133
Condition33%1/3
UNIVERSAL::DOES::does($val, "Treex::PML::Schema::XMLNode") or not ref $val
l!l&&r!l&&!r
13300
MC/DC0%0/2
UNIVERSAL::DOES::does($val, "Treex::PML::Schema::XMLNode") or not ref $val
UNIVERSAL::DOES::does($val, "Treex::PML::Schema::XMLNode") not ref $val
Truth table33%1/3
A or not(B)
A= UNIVERSAL::DOES::does($val, "Treex::PML::Schema::XMLNode")B= not ref $val
ABresult
1X1
011
000
73 (UNIVERSAL::DOES::does($val,'Treex::PML::Schema::XMLNode') or !ref($val)) ? [$name,$val] : ()
74 264 ▶ } grep {!/^[-@]/ and !exists($exclude{$_})} keys %$self;
Condition100%3/3
/^[-\@]/ || exists $exclude{$_}
l!l&&r!l&&!r
1938270192
MC/DC100%2/2
/^[-\@]/ || exists $exclude{$_}
/^[-\@]/ exists $exclude{$_}
Truth table100%3/3
A || B
A= /^[-\@]/B= exists $exclude{$_}
ABresult
1X1
011
000
75 return (
76 269 (grep { !ref($_->[1]) } @children),
77 264 sort { $a->[1]{'-#'} <=> $b->[1]{'-#'} } grep { ref($_->[1]) } @children
78 )
79 }
80 170 ▶sub serialize_children {
Subroutine serialize_children: called CC=7 SCAR=19.9
Pod undocumented
81 170 my ($self,$opts,$children)=@_;
82 170 ▶ my $writer = $opts->{writer} || croak __PACKAGE__."->serialize: missing required option 'writer'!\n";
Condition50%1/2
$opts->{'writer'} || croak("Treex::PML::Schema::XMLNode->serialize: missing required option 'writer'!\n")
l!l
1700
MC/DC0%0/1
$opts->{'writer'} || croak("Treex::PML::Schema::XMLNode->serialize: missing required option 'writer'!\n")
$opts->{'writer'}
Truth table50%1/2
A
A= $opts->{'writer'}
Aresult
11
00
83 170 my $ns = $opts->{DefaultNs};
84 170 ▶ $children ||= [$self->serialize_get_children($opts)];
Condition50%1/2
$children ||= [$self->serialize_get_children($opts)]
l!l
1700
MC/DC0%0/1
$children ||= [$self->serialize_get_children($opts)]
$children
Truth table50%1/2
A
A= $children
Aresult
11
00
85 170 for my $child (@$children) {
86 317 my ($key,$value) = @$child;
87 317 ▶ if (UNIVERSAL::DOES::does($value,'Treex::PML::Schema::XMLNode')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::DOES::does($value, 'Treex::PML::Schema::XMLNode')) { } 269 48 317
88 269 $value->serialize($opts);
89 } else {
90 48 my $tag = [$ns,$key];
91 48 ▶ $writer->startTag($tag) if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 48 0 48
92 48 $writer->characters($value);
93 48 ▶ $writer->endTag($tag) if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 48 0 48
94 }
95 }
96 }
97 280 ▶sub serialize {
Subroutine serialize: called CC=14 SCAR=31.4
Pod undocumented
98 280 my ($self,$opts)=@_;
99 280 ▶ my $writer = $opts->{writer} || croak __PACKAGE__."->serialize: missing required option 'writer'!\n";
Condition50%1/2
$opts->{'writer'} || croak("Treex::PML::Schema::XMLNode->serialize: missing required option 'writer'!\n")
l!l
2800
MC/DC0%0/1
$opts->{'writer'} || croak("Treex::PML::Schema::XMLNode->serialize: missing required option 'writer'!\n")
$opts->{'writer'}
Truth table50%1/2
A
A= $opts->{'writer'}
Aresult
11
00
100 280 my $xml_name = $self->{-xml_name};
101 280 ▶ if ($xml_name =~/^#/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($xml_name =~ /^#/) { } 10 270 280
elsif ($xml_name =~ /^{(.*)}(.*)$/ or $xml_name =~ /^()([^#].*)$/) { } 270 0 270
102 10 ▶ if ($xml_name =~/^#text/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($xml_name =~ /^#text/) { } 0 10 10
elsif ($xml_name =~ /^#comment/) { } 10 0 10
elsif ($xml_name =~ /^#processing-instruction/) { } 0 0 0
elsif ($xml_name =~ /^#other/) { } 0 0 0
103 0 $writer->characters($self->{-value});
104 } elsif ($xml_name =~/^#comment/) {
105 10 my $value = $self->{-value};
106 10 $value=~s/^ | $//g; # remove a leading and trailing space - XML::Writer addes them
107 10 $writer->comment($value);
108 } elsif ($xml_name =~/^#processing-instruction/) {
109 0 $writer->pi($self->{-name}, $self->{-value});
110 } elsif ($xml_name =~/^#other/) {
111 0 $writer->raw($self->{-xml});
112 } else {
113 # ignoring
114 }
115 ▶ } elsif ($xml_name=~/^{(.*)}(.*)$/ or $xml_name=~/^()([^#].*)$/) {
Condition33%1/3
$xml_name =~ /^{(.*)}(.*)$/ or $xml_name =~ /^()([^#].*)$/
l!l&&r!l&&!r
02700
MC/DC0%0/2
$xml_name =~ /^{(.*)}(.*)$/ or $xml_name =~ /^()([^#].*)$/
$xml_name =~ /^{(.*)}(.*)$/ $xml_name =~ /^()([^#].*)$/
Truth table33%1/3
A or B
A= $xml_name =~ /^{(.*)}(.*)$/B= $xml_name =~ /^()([^#].*)$/
ABresult
1X1
011
000
116 270 my ($ns,$name)=($1,$2);
117 270 ▶ my $attrs = $self->serialize_attributes($opts) || [];
Condition50%1/2
$self->serialize_attributes($opts) || []
l!l
2700
MC/DC0%0/1
$self->serialize_attributes($opts) || []
$self->serialize_attributes($opts)
Truth table50%1/2
A
A= $self->serialize_attributes($opts)
Aresult
11
00
118 270 ▶ my $prefix = $self->{-xml_prefix} || '';
Condition50%1/2
$self->{'-xml_prefix'} || ''
l!l
0270
MC/DC0%0/1
$self->{'-xml_prefix'} || ''
$self->{'-xml_prefix'}
Truth table50%1/2
A
A= $self->{'-xml_prefix'}
Aresult
11
00
119 270 ▶ $ns ||= $opts->{DefaultNs};
Condition33%1/3
$ns ||= $opts->{"DefaultNs"}
l!l&&r!l&&!r
02700
MC/DC0%0/2
$ns ||= $opts->{"DefaultNs"}
$ns $opts->{"DefaultNs"}
Truth table33%1/3
A ||= B
A= $nsB= $opts->{"DefaultNs"}
ABresult
1X1
011
000
120 270 ▶ if (($ns ne $opts->{DefaultNs})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns ne $opts->{"DefaultNs"}) 0 270 270
121 0 $writer->addPrefix($ns => $prefix);
122 }
123 270 $writer->addPrefix($ns => $prefix);
124 {
125 270 my @children = $self->serialize_get_children($opts);
126 270 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) { } 170 100 270
127 170 $writer->startTag([$ns,$name], @$attrs);
128 170 $self->serialize_children($opts,\@children);
129 170 $writer->endTag([$ns,$name]);
130 } else {
131 100 $writer->emptyTag([$ns,$name], @$attrs);
132 }
133 }
134 }
135 }
136  
137 11 ▶sub write {
Subroutine write: called CC=15 SCAR=36.5
Pod documented
138 11 my ($self,$opts)=@_;
139 11 my $fh;
140 my $have_backup;
141 11 my $filename = $opts->{filename};
142 11 ▶ if (!defined($opts->{fh}) and
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts->{"fh"} || defined $opts->{"string"} and defined $filename) 11 0 11
Condition33%2/6
not defined $opts->{"fh"} || defined $opts->{"string"} and defined $filename
!ll&&!rl&&r
0011
defined $opts->{'fh'} || defined $opts->{'string'}
l!l&&r!l&&!r
0011
MC/DC0%0/3
not defined $opts->{"fh"} || defined $opts->{"string"} and defined $filename
defined $opts->{'fh'} defined $opts->{'string'} defined $filename
Truth table25%1/4
not(A || B) and C
A= defined $opts->{'fh'}B= defined $opts->{'string'}C= defined $filename
ABCresult
1XX0
01X0
0000
0011
143 !defined($opts->{string}) and
144 defined($filename)) {
145 11 ▶ unless ($opts->{no_backups}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"no_backups"}) 11 0 11
146 11 ▶ eval { Treex::PML::IO::rename_uri($filename,$filename."~"); $have_backup=1; } || carp($@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { Treex::PML::IO::rename_uri($filename, $filename . '~'); $have_backup = 1 } } or carp($@) 11 0 11
147 }
148 11 ▶ $fh = Treex::PML::IO::open_backend($filename,'w')
Condition50%1/2
Treex::PML::IO::open_backend($filename, 'w') || die("Cannot open $filename for writing: $!")
l!l
110
MC/DC0%0/1
Treex::PML::IO::open_backend($filename, 'w') || die("Cannot open $filename for writing: $!")
Treex::PML::IO::open_backend($filename, 'w')
Truth table50%1/2
A
A= Treex::PML::IO::open_backend($filename, 'w')
Aresult
11
00
149 || die "Cannot open $filename for writing: $!";
150 11 binmode $fh;
151 }
152 11 eval {
153 my $writer = XML::Writer->new(
154 OUTPUT => ($opts->{fh} || $opts->{string} || $fh ),
155 DATA_MODE => $opts->{no_indent} ? 0 : 1,
156 11 ▶ DATA_INDENT => $opts->{no_indent} ? 0 : 1,
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'no_indent'} ? : 0 11 11
$opts->{'no_indent'} ? : 0 11 11
Condition33%2/6
$opts->{'fh'} || $opts->{'string'} || $fh
l!l&&r!l&&!r
0110
$opts->{"fh"} or $opts->{"string"}
l!l&&r!l&&!r
0011
MC/DC0%0/3
$opts->{'fh'} || $opts->{'string'} || $fh
$opts->{"fh"} $opts->{"string"} $fh
Truth table25%1/4
A or B || C
A= $opts->{"fh"}B= $opts->{"string"}C= $fh
ABCresult
1XX1
01X1
0011
0000
157 NAMESPACES => 1,
158 PREFIX_MAP => {
159 (Treex::PML::Schema->PML_SCHEMA_NS) => '',
160 });
161 11 $self->serialize({
162 writer => $writer,
163 DefaultNs => Treex::PML::Schema->PML_SCHEMA_NS,
164 });
165 11 $writer->end();
166 };
167 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
168 0 my $err=$@;
169 0 ▶ $have_backup && eval { Treex::PML::IO::rename_uri($filename."~",$filename) };
Branch: 1 branch
BranchTrueFalseTotal
$have_backup and eval { do { Treex::PML::IO::rename_uri($filename . '~', $filename) } } 0 0 0
170 0 ▶ $err.=$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
171 0 carp("Error while saving schema: $err\n");
172 }
173 11 ▶ Treex::PML::IO::close_backend($fh) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 11 0 11
174 }
175  
176 2869 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
177 2869 my ($self)=@_;
178 2869 %$self=(); # this should not be needed, but
179 # without it, perl 5.10 leaks on weakened
180 # structures, try:
181 # Scalar::Util::weaken({}) while 1
182  
183 }
184  
185  
186 9 1;
187 __END__
« PML/Backend/NTRED.pm ↑ Summary PML/FSFormat.pm »