1 package Equinox::Migration::MapDrivenMARCXMLProc;
7 use Equinox::Migration::SubfieldMapper 1.003;
12 Equinox::Migration::MapDrivenMARCXMLProc
20 our $VERSION = '1.000';
27 use Equinox::Migration::MapDrivenMARCXMLProc;
35 Takes two required arguments: C<mapfile> (which will be passed along
36 to L<Equinox::Migration::SubfieldMapper> as the basis for its map),
37 and C<marcfile> (the MARC data to be processed).
39 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( mapfile => FILE,
45 my ($class, %args) = @_;
47 my $self = bless { mods => { multi => {},
50 data => { recs => undef, # X::T record objects
51 rptr => 0, # next record pointer
52 crec => undef, # parsed record storage
53 tmap => undef, # tag_id-to-tag array map
57 # initialize map and taglist
58 die "Argument 'mapfile' must be specified\n" unless (defined $args{mapfile});
59 my @mods = keys %{$self->{mods}};
60 $self->{map} = Equinox::Migration::SubfieldMapper->new( file => $args{mapfile},
62 $self->{data}{tags} = $self->{map}->tags;
65 die "Argument 'marcfile' must be specified\n" unless (defined $args{marcfile});
66 if (-r $args{marcfile}) {
67 $self->{twig} = XML::Twig->new;
68 $self->{twig}->parsefile($args{marcfile});
69 my @records = $self->{twig}->root->children;
70 $self->{data}{recs} = \@records;
72 die "Can't open marc file: $!\n";
81 Extracts data from the next record, per the mapping file. Returns a
82 normalized datastructure (see L</format_record> for details) on
83 success; returns 0 otherwise.
85 while (my $rec = $m->parse_record) {
86 # handle extracted record data
94 # get the next record and wipe current parsed record
95 return 0 unless defined $self->{data}{recs}[ $self->{data}{rptr} ];
96 my $record = $self->{data}{recs}[ $self->{data}{rptr} ];
97 $self->{data}{crec} = { egid => undef, tags => undef };
98 $self->{data}{tmap} = {};
100 my @fields = $record->children;
102 { $self->process_field($f) }
104 # cleanup memory and increment pointer
106 $self->{data}{rptr}++;
108 # check for required fields
109 $self->check_required;
111 return $self->{data}{crec};
115 my ($self, $field) = @_;
116 my $map = $self->{map};
117 my $tag = $field->{'att'}->{'tag'};
118 my $crec = $self->{data}{crec};
119 my $tmap = $self->{data}{tmap};
122 unless (defined $tag) {
129 my $sub = $field->first_child('subfield');
130 $crec->{egid} = $sub->text;
133 if ($map->has($tag)) {
134 push @{$crec->{tags}}, { tag => $tag, uni => undef, multi => undef };
135 push @{$tmap->{$tag}}, (@{$crec->{tags}} - 1);
136 my @subs = $field->children('subfield');
138 { $self->process_subs($tag, $sub) }
139 # check map to ensure all declared subs have a value
140 my $mods = $map->mods($field);
141 for my $mappedsub ( @{ $map->subfields($tag) } ) {
142 next if $mods->{multi};
143 $crec->{tags}[-1]{uni}{$mappedsub} = ''
144 unless defined $crec->{tags}[-1]{uni}{$mappedsub};
150 my ($self, $tag, $sub) = @_;
151 my $map = $self->{map};
152 my $code = $sub->{'att'}->{'code'};
154 # handle unmapped tag/subs
155 return unless ($map->has($tag, $code));
157 # fetch our datafield struct and fieldname
158 my $dataf = $self->{data}{crec}{tags}[-1];
159 my $field = $map->field($tag, $code);
162 if (my $mods = $map->mods($field)) {
163 if ($mods->{multi}) {
164 my $name = $tag . $code;
165 push @{$dataf->{multi}{$name}}, $sub->text;
170 die "Multiple occurances of a non-multi field: $tag$code at rec ",
171 ($self->{data}{rptr} + 1),"\n" if (defined $dataf->{uni}{$code});
172 $dataf->{uni}{$code} = $sub->text;
178 my $mods = $self->{map}->mods;
179 my $crec = $self->{data}{crec};
181 for my $tag_id (keys %{$mods->{required}}) {
182 for my $code (@{$mods->{required}{$tag_id}}) {
185 for my $tag (@{$crec->{tags}}) {
186 $found = 1 if ($tag->{multi}{($tag_id . $code)});
187 $found = 1 if ($tag->{uni}{$code});
190 die "Required mapping $tag_id$code not found in rec ",$self->{data}{rptr},"\n"
199 MapDrivenMARCXMLProc implements the following modifiers, and passes
200 them to L<Equinox::Migration::SubfieldMapper>, meaning that specifying
201 any other modifiers in a MDMP map file will cause a fatal error when
206 If a mapping is declared to be C<multi>, then MDMP expects to see more
207 than one instance of that subfield per datafield, and the data is
208 handled accordingly (see L</PARSED RECORDS> below).
210 Occurring zero or one time is legal for a C<multi> mapping.
212 A mapping which is not flagged as C<multi>, but which occurs more than
213 once per datafield will cause a fatal error.
217 By default, if a mapping does not occur in a datafield, processing
218 continues normally. if a mapping has the C<required> modifier,
219 however, it must appear, or a fatal error will occur.
221 =head1 PARSED RECORDS
225 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new(ARGUMENTS);
226 $rec = $m->parse_record;
228 Then C<$rec> will look like:
231 egid => evergreen_record_id,
235 multi => { (tag_id . sub_code) => [ val1, val2, ... ] },
236 uni => { code => value, code2 => value2, ... },
242 That is, there is an C<egid> key which points to the Evergreen ID of
243 that record, and a C<tags> key which points to an arrayref.
247 A reference to a list of anonymous hashes, one for each instance of
248 each tag which occurs in the map.
250 Each tag hash holds its own id (e.g. C<998>), and two references to
251 two more hashrefs, C<multi> and C<uni>.
253 The C<multi> hash holds the extracted data for tag/sub mappings which
254 have the C<multiple> modifier on them. The keys in C<multi> are
255 composed of the tag id and subfield code, catenated
256 (e.g. C<901c>). The values are arrayrefs containing the content of all
257 instances of that subfield in that instance of that tag. If no tags
258 are defined as C<multi>, it will be C<undef>.
260 The C<uni> hash holds data for tag/sub mappings which occur only once
261 per instance of a tag (but may occur multiple times in a record due to
262 there being multiple instances of that tag in a record). Keys are
263 subfield codes and values are subfield content.
265 All C<uni> subfields occuring in the map are guaranteed to be
266 defined. Sufields which are mapped but do not occur in a particular
267 datafield will be given a value of '' (the null string) in the current
268 record struct. Oppose subfields which are not mapped, which will be
274 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
278 Please report any bugs or feature requests to the above email address.
282 You can find documentation for this module with the perldoc command.
284 perldoc Equinox::Migration::MapDrivenMARCXMLProc
287 =head1 COPYRIGHT & LICENSE
289 Copyright 2009 Equinox, all rights reserved.
291 This program is free software; you can redistribute it and/or modify it
292 under the same terms as Perl itself.
297 1; # End of Equinox::Migration::MapDrivenMARCXMLProc