| 1 | package WebSource::File; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use WebSource::Module; | ||
| 5 | use Carp; | ||
| 6 | #use DB_File; | ||
| 7 | use IO::Handle; | ||
| 8 | |||
| 9 | our @ISA = ('WebSource::Module'); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | WebSource::File : Fetchs it input from a file (or standard | ||
| 14 | input if none is specified) | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | A B<File> operator returns as item each line read from a file. | ||
| 19 | |||
| 20 | <ws:file name="opname" | ||
| 21 | [ filename="file" ] | ||
| 22 | forward-to="ops" /> | ||
| 23 | |||
| 24 | The C<forward-to> and C<name> attributes have there usual signification. | ||
| 25 | |||
| 26 | The C<filename> attribut allows to set which file the items are read from. | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | $file = WebSource::File->new(wsnode => $node); | ||
| 31 | |||
| 32 | # for the rest it works as a WebSource::Module | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =over 2 | ||
| 37 | |||
| 38 | =item B<< $source = WebSource->new(desc => $node); >> | ||
| 39 | |||
| 40 | Create a new File module; | ||
| 41 | |||
| 42 | =item B<< $file->handle($env); >> | ||
| 43 | |||
| 44 | Reads one line from the file and gives it as output; | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub _init_ { | ||
| 49 | my $self = shift; | ||
| 50 | $self->SUPER::_init_; | ||
| 51 | my $wsd = $self->{wsdnode}; | ||
| 52 | if($wsd && $wsd->hasAttribute("pattern")) { | ||
| 53 | my $pattern = $wsd->getAttribute("pattern"); | ||
| 54 | $self->log(1,"Found file pattern <$pattern>"); | ||
| 55 | foreach my $filename (glob($pattern)) { | ||
| 56 | if( -f $filename) { | ||
| 57 | $self->log(1,"Adding <$filename> from pattern <$pattern>"); | ||
| 58 | $self->push(WebSource::Envelope->new( | ||
| 59 | type => "text/string", | ||
| 60 | data => $filename | ||
| 61 | )); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub handle { | ||
| 69 | my $self = shift; | ||
| 70 | my $env = shift; | ||
| 71 | |||
| 72 | my $file = $env->data; | ||
| 73 | my $io; | ||
| 74 | if($file eq 'stdin') { | ||
| 75 | $io = IO::Handle->new(); | ||
| 76 | $io->fdopen(fileno(STDIN),"r"); | ||
| 77 | } else { | ||
| 78 | $io = IO::File->new($file,"r"); | ||
| 79 | } | ||
| 80 | |||
| 81 | my $data = ""; | ||
| 82 | while(my $line = <$io>) { | ||
| 83 | $data .= $line; | ||
| 84 | } | ||
| 85 | $io->close(); | ||
| 86 | |||
| 87 | return WebSource::Envelope->new ( | ||
| 88 | type => "text/html", | ||
| 89 | data => $data, | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =head1 SEE ALSO | ||
| 96 | |||
| 97 | WebSource::Module | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | 1; |