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 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,
33 C => \&print_linecontext,
44 my @spinner = qw(- / | \\);
47 while (my $line = getline()) {
48 unless ($count % 2000) {
49 print "\rWorking... ", $spinner[$sidx];
50 $sidx = $sidx > $#spinner ? 0 : $sidx++;
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;
75 # stow tag data if we're looking at it
76 if ($line =~ m/<datafield tag="(\d{3})" ind1="(.)" ind2="(.)">/) {
77 $reccontext{tag} = $1;
78 $reccontext{ind1} = $2;
79 $reccontext{ind2} = $3;
83 if ($line =~ /&/ && $line !~ /&\w{1,7};/)
84 { edit("Looks like naked ampersand", $line); next }
86 # subfields can't be non-alphanumeric
87 if ($line =~ /<subfield code="[^[:alnum:]]"/)
88 { edit("Junk in subfield", $line); next }
91 print $NUMARC "</xml>\n";
92 print $EXMARC "</xml>\n";
96 Handles the Term::ReadLine loop
101 my ($msg, $line_in) = @_;
102 print $OUT "\r".$msg, " at line $count:\n";
105 my $line = $term->readline('yaz-cleanup>');
106 if (length $line == 1)
107 { next unless (defined $commands{$line}) }
108 if (defined $commands{$line}) {
109 my $term = $commands{$line}->($line_in);
112 if ($linecontext[3] eq " [LINE KILLED]\n") {
113 push @record, "$line\n"
115 $record[-1] = "$line\n";
117 $linecontext[3] = "$line\n";
125 Reads from the incoming MARC file; returns lines into the driver
126 loop. Batches records for output, and maintains the linecontext listing.
134 if ($l =~ /<record>/) {
138 } elsif ($l =~ m|</record>|) {
139 write_record($NUMARC) if $reccount;
149 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";
151 print $FH "</collection>\n";
154 sub update_linecontext {
156 push @linecontext, $line2;
157 shift @linecontext if (@linecontext > 5);
160 #-----------------------------------------------------------------------------------
162 #-----------------------------------------------------------------------------------
165 print "\n Tag: ",$reccontext{tag}, " Ind1:'",
166 $reccontext{ind1},"' Ind2:'", $reccontext{ind2}, "'";
171 sub print_linecontext {
172 print $OUT "\n", join(' |','',@linecontext[0..2]);
173 print $OUT '==> |', $linecontext[3];
174 print $OUT ' |', $linecontext[4],"\n";
180 print $OUT "\n$line_in\n";
186 $linecontext[3] = " [LINE KILLED]\n";
192 my $line = <MARC>; $count++;
193 update_linecontext();
194 until ($line =~ m|</record>|) {
196 $line = <MARC>; $count++;
197 update_linecontext();
200 write_record($EXMARC);
204 sub commit_edit { return 1 }
209 Type a replacement for the indicated line, or enter a command.
211 Commands: c Show record context ('C' for brief context)
212 k Kill this line (remove from record)
213 m Merge indicated line with previous line [NOT IMPLEMENTED]
215 t Commit changes and resume stream edit
216 x Write this record to the exception file instead of output