1 package Equinox::Migration::SubfieldMapper;
8 Equinox::Migration::SubfieldMapper - Generate named-field to MARC tag map from file
16 our $VERSION = '1.004';
21 Using a file as input, E::M::SM generates a mapping of MARC subfields
22 to arbitrary field names, and provides several access mechanisms to
25 use Equinox::Migration::SubfieldMapper;
33 Takes one optional argument, C<file>. If this is speficied, the tag
34 list will be populated as per that file on instantiation.
36 Returns a E::M::SM object.
41 my ($class, %args) = @_;
43 my $self = bless { conf => { mods => undef },
48 die "Argument 'mods' is wrong type\n"
49 unless (ref $args{mods} eq "ARRAY");
50 for my $mod ( @{$args{mods}} )
51 { $self->{conf}{mods}{$mod} = 1 }
56 $self->{conf}{file} = $args{file};
59 die "Can't open file: $!\n";
68 Ask it whether your mapping has various things, and it'll let you know.
70 $sm->has('fieldname') # is this fieldname mapped?
71 $sm->has(901) # are there any mappings for this tag?
72 $sm->has(650,'c') # is this tag/subfield combo mapped?
73 $sm->has('name', 245, 'a') # is this name mapped to 245$a?
75 Returns 1 if true, 0 if false.
77 FIXME: use named params instead of positional
82 my ($self, @chunks) = @_;
83 return undef unless (defined $chunks[0]);
85 if ($chunks[0] =~ /^\d/) {
86 if (defined $chunks[1]) {
87 return 1 if ( defined $self->{tags}{$chunks[0]}{$chunks[1]} );
90 return 1 if ( defined $self->{tags}{$chunks[0]} );
94 if (defined $chunks[2]) {
95 return 1 if ( $self->{fields}{$chunks[0]}{tag} eq $chunks[1] and
96 $self->{fields}{$chunks[0]}{sub} eq $chunks[2] );
98 } elsif (defined $chunks[1]) {
99 return 1 if ( $self->{fields}{$chunks[0]}{tag} eq $chunks[1] );
102 return 1 if ( defined $self->{fields}{$chunks[0]} );
110 Returns an arrayref containing the tags defined in the map.
112 my $tags = $sfm->tags;
113 for my tag ( @{$tags} ) {
114 my $subs = $sfm->subfields($tag);
122 return [ keys %{$self->{tags}} ];
127 Given a tag, return an arrayref of the subfields mapped with that tag.
129 my $tags = $sfm->tags;
130 for my tag ( @{$tags} ) {
131 my $subs = $sfm->subfields($tag);
135 Returns C<undef> if C<tag> is not mapped.
140 my ($self, $tag) = @_;
141 return undef unless $self->has($tag);
142 return [ keys %{$self->{tags}{$tag}} ];
148 Given a tag and subfield code,
150 my $fname = $sm->field(945, 'p')
152 return the name mapped to them. Returns C<undef> if no mapping exists.
157 my ($self, $tag, $sub) = @_;
158 return undef unless (defined $tag and defined $sub);
159 return undef unless $self->has($tag, $sub);
160 return $self->{tags}{$tag}{$sub};
165 With no argument, returns a hashref containing all modifiers for the entire map:
169 tag => [ list_of subs ],
175 Given a fieldname, returns a hashref of the modifiers set on that mapping.
177 $self->mods('fieldname')
179 Returns undef is nothing is defined.
184 my ($self, $field) = @_;
185 return $self->{allmods} unless defined $field;
186 return undef unless $self->has($field);
187 return undef unless (%{ $self->{fields}{$field}{mods} });
188 return $self->{fields}{$field}{mods};
193 Returns the content filters set on a mapping
195 $self->filters('fieldname')
197 If there are no filters, C<undef> will be returned. Else a listref
203 my ($self, $field) = @_;
204 return undef unless $self->has($field);
205 return undef unless ($self->{fields}{$field}{filt});
206 return $self->{fields}{$field}{filt};
209 =head1 MAP CONSTRUCTION METHODS
211 These methods are not generally accessed from user code.
215 Generate initial mapping from file.
220 my ($self, $file) = @_;
222 open TAGFILE, '<', $self->{conf}{file};
228 my @tokens = split /\s+/;
230 my $map = { mods => [], filt => [] };
231 $map->{field} = shift @tokens;
232 $map->{tag} = shift @tokens;
233 while (my $tok = shift @tokens) {
234 last if ($tok =~ m/^#/);
235 if ($tok =~ m/^[a-z]:'/ and $tok !~ /'$/) {
236 $tok .= ' ' . shift @tokens
237 until ($tokens[0] =~ m/'$/);
238 $tok .= ' ' . shift @tokens;
243 { push @{$map->{mods}}, $tok }
244 elsif ($tok =~ m/^f:/)
245 { push @{$map->{filt}}, $tok }
246 elsif ($tok =~ m/^[a-z0-9]$/)
247 { $map->{sub} = $tok }
249 { die "Unknown chunk '$tok' at line $.\n" }
257 Add new item to mapping. Not usually called directly from user code.
261 Where C<$map> is a hashref that, at a minimum, looks like
263 { field => "value", tag => NNN, sub => X }
265 and may also have the key/value pairs
273 my ($self, $map) = @_;
275 # trim the mods and filters
277 my $filt = []; my %filt = ();
278 for my $m (@{$map->{mods}}) {
279 die "Modifier collision '$m' at line $." if $mods->{$m};
282 push @{$self->{allmods}{$m}{ $map->{tag} }}, $map->{sub};
284 for my $f (@{$map->{filt}}) {
285 die "Filter collision '$f' at line $." if $filt{$f};
287 push @{$filt}, $f; $filt{$f} = 1;
289 $map->{mods} = $mods;
290 $map->{filt} = $filt;
292 # check bits for validity
293 $self->validate($map);
295 # add data to the fields hash
296 $self->{fields}{ $map->{field} } = { tag => $map->{tag},
298 mods => $map->{mods},
301 # and to the tags hash
302 $self->{tags}{ $map->{tag} }{ $map->{sub} } = $map->{field};
307 Passed a reference to the hash given to C<add>, validate scans its
308 contents for common errors and dies if there is an issue.
310 * field, tag, and sub are required
311 * fieldnames must start with a letter
312 * fieldnames must be unique
313 * tag must be between 0 and 999
314 * subfield code must be a single alphanumeric character
315 * tag+subfield can only be mapped once
316 * if a list of allowable mod values was given in the call to
317 C<new>, any modifiers must be on that list
322 my ($self, $map) = @_;
324 $.= 1 unless defined $.;
326 die "Required field missing (line $.)\n"
327 unless (defined $map->{field} and defined $map->{tag} and defined $map->{sub});
329 die "Fieldnames must start with letter (line $.)\n"
330 unless ($map->{field} =~ /^[a-zA-z]/);
332 die "Invalid tag (line $.)\n"
333 if ($map->{tag} =~ /[^\d\-]/ or $map->{tag} < 0 or $map->{tag} > 999);
335 die "Invalid subfield code (line $.)\n"
336 if (length $map->{sub} != 1 or $map->{sub} =~ /[^a-zA-Z0-9]/);
338 # test mod names if we have a set to check against
339 if (defined $self->{conf}{mods}) {
340 for my $mod ( keys %{$map->{mods}} ) {
341 die "Modifier '$mod' not allowed\n"
342 unless $self->{conf}{mods}{$mod};
346 die "Fieldnames must be unique (line $.)\n"
347 if (defined $self->{fields}{$map->{field}});
349 die "Subfields cannot be mapped twice (line $.)\n"
350 if (defined $self->{tags}{$map->{tag}}{$map->{sub}});
357 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
361 Please report any bugs or feature requests to the above email address.
365 You can find documentation for this module with the perldoc command.
367 perldoc Equinox::Migration::SubfieldMapper
370 =head1 COPYRIGHT & LICENSE
372 Copyright 2009 Equinox, all rights reserved.
374 This program is free software; you can redistribute it and/or modify it
375 under the same terms as Perl itself.
380 1; # End of Equinox::Migration::SimpleTagList