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 => {},
51 data => { recs => undef, # X::T record objects
52 rptr => 0, # next record pointer
53 crec => undef, # parsed record storage
54 tmap => undef, # tag_id-to-tag array map
58 # initialize map and taglist
59 die "Argument 'mapfile' must be specified\n" unless (defined $args{mapfile});
60 my @mods = keys %{$self->{mods}};
61 $self->{map} = Equinox::Migration::SubfieldMapper->new( file => $args{mapfile},
63 $self->{data}{tags} = $self->{map}->tags;
66 die "Argument 'marcfile' must be specified\n" unless (defined $args{marcfile});
67 if (-r $args{marcfile}) {
68 $self->{twig} = XML::Twig->new;
69 $self->{twig}->parsefile($args{marcfile});
70 my @records = $self->{twig}->root->children;
71 $self->{data}{recs} = \@records;
73 die "Can't open marc file: $!\n";
82 Extracts data from the next record, per the mapping file. Returns a
83 normalized datastructure (see L</format_record> for details) on
84 success; returns 0 otherwise.
86 while (my $rec = $m->parse_record) {
87 # handle extracted record data
95 # get the next record and wipe current parsed record
96 return 0 unless defined $self->{data}{recs}[ $self->{data}{rptr} ];
97 my $record = $self->{data}{recs}[ $self->{data}{rptr} ];
98 $self->{data}{crec} = { egid => undef, tags => undef };
99 $self->{data}{tmap} = {};
101 my @fields = $record->children;
103 { $self->process_field($f) }
105 # cleanup memory and increment pointer
107 $self->{data}{rptr}++;
109 # check for required fields
110 $self->check_required;
112 return $self->{data}{crec};
116 my ($self, $field) = @_;
117 my $map = $self->{map};
118 my $tag = $field->{'att'}->{'tag'};
119 my $crec = $self->{data}{crec};
120 my $tmap = $self->{data}{tmap};
123 unless (defined $tag) {
130 my $sub = $field->first_child('subfield');
131 $crec->{egid} = $sub->text;
134 if ($map->has($tag)) {
135 push @{$crec->{tags}}, { tag => $tag, uni => undef, multi => undef };
136 push @{$tmap->{$tag}}, (@{$crec->{tags}} - 1);
137 my @subs = $field->children('subfield');
139 { $self->process_subs($tag, $sub) }
140 # check map to ensure all declared subs have a value
141 my $mods = $map->mods($field);
142 for my $mappedsub ( @{ $map->subfields($tag) } ) {
143 next if $mods->{multi};
144 $crec->{tags}[-1]{uni}{$mappedsub} = ''
145 unless defined $crec->{tags}[-1]{uni}{$mappedsub};
151 my ($self, $tag, $sub) = @_;
152 my $map = $self->{map};
153 my $code = $sub->{'att'}->{'code'};
155 # handle unmapped tag/subs
156 return unless ($map->has($tag, $code));
158 # fetch our datafield struct and fieldname
159 my $dataf = $self->{data}{crec}{tags}[-1];
160 my $field = $map->field($tag, $code);
163 for my $filter ( @{$map->filters($field)} ) {
164 return if ($sub->text =~ /$filter/i);
166 # handle multi modifier
167 if (my $mods = $map->mods($field)) {
168 if ($mods->{multi}) {
169 my $name = $tag . $code;
170 push @{$dataf->{multi}{$name}}, $sub->text;
175 # if this were a multi field, it would be handled already. make sure its a singleton
176 die "Multiple occurances of a non-multi field: $tag$code at rec ",
177 ($self->{data}{rptr} + 1),"\n" if (defined $dataf->{uni}{$code});
179 # everything seems okay
180 $dataf->{uni}{$code} = $sub->text;
186 my $mods = $self->{map}->mods;
187 my $crec = $self->{data}{crec};
189 for my $tag_id (keys %{$mods->{required}}) {
190 for my $code (@{$mods->{required}{$tag_id}}) {
193 for my $tag (@{$crec->{tags}}) {
194 $found = 1 if ($tag->{multi}{($tag_id . $code)});
195 $found = 1 if ($tag->{uni}{$code});
198 die "Required mapping $tag_id$code not found in rec ",$self->{data}{rptr},"\n"
207 MapDrivenMARCXMLProc implements the following modifiers, and passes
208 them to L<Equinox::Migration::SubfieldMapper>, meaning that specifying
209 any other modifiers in a MDMP map file will cause a fatal error when
214 If a mapping is declared to be C<multi>, then MDMP expects to see more
215 than one instance of that subfield per datafield, and the data is
216 handled accordingly (see L</PARSED RECORDS> below).
218 Occurring zero or one time is legal for a C<multi> mapping.
220 A mapping which is not flagged as C<multi>, but which occurs more than
221 once per datafield will cause a fatal error.
225 By default, if a mapping does not occur in a datafield, processing
226 continues normally. if a mapping has the C<required> modifier,
227 however, it must appear, or a fatal error will occur.
229 =head1 PARSED RECORDS
233 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new(ARGUMENTS);
234 $rec = $m->parse_record;
236 Then C<$rec> will look like:
239 egid => evergreen_record_id,
243 multi => { (tag_id . sub_code) => [ val1, val2, ... ] },
244 uni => { code => value, code2 => value2, ... },
250 That is, there is an C<egid> key which points to the Evergreen ID of
251 that record, and a C<tags> key which points to an arrayref.
255 A reference to a list of anonymous hashes, one for each instance of
256 each tag which occurs in the map.
258 Each tag hash holds its own id (e.g. C<998>), and two references to
259 two more hashrefs, C<multi> and C<uni>.
261 The C<multi> hash holds the extracted data for tag/sub mappings which
262 have the C<multiple> modifier on them. The keys in C<multi> are
263 composed of the tag id and subfield code, catenated
264 (e.g. C<901c>). The values are arrayrefs containing the content of all
265 instances of that subfield in that instance of that tag. If no tags
266 are defined as C<multi>, it will be C<undef>.
268 The C<uni> hash holds data for tag/sub mappings which occur only once
269 per instance of a tag (but may occur multiple times in a record due to
270 there being multiple instances of that tag in a record). Keys are
271 subfield codes and values are subfield content.
273 All C<uni> subfields occuring in the map are guaranteed to be
274 defined. Sufields which are mapped but do not occur in a particular
275 datafield will be given a value of '' (the null string) in the current
276 record struct. Oppose subfields which are not mapped, which will be
282 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
286 Please report any bugs or feature requests to the above email address.
290 You can find documentation for this module with the perldoc command.
292 perldoc Equinox::Migration::MapDrivenMARCXMLProc
295 =head1 COPYRIGHT & LICENSE
297 Copyright 2009 Equinox, all rights reserved.
299 This program is free software; you can redistribute it and/or modify it
300 under the same terms as Perl itself.
305 1; # End of Equinox::Migration::MapDrivenMARCXMLProc