PML/Schema/Reader.pm

filter: Statementstmt 86.9% 113 / 130 Branchbran 63.4% 52 / 82 Conditioncond 30.0% 12 / 40 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 64.8% 192 / 296 CC 58 SCAR 38.8
CC
58
Cov
70%
new
88.3
parse_element
50.0
reader
2.5
CRAP 48.3
« PML/Schema/CDATA.pm ↑ Summary PML/Node.pm »
1 package Treex::PML::Schema::Reader;
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  
13 9 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 67 ▶sub new {
Subroutine new: called CC=17 SCAR=44.8
Pod undocumented
17 67 my ($class,$opts)=@_;
18 67 my $URL = $opts->{URL};
19 67 my @common = (
20 no_xinclude_nodes => 1,
21 no_cdata => 1,
22 expand_xinclude => 1,
23 no_blanks => 1,
24 expand_entities => 1,
25 suppress_errors => 0,
26 suppress_warnings => 0,
27 );
28 67 ▶ if ($opts->{validate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"validate"}) 0 67 67
29 0 ▶ my $rng = $opts->{relaxng_schema} || Treex::PML::FindInResources('pml_schema_inline.rng');
Condition0%0/3
$opts->{'relaxng_schema'} || Treex::PML::FindInResources('pml_schema_inline.rng')
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'relaxng_schema'} || Treex::PML::FindInResources('pml_schema_inline.rng')
$opts->{'relaxng_schema'} Treex::PML::FindInResources('pml_schema_inline.rng')
Truth table0%0/3
A || B
A= $opts->{'relaxng_schema'}B= Treex::PML::FindInResources('pml_schema_inline.rng')
ABresult
1X1
011
000
30 0 ▶ if (defined $rng) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rng) { } 0 0 0
31 0 push @common, (RelaxNG => $rng);
32 } else {
33 0 warn __PACKAGE__.": Validation requested, but 'pml_schema_inline.rng' was not found in the ResourcePath: ".Treex::PML::ResourcePath()."\n";
34 }
35 }
36 67 my ($reader,$fh);
37 # print "loading schema $opts->{URL}\n";
38 67 ▶ if ($opts->{string}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'string'}) { } 0 67 67
elsif ($opts->{'fh'}) { } 0 67 67
elsif (builtin::blessed($opts->{'reader'}) and $opts->{'reader'}->isa('XML::LibXML::Reader')) { } 0 67 67
39 0 ▶ $URL ||= 'string://';
Condition0%0/2
$URL ||= "string://"
l!l
00
MC/DC0%0/1
$URL ||= "string://"
$URL
Truth table0%0/2
A
A= $URL
Aresult
11
00
40 $reader = XML::LibXML::Reader->new(string => $opts->{string},
41 0 ▶ @common,
Branch: 1 branch
BranchTrueFalseTotal
$reader = "XML::LibXML::Reader"->new("string", $opts->{"string"}, @common, "URI", $URL) or die "Error reading string ($URL)" 0 0 0
42 URI => $URL,
43 )
44 or die "Error reading string ($URL)";
45 } elsif ($opts->{fh}) {
46 0 ▶ $URL ||= 'fh://';
Condition0%0/2
$URL ||= "fh://"
l!l
00
MC/DC0%0/1
$URL ||= "fh://"
$URL
Truth table0%0/2
A
A= $URL
Aresult
11
00
47 0 ▶ $reader = XML::LibXML::Reader->new(IO => $opts->{string}, @common )
Branch: 1 branch
BranchTrueFalseTotal
$reader = "XML::LibXML::Reader"->new("IO", $opts->{"string"}, @common) or die "Error reading file-handle $fh ($URL)" 0 0 0
48 or die "Error reading file-handle $fh ($URL)";
49 ▶ } elsif (blessed($opts->{reader}) and $opts->{reader}->isa('XML::LibXML::Reader')) {
Condition33%1/3
builtin::blessed($opts->{"reader"}) and $opts->{"reader"}->isa("XML::LibXML::Reader")
!ll&&!rl&&r
6700
MC/DC0%0/2
builtin::blessed($opts->{"reader"}) and $opts->{"reader"}->isa("XML::LibXML::Reader")
builtin::blessed($opts->{"reader"}) $opts->{"reader"}->isa("XML::LibXML::Reader")
Truth table33%1/3
A and B
A= builtin::blessed($opts->{"reader"})B= $opts->{"reader"}->isa("XML::LibXML::Reader")
ABresult
0X0
100
111
50 0 $reader = $opts->{reader};
51 0 ▶ $URL ||= $reader->document->URI;
Condition0%0/3
$URL ||= $reader->document->URI
l!l&&r!l&&!r
000
MC/DC0%0/2
$URL ||= $reader->document->URI
$URL $reader->document->URI
Truth table0%0/3
A ||= B
A= $URLB= $reader->document->URI
ABresult
1X1
011
000
52 } else {
53 67 my $file = $opts->{URL};
54 67 ▶ print STDERR "parsing schema $file\n" if $Treex::PML::Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::Debug 0 67 67
55 67 $fh = eval { Treex::PML::IO::open_uri($file) };
56 67 ▶ croak "Couldn't open PML schema file '$file'\n".$@ if (!$fh || $@);
Branch: 1 branch
BranchTrueFalseTotal
if not $fh or $@ 0 67 67
Condition33%1/3
not $fh or $@
l!l&&r!l&&!r
0067
MC/DC0%0/2
not $fh or $@
not $fh $@
Truth table33%1/3
not(A) or B
A= not $fhB= $@
ABresult
1X1
011
000
57 67 ▶ $reader = XML::LibXML::Reader->new(FD => $fh, @common, URI => $URL )
Branch: 1 branch
BranchTrueFalseTotal
$reader = "XML::LibXML::Reader"->new("FD", $fh, @common, "URI", $URL) or die "Error reading $file" 67 0 67
58 or die "Error reading $file";
59 }
60 67 return bless [$reader,$opts,$fh], $class;
61 }
62 67 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.1
63 67 my ($self)=@_;
64 67 my $fh = $self->file_handle;
65 67 ▶ Treex::PML::IO::close_uri($fh) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 67 0 67
66 }
67 3030 ▶sub reader {
Subroutine reader: called CC=2 SCAR=9.2
Pod undocumented
68 3030 ▶ return ref($_[0]) && $_[0][0];
Condition33%1/3
ref $_[0] && $_[0][0]
!ll&&!rl&&r
003030
MC/DC0%0/2
ref $_[0] && $_[0][0]
ref $_[0] $_[0][0]
Truth table33%1/3
A && B
A= ref $_[0]B= $_[0][0]
ABresult
0X0
100
111
69 }
70 2963 ▶sub options {
Subroutine options: called CC=2 SCAR=9.2
Pod undocumented
71 2963 ▶ return ref($_[0]) && $_[0][1];
Condition33%1/3
ref $_[0] && $_[0][1]
!ll&&!rl&&r
002963
MC/DC0%0/2
ref $_[0] && $_[0][1]
ref $_[0] $_[0][1]
Truth table33%1/3
A && B
A= ref $_[0]B= $_[0][1]
ABresult
0X0
100
111
72 }
73 67 ▶sub file_handle {
Subroutine file_handle: called CC=2 SCAR=9.2
Pod undocumented
74 67 ▶ return ref($_[0]) && $_[0][2];
Condition33%1/3
ref $_[0] && $_[0][2]
!ll&&!rl&&r
0067
MC/DC0%0/2
ref $_[0] && $_[0][2]
ref $_[0] $_[0][2]
Truth table33%1/3
A && B
A= ref $_[0]B= $_[0][2]
ABresult
0X0
100
111
75 }
76  
77 2963 ▶sub parse_element {
Subroutine parse_element: called CC=38 SCAR=39.1
Pod undocumented
78 2963 my ($self,$parent)=@_;
79 2963 my $reader = $self->reader;
80 2963 my $opts = $self->options;
81 2963 my (@children,@attrs);
82 2963 my $el_ns = $reader->namespaceURI;
83 2963 my $el_name = $reader->localName;
84 2963 ▶ my $has_default_ns = $el_ns eq $opts->{DefaultNs} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$el_ns eq $opts->{'DefaultNs'} ? : 2963 0 2963
85 2963 ▶ my $el_ns_name = ($has_default_ns) ? $el_name : '{'.$el_ns.'}'.$el_name;
Branch: 1 branch
BranchTrueFalseTotal
$has_default_ns ? : 2963 0 2963
86 2963 my $prefix = $reader->prefix;
87 2963 ▶ my %val = (
Branch: 2 branches
BranchTrueFalseTotal
$has_default_ns ? : 2963 0 2963
defined $prefix && length $prefix ? : 0 2963 2963
Condition33%1/3
defined $prefix and length $prefix
!ll&&!rl&&r
296300
MC/DC0%0/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table33%1/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
88 -xml_name => $el_ns_name,
89 ($has_default_ns ? () : (-xml_ns => $el_ns)),
90 (defined($prefix) && length($prefix) ? (-xml_prefix => $prefix) : ()),
91 -parent => $parent,
92 -attributes => \@attrs,
93 );
94 2963 ▶ weaken($val{-parent}) if $val{-parent};
Branch: 1 branch
BranchTrueFalseTotal
if $val{"-parent"} 2896 67 2963
95  
96 2963 ▶ if ($reader->moveToFirstAttribute==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->moveToFirstAttribute == 1) 2062 901 2963
97 2062 ▶ do {{
Branch: 1 branch
BranchTrueFalseTotal
$reader->moveToNextAttribute and do { do { { my $name = $reader->name; push @attrs, $name; $val{$name} = $reader->value; } } } 1120 2062 3182
98 3182 my $name = $reader->name;
99 3182 push @attrs,$name;
100 3182 $val{$name} = $reader->value;
101 }} while ($reader->moveToNextAttribute);
102 2062 $reader->moveToElement;
103 }
104 2963 my $obj = \%val;
105 {
106 2963 ▶ my $class = $opts->{Bless}{$el_ns_name} || $opts->{Bless}{'*'};
Condition66%2/3
$opts->{'Bless'}{$el_ns_name} || $opts->{'Bless'}{'*'}
l!l&&r!l&&!r
23536100
MC/DC0%0/2
$opts->{'Bless'}{$el_ns_name} || $opts->{'Bless'}{'*'}
$opts->{'Bless'}{$el_ns_name} $opts->{'Bless'}{'*'}
Truth table66%2/3
A || B
A= $opts->{'Bless'}{$el_ns_name}B= $opts->{'Bless'}{'*'}
ABresult
1X1
011
000
107 2963 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 2963 0 2963
108 2963 bless $obj,$class;
109 2963 ▶ $obj->init($opts) if $obj->can('init');
Branch: 1 branch
BranchTrueFalseTotal
if $obj->can("init") 1081 1882 2963
110 }
111 }
112 2963 my $depth = $reader->depth;
113 2963 my $status;
114 2963 ▶ while (($status = $reader->read==1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status = $reader->read == 1) 5813 0 5813
115 8709 ▶ last unless $reader->depth > $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->depth > $depth 2963 5746 8709
116 5746 my $nodeType = $reader->nodeType;
117 5746 my $chld;
118 5746 my $redo = 0;
119 5746 ▶ if ($nodeType == XML_READER_TYPE_ELEMENT) {
Branch: 5 branches
BranchTrueFalseTotal
if ($nodeType == 1) { } 2896 2850 5746
elsif ($nodeType == 3 or $nodeType == 4) { } 582 2268 2850
elsif ($nodeType == 8) { } 323 1945 2268
elsif ($nodeType == 7) { } 0 1945 1945
elsif ($nodeType == 15 or $nodeType == 14 or $nodeType == 13) { } 1945 0 1945
120 2896 $chld = $self->parse_element($obj);
121 2896 $redo = 1;
122 ▶ } elsif ($nodeType == XML_READER_TYPE_TEXT or
Condition66%2/3
$nodeType == 3 or $nodeType == 4
l!l&&r!l&&!r
58202268
MC/DC50%1/2
$nodeType == 3 or $nodeType == 4
$nodeType == 3 $nodeType == 4
Truth table66%2/3
A or B
A= $nodeType == 3B= $nodeType == 4
ABresult
1X1
011
000
123 $nodeType == XML_READER_TYPE_CDATA) {
124 582 $chld = bless {
125 -xml_name => '#text',
126 -value => $reader->value,
127 }, 'Treex::PML::Schema::XMLNode';
128 } elsif ($nodeType == XML_READER_TYPE_COMMENT) {
129 323 $chld = bless {
130 -xml_name => '#comment',
131 -value => $reader->value,
132 }, 'Treex::PML::Schema::XMLNode';
133 } elsif ($nodeType == XML_READER_TYPE_PROCESSING_INSTRUCTION) {
134 0 $chld = bless {
135 -xml_name => '#processing-instruction',
136 -name => $reader->name,
137 -value => $reader->value,
138 }, 'Treex::PML::Schema::XMLNode';
139 ▶ } elsif ($nodeType == XML_READER_TYPE_END_ELEMENT or
Condition33%2/6
$nodeType == 15 or $nodeType == 14 or $nodeType == 13
l!l&&r!l&&!r
194500
$nodeType == 15 or $nodeType == 14
l!l&&r!l&&!r
194500
MC/DC0%0/3
$nodeType == 15 or $nodeType == 14 or $nodeType == 13
$nodeType == 15 $nodeType == 14 $nodeType == 13
Truth table25%1/4
A or B or C
A= $nodeType == 15B= $nodeType == 14C= $nodeType == 13
ABCresult
1XX1
01X1
0011
0000
140 $nodeType == XML_READER_TYPE_SIGNIFICANT_WHITESPACE or
141 $nodeType == XML_READER_TYPE_WHITESPACE) {
142 1945 next;
143 } else {
144 0 ▶ $chld = bless {
Branch: 1 branch
BranchTrueFalseTotal
$reader->hasValue ? : 0 0 0
145 -xml_name => '#other',
146 -xml_nodetype => $nodeType,
147 -name => $reader->name,
148 ($reader->hasValue ? (-value => $reader->value) : ()),
149 -xml => $reader->readOuterXml,
150 }, 'Treex::PML::Schema::XMLNode';
151 }
152 3801 ▶ push @children, $chld if defined $chld;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chld 3801 0 3801
153 3801 ▶ redo if $redo;
Branch: 1 branch
BranchTrueFalseTotal
if $redo 2896 905 3801
154 }
155 2963 ▶ if ($status == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == -1) 0 2963 2963
156 0 croak "XMLReader error in $opts->{URL} near line ".$reader->lineNumber;
157 }
158  
159 2963 my $i=0;
160 2963 my %try_data;
161 2963 ▶ if (my $cont = $opts->{TextOnly}{$el_ns_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cont = $opts->{'TextOnly'}{$el_ns_name}) { } 582 2381 2963
162 582 my $text;
163 582 foreach my $c (@children) {
164 582 ▶ if ($c->{'-xml_name'} ne '#text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'-xml_name'} ne '#text') { } 0 582 582
165 0 warn "Ignoring unexpected node ".$c->{'-xml_name'}." in a text-only element $el_ns_name\n";
166 } else {
167 582 $text.=$c->{-value};
168 }
169 }
170 582 $val{$cont} = $text;
171 } else {
172 2381 foreach my $c (@children) {
173 3219 $c->{'-#'} = $i++;
174 3219 my $name = $c->{-xml_name};
175 3219 ▶ if (!ref($val{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $val{$name}) 1832 1387 3219
176 1832 ▶ if (exists $val{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $val{$name}) 0 1832 1832
177 0 warn "Collision between an attribute and child-element $name\n";
178 0 $val{'@'.$name} = delete $val{$name}
179 }
180 }
181 3219 my $value;
182 3219 ▶ if (my $cont = $opts->{Stringify}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cont = $opts->{'Stringify'}{$name}) { } 580 2639 3219
183 580 $value = $c->{$cont};
184 580 ▶ $value='' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 580 580
185 } else {
186 2639 $value = $c;
187 }
188 3219 ▶ if ($opts->{Solitary}{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'Solitary'}{$name}) { } 1165 2054 3219
elsif (my $key = $opts->{'KeyAttr'}{$name}) { } 1211 843 2054
189 1165 ▶ if (exists $val{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $val{$name}) 0 1165 1165
190 0 warn "Multiple occurences of the child-element '$name'\n";
191 }
192 1165 $val{$name} = $value
193 } elsif (my $key = $opts->{KeyAttr}{$name}) {
194 1211 my $val = delete $c->{$key};
195 1211 $c->{-name}=$val;
196 1211 $val{$name}{$val} = $value;
197 } else {
198 843 push @{$val{$name}}, $value;
199 }
200 3219 weaken($c->{-parent} = $obj);
201 }
202 2381 $obj->{'-##'} = $i;
203 }
204 2963 ▶ if (UNIVERSAL::can($obj,'post_process')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($obj, "post_process")) 151 2812 2963
205 151 $obj->post_process($opts);
206 }
207 2963 return $obj;
208 }
209  
210  
211 9 1;
212 __END__
« PML/Schema/CDATA.pm ↑ Summary PML/Node.pm »