/[webpac2]/trunk/lib/WebPAC/Input/EBSCO.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Input/EBSCO.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1089 - (hide annotations)
Mon Jan 28 18:30:07 2008 UTC (16 years, 3 months ago) by dpavlin
File size: 2812 byte(s)
 r1702@llin:  dpavlin | 2008-01-28 19:29:41 +0100
 EBSCO text file export support

1 dpavlin 1089 package WebPAC::Input::EBSCO;
2    
3     use warnings;
4     use strict;
5    
6     use WebPAC::Input;
7     use base qw/WebPAC::Common/;
8    
9     use Data::Dump qw/dump/;
10    
11     =head1 NAME
12    
13     WebPAC::Input::EBSCO - support for EBSCO text export
14    
15     =head1 VERSION
16    
17     Version 0.01
18    
19     =cut
20    
21     our $VERSION = '0.01';
22    
23     =head1 SYNOPSIS
24    
25     Open file in EBSCO text export fromat
26    
27     my $input = new WebPAC::Input::EBSCO(
28     path => '/path/to/ebsco.txt',
29     );
30    
31     =head1 FUNCTIONS
32    
33     =head2 new
34    
35     Returns new low-level input API object
36    
37     my $input = new WebPAC::Input::EBSCO(
38     path => '/path/to/ebsco.txt',
39     filter => sub {
40     my ($l,$field_nr) = @_;
41     # do something with $l which is line of input file
42     return $l;
43     },
44     }
45    
46     Options:
47    
48     =over 4
49    
50     =item path
51    
52     path to EBSCO export file
53    
54     =back
55    
56     =cut
57    
58     my $subfields = {
59     'CR' => sub {
60     my @v = split(/, /, shift);
61     my $f;
62     foreach ( qw/author year reference volume page/ ) {
63     if ( my $tmp = shift @v ) {
64     $f->{$_} = $tmp;
65     }
66     }
67     if ( $f->{author} =~ /^\*(.+)/ ) {
68     delete $f->{author};
69     $f->{institution} = $1;
70     }
71     return $f;
72     },
73     };
74    
75     sub new {
76     my $class = shift;
77     my $self = {@_};
78     bless($self, $class);
79    
80     my $arg = {@_};
81    
82     my $log = $self->_get_logger();
83    
84     open( my $fh, '<', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
85    
86     $log->info("reading '$arg->{path}'");
87    
88     my $tag;
89     my $rec;
90    
91     $self->{size} = 0;
92    
93     while( my $line = <$fh> ) {
94     chomp($line);
95     # remove extra spaces at end (?!)
96     $line =~ s/\s+$//;
97    
98     my $v;
99    
100     if ( $line =~ m/^-+$/ ) {
101     # join tags
102     # foreach ( qw/AB DE ID TI/ ) {
103     # $rec->{$_} = join(' ', @{ $rec->{$_} }) if defined $rec->{$_};
104     # }
105     $rec->{'000'} = [ ++$self->{size} ];
106     push @{ $self->{_rec} }, $rec;
107     $rec = {};
108     } elsif ( $line =~ /^(\S\S)-\s(.+)$/ ) {
109     $tag = $1;
110     $v = $2;
111     } else {
112     warn "### skip: $line\n";
113     }
114    
115     if ( defined $v ) {
116     $v = $subfields->{$tag}->($v) if defined $subfields->{$tag};
117    
118     $log->debug("$tag: ", sub { dump( $v ) });
119     push @{ $rec->{$tag} }, $v;
120     }
121    
122     }
123    
124     # save last rec
125     $rec->{'000'} = [ ++$self->{size} ];
126     push @{ $self->{_rec} }, $rec;
127    
128     $log->debug("loaded ", $self->size, " records");
129    
130     $self ? return $self : return undef;
131     }
132    
133     =head2 fetch_rec
134    
135     Return record with ID C<$mfn> from database
136    
137     my $rec = $input->fetch_rec( $mfn, $filter_coderef );
138    
139     =cut
140    
141     sub fetch_rec {
142     my $self = shift;
143    
144     my ( $mfn, $filter_coderef ) = @_;
145    
146     return $self->{_rec}->[$mfn-1];
147     }
148    
149    
150     =head2 size
151    
152     Return number of records in database
153    
154     my $size = $input->size;
155    
156     =cut
157    
158     sub size {
159     my $self = shift;
160     return $self->{size};
161     }
162    
163     =head1 AUTHOR
164    
165     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
166    
167     =head1 COPYRIGHT & LICENSE
168    
169     Copyright 2008 Dobrica Pavlinusic, All Rights Reserved.
170    
171     This program is free software; you can redistribute it and/or modify it
172     under the same terms as Perl itself.
173    
174     =cut
175    
176     1; # End of WebPAC::Input::EBSCO

  ViewVC Help
Powered by ViewVC 1.1.26