/[webpac2]/trunk/lib/WebPAC/Input/XML.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

Contents of /trunk/lib/WebPAC/Input/XML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 970 - (show annotations)
Fri Nov 2 14:29:11 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 2120 byte(s)
 r1483@llin:  dpavlin | 2007-11-02 15:29:09 +0100
 return parsed XML hash

1 package WebPAC::Input::XML;
2
3 use warnings;
4 use strict;
5
6 use WebPAC::Input;
7 use base qw/WebPAC::Common/;
8
9 use XML::Simple;
10 use File::Find;
11
12 use Data::Dump qw/dump/;
13
14 =head1 NAME
15
16 WebPAC::Input::XML - support for reading XML files
17
18 =cut
19
20 our $VERSION = '0.01';
21
22 =head1 FUNCTIONS
23
24 =head2 new
25
26 Returns new low-level input API object
27
28 my $input = new WebPAC::Input::XML(
29 path => '/path/to/XML/records.txt'
30 filter => sub {
31 my ($l,$field_nr) = @_;
32 # do something with $l which is line of input file
33 return $l;
34 },
35 }
36
37 Options:
38
39 =over 4
40
41 =item path
42
43 path to directory with xml files ending in C<.xml>
44
45 =back
46
47 =cut
48
49 sub new {
50 my $class = shift;
51 my $self = {@_};
52 bless($self, $class);
53
54 my $arg = {@_};
55
56 my $log = $self->_get_logger();
57
58 $log->logdie("can't find path ", $arg->{path}, ": $!\n") unless -d $arg->{path};
59
60 $log->info("collecting xml files from ", $arg->{path});
61
62 my @files;
63
64 find({
65 wanted => sub {
66 my $path = $File::Find::name;
67 return unless -f $path && $path =~ m/\.xml$/i;
68 push @files, $path;
69 },
70 follow => 1,
71 }, $arg->{path} );
72
73 $log->info("found ", $#files + 1, " XML files in ", $arg->{path});
74
75 $self->{_files} = \@files;
76
77 $self ? return $self : return undef;
78 }
79
80 =head2 fetch_rec
81
82 Return record with ID C<$mfn> from database
83
84 my $rec = $input->fetch_rec( $mfn, $filter_coderef );
85
86 =cut
87
88 sub fetch_rec {
89 my $self = shift;
90
91 my ( $mfn, $filter_coderef ) = @_;
92
93 my $path = $self->{_files}->[ $mfn - 1 ] || return;
94
95 my $log = $self->_get_logger();
96
97 my $xml = XMLin( $path, ForceArray => 1 ) || $log->logdie("can't open $path: $!");
98
99 warn "### fetch_rec( $mfn ) => $path => xml is ",dump($xml);
100
101 # add mfn
102 $xml->{'000'} = [ $mfn ];
103
104 return $xml;
105 }
106
107
108 =head2 size
109
110 Return number of records in database
111
112 my $size = $input->size;
113
114 =cut
115
116 sub size {
117 my $self = shift;
118 return $#{$self->{_files}} + 1;
119 }
120
121 =head1 AUTHOR
122
123 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
124
125 =head1 COPYRIGHT & LICENSE
126
127 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
128
129 This program is free software; you can redistribute it and/or modify it
130 under the same terms as Perl itself.
131
132 =cut
133
134 1;

  ViewVC Help
Powered by ViewVC 1.1.26