~ubuntu-branches/ubuntu/saucy/libmarc-xml-perl/saucy

« back to all changes in this revision

Viewing changes to lib/MARC/File/SAX.pm

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2007-04-13 21:28:32 UTC
  • Revision ID: james.westby@ubuntu.com-20070413212832-51ppwi54zrwblx1b
Tags: upstream-0.85
ImportĀ upstreamĀ versionĀ 0.85

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 qw(utf8_to_marc8);
 
17
 
 
18
sub start_element {
 
19
    my ( $self, $element ) = @_;
 
20
    my $name = $element->{ Name };
 
21
    if ( $name eq 'leader' ) { 
 
22
        $self->{ tag } = 'LDR';
 
23
    } elsif ( $name eq 'controlfield' ) {
 
24
        $self->{ tag } = $element->{ Attributes }{ '{}tag' }{ Value };
 
25
    } elsif ( $name eq 'datafield' ) { 
 
26
        $self->{ tag } = $element->{ Attributes }{ '{}tag' }{ Value };
 
27
        $self->{ i1 } = $element->{ Attributes }{ '{}ind1' }{ Value };
 
28
        $self->{ i2 } = $element->{ Attributes }{ '{}ind2' }{ Value };
 
29
    } elsif ( $name eq 'subfield' ) { 
 
30
        $self->{ subcode } = $element->{ Attributes }{ '{}code' }{ Value };
 
31
    }
 
32
}
 
33
 
 
34
sub end_element { 
 
35
    my ( $self, $element ) = @_;
 
36
    my $name = $element->{ Name };
 
37
    if ( $name eq 'subfield' ) { 
 
38
        push @{ $self->{ subfields } }, $self->{ subcode };
 
39
        
 
40
        if ($self->{ transcode }) {
 
41
           push @{ $self->{ subfields } }, utf8_to_marc8($self->{ chars });
 
42
        } else {
 
43
           push @{ $self->{ subfields } }, $self->{ chars } ;
 
44
        }
 
45
 
 
46
        $self->{ chars } = '';
 
47
        $self->{ subcode } = '';
 
48
    } elsif ( $name eq 'controlfield' ) { 
 
49
        $self->{ record }->append_fields(
 
50
            MARC::Field->new( $self->{ tag }, $self->{ chars } )
 
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
        my $ldr = $self->{ chars };
 
70
        $self->{ transcode }++
 
71
                if (substr($ldr,9,1) eq 'a' and $self->{toMARC8});
 
72
        
 
73
        substr($ldr,9,1,' ') if ($self->{ transcode });
 
74
        $self->{ record }->leader( $ldr );
 
75
        $self->{ chars } = '';
 
76
        $self->{ tag } = '';
 
77
    }
 
78
 
 
79
}
 
80
 
 
81
sub characters {
 
82
    my ( $self, $chars ) = @_;
 
83
    if ( $self->{ subcode } or ( $self->{ tag } and 
 
84
        ( $self->{ tag } eq 'LDR' or $self->{ tag } < 10 ) ) ) { 
 
85
        $self->{ chars } .= $chars->{ Data };
 
86
    } 
 
87
}
 
88
 
 
89
1;