blib/lib/TBX/Min.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Build ↑ Summary
1 #
2 # This file is part of TBX-Min
3 #
4 # This software is copyright (c) 2013 by Alan Melby.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package TBX::Min;
10 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 our $VERSION = '0.06'; # VERSION
13 # ABSTRACT: Read, write and edit TBX-Min files
14 7 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use autodie;
16 use Path::Tiny;
17 use Carp;
18 use TBX::Min::Entry;
19 use TBX::Min::LangGroup;
20 use TBX::Min::TermGroup;
21 use Import::Into;
22 use DateTime::Format::ISO8601;
23 use Try::Tiny;
24  
25 # Use Import::Into to export subclasses into caller
26 sub import {
27 my $target = caller;
28 TBX::Min::Entry->import::into($target);
29 TBX::Min::LangGroup->import::into($target);
30 TBX::Min::TermGroup->import::into($target);
31 return;
32 }
33  
34 sub new_from_xml {
35 my ($class, $data) = @_;
36  
37 if(!$data){
38 croak 'missing required data argument';
39 }
40  
41 my $fh = _get_handle($data);
42  
43 # build a twig out of the input document
44 my $twig = XML::Twig->new(
45 output_encoding => 'UTF-8',
46 # do_not_chain_handlers => 1, #can be important when things get complicated
47 keep_spaces => 0,
48  
49 # these store new entries, langGroups and termGroups
50 start_tag_handlers => {
51 entry => \&_conceptStart,
52 langGroup => \&_langStart,
53 termGroup => \&_termGrpStart,
54 },
55  
56 TwigHandlers => {
57 TBX => \&_check_dialect,
58 # header attributes become attributes of the TBX::Min object
59 id => \&_headerAtt,
60 description => \&_headerAtt,
61 dateCreated => \&_date_created,
62 creator => \&_headerAtt,
63 license => \&_headerAtt,
64 directionality => \&_directionality,
65 languages => \&_languages,
66  
67 # becomes part of the current TBX::Min::Entry object
68 subjectField => sub {
69 shift->{tbx_min_entries}->[-1]->subject_field($_->text)},
70  
71 # these become attributes of the current TBX::Min::TermGroup object
72 term => sub {shift->{tbx_min_current_term_grp}->term($_->text)},
73 partOfSpeech => sub {
74 shift->{tbx_min_current_term_grp}->part_of_speech($_->text)},
75 note => sub {shift->{tbx_min_current_term_grp}->note($_->text)},
76 customer => sub {
77 shift->{tbx_min_current_term_grp}->customer($_->text)},
78 termStatus => sub {
79 shift->{tbx_min_current_term_grp}->status($_->text)},
80 }
81 );
82  
83 # use handlers to process individual tags, then grab the result
84 $twig->parse($fh);
85 my $self = $twig->{tbx_min_att};
86 $self->{entries} = $twig->{tbx_min_entries} || [];
87 bless $self, $class;
88 return $self;
89 }
90  
91 sub _get_handle {
92 my ($data) = @_;
93 my $fh;
94 if((ref $data) eq 'SCALAR'){
95 open $fh, '<', $data; ## no critic(RequireBriefOpen)
96 }else{
97 $fh = path($data)->filehandle('<');
98 }
99 return $fh;
100 }
101  
102 sub new {
103 my ($class, $args) = @_;
104 my $self;
105 if((ref $args) eq 'HASH'){
106 #don't store a plain string for datetime
107 if(my $dt_string = $args->{date_created}){
108 $args->{date_created} = _parse_datetime($dt_string);
109 }
110 if(exists $args->{directionality}){
111 _validate_dir($args->{directionality});
112 }
113 $self = $args;
114 }else{
115 $self = {};
116 }
117 $self->{entries} ||= [];
118 return bless $self, $class;
119 }
120  
121 sub id {
122 my ($self, $id) = @_;
123 if($id) {
124 return $self->{id} = $id;
125 }
126 return $self->{id};
127 }
128  
129 sub description {
130 my ($self, $description) = @_;
131 if($description) {
132 return $self->{description} = $description;
133 }
134 return $self->{description};
135 }
136  
137 sub date_created {
138 my ($self, $date_created) = @_;
139 if($date_created) {
140 return $self->{date_created} =
141 _parse_datetime($date_created);
142 }
143 if(my $dt = $self->{date_created}){
144 return $dt->iso8601;
145 }
146 return;
147 }
148  
149 sub _parse_datetime {
150 my ($dt_string) = @_;
151 my $dt;
152 try{
153 $dt = DateTime::Format::ISO8601->parse_datetime($dt_string);
154 }catch{
155 croak 'date is not in ISO8601 format';
156 };
157 return $dt;
158 }
159  
160 sub creator {
161 my ($self, $creator) = @_;
162 if($creator) {
163 return $self->{creator} = $creator;
164 }
165 return $self->{creator};
166 }
167  
168 sub license {
169 my ($self, $license) = @_;
170 if($license) {
171 return $self->{license} = $license;
172 }
173 return $self->{license};
174 }
175  
176 sub directionality {
177 my ($self, $directionality) = @_;
178 if(defined $directionality) {
179 _validate_dir($directionality);
180 return $self->{directionality} = $directionality;
181 }
182 return $self->{directionality};
183 }
184  
185 sub _validate_dir {
186 my ($dir) = @_;
187 if($dir ne 'bidirectional' and $dir ne 'monodirectional'){
188 croak "Illegal directionality '$dir'";
189 }
190 return;
191 }
192  
193  
194 sub source_lang {
195 my ($self, $source_lang) = @_;
196 if($source_lang) {
197 return $self->{source_lang} = $source_lang;
198 }
199 return $self->{source_lang};
200 }
201  
202 sub target_lang {
203 my ($self, $target_lang) = @_;
204 if($target_lang) {
205 return $self->{target_lang} = $target_lang;
206 }
207 return $self->{target_lang};
208 }
209  
210 sub entries { ## no critic(RequireArgUnpacking)
211 my ($self) = @_;
212 if (@_ > 1){
213 croak 'extra argument found (entries is a getter only)';
214 }
215 return $self->{entries};
216 }
217  
218 sub add_entry {
219 my ($self, $entry) = @_;
220 if( !$entry || !$entry->isa('TBX::Min::Entry') ){
221 croak 'argument to add_entry should be a TBx::Min::Entry';
222 }
223 push @{$self->{entries}}, $entry;
224 return;
225 }
226  
227 sub as_xml {
228 my ($self) = @_;
229  
230 # construct the whole document using XML::Twig::El's
231 my $root = XML::Twig::Elt->new(TBX => {dialect => 'TBX-Min'});
232 my $header = XML::Twig::Elt->new('header')->paste($root);
233  
234 # each of these header elements is a simple element with text
235 for my $header_att (
236 qw(id creator license directionality description)){
237 next unless $self->{$header_att};
238 XML::Twig::Elt->new($header_att,
239 $self->{$header_att})->paste(last_child => $header);
240 }
241 if($self->source_lang || $self->target_lang){
242 my @atts;
243 push @atts, (source => $self->source_lang) if $self->source_lang;
244 push @atts, (target => $self->target_lang) if $self->target_lang;
245 XML::Twig::Elt->new(languages => {@atts})->paste(
246 last_child => $header)
247 }
248 if(my $dt = $self->{date_created}){
249 XML::Twig::Elt->new(dateCreated => $dt->iso8601)->paste(
250 last_child => $header);
251 }
252  
253 my $body = XML::Twig::Elt->new('body')->paste(last_child => $root);
254 for my $entry (@{$self->entries}){
255 my $entry_el = XML::Twig::Elt->new(
256 entry => {$entry->id ? (id => $entry->id) : ()})->
257 paste(last_child => $body);
258 if(my $sf = $entry->subject_field){
259 XML::Twig::Elt->new(subjectField => $sf)->paste(
260 last_child => $entry_el);
261 }
262 for my $langGrp (@{$entry->lang_groups}){
263 my $lang_el = XML::Twig::Elt->new(langGroup =>
264 {$langGrp->code ? ('xml:lang' => $langGrp->code) : ()}
265 )->paste(last_child => $entry_el);
266 for my $termGrp (@{$langGrp->term_groups}){
267 my $term_el = XML::Twig::Elt->new('termGroup')->paste(
268 last_child => $lang_el);
269 if (my $term = $termGrp->term){
270 XML::Twig::Elt->new(term => $term)->paste(
271 last_child => $term_el);
272 }
273  
274 if (my $customer = $termGrp->customer){
275 XML::Twig::Elt->new(customer => $customer)->paste(
276 last_child => $term_el);
277 }
278  
279 if (my $note = $termGrp->note){
280 XML::Twig::Elt->new(note => $note)->paste(
281 last_child => $term_el);
282 }
283  
284 if (my $status = $termGrp->status){
285 XML::Twig::Elt->new(termStatus => $status )->paste(
286 last_child => $term_el);
287 }
288  
289 if (my $pos = $termGrp->part_of_speech){
290 XML::Twig::Elt->new(partOfSpeech => $pos)->paste(
291 last_child => $term_el);
292 }
293  
294 } # end termGroup
295 } # end langGroup
296 } # end entry
297  
298 # return pretty-printed string
299 XML::Twig->set_pretty_print('indented');
300 return \$root->sprint;
301 }
302  
303 ######################
304 ### XML TWIG HANDLERS
305 ######################
306  
307 # croak if the user happened to use the wrong dialect of TBX
308 sub _check_dialect {
309 my (undef, $node) = @_;
310 my $type = $node->att('dialect') || 'unknown';
311 my $expected = 'TBX-Min';
312 if($type ne $expected){
313 croak "Input TBX is $type (should be '$expected')";
314 }
315 return 1;
316 }
317  
318 # most of the twig handlers store state on the XML::Twig object.
319 # A bit kludgy, but it works.
320  
321 sub _headerAtt {
322 my ($twig, $node) = @_;
323 $twig->{tbx_min_att}->{_decamel($node->name)} = $node->text;
324 return 1;
325 }
326  
327 sub _directionality {
328 my ($twig, $node) = @_;
329 _validate_dir($node->text);
330 $twig->{tbx_min_att}->{directionality} = $node->text;
331 return 1;
332 }
333  
334 sub _date_created {
335 my ($twig, $node) = @_;
336 $twig->{tbx_min_att}->{date_created} =
337 _parse_datetime($node->text);
338 return;
339 }
340  
341 # turn camelCase into camel_case
342 sub _decamel {
343 my ($camel) = @_;
344 $camel =~ s/([A-Z])/_\l$1/g;
345 return $camel;
346 }
347  
348 sub _languages{
349 my ($twig, $node) = @_;
350 if(my $source = $node->att('source')){
351 ${ $twig->{'tbx_min_att'} }{'source_lang'} = $source;
352 }
353 if(my $target = $node->att('target')){
354 ${ $twig->{'tbx_min_att'} }{'target_lang'} = $target;
355 }
356 return 1;
357 }
358  
359 # add a new concept entry to the list of those found in this file
360 sub _conceptStart {
361 my ($twig, $node) = @_;
362 my $concept = TBX::Min::Entry->new();
363 if($node->att('id')){
364 $concept->id($node->att('id'));
365 }else{
366 carp 'found entry missing id attribute';
367 }
368 push @{ $twig->{tbx_min_entries} }, $concept;
369 return 1;
370 }
371  
372 #just set the subject_field of the current concept
373 sub _subjectField {
374 my ($twig, $node) = @_;
375 $twig->{tbx_min_entries}->[-1]->
376 subject_field($node->text);
377 return 1;
378 }
379  
380 # Create a new LangGroup, add it to the current concept,
381 # and set it as the current LangGroup.
382 sub _langStart {
383 my ($twig, $node) = @_;
384 my $lang = TBX::Min::LangGroup->new();
385 if($node->att('xml:lang')){
386 $lang->code($node->att('xml:lang'));
387 }else{
388 carp 'found langGroup missing xml:lang attribute';
389 }
390  
391 $twig->{tbx_min_entries}->[-1]->add_lang_group($lang);
392 $twig->{tbx_min_current_lang_grp} = $lang;
393 return 1;
394 }
395  
396 # Create a new termGroup, add it to the current langGroup,
397 # and set it as the current termGroup.
398 sub _termGrpStart {
399 my ($twig) = @_;
400 my $term = TBX::Min::TermGroup->new();
401 $twig->{tbx_min_current_lang_grp}->add_term_group($term);
402 $twig->{tbx_min_current_term_grp} = $term;
403 return 1;
404 }
405  
406 1;
407  
408 __END__
409  
410 =pod
411  
412 =head1 NAME
413  
414 TBX::Min - Read, write and edit TBX-Min files
415  
416 =head1 VERSION
417  
418 version 0.06
419  
420 =head1 SYNOPSIS
421  
422 use TBX::Min;
423 my $min = TBX::Min->new('/path/to/file.tbx');
424 my $entries = $min->entries;
425 my $entry = TBX::Min::Entry->new({id => 'B001'});
426 $min->add_entry($entry);
427  
428 =head1 DESCRIPTION
429  
430 This module allows you to read, write and edit the contents of TBX-Min
431 data.
432  
433 C<use>ing this module also automatically C<use>s L<TBX::Min::Entry>,
434 L<TBX::Min::LangGroup>, and L<TBX::Min::TermGroup> via
435 L<Import::Into>. LangGroups contain TermGroups, Entries contain
436 LangGroups, and this class contains Entries. These correspond to the
437 three levels of information found in TML. You can build up TBX::Min
438 documents this way and then print them via L</as_xml>. You can also
439 read an entire TBX-Min XML document for editing via L</new_from_xml>.
440  
441 =head1 TBX-Min
442  
443 TBX-Min is a minimal, DCT-style dialect of TBX. It's purpose is to
444 represent extremely simple termbases, such as spreadsheets, and to
445 be as human eye-friendly as possible. TBX-Min did not evolve from
446 any other XML dialect, and so does not have historical artifacts
447 such as "martif".
448  
449 DCT stands for "Data Category as Tag Name". Whereas in most TBX
450 dialects categories such as C<partOfSpeech> are indicated through
451 attributes, in TBX-Min the tag names represent categories. This
452 makes for a very readable document. While TBX-Min documents do
453 conform to TML (Terminological Markup Language) structure, DCT
454 documents cannot be checked by the
455 L<TBX-Checker|https://sourceforge.net/projects/tbxutil/>.
456  
457 If you need more complex or information-rich termbases, we suggest
458 you use TBX-Basic or even TBX-Default. If you have a TBX-Min document
459 and would like to upgrade it to TBX-Basic, see L<Convert::TBX::Min>.
460 Alternatively if you would like to change your TBX-Basic to TBX-Min,
461 see L<Convert::TBX::Basic>.
462  
463 =head1 METHODS
464  
465 =head2 C<new_from_xml>
466  
467 Creates a new instance of TBX::Min. The single argument should be either a
468 string pointer containing the TBX-Min XML data or the name of the file
469 containing this data is required.
470  
471 =head2 C<new>
472  
473 Creates a new C<TBX::Min> instance. Optionally you may pass in
474 a hash reference which is used to initialize the object. The allowed hash
475 fields are C<id>, C<description>, C<date_created>, C<creator>, C<license>,
476 C<directionality>, C<source_lang> and C<target_lang>, which correspond to
477 methods of the same name, and C<entries>, which should be an array reference
478 containing C<TBX::Min::Entry> objects. This method croaks if
479 C<date_created> is not in ISO 8601 format.
480  
481 =head2 C<id>
482  
483 Get or set the document id. This should be a unique string
484 identifying this glossary.
485  
486 =head2 C<description>
487  
488 Get or set the document description.
489  
490 =head2 C<date_created>
491  
492 Get or set the the date that the document was created. This should be a
493 string in ISO 8601 format. This method croaks if C<date_created> is not
494 in ISO 8601 format.
495  
496 =head2 C<creator>
497  
498 Get or set the name of the document creator.
499  
500 =head2 C<license>
501  
502 Get or set the document license string.
503  
504 =head2 C<directionality>
505  
506 Get or set the document directionality string. This string represents
507 the direction of translation this document is designed for.
508  
509 =head2 C<source_lang>
510  
511 Get or set the code representing the document source language. This should
512 be ISO 639 and 3166 (e.g. C<en-US>, C<de>, etc.).
513  
514 =head2 C<target_lang>
515  
516 Get or set the code representing the document target language. This should
517 be ISO 639 and 3166 (e.g. C<en-US>, C<de>, etc.).
518  
519 =head2 C<entries>
520  
521 Returns an array ref containing the C<TBX::Min::Entry> objects contained
522 in the document.The array ref is the same one used to store the objects
523 internally, so additions or removals from the array will be reflected in future
524 calls to this method.
525  
526 =head2 C<add_entry>
527  
528 Adds the input C<TBX::Min::Entry> object to the list of language groups
529 contained by this object.
530  
531 =head2 C<as_xml>
532  
533 Returns a scalar reference containing an XML representation of this
534 TBX-Min document. The data is a UTF-8 encoded string.
535  
536 =head1 CAVEATS
537  
538 TBX::Min does not as of yet fully validate TBX-Min documents. It is
539 possible to create non-validating XML via the L<as_xml> method.
540 This should be fixed in the future.
541  
542 =head1 SEE ALSO
543  
544 The following related modules:
545  
546 =over
547  
548 =item L<TBX::Min::Entry>
549  
550 =item L<TBX::Min::LangGroup>
551  
552 =item L<TBX::Min::TermGroup>
553  
554 =item L<Convert::TBX::Min>
555  
556 =item L<Convert::TBX::Basic>
557  
558 =back
559  
560 Schema for valiating TBX-Min files are available on
561 L<GitHub|https://github.com/byutrg/TBX-Spec>.
562  
563 =head1 AUTHOR
564  
565 Nathan Glenn <garfieldnate@gmail.com>
566  
567 =head1 COPYRIGHT AND LICENSE
568  
569 This software is copyright (c) 2013 by Alan Melby.
570  
571 This is free software; you can redistribute it and/or modify it under
572 the same terms as the Perl 5 programming language system itself.
573  
574 =cut
« Build ↑ Summary