/[webpac]/branches/cpi/parse_format.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

Diff of /branches/cpi/parse_format.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 40 by dpavlin, Sat Mar 15 21:33:36 2003 UTC revision 170 by dpavlin, Sun Nov 23 15:42:16 2003 UTC
# Line 1  Line 1 
1  #-------------------------------------------------------------  #-------------------------------------------------------------
2  #  #
3  # parse_format('format',$isis_row);  # parse_format(...)
4  #  #
5    
 use isis_sf;  
   
6  sub parse_format {  sub parse_format {
7            my $type = shift || die "parset_format must be called with type!";
8            my $format = shift || die "parse_format must be called with format!";
9            my $row = shift || die "parse_format must be called with row!";
10            my $i = shift || 0;     # isis repeatable number
11            my $codepage = shift || die "parse_format must be called with codepage!";
12            if ($type eq "isis") {
13                    return parse_iso_format($format,$row,$i,$codepage,'isis_sf');
14            } elsif ($type eq "excel") {
15                    return parse_excel_format($format,$row,$i,$codepage);
16            } elsif ($type eq "marc") {
17                    return parse_iso_format($format,$row,$i,$codepage,'marc_sf');
18            } elsif ($type eq "feed") {
19                    return parse_feed_format($format,$row,$i,$codepage);
20            }
21    }
22    
23    #-------------------------------------------------------------
24    
25    sub parse_iso_format {
26    
27          my $format = shift;          my $format = shift;
28          my $row = shift;          my $row = shift;
29          my $i = shift || 0;     # isis repeatable number          my $i = shift;
30          my $codepage = shift || die;          my $codepage = shift;
31    
32            my $func = shift || die "need to know which sub-field function to use";
33    
34            require $func.".pm";
35    
36            my $out;
37            my $out_swish;
38    
39            my $display;
40            my $swish;
41    
42            sub cnv_cp {
43                    my $codepage = shift;
44                    my $tmp = shift || return;
45                    if ($codepage) {
46                            $tmp = $codepage->convert($tmp) || print STDERR "iso: '$tmp' can't convert\n";
47                    }
48                    return $tmp;
49            }
50    
51            # if format doesn't exits, store it in cache
52            if (! defined($cache->{format}->{$format})) {
53    #               print STDERR "parsing format for '$format'\n";
54                    my @fmt;
55    
56                    my $f = $format;
57    
58                    if ($f =~ s/^([^\d]+)//) {
59                            if ($f) {       # there is more to parse
60                                    push @fmt,$1;
61                            } else {
62                                    @fmt = ('',$1,undef,'');
63    #print STDERR "just one field: $1\n";
64                            }
65                    } else {
66                            push @fmt,'';
67                    }
68    
69                    while ($f) {
70    #       print STDERR "\n#### $f";
71                            # this is EBSCO special to support numeric subfield in
72                            # form of 856#3
73                            if ($f =~ s/^(\d\d\d)#*(\w?)//) {
74                                    push @fmt,$1;
75                                    if ($2) {
76                                            push @fmt,$2;
77                                    } else {
78                                            push @fmt,undef;
79                                    }
80                            # this might be our local scpeciality -- fields 10 and 11
81                            # (as opposed to 010 and 011) so they are strictly listed
82                            # here
83                            } elsif ($f =~ s/^(1[01])//) {
84                                    push @fmt,$1;
85                                    push @fmt,undef;
86                            } elsif ($f =~ s/^mfn//i) {
87                                    push @fmt,'mfn';
88                                    push @fmt,'';
89                            } elsif ($f =~ s/^([^\d]+)(\d{0,3})/$2/) {
90                                    push @fmt,$1;
91                            } elsif ($f =~ s/^([^\d]+\d{0,2})//) {
92                                    push @fmt,$1;
93                            } elsif ($f =~ s/^(\d{1,2})//) {
94                                    push @fmt,$1;
95                            } else {
96                                    print STDERR "unparsed format: $f\n";
97                                    $f = "";
98                            }
99                    }
100                    push @fmt,'' if ($#fmt % 3 != 0);       # add empty suffix
101                    $cache->{format}->{$format} = \@fmt;
102                    
103    #               print STDERR "storing format for '$format': [",join("|",@fmt),"]\n";
104    #               print STDERR "storing format for '$format':",Dumper(@fmt),"\n";
105    #               print STDERR Dumper($cache->{format}->{$format});
106            }
107    
108            # now produce actual record
109            my $tmp = $cache->{format}->{$format} || die "no format cache for '$format'";
110            my @fmt = @{$tmp};
111    #       print STDERR "using format for '$format':",Dumper(@fmt),"\n";
112    #       print STDERR "tmp ",Dumper($tmp);
113    #       print STDERR "cache: ",Dumper($cache->{format}->{$format});
114    
115            # prefix
116            my $prefix = shift @fmt;
117            my $sufix;
118            while($#fmt > 1) {
119                    my $f = shift @fmt || die "BUG: field name can't be empty!";
120                    my $sf = shift @fmt;
121    
122                    if ($f eq 'mfn' && $i == 0) {
123                            $display .= $sufix if ($display);
124                            $display .= $row->{mfn};
125                    } else {
126                            my $val = &$func($row,$f,$sf,$i);
127                            if ($val) {
128    #                               print STDERR "val: $val\n";
129                                    my $tmp = cnv_cp($codepage,$val);
130                                    if ($display) {
131                                            $display .= $sufix.$tmp;
132                                    } else {
133                                            $display = $tmp;
134                                    }
135                                    $swish .= $tmp." ";
136                            }
137                    }
138                    $sufix = shift @fmt;
139            }
140            $display = $prefix.$display.$sufix if ($display);
141            print STDERR "format left unused: [",join("|",@fmt),"]\n" if (@fmt);
142    
143    #       print STDERR "display: $display swish: $swish\n";
144    
145            return ($swish,$display);
146    }
147    
148    #-------------------------------------------------------------
149    
150    sub parse_excel_format {
151            my $format = shift;
152            my $row = shift;
153            my $i = shift;
154            my $codepage = shift;
155    
156            return if ($i > 0);     # Excel doesn't support repeatable fields
157    
158          my $out;          my $out;
159          my $out_swish;          my $out_swish;
160    
161          my $prefix = "";          my $prefix = "";
162          if ($format =~ s/^([^\d]+)//) {          if ($format =~ s/^([^A-Z\|]{1,3})//) {
163                  $prefix = $1;                  $prefix = $1;
164          }          }
165    
166          my $display;          my $display;
167          my $swish;          my $swish;
168    
169          while ($format) {          while ($format && length($format) > 0) {
170  #print STDERR "\n#### $format";  #print STDERR "\n#### $format #";
171                  if ($format =~ s/^(\d\d\d)(\w?)//) {                  if ($format =~ s/^\|([A-Z]{1,2})\|//) {
172                          my $isis_tmp = isis_sf($row,$1,$2,$i);  #print STDERR "--$1-> $format -[",length($format),"] ";
173                          if ($isis_tmp) {                          if ($row->{$1}) {
174                                  $isis_tmp = $codepage->convert($isis_tmp) if ($codepage);                                  my $tmp = $row->{$1};
175                                  $display .= $prefix . $isis_tmp;                                  if ($codepage) {
176                                  $swish .= $isis_tmp." ";                                          $tmp = $codepage->convert($tmp) || warn "excel: $1 '$tmp' can't convert";
177  #print STDERR " == $isis_tmp";                                  }
178                                    $display .= $prefix . $tmp;
179                                    $swish .= $tmp." ";
180    #print STDERR " == $tmp";
181                          }                          }
182                          $prefix = "";                          $prefix = "";
183                  # this might be our local scpeciality -- fields 10 and 11                  } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$2/) {
184                  # (as opposed to 010 and 011) so they are strictly listed                          $prefix .= $1 if ($display);
185                  # here                  } else {
186                  } elsif ($format =~ s/^(1[01])//) {                          print STDERR "unparsed format: $format\n";
187                          my $isis_tmp = isis_sf($row,$1,undef,$i);                          $prefix .= $format;
188                          if ($isis_tmp) {                          $format = "";
189                                  $isis_tmp = $codepage->convert($isis_tmp) if ($codepage);                  }
190                                  $display .= $prefix . $isis_tmp;  #print STDERR " display: $display swish: $swish [format: $format]";
191                                  $swish .= $isis_tmp." ";          }
192            # add suffix
193            $display .= $prefix if ($display);
194    
195            return ($swish,$display);
196    }
197    
198    #-------------------------------------------------------------
199    
200    sub parse_feed_format {
201            my $format = shift;
202            my $data = shift;
203            my $i = shift;
204            my $codepage = shift;
205    
206            # XXX feed doesn't support repeatable fields, but they really
207            # should, This is a bug. It should be fixed!
208            return if ($i > 0);
209    
210            my $out;
211            my $out_swish;
212    
213            my $prefix = "";
214            if ($format =~ s/^([^\d\|]{1,3})//) {
215                    $prefix = $1;
216            }
217    
218            my $display;
219            my $swish;
220    
221            while ($format && length($format) > 0) {
222    #print STDERR "\n#### $format #";
223                    if ($format =~ s/^\|(\d+)\|//) {
224    #print STDERR "--$1-> $format -[",length($format),"] ";
225                            if ($data->{$1}) {
226                                    my $tmp = $data->{$1};
227                                    if ($codepage) {
228                                            $tmp = $codepage->convert($tmp) || warn "feed: $1 '$tmp' can't convert\n";
229                                    }
230                                    $display .= $prefix . $tmp;
231                                    $swish .= $tmp." ";
232    #print STDERR " == $tmp";
233                          }                          }
234                          $prefix = "";                          $prefix = "";
235                  } elsif ($format =~ s/^mfn//i) {                  } elsif ($format =~ s/^([^\d\|]+)(\|\d+\|)/$2/) {
                         $display .= $prefix . $row->{mfn};  
                         $prefix = "";  
                 } elsif ($format =~ s/^([^\d]+)(\d{0,3})/$2/) {  
                         $prefix .= $1 if ($display);  
                 } elsif ($format =~ s/^([^\d]+\d{0,2})//) {  
                         $prefix .= $1 if ($display);  
                 } elsif ($format =~ s/^(\d{1,2})//) {  
236                          $prefix .= $1 if ($display);                          $prefix .= $1 if ($display);
237                  } else {                  } else {
238                          print STDERR "unparsed format: $format\n";                          print STDERR "unparsed format: $format\n";
239                          $prefix .= $format;                          $prefix .= $format;
240                          $format = "";                          $format = "";
241                  }                  }
242    #print STDERR " display: $display swish: $swish [format: $format]";
243          }          }
244          # add suffix          # add suffix
245          $display .= $prefix if ($display);          $display .= $prefix if ($display);
# Line 66  sub parse_format { Line 248  sub parse_format {
248  }  }
249    
250  #-------------------------------------------------------------  #-------------------------------------------------------------
251    
252  1;  1;

Legend:
Removed from v.40  
changed lines
  Added in v.170

  ViewVC Help
Powered by ViewVC 1.1.26