11 my $term = new Term::ReadLine 'yaz-cleanup';
12 my $OUT = $term->OUT || \*STDOUT;
18 my @record = (); # current record storage
20 my @linecontext= (); # last 5 lines of file
22 my $input = shift || 'incoming.marc.xml';
24 open MARC, '<', $input;
25 open my $NUMARC, '>', 'incoming.clean.marc.xml';
26 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
27 open my $EXMARC, '>', 'incoming.exceptions.marc.xml';
28 print $EXMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
29 open MARC2, '<', $input;
32 # this is the dispatch table which drives command selection in
34 my %commands = ( c => \&print_context,
35 C => \&print_linecontext,
47 my @spinner = qw(- / | \\);
50 while (my $line = getline()) {
51 unless ($count % 2000) {
52 print "\rWorking... ", $spinner[$sidx];
53 $sidx = ($sidx == $#spinner) ? 0 : $sidx + 1;
57 # catch empty datafield elements
58 if ($line =~ m|</datafield>|) {
59 if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
60 pop @record; pop @record;
61 print $OUT "\rEmpty datafield scrubbed at line $count\n";
66 # clean misplaced dollarsigns
67 if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
68 $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
69 print $OUT "\rDollar sign in subfield code corrected at line $count\n";
72 # clean up tags with spaces in them
73 $line =~ s/tag=" /tag="00/g;
74 $line =~ s/tag=" /tag="0/g;
75 $line =~ s/tag="-/tag="0/g;
76 $line =~ s/tag="(\d\d) /tag="0$1/g;
78 # stow tag data if we're looking at it
79 if ($line =~ m/<datafield tag="(\d{3})" ind1="(.)" ind2="(.)">/) {
80 $reccontext{tag} = $1;
81 $reccontext{ind1} = $2;
82 $reccontext{ind2} = $3;
86 if ($line =~ /&/ && $line !~ /&\w+?;/)
87 { edit("Naked ampersand", $line); next }
89 # subfields can't be non-alphanumeric
90 if ($line =~ /<subfield code="(.+?)"/) {
92 if ($match =~ /\P{IsAlnum}/) {
93 edit("Junk in subfield code", $line);
99 print $NUMARC "</xml>\n";
100 print $EXMARC "</xml>\n";
104 Handles the Term::ReadLine loop
109 my ($msg, $line_in) = @_;
110 print $OUT "\r".$msg, " at line $count:\n";
113 my $line = $term->readline('marc-cleanup>');
114 if (length $line < 2)
115 { next unless (defined $commands{$line}) }
116 if (defined $commands{$line}) {
117 my $term = $commands{$line}->($line_in);
120 if ($linecontext[3] eq " [LINE KILLED]\n") {
121 push @record, "$line\n"
123 $record[-1] = "$line\n";
125 $linecontext[3] = "$line\n";
133 Reads from the incoming MARC file; returns lines into the driver
134 loop. Batches records for output, and maintains the linecontext listing.
142 if ($l =~ /<record>/) {
146 } elsif ($l =~ m|</record>|) {
148 write_record($NUMARC) if $reccount;
158 print $FH '<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" xmlns="http://www.loc.gov/MARC21/slim">',"\n";
160 print $FH "</collection>\n";
163 sub update_linecontext {
165 push @linecontext, $line2;
166 shift @linecontext if (@linecontext > 5);
169 #-----------------------------------------------------------------------------------
171 #-----------------------------------------------------------------------------------
174 print "\n Tag:",$reccontext{tag}, " Ind1:'",
175 $reccontext{ind1},"' Ind2:'", $reccontext{ind2}, "'";
180 sub print_linecontext {
181 print $OUT "\n", join(' |','',@linecontext[0..2]);
182 print $OUT '==> |', $linecontext[3];
183 print $OUT ' |', $linecontext[4],"\n";
189 print $OUT "\n$line_in\n";
194 my $last = pop @record;
196 $record[-1] =~ s/\n//;
197 $record[-1] = join('', $record[-1], $last);
199 push @temp, @linecontext[0..1];
200 $temp[3] = $record[-1];
201 $temp[4] = $linecontext[4];
202 @linecontext = @temp;
209 $linecontext[3] = " [LINE KILLED]\n";
215 my $line = <MARC>; $count++;
216 update_linecontext();
217 until ($line =~ m|</record>|) {
219 $line = <MARC>; $count++;
220 update_linecontext();
223 write_record($EXMARC);
227 sub commit_edit { return 1 }
232 Type a replacement for the indicated line, or enter a command.
234 Commands: c Show record context ('C' for brief context)
235 k Kill this line (remove from record)
236 m Merge indicated line with previous line
238 t Commit changes and resume stream edit
239 x Write this record to the exception file instead of output