add copyright statement and GPL2+ license statement
[migration-tools.git] / Equinox-Migration / lib / Equinox / Migration / MARCXMLSampler.pm
index d6fc65e..60f5ec9 100644 (file)
@@ -1,5 +1,21 @@
 package Equinox::Migration::MARCXMLSampler;
 
+# Copyright 2009-2012, Equinox Software, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
 use warnings;
 use strict;
 
@@ -19,7 +35,6 @@ Version 1.003
 
 our $VERSION = '1.003';
 
-my $xmltwig;
 my $taglist;
 my $dstore;
 
@@ -74,15 +89,6 @@ sub new {
     my $self = bless { data => $dstore,
                      }, $class;
 
-    # initialize twig
-    die "Argument 'marcfile' must be specified\n" unless ($args{marcfile});
-    if (-r $args{marcfile}) {
-        $xmltwig = XML::Twig->new( twig_handlers => { record => \&parse_record } );
-        $self->{conf}{marc} = $args{marcfile};
-    } else {
-        die "Can't open marc file: $!\n";
-    }
-
     # if we have a sample arg, create the sample map
     die "Can't use a mapfile and mapstring\n"
       if ($args{mapfile} and $args{mapstring});
@@ -91,10 +97,16 @@ sub new {
     $taglist = Equinox::Migration::SimpleTagList->new(str => $args{mapstring})
         if ($args{mapstring});
 
-    # do the xml processing
-    $xmltwig->parsefile( $self->{conf}{marc} );
+    # initialize twig and process xml
+    die "Argument 'marcfile' must be specified\n" unless ($args{marcfile});
+    if (-r $args{marcfile}) {
+        my $xmltwig = XML::Twig->new( twig_handlers => { record => \&parse_record } );
+        $xmltwig->parsefile( $args{marcfile} );
+    } else {
+        die "Can't open marc file: $!\n";
+    }
 
-    # hand ourselves back for 
+    # hand ourselves back for datastore manipulation
     return $self;
 }
 
@@ -121,7 +133,7 @@ sub parse_record {
 sub process_field {
     my ($field) = @_;
     my $tag = $field->{'att'}->{'tag'};
-    return unless ($tag and $tag > 9);
+    return unless ($tag and ($tag =~ /[^0-9]/ or $tag > 9));
 
     # increment raw tag count
     $dstore->{tcnt}++;
@@ -146,7 +158,8 @@ sub process_subs {
     # handle unmapped tag/subs
     my $samp = $dstore->{samp};
     # set a value, total-seen count and records-seen-in count
-    $samp->{$tag}{$code}{value} = $sub->text unless $samp->{$tag}{$code};
+    $samp->{$tag}{$code}{value} = $sub->text unless ($samp->{$tag}{$code}{value} and
+                                                     $samp->{$tag}{$code}{value} =~ /\w/);
     $samp->{$tag}{$code}{count}++;
     $samp->{$tag}{$code}{tcnt}++ unless ( defined $samp->{$tag}{$code}{last} and
                                           $samp->{$tag}{$code}{last} == $dstore->{tcnt} );