blib/lib/Uplug/IO/Any.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 182 CC 25 SCAR 52.4
CC
25
Cov
0%
new
420.0
CheckExtender
42.0
GetTempFileName
2.0
CRAP 189.0
1 #-*-perl-*-
2 #####################################################################
3 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # Uplug::IO::Any - virtual class for arbitrary streams
20 #
21 #
22 #####################################################################
23  
24  
25 =head1 NAME
26  
27 Uplug::IO::Any - libraries for handling various kinds of input/output
28  
29 =head1 SYNOPSIS
30  
31 use Uplug::IO::Any;
32 use Uplug::Data;
33  
34 %InSpec = (
35 format => 'text',
36 file => $input_filename,
37 access_mode => 'read',
38 encoding => 'iso-8859-1' );
39  
40 %OutSpec = (
41 format => 'xml',
42 file => $output_filename,
43 access_mode => 'overwrite',
44 root => 's' );
45  
46  
47 $input = new Uplug::IO::Any( \%InSpec )
48 $output = new Uplug::IO::Any( \%OutSpec )
49  
50 $data=Uplug::Data->new();
51  
52 while ($input->read($data)){
53 # do somwthing with the data
54 $output->write($data);
55 }
56  
57 $input->close;
58 $output->close;
59  
60 =head1 DESCRIPTION
61  
62 This is a class factory for creating data streams of various kinds. Supported sub-classes are:
63  
64 Uplug::IO::Text ........... plain text
65 Uplug::IO::XML ............ generic XML class
66  
67 Uplug::IO::XCESAlign ...... XCES-based sentence alignment
68 Uplug::IO::MosesWordAlign . word alignment in Moses format
69 Uplug::IO::PlugXML ........ parallel corpus format (used in the project PLUG)
70 Uplug::IO::LWA ............ format used by the Linköping Word Aligner (PLUG)
71 Uplug::IO::LiuAlign ....... Linköping's parallel corpus format (PLUG)
72  
73 Uplug::IO::DBM ............ databases using AnyDBM
74 Uplug::IO::Tab ............ tab-separated data
75 Uplug::IO::Storable ....... storable objects
76 Uplug::IO::Collection ..... generic class to combine several input streams
77  
78 =cut
79  
80  
81 package Uplug::IO::Any;
82  
83 use strict;
84 use vars qw(@ISA);
85 use vars qw($DefaultFormat);
86 use FindBin qw($Bin);
87  
88 use IO::File;
89 use POSIX qw(tmpnam);
90  
91 use autouse 'Uplug::Config';
92  
93 use Uplug::IO;
94 use Uplug::IO::Text;
95 use Uplug::IO::XML;
96 use Uplug::IO::PlugXML;
97 use Uplug::IO::Tab;
98 use Uplug::IO::Collection;
99 use Uplug::IO::LiuAlign;
100 use Uplug::IO::DBM;
101 use Uplug::IO::XCESalign;
102 use Uplug::IO::LWA;
103 use Uplug::IO::Storable;
104 use Uplug::IO::MosesWordAlign;
105  
106  
107 @ISA = qw( Uplug::IO );
108  
109  
110 =head1 Methods
111  
112 =head2 Constructor
113  
114 $handler = new Uplug::IO::Any( \%spec, $format );
115  
116 Create a new I/O handler according to the specifications of C<%spec> and the optional format C<$format>. If C<%spec> includes the key C<stream name>: Try to load the specifications of a named stream (see L<Uplug::Config> for more information).
117  
118 Accepted data formats:
119  
120 IO-class format parameter
121 -----------------------------------------------
122 Uplug::IO::Text ........... text
123 Uplug::IO::XML ............ xml
124  
125 Uplug::IO::XCESAlign ...... align | xces
126 Uplug::IO::MosesWordAlign . moses
127 Uplug::IO::PlugXML ........ plug
128 Uplug::IO::LWA ............ lwa
129 Uplug::IO::LiuAlign ....... liu | koma
130  
131 Uplug::IO::DBM ............ dbm
132 Uplug::IO::Tab ............ tab | uwa tab
133 Uplug::IO::Storable ....... storable
134 Uplug::IO::Collection ..... collection
135  
136 If no format is given: Check file name extension:
137  
138 *.dbm ..................... Uplug::IO::DBM
139 *.uwa ..................... Uplug::IO::Tab
140 *.txt ..................... Uplug::IO::Text
141 *.xml ..................... Uplug::IO::XML
142  
143 =cut
144  
145  
146 sub new{
147 my $class=shift;
148 my ($stream,$format);
149 if (ref($_[0]) eq 'HASH'){$stream = shift;}
150 else{$stream={};$format = shift;}
151  
152 ## ---------------------------------
153 ## 'stream name' => look for a 'named stream' and ignore all other settings
154  
155 if (defined $stream->{'stream name'}){
156 $stream=&Uplug::Config::GetNamedIO($stream);
157 }
158 if ((not defined $format) and (defined $stream->{format})){
159 $format=$stream->{format};
160 }
161  
162 ##-----------------------------
163 ## create data stream object according to format settings
164  
165 if ($format=~/^text$/i){return Uplug::IO::Text->new();}
166 elsif ($format=~/^moses/i){return Uplug::IO::MosesWordAlign->new();}
167 elsif ($format=~/^koma(\s|\Z)/i){return Uplug::IO::LiuAlign->new();}
168 elsif ($format=~/^align(\s|\Z)/i){return Uplug::IO::XCESalign->new();}
169 elsif ($format=~/^liu\s*xml$/i){return Uplug::IO::LiuAlign->new();}
170 elsif ($format=~/^xces(\s|\Z)/i){return Uplug::IO::XCESalign->new();}
171 elsif ($format=~/^xml$/i){return Uplug::IO::XML->new();}
172 elsif ($format=~/^plug$/i){return Uplug::IO::PlugXML->new();}
173 elsif ($format=~/^lwa/i){return Uplug::IO::LWA->new();}
174 elsif ($format=~/^tab$/i){return Uplug::IO::Tab->new();}
175 elsif ($format=~/^uwa\s+tab$/i){return Uplug::IO::Tab->new();}
176 elsif ($format=~/^collection$/i){return Uplug::IO::Collection->new();}
177 elsif ($format=~/^dbm$/i){return Uplug::IO::DBM->new();}
178 elsif ($format=~/^stor/i){return Uplug::IO::Storable->new();}
179  
180 ##-----------------------------
181 ## try to find the appropriate format ...
182  
183 elsif (defined $stream->{file}){
184 if ($format=&CheckExtender($stream->{file})){
185 $stream->{format}=$format;
186 return Uplug::IO::Any->new($stream);
187 }
188 }
189 warn "# Uplug::IO::Any: no format specification found!\n";
190 return undef;
191 }
192  
193  
194 sub CheckExtender{
195 my $file=shift;
196  
197 if ($file=~/\.dbm$/){return 'dbm';}
198 elsif ($file=~/\.uwa$/){return 'uwa tab';}
199 elsif ($file=~/\.txt$/){return 'text';}
200 elsif ($file=~/\.liu$/){return 'align';}
201 elsif ($file=~/\.xml$/){return 'xml';}
202 return undef;
203 }
204  
205  
206 sub GetTempFileName{
207 my $fh;
208 my $file;
209 do {$file=tmpnam();}
210 until ($fh=IO::File->new($file,O_RDWR|O_CREAT|O_EXCL));
211 $fh->close;
212 return $file;
213 }
214  
215 1;
216  
217  
218 __END__