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="(.{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 # tags must be numeric
90 if ($line =~ /<datafield tag="(.+?)"/) {
93 edit("Non-numerics in tag", $line);
98 # subfields can't be non-alphanumeric
99 if ($line =~ /<subfield code="(.+?)"/) {
101 if ($match =~ /\P{IsAlnum}/) {
102 edit("Junk in subfield code", $line);
108 print $NUMARC "</xml>\n";
109 print $EXMARC "</xml>\n";
113 Handles the Term::ReadLine loop
118 my ($msg, $line_in) = @_;
119 print $OUT "\r".$msg, " at line $count:\n";
122 my $line = $term->readline('marc-cleanup>');
123 if (length $line < 2)
124 { next unless (defined $commands{$line}) }
125 if (defined $commands{$line}) {
126 my $term = $commands{$line}->($line_in);
129 if ($linecontext[3] eq " [LINE KILLED]\n") {
130 push @record, "$line\n"
132 $record[-1] = "$line\n";
134 $linecontext[3] = "$line\n";
142 Reads from the incoming MARC file; returns lines into the driver
143 loop. Batches records for output, and maintains the linecontext listing.
151 if ($l =~ /<record>/) {
155 } elsif ($l =~ m|</record>|) {
157 write_record($NUMARC) if $reccount;
167 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";
169 print $FH "</collection>\n";
172 sub update_linecontext {
174 push @linecontext, $line2;
175 shift @linecontext if (@linecontext > 5);
178 #-----------------------------------------------------------------------------------
180 #-----------------------------------------------------------------------------------
183 print "\n Tag:",$reccontext{tag}, " Ind1:'",
184 $reccontext{ind1},"' Ind2:'", $reccontext{ind2}, "'";
189 sub print_linecontext {
190 print $OUT "\n", join(' |','',@linecontext[0..2]);
191 print $OUT '==> |', $linecontext[3];
192 print $OUT ' |', $linecontext[4],"\n";
198 print $OUT "\n$line_in\n";
203 my $last = pop @record;
205 $record[-1] =~ s/\n//;
206 $record[-1] = join('', $record[-1], $last);
208 push @temp, @linecontext[0..1];
209 $temp[3] = $record[-1];
210 $temp[4] = $linecontext[4];
211 @linecontext = @temp;
218 $linecontext[3] = " [LINE KILLED]\n";
224 my $line = <MARC>; $count++;
225 update_linecontext();
226 until ($line =~ m|</record>|) {
228 $line = <MARC>; $count++;
229 update_linecontext();
232 write_record($EXMARC);
236 sub commit_edit { return 1 }
241 Type a replacement for the indicated line, or enter a command.
243 Commands: c Show record context ('C' for brief context)
244 k Kill this line (remove from record)
245 m Merge indicated line with previous line
247 t Commit changes and resume stream edit
248 x Write this record to the exception file instead of output