changing verbage for bib loading table
[migration-tools.git] / destiny / destiny_xml_to_tsv.pl
1 #!/usr/bin/perl -w
2
3 # Copyright 2009-2016, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 # Take a file that starts off like this:
20 #
21 # <?xml version="1.0" encoding="UTF-8" ?>
22 # <DestinyCustomReport>
23 #  <Row>
24 #
25 # Munge it like so:
26 #
27 # cat file | xml2 | cut -f4- -d/  > file.munged
28 #
29 # And feed the results to this script to create a .tsv version of the data.
30 #
31 # destiny_xml_to_tsv.pl file.munged > file.munged.tsv
32 #
33
34
35 my @records;
36 my $serial = 0;
37 my $line = 0;
38 my $field = '';
39 my %unique_fields;
40
41
42 # Load each record
43 while (<>) {
44     s/\r\n/\n/g;
45 # print STDERR "Loaded this line: " . $_;
46
47         # Is this the start of a new record?
48         if ( /^$/ ) {
49                 $line = 0;
50                 $serial++;
51                 print STDERR "Processing record $serial.\n";
52                 next;
53         }
54
55         # If this isn't the start of the new record, it's a new line in the present record.
56         $line++;
57
58         # Looks like we've got some actual data!  Let's store it.
59         # FIXME: For large batches of data, we may run out of memory and should store this on disk.
60         if ( /^(.*?)=(.*)$/ ) {
61
62                 $field = $1;
63                 $unique_fields{$field} = 1;
64                 $records[$serial]{$field} = $2;
65
66                 print STDERR "Data extracted: \$records[$serial]{'$field'} = '$2'\n";
67
68                 next;
69         }       
70
71         # This is the continuation of the previous line.
72         else {
73                 chomp($_);
74                 $records[$serial]{$field} .= ' ' . $_;
75                 print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
76         }
77
78 }
79
80 print STDERR "Loaded " . scalar(@records) . " records.\n";
81
82
83 # Print a header line
84 print "SERIAL\t";
85 @sorted_fields = sort keys %unique_fields;
86 foreach $i (@sorted_fields) {
87         print "$i\t";
88 }
89 print "\n";
90
91
92 # Print the results
93 for (my $u = 0; $u < @records; $u++) {
94         print "$u\t";   
95         foreach $f (@sorted_fields) {
96                 if (defined $records[$u]{$f}) {
97                         print $records[$u]{$f};
98                 }
99         print "\t";
100         }
101         print "\n";
102 }
103
104 print STDERR "Wrote " . scalar(@records) . " records.\n";
105 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS