blib/lib/XML/TMX/Writer.pm

filter: Statementstmt 91.0% 143 / 157 Branchbran 64.5% 40 / 62 Conditioncond 57.6% 15 / 26 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 79.2% 222 / 280 CC 48 SCAR 26.6
CC
48
Cov
81%
add_tu
24.6
start_tmx
19.9
_write_props
7.4
CRAP 14.3
« blib/lib/XML/TMX/Reader.pm ↑ Summary scripts/tmx2tmx »
1 package XML::TMX::Writer;
2 8 $XML::TMX::Writer::VERSION = '0.39';
3 # ABSTRACT: Perl extension for writing TMX files
4  
5 8 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our @ISA = 'Exporter';
11 8 our @EXPORT_OK = qw();
12  
13  
14 6 ▶sub new {
Subroutine new: called CC=4 SCAR=15.2
Pod documented
15 6 my $proto = shift;
16 6 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
17 6 my %ops = @_;
18 6 my $self = { OUTPUT => \*STDOUT };
19 6 ▶ binmode $self->{OUTPUT}, ":utf8" unless exists $ops{-encoding} and $ops{-encoding} !~ /utf.?8/i;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ops{"-encoding"} and not $ops{"-encoding"} =~ /utf.?8/i 6 0 6
Condition33%1/3
exists $ops{"-encoding"} and not $ops{"-encoding"} =~ /utf.?8/i
!ll&&!rl&&r
600
MC/DC0%0/2
exists $ops{"-encoding"} and not $ops{"-encoding"} =~ /utf.?8/i
exists $ops{"-encoding"} not $ops{"-encoding"} =~ /utf.?8/i
Truth table33%1/3
A and not(B)
A= exists $ops{"-encoding"}B= not $ops{"-encoding"} =~ /utf.?8/i
ABresult
0X0
100
111
20 6 bless($self, $class);
21 6 return($self);
22 }
23  
24  
25 6 ▶sub start_tmx {
Subroutine start_tmx: called CC=17 SCAR=29.9
Pod documented
26 6 my $self = shift;
27 6 my %options = @_;
28 6 my %o;
29  
30 6 my @time = gmtime(time);
31 6 $o{'creationdate'} = sprintf("%d%02d%02dT%02d%02d%02dZ", $time[5]+1900,
32 $time[4]+1, $time[3], $time[2], $time[1], $time[0]);
33  
34 6 ▶ my $encoding = $options{encoding} || "UTF-8";
Condition100%2/2
$options{'encoding'} || 'UTF-8'
l!l
42
MC/DC100%1/1
$options{'encoding'} || 'UTF-8'
$options{'encoding'}
Truth table100%2/2
A
A= $options{'encoding'}
Aresult
11
00
35  
36 6 ▶ if (defined($options{'-output'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-output"}) 5 1 6
37 5 delete $self->{OUTPUT}; # because it is a glob
38 5 ▶ open $self->{OUTPUT}, ">", $options{'-output'}
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"OUTPUT"}, ">", $options{"-output"} or die qq[Cannot open file '$options{"-output"}': $!\n] 5 0 5
39 or die "Cannot open file '$options{'-output'}': $!\n";
40 }
41  
42 6 ▶ if ($encoding =~ m!utf.?8!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /utf.?8/i) 6 0 6
43 6 binmode $self->{OUTPUT}, ":utf8"
44 }
45 6 $self->_write("<?xml version=\"1.0\" encoding=\"$encoding\"?>\n");
46  
47 6 my @valid_segtype = qw'block sentence paragraph phrase';
48 0 ▶ if(defined($options{SEGTYPE}) && grep { $_ eq $options{SEGTYPE} } @valid_segtype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'SEGTYPE'} and grep {$_ eq $options{'SEGTYPE'};} @valid_segtype) { } 0 6 6
Condition33%1/3
defined $options{"SEGTYPE"} and grep {$_ eq $options{"SEGTYPE"};} @valid_segtype
!ll&&!rl&&r
600
MC/DC0%0/2
defined $options{"SEGTYPE"} and grep {$_ eq $options{"SEGTYPE"};} @valid_segtype
defined $options{"SEGTYPE"} grep {$_ eq $options{"SEGTYPE"};} @valid_segtype
Truth table33%1/3
A and B
A= defined $options{"SEGTYPE"}B= grep {$_ eq $options{"SEGTYPE"};} @valid_segtype
ABresult
0X0
100
111
49 0 $o{segtype} = $options{SEGTYPE};
50 } else {
51 6 $o{segtype} = 'sentence'
52 }
53  
54 6 ▶ $o{'creationtool'} = $options{tool} || 'XML::TMX::Writer';
Condition50%1/2
$options{'tool'} || 'XML::TMX::Writer'
l!l
06
MC/DC0%0/1
$options{'tool'} || 'XML::TMX::Writer'
$options{'tool'}
Truth table50%1/2
A
A= $options{'tool'}
Aresult
11
00
55 6 ▶ $o{'creationtoolversion'} = $options{toolversion} || $XML::TMX::Writer::VERSION;
Condition33%1/3
$options{'toolversion'} || $XML::TMX::Writer::VERSION
l!l&&r!l&&!r
060
MC/DC0%0/2
$options{'toolversion'} || $XML::TMX::Writer::VERSION
$options{'toolversion'} $XML::TMX::Writer::VERSION
Truth table33%1/3
A || B
A= $options{'toolversion'}B= $XML::TMX::Writer::VERSION
ABresult
1X1
011
000
56 6 ▶ $o{'o-tmf'} = $options{srctmf} || 'plain text';
Condition50%1/2
$options{'srctmf'} || 'plain text'
l!l
06
MC/DC0%0/1
$options{'srctmf'} || 'plain text'
$options{'srctmf'}
Truth table50%1/2
A
A= $options{'srctmf'}
Aresult
11
00
57 6 ▶ $o{'adminlang'} = $options{adminlang} || 'en';
Condition100%2/2
$options{'adminlang'} || 'en'
l!l
51
MC/DC100%1/1
$options{'adminlang'} || 'en'
$options{'adminlang'}
Truth table100%2/2
A
A= $options{'adminlang'}
Aresult
11
00
58 6 ▶ $o{'srclang'} = $options{srclang} || 'en';
Condition100%2/2
$options{'srclang'} || 'en'
l!l
51
MC/DC100%1/1
$options{'srclang'} || 'en'
$options{'srclang'}
Truth table100%2/2
A
A= $options{'srclang'}
Aresult
11
00
59 6 ▶ $o{'datatype'} = $options{datatype} || 'plaintext';
Condition100%2/2
$options{'datatype'} || 'plaintext'
l!l
51
MC/DC100%1/1
$options{'datatype'} || 'plaintext'
$options{'datatype'}
Truth table100%2/2
A
A= $options{'datatype'}
Aresult
11
00
60  
61 6 ▶ defined($options{srcencoding}) and $o{'o-encoding'} = $options{srcencoding};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{"srcencoding"} and $o{"o-encoding"} = $options{"srcencoding"} 0 6 6
62 6 ▶ defined($options{id}) and $o{'creationid'} = $options{id};
Branch: 1 branch
BranchTrueFalseTotal
defined $options{"id"} and $o{"creationid"} = $options{"id"} 1 5 6
63  
64 6 $self->_startTag(0, 'tmx', 'version' => 1.4)->_nl;
65 6 $self->_startTag(1, 'header', %o)->_nl;
66  
67 6 ▶ $self->_write_props(2, $options{'-prop'}) if defined $options{'-prop'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"-prop"} 6 0 6
68 6 ▶ $self->_write_notes(2, $options{'-note'}) if defined $options{'-note'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"-note"} 6 0 6
69  
70 6 $self->_indent(1)->_endTag('header')->_nl;
71  
72 6 $self->_startTag(0,'body')->_nl->_nl;
73 }
74  
75 34 ▶sub _write_props {
Subroutine _write_props: called CC=7 SCAR=20.1
76 34 my ($self, $indent, $props) = @_;
77 34 ▶ return unless ref($props) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $props eq "HASH" 0 34 34
78 34 for my $key (sort keys %$props) {
79 54 ▶ if (ref($props->{$key}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $props->{$key} eq 'ARRAY') { } 28 26 54
80 28 for my $val (@{$props->{$key}}) {
81 37 ▶ if ($key eq "_") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '_') { } 0 37 37
82 0 $self->_startTag($indent, 'prop');
83 } else {
84 37 $self->_startTag($indent, prop => (type => $key));
85 }
86 37 $self->_characters($val);
87 37 $self->_endTag('prop')->_nl;
88 }
89 } else {
90 26 ▶ if ($key eq "_") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '_') { } 0 26 26
91 0 $self->_startTag($indent, 'prop');
92 } else {
93 26 $self->_startTag($indent, prop => (type => $key));
94 }
95 26 $self->_characters($props->{$key});
96 26 $self->_endTag('prop')->_nl;
97 }
98 }
99 }
100  
101 34 ▶sub _write_notes {
Subroutine _write_notes: called CC=3 SCAR=11.0
102 34 my ($self, $indent, $notes) = @_;
103 34 ▶ return unless ref($notes) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $notes eq "ARRAY" 0 34 34
104 34 for my $p (@{$notes}) {
105 88 $self->_startTag($indent, 'note');
106 88 $self->_characters($p);
107 88 $self->_endTag('note')->_nl;
108 }
109 }
110  
111  
112 21 ▶sub add_tu {
Subroutine add_tu: called CC=19 SCAR=32.0
Pod documented
113 21 my $self = shift;
114 21 my %tuv = @_;
115 21 my %prop = ();
116 21 my @note = ();
117 21 my %opt;
118  
119 21 my $verbatim = 0;
120 21 my $cdata = 0;
121  
122 21 ▶ if (exists($tuv{-raw})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tuv{"-raw"}) 0 21 21
123 # value already includes <tu> tags, hopefully, at least!
124 # so we will not mess with it.
125 0 $self->_write($tuv{-raw});
126 0 return;
127 }
128  
129 21 for my $key (qw'id datatype segtype srclang creationid creationdate changedate changeid') {
130 168 ▶ if (exists($tuv{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tuv{$key}) 41 127 168
131 41 $opt{$key} = $tuv{$key};
132 41 delete $tuv{$key};
133 }
134 }
135 21 ▶ if (defined($tuv{srcencoding})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tuv{"srcencoding"}) 0 21 21
136 0 $opt{'o-encoding'} = $tuv{srcencoding};
137 0 delete $tuv{srcencoding};
138 }
139 21 ▶ $verbatim++ if defined $tuv{-verbatim};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tuv{"-verbatim"} 0 21 21
140 21 ▶ delete $tuv{-verbatim} if exists $tuv{-verbatim};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tuv{"-verbatim"} 20 1 21
141  
142 21 ▶ if (defined($tuv{"-prop"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tuv{"-prop"}) 21 0 21
143 21 %prop = %{$tuv{"-prop"}};
144 21 delete $tuv{"-prop"};
145 }
146 21 ▶ if (defined($tuv{"-note"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tuv{"-note"}) 21 0 21
147 21 @note = @{$tuv{"-note"}};
148 21 delete $tuv{"-note"};
149 }
150 21 ▶ if (defined($tuv{"-n"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tuv{"-n"}) 0 21 21
151 0 $opt{id}=$tuv{"-n"};
152 0 delete $tuv{"-n"};
153 }
154  
155 21 $self->_startTag(0,'tu', %opt)->_nl;
156  
157 ### write the prop s <prop type="x-name">problemas 23</prop>
158 21 $self->_write_props(3, \%prop);
159 21 $self->_write_notes(3, \@note);
160  
161 21 for my $lang (sort keys %tuv) {
162 42 my $cdata = 0;
163 42 $self->_startTag(1, 'tuv', 'xml:lang' => $lang);
164 42 ▶ if (ref($tuv{$lang}) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tuv{$lang} eq "HASH") 41 1 42
165 41 ▶ $cdata++ if defined($tuv{$lang}{-iscdata});
Branch: 1 branch
BranchTrueFalseTotal
if defined $tuv{$lang}{"-iscdata"} 0 41 41
166 41 ▶ delete($tuv{$lang}{-iscdata}) if exists($tuv{$lang}{-iscdata});
Branch: 1 branch
BranchTrueFalseTotal
if exists $tuv{$lang}{"-iscdata"} 0 41 41
167  
168 41 ▶ $self->_write_props(2, $tuv{$lang}{-prop}) if exists $tuv{$lang}{-prop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tuv{$lang}{"-prop"} 7 34 41
169 41 ▶ $self->_write_notes(2, $tuv{$lang}{-note}) if exists $tuv{$lang}{-note};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tuv{$lang}{"-note"} 7 34 41
170 41 ▶ $tuv{$lang} = $tuv{$lang}{-seg} || "";
Condition50%1/2
$tuv{$lang}{'-seg'} || ''
l!l
410
MC/DC0%0/1
$tuv{$lang}{'-seg'} || ''
$tuv{$lang}{'-seg'}
Truth table50%1/2
A
A= $tuv{$lang}{'-seg'}
Aresult
11
00
171 }
172 42 $self->_startTag(0, 'seg');
173 42 ▶ if ($verbatim) {
Branch: 2 branches
BranchTrueFalseTotal
if ($verbatim) { } 0 42 42
elsif ($cdata) { } 0 42 42
174 0 $self->_write($tuv{$lang});
175 } elsif ($cdata) {
176 0 $self->_write("<![CDATA[");
177 0 $self->_write($tuv{$lang});
178 0 $self->_write("]]>");
179 } else {
180 42 $self->_characters($tuv{$lang});
181 }
182 42 $self->_endTag('seg');
183 42 $self->_endTag('tuv')->_nl;
184 }
185 21 $self->_endTag('tu')->_nl->_nl;
186 }
187  
188  
189  
190 5 ▶sub end_tmx {
Subroutine end_tmx: called CC=1 SCAR=0.0
Pod documented
191 5 my $self = shift();
192 5 $self->_endTag('body')->_nl;
193 5 $self->_endTag('tmx')->_nl;
194 5 close($self->{OUTPUT});
195 }
196  
197  
198 1321 ▶sub _write {
Subroutine _write: called CC=1 SCAR=0.0
199 1321 my $self = shift;
200 1321 print {$self->{OUTPUT}} @_;
201 1321 return $self;
202 }
203  
204 296 ▶sub _nl {
Subroutine _nl: called CC=1 SCAR=0.0
205 296 my $self = shift;
206 296 $self->_write("\n");
207 }
208  
209 274 ▶sub _startTag {
Subroutine _startTag: called CC=2 SCAR=6.9
210 274 my ($self, $indent, $tagName, %attributes) = @_;
211 274 my $attributes = "";
212 274 ▶ $attributes = " ".join(" ",map {"$_=\"$attributes{$_}\""} sort keys %attributes) if %attributes;
Branch: 1 branch
BranchTrueFalseTotal
if %attributes 138 136 274
213 274 $self->_indent($indent)->_write("<$tagName$attributes>");
214 }
215  
216 280 ▶sub _indent {
Subroutine _indent: called CC=1 SCAR=0.0
217 280 my ($self, $indent) = @_;
218 280 $indent = " " x $indent;
219 280 $self->_write($indent);
220 }
221  
222 193 ▶sub _characters {
Subroutine _characters: called CC=2 SCAR=6.9
223 193 my ($self, $text) = @_;
224  
225 193 ▶ $text = "" unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 193 193
226 193 $text =~ s/\n/ /g;
227 193 $text =~ s/ +/ /g;
228 193 $text =~ s/&/&amp;/g;
229 193 $text =~ s/</&lt;/g;
230 193 $text =~ s/>/&gt;/g;
231 193 $text =~ s!&lt;(b|emph)&gt;(.+?)&lt;/\1&gt;!<$1>$2</$1>!gs;
232  
233 193 $self->_write($text);
234 }
235  
236 272 ▶sub _endTag {
Subroutine _endTag: called CC=1 SCAR=0.0
237 272 my ($self, $tagName) = @_;
238  
239 272 $self->_write("</$tagName>");
240 }
241  
242 8 1;
243  
244 __END__
« blib/lib/XML/TMX/Reader.pm ↑ Summary scripts/tmx2tmx »