1 package Equinox::Migration::SubfieldMapper;
8 Equinox::Migration::SubfieldMapper - Generate named-field to MARC tag map from file
16 our $VERSION = '1.000';
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;
27 my $stl = Equinox::Migration::SubfieldMapper->new( file => ".txt" );
28 my $tags = $stl->as_hashref;
32 my $stl = Equinox::Migration::SubfieldMapper->new( file => ".txt" );
33 if ( $stl->has($foo) ) {
34 # if $foo is an element of $stl's parsed list
44 Takes one optional argument, C<file>. If this is speficied, the tag
45 list will be populated as per that file on instantiation.
47 Returns a E::M::STL object.
52 my ($class, %args) = @_;
54 my $self = bless { conf => { mods => undef },
60 $self->{conf}{file} = $args{file};
63 die "Can't open file: $!\n";
68 die "Argument 'mods' is wrong type\n"
69 unless (ref $args{mods} eq "ARRAY");
70 for my $mod ( @{$args{mods}} )
71 { $self->{conf}{mods}{$mod} = 1 }
79 Ask it whether your mapping has various things, and it'll let you know.
81 $sm->has('fieldname') # is this fieldname mapped?
82 $sm->has(901) # are there any mappings for this tag?
83 $sm->has(650,'c') # is this tag/subfield combo mapped?
84 $sm->has('name', 245, 'a') # is this name mapped to 245$a?
86 Returns 1 if true, 0 if false.
88 FIXME: use named params instead of positional
93 my ($self, @chunks) = @_;
94 return undef unless (defined $chunks[0]);
96 if ($chunks[0] =~ /^\d/) {
97 if (defined $chunks[1]) {
98 return 1 if ( defined $self->{tags}{$chunks[0]}{$chunks[1]} );
101 return 1 if ( defined $self->{tags}{$chunks[0]} );
105 if (defined $chunks[2]) {
106 return 1 if ( $self->{fields}{$chunks[0]}{tag} eq $chunks[1] and
107 $self->{fields}{$chunks[0]}{sub} eq $chunks[2] );
109 } elsif (defined $chunks[1]) {
110 return 1 if ( $self->{fields}{$chunks[0]}{tag} eq $chunks[1] );
113 return 1 if ( defined $self->{fields}{$chunks[0]} );
121 Given a tag and subfield code,
123 my $fname = $sm->field(945, 'p')
125 return the name mapped to them. Returns C<undef> if no mapping exists.
130 my ($self, $tag, $sub) = @_;
131 return undef unless (defined $tag and defined $sub);
132 return undef unless $self->has($tag, $sub);
133 return $self->{tags}{$tag}{$sub};
138 Returns the modifiers set on a mapping.
140 $self->mods('fieldname')
142 If there are no modifiers, C<undef> will be returned. Else a listref
148 my ($self, $field) = @_;
149 return undef unless $self->has($field);
150 return $self->{fields}{$field}{mods};
155 Returns the content filters set on a mapping
157 $self->filters('fieldname')
159 If there are no filters, C<undef> will be returned. Else a listref
165 my ($self, $field) = @_;
166 return undef unless $self->has($field);
167 return $self->{fields}{$field}{filt};
174 Generate initial mapping from file.
179 my ($self, $file) = @_;
181 open TAGFILE, '<', $self->{conf}{file};
187 my @tokens = split /\s+/;
189 my $map = { mods => [], filt => [] };
190 $map->{field} = shift @tokens;
191 $map->{tag} = shift @tokens;
192 while (my $tok = shift @tokens) {
193 last if ($tok =~ m/^#/);
194 if ($tok =~ m/^[a-z]:'/) {
195 $tok .= ' ' . shift @tokens
196 until ($tokens[0] =~ m/'$/);
197 $tok .= ' ' . shift @tokens;
202 { push @{$map->{mods}}, $tok }
203 elsif ($tok =~ m/^f:/)
204 { push @{$map->{filt}}, $tok }
205 elsif ($tok =~ m/^[a-z0-9]$/)
206 { $map->{sub} = $tok }
208 { die "Unknown chunk '$tok' at line $.\n" }
216 Add new item to mapping. Not usually called directly from user code.
220 Where C<$map> is a hashref that, at a minimum, looks like
222 { field => "value", tag => NNN, sub => X }
224 and may also have the key/value pairs
232 my ($self, $map) = @_;
234 # trim the mods and filters
235 my $mods = []; my %mods = ();
236 my $filt = []; my %filt = ();
237 for my $m (@{$map->{mods}}) {
238 die "Modifier collision '$m' at line $." if $mods{$m};
240 push @{$mods}, $m; $mods{$m} = 1;
242 for my $f (@{$map->{filt}}) {
243 die "Modifier collision '$f' at line $." if $filt{$f};
245 push @{$filt}, $f; $filt{$f} = 1;
247 $map->{mods} = $mods;
248 $map->{filt} = $filt;
250 # check bits for validity
251 $self->validate($map);
253 # add data to the fields hash
254 $self->{fields}{ $map->{field} } = { tag => $map->{tag},
256 mods => $map->{mods},
259 # and to the tags hash
260 $self->{tags}{ $map->{tag} }{ $map->{sub} } = $map->{field};
265 Passed a reference to the hash given to C<add>, validate scans its
266 contents for common errors and dies if there is an issue.
268 * field, tag, and sub are required
269 * fieldnames must start with a letter
270 * fieldnames must be unique
271 * tag must be between 0 and 999
272 * subfield code must be a single alphanumeric character
273 * tag+subfield can only be mapped once
274 * if a list of allowable mod values was given in the call to
275 C<new>, any modifiers must be on that list
280 my ($self, $map) = @_;
282 $.= 1 unless defined $.;
284 die "Required field missing (line $.)\n"
285 unless (defined $map->{field} and defined $map->{tag} and defined $map->{sub});
287 die "Fieldnames must start with letter (line $.)\n"
288 unless ($map->{field} =~ /^[a-zA-z]/);
290 die "Invalid tag (line $.)\n"
291 if ($map->{tag} =~ /[^\d\-]/ or $map->{tag} < 0 or $map->{tag} > 999);
293 die "Invalid subfield code (line $.)\n"
294 if (length $map->{sub} != 1 or $map->{sub} =~ /[^a-zA-Z0-9]/);
296 # test mod names if we have a set to check against
297 if (defined $self->{conf}{mods}) {
298 for my $mod ( @{$map->{mods}} ) {
299 die "Modifier '$mod' not allowed\n"
300 unless $self->{conf}{mods}{$mod};
304 die "Fieldnames must be unique (line $.)\n"
305 if (defined $self->{fields}{$map->{field}});
307 die "Subfields cannot be mapped twice (line $.)\n"
308 if (defined $self->{tags}{$map->{tag}}{$map->{sub}});
315 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
319 Please report any bugs or feature requests to the above email address.
323 You can find documentation for this module with the perldoc command.
325 perldoc Equinox::Migration::SubfieldMapper
328 =head1 COPYRIGHT & LICENSE
330 Copyright 2009 Equinox, all rights reserved.
332 This program is free software; you can redistribute it and/or modify it
333 under the same terms as Perl itself.
338 1; # End of Equinox::Migration::SimpleTagList