-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeEML.pl
executable file
·155 lines (133 loc) · 4.41 KB
/
writeEML.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Std;
use MB2EML::EML;
use MB2EML::Metabase;
use XML::LibXML;
my $databaseName;
my $datasetId;
my $eml;
my $emlDoc;
my $endId;
my $errFilename;
my $idsRef;
my $mb;
my $outFilename;
my $startId;
# print help info and exit
our $opt_h;
# name of output directory
our $opt_d;
# Check EML document validity with XML Schema
our $opt_v;
# run KNB EMLparser
our $opt_p;
# run with/without verbose
our $opt_x;
sub usage {
print "writeel.pl - write EML documents from Metabase\n\n";
print "Usage: \n";
print "mb2eml.pl [options] database datasetID\n\n";
print "Options: \n";
print "-d \toutput directory for generated EML docuemnts . If this option is specified,\n";
print " \tthen a file is created for each datasetId with the name format of \"<datasetName>-<datasetId>.xml\"\n";
print " \tfor example, \"mcr_metabase-10.xml\". If this option is not specified, then all output will be sent to stdout.\n";
print "-p \tcheck the generated EML document with the KNB \"runParser\" program \n";
print "-v \trun in verbose mode.\n";
print "-x \tcheck the validity of the generated EML document against the EML XML Schema.\n\n";
print "The argument \"datasetID can be specified as one of the following:\n\n";
print " \to a single integer, i.e. \"10\".\n";
print " \to a range of integers, i.e. \"10-20\"\n";
print " \to the keyword \"all\", meaning all datasetIds for the specified databaseName.\n\n";
print "For example, the command:\n\n";
print " \t./writeEML.pl -v mcr_metabase 10-20\n\n";
print "will create EML documents from the \"mcr_metabase\" database schema for all document IDs between 10 and 20, with verbose mode on.\n";
}
# Get command line flags
getopts('hpvxd:');
$opt_h = 0, if (not defined $opt_h);
$opt_p = 0, if (not defined $opt_p);
$opt_v = 0, if (not defined $opt_v);
$opt_x = 0, if (not defined $opt_x);
if ($opt_h) {
usage();
exit;
}
# If no command line arguments or too few are passed in, then print usage info and exit
if ($#ARGV == -1 || $#ARGV < 1) {
usage();
exit;
}
if (defined $opt_d) {
if (not -d $opt_d) {
die "Error: output directory \"$opt_d\" does not exist.\n";
}
}
$databaseName = $ARGV[0];
# datasetId can be specified as an integer, i.e. '10' or a range of integers, i.e. '10-20', or 'all'
$datasetId = $ARGV[1];
$mb = MB2EML::Metabase->new({ databaseName => $databaseName });
$idsRef = $mb->searchDatasetIds();
# Free object and db connection.
undef $mb;
my @ids;
# If $datasetId is specified as a range then use all existing datasets in that range.
if ($datasetId =~ /(\d+)-(\d+)/) {
$startId = $1;
$endId = $2;
for my $id (@$idsRef) {
if ($id >= $startId && $id <= $endId) {
push (@ids, $id);
}
}
# If $datasetId is specified as a list, use all ids in the list.
} elsif ($datasetId =~ /(\d+),(\d+)/) {
@ids = split(/,/, $datasetId);
# If keyword "all" is specified for $aatasetId, use all ids.
} elsif ($datasetId eq "all") {
@ids = @$idsRef;
# If single $datasetId is used, then use it!
} elsif ($datasetId =~ /(\d+)/) {
push (@ids, $datasetId);
} else {
warn "Invalid datasetId: $datasetId\n";
usage();
exit;
}
for my $id (@ids) {
if (defined $opt_d) {
$errFilename = $opt_d . "/" . $databaseName . "-" . $id . ".err";
open STDERR, ">", $errFilename;
}
# Create a new EML object that will be used to assemble the Metabase data into EML
eval {
$eml = MB2EML::EML->new( { databaseName => $databaseName, datasetId => $id} );
};
if ($@) {
print STDERR $id . ": Error initializing EML document: $@\n";
}
# Write out the EML object as XML
# $opt_v = 1 causes schema validation against the eml.xsd to be performed
# $opt_x = 1 causes the KNB EML parser to be run
eval {
$emlDoc = $eml->writeXML(validate => $opt_p, runEMLParser => $opt_x, verbose => $opt_v);
};
if ($@) {
print STDERR $id . ": Error writing EML document: $@\n";
}
if (defined $opt_d) {
$outFilename = $opt_d . "/" . $databaseName . "-" . $id . ".xml";
if ($opt_v) {
print "Writing document $outFilename...\n";
}
open FILE, ">", $outFilename or die $!;
print FILE $emlDoc;
close FILE;
close STDERR;
} else {
print $emlDoc;
}
undef $emlDoc;
undef $eml;
}