PML/Backend/CSTS.pm

filter: Statementstmt 45.1% 46 / 102 Branchbran 19.6% 11 / 56 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 72.2% 13 / 18 Podpod 0.0% 0 / 7 totaltotal 34.6% 72 / 208 CC 35 SCAR 37.3
CC
35
Cov
34%
open_backend
133.3
close_backend
30.0
test_nsgmls
23.6
CRAP 41.9
« PML/Node.pm ↑ Summary PML/Schema/List.pm »
1 package Treex::PML::Backend::CSTS;
2  
3 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 $VERSION='2.29'; # version template
6 }
7  
8 2 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Treex::PML::IO qw(set_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Treex::PML::Backend::CSTS::Csts2fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Treex::PML::Backend::CSTS::Fs2csts;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use vars qw($sgmls $sgmlsopts $doctype $csts_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶sub default_settings {
Subroutine default_settings: called CC=9 SCAR=26.0
Pod undocumented
18 2 ▶ $sgmls = "nsgmls" unless $sgmls;
Branch: 1 branch
BranchTrueFalseTotal
unless $sgmls 2 0 2
19 2 ▶ $sgmlsopts = "-i preserve.gen.entities" unless $sgmlsopts;
Branch: 1 branch
BranchTrueFalseTotal
unless $sgmlsopts 2 0 2
20 2 ▶ unless ($doctype and -f $doctype) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doctype and -f $doctype) 2 0 2
Condition33%1/3
$doctype and -f $doctype
!ll&&!rl&&r
200
MC/DC0%0/2
$doctype and -f $doctype
$doctype -f $doctype
Truth table33%1/3
A and B
A= $doctypeB= -f $doctype
ABresult
0X0
100
111
21 2 $doctype = eval { File::ShareDir::module_file(__PACKAGE__,'csts.doctype') };
22 2 ▶ unless (defined($doctype) and -f $doctype) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doctype and -f $doctype) 2 0 2
Condition33%1/3
defined $doctype and -f $doctype
!ll&&!rl&&r
200
MC/DC0%0/2
defined $doctype and -f $doctype
defined $doctype -f $doctype
Truth table33%1/3
A and B
A= defined $doctypeB= -f $doctype
ABresult
0X0
100
111
23 2 $doctype = Treex::PML::IO::CallerDir(File::Spec->catfile(qw(CSTS share csts.doctype)));
24 }
25 2 ▶ unless (-f $doctype) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $doctype) 0 2 2
26 0 $doctype = Treex::PML::FindInResources("csts.doctype");
27 }
28 }
29 2 ▶ $sgmls_command='%s %o %d %f' unless $sgmls_command;
Branch: 1 branch
BranchTrueFalseTotal
unless $sgmls_command 2 0 2
30 2 $csts_encoding = 'iso-8859-2'; # this the encoding of CSTS by definition
31 }
32  
33 2 my %stderr_pool;
34 0 ▶sub open_backend {
Subroutine open_backend: not called CC=12 SCAR=48.9
Pod undocumented
35 0 my ($filename, $mode, $encoding)=@_;
36 0 ▶ if ($mode eq 'w') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'w') { } 0 0 0
elsif ($mode eq 'r') { } 0 0 0
37 0 return Treex::PML::IO::open_backend($filename,$mode,$csts_encoding);
38 } elsif ($mode eq 'r') {
39 0 my $fh = undef;
40 0 my $cmd = $sgmls_command;
41 0 ▶ $doctype = Treex::PML::FindInResources($doctype) unless -f $doctype;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $doctype 0 0 0
42 0 ▶ print STDERR "$cmd\n" if $Treex::PML::Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::Debug 0 0 0
43 0 $cmd=~s/\%s/$sgmls/g;
44 0 $cmd=~s/\%o/$sgmlsopts/g;
45 0 $cmd=~s/\%d/$doctype/g;
46 0 $cmd=~s/\%f/-/g;
47 0 ▶ warn "[r $cmd]\n" if $Treex::PML::Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::Debug 0 0 0
48 2 ▶ no integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 {
51 0 my $err = File::Temp->new(UNLINK => 1);
52 0 $err->autoflush(1);
53 0 ▶ open my $olderr, ">&", \*STDERR or die "Can't dup STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $olderr, ">&", \*STDERR or die "Can't dup STDERR: $!" 0 0 0
54 0 ▶ open STDERR, ">&", $err or die "Can't dup temporary filehandle as STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $err or die "Can't dup temporary filehandle as STDERR: $!" 0 0 0
55 0 eval {
56 0 $fh = set_encoding(Treex::PML::IO::open_pipe($filename,'r',$cmd),$csts_encoding);
57 0 ▶ binmode $fh,':crlf' if $fh and $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $fh and $^O eq "MSWin32" 0 0 0
Condition0%0/3
$fh and $^O eq "MSWin32"
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and $^O eq "MSWin32"
$fh $^O eq "MSWin32"
Truth table0%0/3
A and B
A= $fhB= $^O eq "MSWin32"
ABresult
0X0
100
111
58 };
59 0 close(STDERR);
60 0 ▶ open STDERR, ">&", $olderr or die "Can't dup old STDERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or die "Can't dup old STDERR: $!" 0 0 0
61 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
62 0 close $err;
63 0 die $@;
64 }
65 0 $stderr_pool{$fh} = $err;
66 0 return $fh;
67 }
68 } else {
69 0 die "unknown mode $mode\n";
70 }
71 }
72  
73 0 ▶sub close_backend {
Subroutine close_backend: not called CC=5 SCAR=34.0
Pod undocumented
74 0 my ($fh)=@_;
75 0 ▶ if (exists $stderr_pool{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $stderr_pool{$fh}) 0 0 0
76 0 my $err = delete $stderr_pool{$fh};
77 0 seek($err,0,SEEK_SET);
78 0 local $/;
79 0 my $warnings = <$err>;
80 0 close($err);
81 0 ▶ if (defined $warnings and length $warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $warnings and length $warnings) 0 0 0
Condition0%0/3
defined $warnings and length $warnings
!ll&&!rl&&r
000
MC/DC0%0/2
defined $warnings and length $warnings
defined $warnings length $warnings
Truth table0%0/3
A and B
A= defined $warningsB= length $warnings
ABresult
0X0
100
111
82 0 warn $warnings;
83 }
84 }
85 0 ▶ unless (Treex::PML::IO::close_backend($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Treex::PML::IO::close_backend($fh)) 0 0 0
86 0 die "$sgmls ended with error code $?\n";
87 }
88 0 return 1;
89 }
90  
91 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
Pod undocumented
92 0 Treex::PML::Backend::CSTS::Csts2fs::read(@_);
93 }
94  
95 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod undocumented
96 0 Treex::PML::Backend::CSTS::Fs2csts::write(@_);
97 }
98  
99 1 ▶sub test_nsgmls {
Subroutine test_nsgmls: called CC=9 SCAR=31.6
Pod undocumented
100 1 ▶ return 1 if (-x $sgmls);
Branch: 1 branch
BranchTrueFalseTotal
if -x $sgmls 0 1 1
101 1 ▶ foreach (split(($^O eq 'MSWin32' ? ';' : ':'),$ENV{PATH})) {
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
102 10 ▶ if (-x "$_".($^O eq 'MSWin32' ? "\\" : "/")."$sgmls") {
Branch: 2 branches
BranchTrueFalseTotal
if (-x "$_" . ($^O eq "MSWin32" ? "\\" : "/") . "$sgmls") 0 10 10
$^O eq 'MSWin32' ? : 0 10 10
103 0 ▶ unless (-f $doctype) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $doctype) 0 0 0
104 0 ▶ warn("CSTS doctype not found: $doctype\n") if $Treex::PML::Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::Debug 0 0 0
105 0 return 0;
106 }
107 0 return 1;
108 }
109 }
110 1 ▶ warn("nsgmls not found at $sgmls\n") if $Treex::PML::Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::Debug 1 0 1
111 1 return 0;
112 }
113  
114 0 ▶sub test {
Subroutine test: not called CC=4 SCAR=30.0
Pod undocumented
115 0 my ($f,$encoding)=@_;
116  
117 0 ▶ return 0 unless test_nsgmls();
Branch: 1 branch
BranchTrueFalseTotal
unless test_nsgmls() 0 0 0
118 0 ▶ if (ref($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f) { } 0 0 0
119 0 my $line=$f->getline();
120 0 return $line=~/^\s*<csts[ >]|^<!DOCTYPE csts/;
121 } else {
122 0 my $fh = Treex::PML::IO::open_backend($f,"r");
123 0 ▶ my $test = $fh && test($fh);
Condition0%0/3
$fh && test($fh)
!ll&&!rl&&r
000
MC/DC0%0/2
$fh && test($fh)
$fh test($fh)
Truth table0%0/3
A && B
A= $fhB= test($fh)
ABresult
0X0
100
111
124 0 Treex::PML::IO::close_backend($fh);
125 0 return $test;
126 }
127 }
128  
129 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
130 2 default_settings();
131 }
132  
133 2 1;
134 __END__
« PML/Node.pm ↑ Summary PML/Schema/List.pm »