Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub convertMARCtoXML
- {
- my $self = shift;
- my $marc = shift;
- print "headed into as_xml\n";
- my $thisXML = '';
- # Turn on dying from warnings
- # MARC::Charset can throw warnings here, and we don't want to continue if we get some
- local $SIG{__WARN__} = sub { die @_; };
- local $@;
- eval
- {
- $thisXML = $marc->as_xml();
- 1;
- } or do
- {
- $marc->encoding('UTF-8');
- $thisXML = $marc->as_xml();
- };
- # Turn off dying from warnings
- local $SIG{__WARN__} = sub { };
- $thisXML =~ s/\n//sog;
- $thisXML =~ s/^<\?xml.+\?\s*>//go;
- $thisXML =~ s/>\s+</></go;
- $thisXML =~ s/\p{Cc}//go;
- $thisXML = entityize($self, $thisXML);
- $thisXML =~ s/[\x00-\x1f]//go;
- $thisXML =~ s/^\s+//;
- $thisXML =~ s/\s+$//;
- $thisXML =~ s/<record><leader>/<leader>/;
- $thisXML =~ s/<collection/<record/;
- $thisXML =~ s/<\/record><\/collection>/<\/record>/;
- return $thisXML;
- }
- sub readMARCFile
- {
- my $self = shift;
- my $marcFile = shift;
- my $fExtension = getFileExt($self, $marcFile);
- my $file;
- $self->{log}->addLine("Reading $marcFile");
- $file = MARC::File::USMARC->in($marcFile) if $fExtension !=~ m/xml/;
- $file = MARC::File::XML->in($marcFile) if $fExtension =~ m/xml/;
- my @ret;
- local $@;
- eval
- {
- while ( my $marc = $file->next() )
- {
- push (@ret, $marc);
- }
- 1; # ok
- } or do
- {
- $file->close();
- @ret = @{readMARCFileRaw($self, $marcFile)};
- };
- $file->close();
- undef $file;
- return \@ret;
- }
- sub readMARCFileRaw
- {
- my $self = shift;
- my $marcFile = shift;
- use IO::File;
- IO::File->input_record_separator("\x1E\x1D");
- my $file = IO::File->new("< $marcFile");
- $self->{log}->addLine("Reading $marcFile");
- my @ret;
- my $count = 0;
- while (my $raw = <$file>)
- {
- $count++;
- my $marc = MARC::Record->new_from_usmarc($raw);
- my @warnings = $marc->warnings();
- if (@warnings)
- {
- $self->addTrace("readMARCFileRaw", "$marcFile could not be fully read. Record $count had warnings");
- $self->setError("$marcFile could not be fully read. Record $count had warnings");
- }
- push (@ret, $marc);
- }
- $file->close();
- IO::File->input_record_separator("\n");
- undef $file;
- undef $count;
- return \@ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement