11 my $term = new Term::ReadLine 'yaz-cleanup';
12 my $OUT = $term->OUT || \*STDOUT;
13 print $OUT "Interactive MARC Stream Editor starting up\n";
19 my @record = (); # current record storage
20 my @context= (); # last 5 lines of file
22 open MARC, '<', 'incoming.marc.xml';
23 open my $NUMARC, '>', 'incoming.clean.marc.xml';
24 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
25 open my $EXMARC, '>', 'incoming.exceptions.marc.xml';
26 print $EXMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
27 open MARC2, '<', 'incoming.marc.xml';
30 # this is the dispatch table which drives command selection in
32 my %commands = ( c => \&print_context,
43 my @spinner = qw(- / | \\);
46 while (my $line = getline()) {
47 unless ($count % 2000) {
48 print "\rWorking... ", $spinner[$sidx];
50 $sidx = 0 if ($sidx > $#spinner);
54 # catch empty datafield elements
55 if ($line =~ m|</datafield>|) {
56 if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
57 pop @record; pop @record;
58 print $OUT "\rEmpty datafield scrubbed at line $count\n";
63 # clean misplaced dollarsigns
64 if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
65 $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
66 print $OUT "\rDollar sign in subfield code corrected at line $count\n";
69 # clean up tags with spaces in them
70 $line =~ s/tag=" /tag="00/g;
71 $line =~ s/tag=" /tag="0/g;
72 $line =~ s/tag="-/tag="0/g;
73 $line =~ s/tag="(\d\d) /tag="0$1/g;
76 if ($line =~ /&/ && $line !~ /&\w{1,7};/)
77 { edit("Looks like naked ampersand", $line); next }
79 # subfields can't be non-alphanumeric
80 if ($line =~ /<subfield code="[^[:alnum:]]"/)
81 { edit("Junk in subfield", $line); next }
84 print $NUMARC "</xml>\n";
85 print $EXMARC "</xml>\n";
89 Handles the Term::ReadLine loop
94 my ($msg, $line_in) = @_;
95 print $OUT "\r".$msg, " at line $count:\n";
98 my $line = $term->readline('yaz-cleanup>');
99 if (defined $commands{$line}) {
100 my $term = $commands{$line}->($line_in);
103 if ($context[3] eq " [LINE KILLED\n]") {
104 push @record, "$line\n"
106 $record[-1] = "$line\n";
108 $context[3] = "$line\n";
116 Reads from the incoming MARC file; returns lines into the driver
117 loop. Batches records for output, and maintains the context listing.
125 if ($l =~ /<record>/) {
128 } elsif ($l =~ m|</record>|) {
129 write_record($NUMARC) if $reccount;
139 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";
141 print $FH "</collection>\n";
146 push @context, $line2;
147 shift @context if (@context > 5);
150 #-----------------------------------------------------------------------------------
152 #-----------------------------------------------------------------------------------
155 print $OUT "\n", join(' |','',@context[0..2]);
156 print $OUT '==> |', $context[3];
157 print $OUT ' |', $context[4],"\n";
163 print $OUT "\n$line_in\n";
167 sub commit_edit { return 1 }
171 $context[3] = " [LINE KILLED]\n";
178 until ($line =~ m|</record>|) {
184 write_record($EXMARC);
190 Type a replacement for the indicated line, or enter a command.
192 Commands: c Show line context
193 k Kill this line (remove from record)
195 t Commit changes and resume stream edit
196 x Write this record to the exception file instead of output