package Catmandu::Exporter::MARC; use Catmandu::Sane; use Catmandu::Util qw(xml_escape is_different); use Moo; with 'Catmandu::Exporter'; has type => (is => 'ro', default => sub { 'XML' }); has xml_declaration => (is => 'ro'); has collection => (is => 'ro'); has record_format => (is => 'ro', default => sub { 'raw' }); has record => (is => 'ro', lazy => 1, default => sub { 'record' }); sub add { my ($self, $data) = @_; my @out; if (!$self->count) { if ($self->xml_declaration) { push @out, Catmandu::Util::xml_declaration; } if ($self->collection) { push @out, qq(); } } push @out, $self->collection ? '' : qq(); if ($self->record_format eq 'raw') { # raw MARC array for my $field (@{$data->{$self->record}}) { my ($tag, $ind1, $ind2, @data) = @$field; if ($tag eq 'LDR') { push @out, '', xml_escape($data[1]), ''; } elsif ($tag =~ /^00/) { push @out, '', xml_escape($data[1]), ''; } elsif ($tag !~ /^00.|FMT|LDR/) { push @out, ''; while (@data) { my ($code, $val) = splice(@data, 0, 2); next unless $code =~ /[A-Za-z0-9]/; push @out, '', xml_escape($val), ''; } push @out, ''; } } } else { # MARC-in-JSON push @out, '', xml_escape($data->{leader}), ''; for my $field (@{$data->{fields}}) { my ($tag) = keys %$field; my $val = $field->{$tag}; if (ref $val) { push @out, ''; for my $subfield (@{$val->{subfields}}) { my ($code) = keys %$subfield; push @out, '', xml_escape($subfield->{$code}), ''; } push @out, ''; } else { push @out, '', xml_escape($val), ''; } } } $self->fh->print(join "", @out, ''); } sub commit { my ($self) = @_; if ($self->collection) { $self->fh->print(''); } } =head1 NAME Catmandu::Exporter::MARC - serialize parsed MARC data =head1 SYNOPSIS use Catmandu::Exporter::MARC; my $exporter = Catmandu::Exporter::MARC->new(file => "marc.xml", type => "XML"); my $data = { record => [ ... [245, '1', '0', 'a', 'Cross-platform Perl /', 'c', 'Eric F. Johnson.'], ... ], }; $exporter->add($data); $exporter->commit; # to serialize MARC-in-JSON: my $exporter = Catmandu::Exporter::MARC->new(record_format => "MARC-in-JSON"); =cut 1;