lib/Serge/Engine/Plugin/parse_xliff.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 SCAR 0.0
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Serge/Engine/Plugin/parse_ts.pm ↑ Summary lib/Serge.pm »
1 package Serge::Engine::Plugin::parse_xliff;
2 1 ▶use parent Serge::Engine::Plugin::Base::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Serge::Util qw(xml_escape_strref xml_unescape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 sub name {
14 return 'XLIFF parser plugin';
15 }
16  
17 sub init {
18 my $self = shift;
19  
20 $self->SUPER::init(@_);
21  
22 $self->{errors} = {};
23  
24 $self->merge_schema({
25 email_from => 'STRING',
26 email_to => 'ARRAY',
27 email_subject => 'STRING',
28 });
29  
30 $self->add('after_job', \&report_errors);
31 }
32  
33 sub validate_data {
34 my $self = shift;
35  
36 $self->SUPER::validate_data;
37  
38 if (!defined $self->{data}->{email_from}) {
39 print "WARNING: 'email_from' is not defined. Will skip sending any reports.\n";
40 }
41  
42 if (!defined $self->{data}->{email_to}) {
43 print "WARNING: 'email_to' is not defined. Will skip sending any reports.\n";
44 }
45 }
46  
47 sub report_errors {
48 my ($self, $phase) = @_;
49  
50 my $email_from = $self->{data}->{email_from};
51 if (!$email_from) {
52 $self->{errors} = {};
53 return;
54 }
55  
56 my $email_to = $self->{data}->{email_to};
57 if (!$email_to) {
58 $self->{errors} = {};
59 return;
60 }
61  
62 my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: XLIFF Parse Errors');
63  
64 my $text;
65 foreach my $key (sort keys %{$self->{errors}}) {
66 my $pre_contents = $self->{errors}->{$key};
67 xml_escape_strref(\$pre_contents);
68 $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n";
69 }
70  
71 $self->{errors} = {};
72  
73 if ($text) {
74 $text = qq|
75 <html>
76 <head>
77 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
78 </head>
79 <body style="font-family: sans-serif; font-size: 120%">
80  
81 <p>
82 # This is an automatically generated message.
83  
84 The following parsing errors were found when attempting to localize resource files.
85 </p>
86  
87 $text
88  
89 </body>
90 </html>
91 |;
92  
93 Serge::Mail::send_html_message(
94 $email_from, # from
95 $email_to, # to (list)
96 $email_subject, # subject
97 $text # message body
98 );
99 }
100 }
101  
102 sub parse {
103 my ($self, $textref, $callbackref, $lang) = @_;
104  
105 die 'callbackref not specified' unless $callbackref;
106  
107 my $tree;
108 eval {
109 $tree = XML::Twig->new()->parse($$textref);
110 $tree->set_indent(' ' x 4);
111 };
112 if ($@) {
113 my $error_text = $@;
114 $error_text =~ s/\t/ /g;
115 $error_text =~ s/^\s+//s;
116  
117 $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error_text;
118  
119 die $error_text;
120 }
121  
122 $self->parse_tree($tree, $callbackref, $lang);
123  
124 return $lang ? $tree->sprint(pretty_print => 'indented') : undef;
125 }
126  
127 sub parse_tree {
128 my ($self, $tree, $callbackref, $lang) = @_;
129  
130 my $version = $tree->root->att('version');
131 ($version =~ m/^(\d+)/) && ($version = $1);
132  
133 $self->{xliff_version} = $version;
134  
135 my $unit_tag;
136 if ($version == 1) {
137 $unit_tag = 'trans-unit';
138 } elsif ($version == 2) {
139 $unit_tag = 'unit';
140 } else {
141 die "Unsupported XLIFF version: '$version'";
142 }
143  
144 my @units = $tree->findnodes('//'.$unit_tag);
145 foreach my $unit (@units) {
146 if ($unit->has_children('segment')) {
147 map {
148 $self->parse_segment($_, $callbackref, $lang);
149 } $unit->children('segment');
150 } else {
151 $self->parse_segment($unit, $callbackref, $lang);
152 }
153 }
154  
155 # put 'target-language' on all <file> tags
156 if ($lang) {
157 map {
158 $_->set_att('target-language' => $lang);
159 } $tree->findnodes('//file');
160 }
161 }
162  
163 sub parse_segment {
164 my ($self, $node, $callbackref, $lang) = @_;
165  
166 my $has_target = $node->has_child('target');
167  
168 # in import mode, try to get translations from <target> tag
169 # and skip units without such tag
170  
171 return if ($self->{import_mode} && !$has_target);
172  
173 my $source_tag = ($self->{import_mode} && $has_target) ? 'target' : 'source';
174  
175 # get as plain text
176 #my $source = $node->first_child($source_tag)->text;
177  
178 # get as raw XML (unsafe, but can support placeholders)
179 my $source = $node->first_child($source_tag)->inner_xml;
180  
181 my $hint = $self->get_context($node);
182 my $translation = &$callbackref($source, undef, $hint, undef, $lang, $node->att('id'));
183  
184 if ($lang) {
185 if ($has_target) {
186 # insert as plain text
187 #xml_escape_strref(\$translation);
188  
189 # add raw XML (unsafe, but can support placeholders)
190 $node->first_child('target')->set_inner_xml($translation);
191 } else {
192 # insert as plain text
193 #$node->insert_new_elt('last_child', 'target' => $translation) if $lang;
194  
195 # insert as raw XML (unsafe, but can support placeholders)
196 $node->insert_new_elt('last_child', 'target')->set_inner_xml($translation);
197 }
198 }
199 }
200  
201 sub get_context {
202 my ($self, $node) = @_;
203  
204 my @ctx_tags;
205 my @ctx_para;
206  
207 while ($node) {
208 if ($node->tag =~ m/^(trans-unit|unit)$/) {
209 unshift @ctx_para, $self->get_notes("Unit note: ", $node);
210 unshift @ctx_tags, "unit-id:".$node->att('id') if $node->att('id') ne '';
211 }
212  
213 if ($node->tag eq 'group') {
214 unshift @ctx_para, $self->get_notes("Group note: ", $node);
215 unshift @ctx_tags, "group-id:".$node->att('id') if $node->att('id') ne '';
216 }
217  
218 if ($node->tag eq 'file') {
219 unshift @ctx_para, $self->get_notes("File note: ", $self->{xliff_version} == 1 ? $node->first_child('header') : $node);
220 unshift @ctx_tags, "file-original:".$node->att('original') if $node->att('original') ne '';
221 last;
222 }
223 $node = $node->parent;
224 }
225  
226 unshift @ctx_para, join(" ", @ctx_tags);
227  
228 return join("\n\n", @ctx_para);
229 }
230  
231 sub get_notes {
232 my ($self, $prefix, $node) = @_;
233  
234 $node = $node->first_child('notes') if defined $node && ($self->{xliff_version} == 2);
235  
236 my @notes;
237  
238 if (defined $node) {
239 map {
240 push @notes, $prefix.$_->text;
241 } $node->children('note');
242 }
243 return @notes;
244 }
245  
246 1;
« lib/Serge/Engine/Plugin/parse_ts.pm ↑ Summary lib/Serge.pm »