| 1 | =head1 NAME |
| 2 | |
| 3 | Catmandu::Exporter::MARC::MARCMaker - Exporter for MARC records to USMARC |
| 4 | |
| 5 | =head1 SYNOPSIS |
| 6 | |
| 7 | # From the command line |
| 8 | $ catmandu convert MARC --type XML to MARC --type MARCMaker < /foo/data.mrc |
| 9 | |
| 10 | # From Perl |
| 11 | use Catmandu; |
| 12 | |
| 13 | my $importer = Catmandu->importer('MARC', file => "/foo/bar.mrc" , type => 'XML'); |
| 14 | my $exporter = Catmandu->exporter('MARC', file => "marc.xml", type => 'MARCMaker' ); |
| 15 | |
| 16 | $exporter->add($importer); |
| 17 | $exporter->commit; |
| 18 | |
| 19 | =head1 CONFIGURATION |
| 20 | |
| 21 | =over |
| 22 | |
| 23 | =item file |
| 24 | |
| 25 | Write output to a local file given by its path or file handle. Alternatively a |
| 26 | scalar reference can be passed to write to a string and a code reference can be |
| 27 | used to write to a callback function. |
| 28 | |
| 29 | =item fh |
| 30 | |
| 31 | Write the output to an L<IO::Handle>. If not specified, |
| 32 | L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output |
| 33 | handle from the C<file> argument or by using STDOUT. |
| 34 | |
| 35 | =item fix |
| 36 | |
| 37 | An ARRAY of one or more fixes or file scripts to be applied to exported items. |
| 38 | |
| 39 | =item encoding |
| 40 | |
| 41 | Binmode of the output stream C<fh>. Set to "C<:utf8>" by default. |
| 42 | |
| 43 | =back |
| 44 | |
| 45 | =head1 METHODS |
| 46 | |
| 47 | See L<Catmandu::Exporter>, L<Catmandu::Addable>, L<Catmandu::Fixable>, |
| 48 | L<Catmandu::Counter>, and L<Catmandu::Logger> for a full list of methods. |
| 49 | |
| 50 | =head1 SEE ALSO |
| 51 | |
| 52 | L<Catmandu::Exporter> |
| 53 | |
| 54 | =cut |
| 55 | package Catmandu::Exporter::MARC::MARCMakerscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/; |
| 56 | use Catmandu::Sanescip-perl cpan Catmandu-MARC 1.32 Catmandu/Sane/; |
| 57 | use Mooscip-perl cpan Catmandu-MARC 1.32 Moo/; |
| 58 | |
| 59 | our $VERSIONscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/$VERSION. = '1.32'; |
| 60 | |
| 61 | withscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/with(). 'Catmandu::Exporter', 'Catmandu::Exporter::MARC::Base'; |
| 62 | |
| 63 | hasscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/has(). record => (is => 'ro' , default => sub { 'record'}); |
| 64 | hasscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/has(). record_format => (is => 'ro' , default => sub { 'raw'} ); |
| 65 | |
| 66 | sub addscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/add(). { |
| 67 | my ($self, $data) = @_scip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/`@_`.; |
| 68 | |
| 69 | if ($self->record_formatscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/record_format(). eq 'MARC-in-JSON') { |
| 70 | $data = $self->_json_to_rawscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/_json_to_raw().($data); |
| 71 | } |
| 72 | |
| 73 | for my $field (@{$data->{record}}) { |
| 74 | my ($field,$ind1,$ind2,@sf) = @$field; |
| 75 | |
| 76 | if (!definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/defined().($ind1) || $ind1 =~ /^\s*$/) { $ind1 = '/'} |
| 77 | if (!definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/defined().($ind2) || $ind2 =~ /^\s*$/) { $ind2 = '/'} |
| 78 | |
| 79 | next unless ($field =~ /^(LDR|\d{3})/); |
| 80 | |
| 81 | my @sf_map = (); |
| 82 | |
| 83 | for (my $i = 0 ; $i < @sf ; $i += 2) { |
| 84 | if ($field eq 'LDR' || $field < 10) { |
| 85 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/push(). @sf_map , $sf[$i+1] if (definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/defined().($sf[$i+1])); |
| 86 | } |
| 87 | else { |
| 88 | pushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/push(). @sf_map |
| 89 | , "\$" . $sf[$i] |
| 90 | , $sf[$i+1] if (definedscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/defined().($sf[$i+1])); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | my $sf_str = joinscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/join().("",@sf_map); |
| 95 | |
| 96 | my $maker_field; |
| 97 | |
| 98 | if ($field =~ /^\d{3}$/ && $field >= 10) { |
| 99 | $maker_field = "=$field $ind1$ind2$sf_str\n"; |
| 100 | } |
| 101 | else { |
| 102 | $maker_field = "=$field $sf_str\n"; |
| 103 | } |
| 104 | |
| 105 | $self->fhscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/fh().->printscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/print().($maker_field); |
| 106 | } |
| 107 | |
| 108 | $self->fhscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/fh().->printscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/print().("\n"); |
| 109 | } |
| 110 | |
| 111 | sub commitscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/commit(). { |
| 112 | my ($self) = @_scip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/`@_`.; |
| 113 | $self->fhscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/fh().->flushscip-perl cpan Catmandu-MARC 1.32 Catmandu/Exporter/MARC/MARCMaker/flush().; |
| 114 | |
| 115 | 1; |
| 116 | } |
| 117 | |
| 118 | 1; |