]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/MARC/File/SAX.pm
bug fixes after testing
[Evergreen.git] / Open-ILS / src / perlmods / MARC / File / SAX.pm
1 package MARC::File::SAX;
2
3 ## no POD here since you don't really want to use this module
4 ## directly. Look at MARC::File::XML instead.
5 ##
6 ## MARC::File::SAX is a SAX handler for parsing XML encoded using the 
7 ## MARC21slim XML schema from the Library of Congress. It builds a MARC::Record
8 ## object up from SAX events.
9 ##
10 ## For more details see: http://www.loc.gov/standards/marcxml/
11
12 use strict;
13 use XML::SAX;
14 use base qw( XML::SAX::Base );
15 use Data::Dumper;
16 use MARC::Charset;
17 use Encode ();
18
19 my $charset = MARC::Charset->new();
20 my $_is_unicode;
21
22 sub start_element {
23     my ( $self, $element ) = @_;
24     my $name = $element->{ Name };
25     if ( $name eq 'leader' ) { 
26         $self->{ tag } = 'LDR';
27     } elsif ( $name eq 'controlfield' ) {
28         $self->{ tag } = $element->{ Attributes }{ '{}tag' }{ Value };
29     } elsif ( $name eq 'datafield' ) { 
30         $self->{ tag } = $element->{ Attributes }{ '{}tag' }{ Value };
31         $self->{ i1 } = $element->{ Attributes }{ '{}ind1' }{ Value };
32         $self->{ i2 } = $element->{ Attributes }{ '{}ind2' }{ Value };
33     } elsif ( $name eq 'subfield' ) { 
34         $self->{ subcode } = $element->{ Attributes }{ '{}code' }{ Value };
35     }
36 }
37
38 sub end_element { 
39     my ( $self, $element ) = @_;
40     my $name = $element->{ Name };
41     if ( $name eq 'subfield' ) { 
42         push( @{ $self->{ subfields } }, $self->{ subcode }, save_space_in_utf8($self->{ chars }) );
43         $self->{ chars } = '';
44         $self->{ subcode } = '';
45     } elsif ( $name eq 'controlfield' ) { 
46         $self->{ record }->append_fields(
47             MARC::Field->new(
48                 $self->{ tag },
49                 save_space_in_utf8($self->{ chars })
50             )
51         );
52         $self->{ chars } = '';
53         $self->{ tag } = '';
54     } elsif ( $name eq 'datafield' ) { 
55         $self->{ record }->append_fields( 
56             MARC::Field->new( 
57                 $self->{ tag }, 
58                 $self->{ i1 }, 
59                 $self->{ i2 },
60                 @{ $self->{ subfields } }
61             )
62         );
63         $self->{ tag } = '';
64         $self->{ i1 } = '';
65         $self->{ i2 } = '';
66         $self->{ subfields } = [];
67         $self->{ chars } = '';
68     } elsif ( $name eq 'leader' ) { 
69         $_is_unicode = 0;
70         my $ldr = $self->{ chars };
71         $_is_unicode++ if (substr($ldr,9,1) eq 'a');
72         substr($ldr,9,1,' ');
73         $self->{ record }->leader( save_space_in_utf8($ldr) );
74         $self->{ chars } = '';
75         $self->{ tag } = '';
76     }
77
78 }
79
80 sub save_space_in_utf8 {
81         my $string = shift;
82         my $output = '';
83         while ($string =~ /(\s*)(\S*)(\s*)/gcsmo) {
84                 $output .= $1 . Encode::encode('latin1',$charset->to_marc8($2)) . $3;# if ($_is_unicode);
85                 #$output .= $1 . $2 . $3 unless ($_is_unicode);
86         }
87         return $output;
88 }
89
90 sub characters {
91     my ( $self, $chars ) = @_;
92     if ( $self->{ subcode } or ( $self->{ tag } and 
93         ( $self->{ tag } eq 'LDR' or $self->{ tag } < 10 ) ) ) { 
94         $self->{ chars } .= $chars->{ Data };
95     } 
96 }
97
98 1;