Exporter/MARC/XML.pm

filter: Statementstmt 87.1% 61 / 70 Branchbran 65.6% 21 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 3 totaltotal 78.8% 93 / 118 CC 18 SCAR 20.7
CC
18
Cov
80%
add
15.0
BUILD
3.3
_line
2.3
CRAP 7.9
« Fix/Inline/marc_map.pm ↑ Summary Fix/Bind/marc_each.pm »
1 package Catmandu::Exporter::MARC::XML;
2 33 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use Catmandu::Util qw(xml_escape is_different :array :is);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 our $VERSION = '1.33';
7  
8 30 with 'Catmandu::Exporter', 'Catmandu::Exporter::MARC::Base', 'Catmandu::Buffer';
9  
10 14 ▶has record => (is => 'ro' , default => sub { 'record'});
Subroutine __ANON__: called CC=1 SCAR=0.0
11 14 ▶has record_format => (is => 'ro' , default => sub { 'raw'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 14 ▶has skip_empty_subfields => (is => 'ro' , default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
13 0 ▶has collection => (is => 'ro' , default => sub { 1 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 0 ▶has xml_declaration => (is => 'ro' , default => sub { 1 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 14 ▶has pretty => (is => 'rw' , default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 14 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=12.0
Pod undocumented
18 14 my ($self, $args) = @_;
19  
20 14 ▶ if ($self->xml_declaration) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xml_declaration) 0 14 14
21 0 $self->buffer_add( Catmandu::Util::xml_declaration );
22 }
23  
24 14 ▶ if ($self->collection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collection) 0 14 14
25 0 $self->_line(0,'<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">');
26 }
27  
28 14 $self->fh->print( join('', @{ $self->buffer } ) );
29 14 $self->clear_buffer;
30 }
31  
32 672 ▶sub _line {
Subroutine _line: called CC=2 SCAR=8.4
33 672 my ($self, $indent, $line) = @_;
34 672 ▶ if ($self->pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pretty) { } 0 672 672
35 0 my $pre = " " x $indent;
36 0 $self->buffer_add( $pre.$line."\n" );
37 } else {
38 672 $self->buffer_add( $line );
39 }
40 }
41  
42 14 ▶sub add {
Subroutine add: called CC=14 SCAR=27.1
Pod undocumented
43 14 my ($self, $data) = @_;
44  
45 14 ▶ my $indent = $self->collection ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->collection ? : 0 14 14
46  
47 14 ▶ if ($self->record_format eq 'MARC-in-JSON') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->record_format eq "MARC-in-JSON") 0 14 14
48 0 $data = $self->_json_to_raw($data);
49 }
50  
51 14 ▶ if ($self->collection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collection) { } 0 14 14
52 0 $self->_line($indent,'<marc:record>');
53 }
54 else {
55 14 $self->_line($indent,'<marc:record xmlns:marc="http://www.loc.gov/MARC21/slim">');
56 }
57  
58 14 my $record = $data->{$self->record};
59  
60 14 for my $field (@$record) {
61 223 my ($tag, $ind1, $ind2, @data) = @$field;
62  
63 223 ▶ $ind1 = ' ' unless defined $ind1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ind1 0 223 223
64 223 ▶ $ind2 = ' ' unless defined $ind2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ind2 0 223 223
65  
66 223 ▶ @data = $self->_clean_raw_data($tag,@data) if $self->skip_empty_subfields;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_empty_subfields 223 0 223
67  
68 223 ▶ next if $tag eq 'FMT';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "FMT" 1 222 223
69 222 ▶ next if @data == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @data == 0 4 218 222
70  
71 218 ▶ if ($tag eq 'LDR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'LDR') { } 11 207 218
elsif ($tag =~ /^00/) { } 47 160 207
72 11 $self->_line($indent+1,'<marc:leader>' . xml_escape($data[1]) . '</marc:leader>');
73 }
74 elsif ($tag =~ /^00/) {
75 47 $self->_line($indent+1,'<marc:controlfield tag="' . xml_escape($tag) . '">' . xml_escape($data[1]) . '</marc:controlfield>');
76 }
77 else {
78 160 $self->_line($indent+1,'<marc:datafield tag="' . xml_escape($tag) . '" ind1="' . $ind1 . '" ind2="' . $ind2 . '">');
79 160 ▶ while (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 266 160 426
80 266 my ($code, $val) = splice(@data, 0, 2);
81 # some special characters are allowed as subfiled codes in local defined field
82 # see https://www.loc.gov/marc/96principl.html#eight 8.4.2.3.
83 266 ▶ next unless $code =~ /[a-z0-9!"#\$%&'\(\)\*\+'-\.\/:;<=>]/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ m[[a-z0-9!"#\$%&'\(\)\*\+'-\./:;<=>]]g 0 266 266
84 266 $self->_line($indent+2,'<marc:subfield code="' . $code . '">' . xml_escape($val) . '</marc:subfield>');
85 }
86 160 $self->_line($indent+1,'</marc:datafield>');
87 }
88 }
89  
90 14 $self->_line($indent,'</marc:record>');
91  
92 14 $self->fh->print( join('', @{ $self->buffer } ) );
93 14 $self->clear_buffer;
94 }
95  
96 14 ▶sub commit {
Subroutine commit: called CC=2 SCAR=7.4
Pod undocumented
97 14 my ($self) = @_;
98  
99 14 ▶ if($self->collection){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collection) 0 14 14
100 0 $self->fh->print('</marc:collection>');
101 }
102  
103 14 $self->fh->flush;
104  
105 14 1;
106 }
107  
108 30 1;
109 __END__
« Fix/Inline/marc_map.pm ↑ Summary Fix/Bind/marc_each.pm »