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 open MARC2, '<', 'incoming.marc.xml';
29 # this is the dispatch table which drives command selection in
31 my %commands = ( c => \&print_context,
42 my @spinner = qw(- / | \\);
45 while (my $line = getline()) {
46 unless ($count % 2000) {
47 print "\rWorking... ", $spinner[$sidx];
49 $sidx = 0 if ($sidx > $#spinner);
53 # catch empty datafield elements
54 if ($line =~ m|</datafield>|) {
55 if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
56 pop @record; pop @record;
57 print $OUT "\rEmpty datafield scrubbed at line $count\n";
62 # clean misplaced dollarsigns
63 if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
64 $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
65 print $OUT "\rDollar sign in subfield code corrected at line $count\n";
68 # clean up tags with spaces in them
69 $line =~ s/tag=" /tag="00/g;
70 $line =~ s/tag=" /tag="0/g;
71 $line =~ s/tag="-/tag="0/g;
72 $line =~ s/tag="(\d\d) /tag="0$1/g;
75 if ($line =~ /&/ && $line !~ /&\w{1,7};/)
76 { edit("Looks like naked ampersand", $line); next }
78 # subfields can't be non-alphanumeric
79 if ($line =~ /<subfield code="[^[:alnum:]]"/)
80 { edit("Junk in subfield", $line); next }
83 print $NUMARC "</xml>\n";
87 Handles the Term::ReadLine loop
92 my ($msg, $line_in) = @_;
93 print $OUT "\r".$msg, " at line $count:\n";
96 my $line = $term->readline('yaz-cleanup>');
97 if (defined $commands{$line}) {
98 my $term = $commands{$line}->($line_in);
101 push @record, $record[-1] if ($context[3] eq " [LINE KILLED\n]");
102 $record[-2] = "$line\n";
103 $context[3] = "$line\n";
111 Reads from the incoming MARC file; returns lines into the driver
112 loop. Batches records for output, and maintains the context listing.
120 if ($l =~ /<record>/) {
123 } elsif ($l =~ m|</record>|) {
124 write_record($NUMARC) if $reccount;
134 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";
136 print $FH "</collection>\n";
141 push @context, $line2;
142 shift @context if (@context > 5);
145 #-----------------------------------------------------------------------------------
147 #-----------------------------------------------------------------------------------
150 print $OUT "\n", join(' |','',@context[0..2]);
151 print $OUT '==> |', $context[3];
152 print $OUT ' |', $context[4],"\n";
158 print $OUT "\n$line_in\n";
162 sub commit_edit { return 1 }
165 my $tmp = pop @record;
168 $context[3] = " [LINE KILLED]\n";
175 until ($line =~ m|</record>|) {
181 write_record($EXMARC);
187 Type a replacement for the indicated line, or enter a command.
189 Commands: c Show line context
190 k Kill this line (remove from record)
192 t Commit changes and resume stream edit
193 x Write this record to the exception file instead of output