PML/Backend/PML.pm

filter: Statementstmt 78.0% 103 / 132 Branchbran 39.7% 27 / 68 Conditioncond 25.5% 12 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 5 totaltotal 54.0% 162 / 300 CC 52 SCAR 45.7
CC
52
Cov
57%
test
188.1
_configure
71.3
read
7.2
CRAP 96.5
« PML/Schema.pm ↑ Summary PML/Schema/Derive.pm »
1 package Treex::PML::Backend::PML;
2  
3 9 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Treex::PML::IO qw(close_backend);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 $VERSION='2.29'; # version template
13 }
14  
15 9 ▶use Treex::PML::Instance qw( :all :diagnostics $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use constant EMPTY => q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 ▶use vars qw($config $config_file $allow_no_trees $config_inc_file $TRANSFORM @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 $TRANSFORM=0;
27 9 @EXPORT_OK = qw(open_backend close_backend test read write);
28 9 $config = undef;
29 9 $config_file = 'pmlbackend_conf.xml';
30 9 $config_inc_file = 'pmlbackend_conf.inc';
31 9 $allow_no_trees = 0;
32 }
33  
34 9 ▶sub configure {
Subroutine configure: called CC=2 SCAR=7.0
Pod undocumented
35 9 my @resource_path = Treex::PML::ResourcePaths();
36 9 my $ret = eval { _configure() };
37 9 my $err = $@;
38 9 Treex::PML::SetResourcePaths(@resource_path);
39 9 ▶ die $err if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 9 9
40 9 $config = $ret;
41 9 return $ret;
42 }
43  
44 9 ▶sub _configure {
Subroutine _configure: called CC=16 SCAR=42.7
45 9 my $cfg;
46 9 my $schema_dir = eval { File::ShareDir::module_dir('Treex::PML') };
47 9 ▶ unless (defined($schema_dir) and length($schema_dir) and -f File::Spec->catfile($schema_dir,'pmlbackend_conf_schema.xml')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema_dir and length $schema_dir and -f "File::Spec"->catfile($schema_dir, "pmlbackend_conf_schema.xml")) 9 0 9
Condition33%2/6
defined $schema_dir and length $schema_dir and -f "File::Spec"->catfile($schema_dir, "pmlbackend_conf_schema.xml")
!ll&&!rl&&r
090
defined $schema_dir and length $schema_dir
!ll&&!rl&&r
009
MC/DC0%0/3
defined $schema_dir and length $schema_dir and -f "File::Spec"->catfile($schema_dir, "pmlbackend_conf_schema.xml")
defined $schema_dir length $schema_dir -f "File::Spec"->catfile($schema_dir, "pmlbackend_conf_schema.xml")
Truth table25%1/4
A and B and C
A= defined $schema_dirB= length $schema_dirC= -f "File::Spec"->catfile($schema_dir, "pmlbackend_conf_schema.xml")
ABCresult
0XX0
10X0
1100
1111
48 9 $schema_dir = Treex::PML::IO::CallerDir(File::Spec->catfile(qw(.. share)));
49 }
50 9 ▶ Treex::PML::AddResourcePath($schema_dir) if defined($schema_dir) and length($schema_dir);
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema_dir and length $schema_dir 9 0 9
Condition33%1/3
defined $schema_dir and length $schema_dir
!ll&&!rl&&r
009
MC/DC0%0/2
defined $schema_dir and length $schema_dir
defined $schema_dir length $schema_dir
Truth table33%1/3
A and B
A= defined $schema_dirB= length $schema_dir
ABresult
0X0
100
111
51 9 my $file = Treex::PML::FindInResources($config_file,{strict=>1});
52 9 ▶ if ($file and -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and -f $file) { } 0 9 9
Condition33%1/3
$file and -f $file
!ll&&!rl&&r
900
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table33%1/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
53 0 _debug("config file: $file");
54 0 $cfg = Treex::PML::Instance->load({filename => $file});
55 } else {
56 9 _debug("using empty pmlbackend_conf.xml file");
57 9 $cfg = Treex::PML::Instance->load({string=><<'_CONFIG_',filename => $file});
58 <?xml version="1.0" encoding="UTF-8"?>
59 <pmlbackend xmlns="http://ufal.mff.cuni.cz/pdt/pml/">
60 <head><schema href="pmlbackend_conf_schema.xml"/></head>
61 <transform_map/>
62 </pmlbackend>
63 _CONFIG_
64 }
65 9 ▶ if ($cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg) 9 0 9
66 9 my @config_files = Treex::PML::FindInResources($config_inc_file,{all=>1});
67 9 ▶ my $T = $cfg->get_root->{transform_map} ||= Treex::PML::Factory->createSeq();
Condition33%1/3
$cfg->get_root->{"transform_map"} ||= "Treex::PML::Factory"->createSeq
l!l&&r!l&&!r
090
MC/DC0%0/2
$cfg->get_root->{"transform_map"} ||= "Treex::PML::Factory"->createSeq
$cfg->get_root->{"transform_map"} "Treex::PML::Factory"->createSeq
Truth table33%1/3
A ||= B
A= $cfg->get_root->{"transform_map"}B= "Treex::PML::Factory"->createSeq
ABresult
1X1
011
000
68 9 for my $file (reverse @config_files) {
69 0 _debug("config include file: $file");
70 0 eval {
71 0 my $c = Treex::PML::Instance->load({filename => $file});
72 # merge
73 0 my $t = $c->get_root->{transform_map};
74 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 0 0
75 0 for my $transform (reverse $t->elements) {
76 0 my $copy = Treex::PML::CloneValue($transform);
77 0 $T->unshift_element_obj($copy);
78 0 ▶ if (ref($copy->value) and $copy->value->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $copy->value and $copy->value->{"id"}) 0 0 0
Condition0%0/3
ref $copy->value and $copy->value->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $copy->value and $copy->value->{"id"}
ref $copy->value $copy->value->{"id"}
Truth table0%0/3
A and B
A= ref $copy->valueB= $copy->value->{"id"}
ABresult
0X0
100
111
79 0 $cfg->hash_id($copy->value->{id}, $copy->value, 1);
80 }
81 }
82 }
83 };
84 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
85 }
86 }
87 9 return $cfg;
88 }
89  
90  
91 ###################
92  
93 32 ▶sub open_backend {
Subroutine open_backend: called CC=3 SCAR=13.1
Pod undocumented
94 32 my ($filename, $mode, $encoding)=@_;
95 32 ▶ my $fh = Treex::PML::IO::open_backend($filename,$mode) # discard encoding
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'w' ? : 0 0 0
Condition50%1/2
Treex::PML::IO::open_backend($filename, $mode) || die("Cannot open $filename for " . ($mode eq 'w' ? 'writing' : 'reading') . ": $!")
l!l
320
MC/DC0%0/1
Treex::PML::IO::open_backend($filename, $mode) || die("Cannot open $filename for " . ($mode eq 'w' ? 'writing' : 'reading') . ": $!")
Treex::PML::IO::open_backend($filename, $mode)
Truth table50%1/2
A
A= Treex::PML::IO::open_backend($filename, $mode)
Aresult
11
00
96 || die "Cannot open $filename for ".($mode eq 'w' ? 'writing' : 'reading').": $!";
97 32 return $fh;
98 }
99  
100 22 ▶sub read ($$) {
Subroutine read: called CC=5 SCAR=19.7
Pod undocumented
101 22 my ($input, $fsfile)=@_;
102 22 ▶ return unless ref($fsfile);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fsfile 0 22 22
103  
104 22 my $ctxt = Treex::PML::Instance->load({fh => $input, filename => $fsfile->filename, config => $config });
105 22 $ctxt->convert_to_fsfile( $fsfile );
106 22 my $status = $ctxt->get_status;
107 22 ▶ if ($status and
Branch: 1 branch
BranchTrueFalseTotal
if ($status and not $Treex::PML::Backend::PML::allow_no_trees || defined $ctxt->get_trees) 0 22 22
Condition16%1/6
$status and not $Treex::PML::Backend::PML::allow_no_trees || defined $ctxt->get_trees
!ll&&!rl&&r
2200
$Treex::PML::Backend::PML::allow_no_trees || defined $ctxt->get_trees
l!l&&r!l&&!r
000
MC/DC0%0/3
$status and not $Treex::PML::Backend::PML::allow_no_trees || defined $ctxt->get_trees
$status $Treex::PML::Backend::PML::allow_no_trees defined $ctxt->get_trees
Truth table25%1/4
A and not(B || C)
A= $statusB= $Treex::PML::Backend::PML::allow_no_treesC= defined $ctxt->get_trees
ABCresult
0XX0
11X0
1010
1001
108 !($allow_no_trees or defined($ctxt->get_trees))) {
109 0 _die("No trees found in the Treex::PML::Instance!");
110 }
111 22 return $status
112 }
113  
114  
115 10 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
116 10 my ($fh,$fsfile)=@_;
117 10 my $ctxt = Treex::PML::Instance->convert_from_fsfile( $fsfile );
118 10 $ctxt->save({ fh => $fh, config => $config });
119 }
120  
121  
122 44 ▶sub test {
Subroutine test: called CC=30 SCAR=52.4
Pod undocumented
123 44 my ($f,$encoding)=@_;
124 44 ▶ if (ref($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f) { } 22 22 44
125 22 local $_;
126 22 ▶ if ($TRANSFORM and $config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::PML::Backend::PML::TRANSFORM and $Treex::PML::Backend::PML::config) { } 0 22 22
Condition33%1/3
$Treex::PML::Backend::PML::TRANSFORM and $Treex::PML::Backend::PML::config
!ll&&!rl&&r
2200
MC/DC0%0/2
$Treex::PML::Backend::PML::TRANSFORM and $Treex::PML::Backend::PML::config
$Treex::PML::Backend::PML::TRANSFORM $Treex::PML::Backend::PML::config
Truth table33%1/3
A and B
A= $Treex::PML::Backend::PML::TRANSFORMB= $Treex::PML::Backend::PML::config
ABresult
0X0
100
111
127 0 ▶ 1 while ($_=$f->getline() and !/\S/);
Branch: 1 branch
BranchTrueFalseTotal
$_ = $f->getline and not /\S/ and do { '???' } 0 0 0
Condition0%0/3
$_ = $f->getline and not /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$_ = $f->getline and not /\S/
$_ = $f->getline not /\S/
Truth table0%0/3
A and not(B)
A= $_ = $f->getlineB= not /\S/
ABresult
0X0
100
111
128 # see <, assume XML
129 0 ▶ return 1 if (defined and /^\s*</);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and /^\s*</ 0 0 0
Condition0%0/3
defined $_ and /^\s*</
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and /^\s*</
defined $_ /^\s*</
Truth table0%0/3
A and B
A= defined $_B= /^\s*</
ABresult
0X0
100
111
130 } else {
131 # only accept PML instances
132 # xmlns:...="..pml-namespace.." must occur in the first tag (on one line)
133  
134 # FIXME: the following code will fail for UTF-16 and UTF-32;
135 # proper fix would be to use XML::LibXML::Reader to read the
136 # first tag (performance impact on processing many files past
137 # PML backend to be measured). Another way to fix for UTF-16 is
138 # to check for UTF-16 BOM (both BE and LE) and decode
139 # accordingly if present; UTF-32 is rarely used and probably not
140 # worth fixing.
141 22 my ($in_first_tag,$in_pi,$in_comment, $past_BOM);
142 22 ▶ while ($_=$f->getline()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = $f->getline) 49 0 49
143 49 ▶ unless ($past_BOM) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($past_BOM) 22 27 49
144 # ignore UTF-8 BOM
145 22 s{^\x{ef}\x{bb}\x{bf}}{};
146 22 $past_BOM = 1;
147 }
148 49 ▶ next if !/\S/; # whitespace
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 5 44 49
149 44 ▶ if ($in_first_tag) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in_first_tag) { } 0 44 44
elsif ($in_pi) { } 0 44 44
elsif ($in_comment) { } 0 44 44
150 0 ▶ last if />/;
Branch: 1 branch
BranchTrueFalseTotal
if />/ 0 0 0
151 0 ▶ return 1 if m{\bxmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1};
Branch: 1 branch
BranchTrueFalseTotal
if m[\bxmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1] 0 0 0
152 0 next;
153 } elsif ($in_pi) {
154 0 ▶ next unless s/^.*?\?>//;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^.*?\?>// 0 0 0
155 0 $in_pi=0;
156 } elsif ($in_comment) {
157 0 ▶ next unless s/^.*?\-->//;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^.*?\-->// 0 0 0
158 0 $in_comment=0;
159 }
160 44 s/^(?:\s*<\?.*?\?>|\s*<!--.*?-->)*\s*//;
161 44 ▶ if (/<\?/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/<\?/) { } 0 44 44
elsif (/<!--/) { } 0 44 44
elsif (/^</) { } 22 22 44
elsif (length $_) { } 0 22 22
162 0 $in_pi=1;
163 } elsif (/<!--/) {
164 0 $in_comment=1;
165 } elsif (/^</) {
166 22 ▶ last if />/;
Branch: 1 branch
BranchTrueFalseTotal
if />/ 22 0 22
167 0 $in_first_tag=1;
168 0 ▶ return 1 if m{^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1};
Branch: 1 branch
BranchTrueFalseTotal
if m[^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1] 0 0 0
169 } elsif (length) {
170 0 return 0; # nothing else allowed before the first tag
171 }
172 }
173 22 ▶ return 0 if !$in_first_tag && !(defined($_) and s/^\s*<//);
Branch: 1 branch
BranchTrueFalseTotal
unless $in_first_tag or defined $_ and s/^\s*<// 0 22 22
Condition33%2/6
$in_first_tag or defined $_ and s/^\s*<//
l!l&&r!l&&!r
0220
defined $_ and s/^\s*<//
!ll&&!rl&&r
0022
MC/DC0%0/3
$in_first_tag or defined $_ and s/^\s*<//
$in_first_tag defined $_ s/^\s*<//
Truth table25%1/4
A or B and C
A= $in_first_tagB= defined $_C= s/^\s*<//
ABCresult
1XX1
0111
00X0
0100
174 22 ▶ return 1 if defined($_) and m{^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and m[^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1] 22 0 22
Condition33%1/3
defined $_ and m[^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1]
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $_ and m[^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1]
defined $_ m[^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1]
Truth table33%1/3
A and B
A= defined $_B= m[^[^>]*xmlns(?::[[:alnum:]]+)?=([\'\"])http://ufal.mff.cuni.cz/pdt/pml/\1]
ABresult
0X0
100
111
175 0 return 0;
176 }
177 } else {
178 22 my $fh = Treex::PML::IO::open_backend($f,"r");
179 22 ▶ my $test = $fh && test($fh,$encoding);
Condition33%1/3
$fh && test($fh, $encoding)
!ll&&!rl&&r
0022
MC/DC0%0/2
$fh && test($fh, $encoding)
$fh test($fh, $encoding)
Truth table33%1/3
A && B
A= $fhB= test($fh, $encoding)
ABresult
0X0
100
111
180 22 Treex::PML::IO::close_backend($fh);
181 22 return $test;
182 }
183 }
184  
185  
186 ######################################################
187  
188  
189 ###################
190 # INIT
191 ###################
192 package Treex::PML::Backend::PML;
193 9 eval {
194 9 configure();
195 };
196 9 ▶Carp::cluck( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
197  
198 9 1;
199  
200 =pod
201  
202 =head1 NAME
203  
204 Treex::PML::Backend::PML - I/O backend for PML documents
205  
206 =head1 SYNOPSIS
207  
208 use Treex::PML;
209 Treex::PML::AddBackends(qw(PML))
210  
211 my $document = Treex::PML::Factory->createDocumentFromFile('input.pml');
212 ...
213 $document->save();
214  
215 =head1 DESCRIPTION
216  
217 This module implements a Treex::PML input/output backend which accepts
218 reads/writes PML files. See L<Treex::PML::Instance> for details.
219  
220 NOTE: L<Treex::PML> enables this backend by default.
221  
222 =head1 COPYRIGHT AND LICENSE
223  
224 Copyright (C) 2006-2010 by Petr Pajas, 2010-2024 Jan Stepanek
225  
226 This library is free software; you can redistribute it and/or modify
227 it under the same terms as Perl itself, either Perl version 5.8.2 or,
228 at your option, any later version of Perl 5 you may have available.
229  
230 =cut
« PML/Schema.pm ↑ Summary PML/Schema/Derive.pm »