working
[migration-tools.git] / Equinox-Migration / lib / Equinox / Migration / MapDrivenMARCXMLProc.pm
1 package Equinox::Migration::MapDrivenXMLProc;
2
3 use warnings;
4 use strict;
5
6 use XML::Twig;
7 use Equinox::Migration::SubfieldMapper;
8
9 =head1 NAME
10
11 Equinox::Migration::MapDrivenXMLProc
12
13 =head1 VERSION
14
15 Version 1.000
16
17 =cut
18
19 our $VERSION = '1.000';
20
21
22 =head1 SYNOPSIS
23
24 Foo
25
26     use Equinox::Migration::MapDrivenXMLProc;
27
28
29 =cut
30
31
32
33 =head1 METHODS
34
35
36 =head2 new
37
38 =cut
39
40 sub new {
41     my ($class, %args) = @_;
42
43     my $self = bless { conf => { count => 0,
44                                  total => 0,
45                                  quiet => 0,
46                                },
47                        map  => Equinox::Migration::SubfieldMapper->new(file => $args{mapfile}),
48                        tags => {},
49                        twig => XML::Twig->new( twig_handlers => { record => \&record } ),
50                      }, $class;
51
52     if ($args{marcfile}) {
53         if (-r $args{marcfile}) {
54             $self->{conf}{marc} = $args{marcfile};
55             $self->generate;
56         } else {
57             die "Can't open marc file: $!\n";
58         }
59     }
60     $self->{twig}->parsefile($self->{conf}{marc});
61
62
63     return $self;
64 }
65
66 sub parse {
67     my ($self) = @_;
68 }
69
70
71 sub emit_status {
72     my ($self) = @_;
73     my $c = $self->{conf};
74     return if $c->{quiet};
75     $c->{count}++;
76     my $percent = int(($c->{count} / $c->{total}) * 100);
77     print STDERR "\r$percent% done (", $c->{count}, ")";
78 }
79
80
81 =head2 XML::Twig CALLBACK ROUTINES
82
83 =head3 record
84
85 =cut
86
87 sub record {
88     my($t, $r)= @_;
89     $self->{holdings} = {};
90
91     my @dfields = $r->children('datafield');
92     for my $d (@dfields) {
93         process_datafields($d);
94     }
95     write_data_out();
96     $r->purge;
97 }
98
99 =head3 process_datafields
100
101 =cut
102
103 sub process_datafields {
104     my ($d) = @_;
105     my $map = $self->{map};
106     my $tag = $d->{'att'}->{'tag'};
107
108     if ($tag == 903) {
109         my $s = $d->first_child('subfield');
110         $self->{holdings}{id} = $s->text;;
111     } elsif ($map->has($tag)) {
112         push @{$self->{holdings}{copies}}, { tag => $tag };
113         my @subs = $d->children('subfield');
114         for my $sub (@subs)
115           { process_subs($tag, $sub) }
116     }
117 }
118
119 =head3 process_subs
120
121 =cut
122
123 sub process_subs {
124     my ($tag, $sub) = @_;
125     my $map  = $self->{map};
126     my $code = $sub->{'att'}->{'code'};
127
128     unless ($map->has($tag, $code)) {
129         # this is a subfield code we don't have mapped. report on it if this is a sample tag
130         push @{$c->{sample}{$tag}}, $code if defined $c->{sample}{tag};
131         return;
132     }
133
134     my $copy = $self->{holdings}{copies}[-1];
135     my $field = $map->field($tag, $code);
136             if ($map->mod($field) eq 'multi') {
137         my $name = $tag . $code;
138         push @{$copy->{multi}{$name}}, $sub->text;
139     } else {
140         $copy->{uni}{$code} = $sub->text;
141     }
142 }
143
144 =head1 AUTHOR
145
146 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
147
148 =head1 BUGS
149
150 Please report any bugs or feature requests to the above email address.
151
152 =head1 SUPPORT
153
154 You can find documentation for this module with the perldoc command.
155
156     perldoc Equinox::Migration::MapDrivenXMLProc
157
158
159 =head1 COPYRIGHT & LICENSE
160
161 Copyright 2009 Equinox, all rights reserved.
162
163 This program is free software; you can redistribute it and/or modify it
164 under the same terms as Perl itself.
165
166
167 =cut
168
169 1; # End of Equinox::Migration::MapDrivenXMLProc