Advertisement
Guest User

Untitled

a guest
Mar 14th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.46 KB | None | 0 0
  1.  
  2. sub convertMARCtoXML
  3. {
  4.     my $self = shift;
  5.     my $marc = shift;
  6.     print "headed into as_xml\n";
  7.     my $thisXML = '';
  8.     # Turn on dying from warnings
  9.     # MARC::Charset can throw warnings here, and we don't want to continue if we get some
  10.     local $SIG{__WARN__} = sub { die @_; };
  11.     local $@;
  12.     eval
  13.     {
  14.         $thisXML = $marc->as_xml();
  15.         1;
  16.     } or do
  17.     {
  18.         $marc->encoding('UTF-8');
  19.         $thisXML = $marc->as_xml();
  20.     };
  21.  
  22.     # Turn off dying from warnings
  23.     local $SIG{__WARN__} = sub {  };
  24.  
  25.     $thisXML =~ s/\n//sog;
  26.     $thisXML =~ s/^<\?xml.+\?\s*>//go;
  27.     $thisXML =~ s/>\s+</></go;
  28.     $thisXML =~ s/\p{Cc}//go;
  29.     $thisXML = entityize($self, $thisXML);
  30.     $thisXML =~ s/[\x00-\x1f]//go;
  31.     $thisXML =~ s/^\s+//;
  32.     $thisXML =~ s/\s+$//;
  33.     $thisXML =~ s/<record><leader>/<leader>/;
  34.     $thisXML =~ s/<collection/<record/;
  35.     $thisXML =~ s/<\/record><\/collection>/<\/record>/;
  36.  
  37.     return $thisXML;
  38. }
  39.  
  40. sub readMARCFile
  41. {
  42.     my $self = shift;
  43.     my $marcFile = shift;
  44.     my $fExtension = getFileExt($self, $marcFile);
  45.     my $file;
  46.     $self->{log}->addLine("Reading $marcFile");
  47.     $file = MARC::File::USMARC->in($marcFile) if $fExtension !=~ m/xml/;
  48.     $file = MARC::File::XML->in($marcFile) if $fExtension =~ m/xml/;
  49.     my @ret;
  50.     local $@;
  51.     eval
  52.     {
  53.         while ( my $marc = $file->next() )
  54.         {
  55.             push (@ret, $marc);
  56.         }
  57.         1;  # ok
  58.     } or do
  59.     {
  60.         $file->close();
  61.         @ret = @{readMARCFileRaw($self, $marcFile)};
  62.     };
  63.  
  64.     $file->close();
  65.     undef $file;
  66.     return \@ret;
  67. }
  68.  
  69. sub readMARCFileRaw
  70. {
  71.     my $self = shift;
  72.     my $marcFile = shift;
  73.  
  74.     use IO::File;
  75.     IO::File->input_record_separator("\x1E\x1D");
  76.  
  77.     my $file = IO::File->new("< $marcFile");
  78.  
  79.     $self->{log}->addLine("Reading $marcFile");
  80.  
  81.     my @ret;
  82.     my $count = 0;
  83.     while (my $raw = <$file>)
  84.     {
  85.         $count++;
  86.         my $marc = MARC::Record->new_from_usmarc($raw);
  87.         my @warnings = $marc->warnings();
  88.         if (@warnings)
  89.         {
  90.             $self->addTrace("readMARCFileRaw", "$marcFile could not be fully read. Record $count had warnings");
  91.             $self->setError("$marcFile could not be fully read. Record $count had warnings");
  92.         }
  93.         push (@ret, $marc);
  94.     }
  95.  
  96.     $file->close();
  97.     IO::File->input_record_separator("\n");
  98.     undef $file;
  99.     undef $count;
  100.     return \@ret;
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement