/[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 62 by dpavlin, Fri Jul 4 20:11:48 2003 UTC revision 176 by dpavlin, Mon Nov 24 01:16:04 2003 UTC
# Line 3  Line 3 
3  # parse_format(...)  # parse_format(...)
4  #  #
5    
   
6  sub parse_format {  sub parse_format {
7          my $type = shift || die "parset_format must be called with type!";          my $type = shift || die "parset_format must be called with type!";
8          my $format = shift || die "parse_format must be called with format!";          my $format = shift || die "parse_format must be called with format!";
# Line 16  sub parse_format { Line 15  sub parse_format {
15                  return parse_excel_format($format,$row,$i,$codepage);                  return parse_excel_format($format,$row,$i,$codepage);
16          } elsif ($type eq "marc") {          } elsif ($type eq "marc") {
17                  return parse_iso_format($format,$row,$i,$codepage,'marc_sf');                  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    
# Line 35  sub parse_iso_format { Line 36  sub parse_iso_format {
36          my $out;          my $out;
37          my $out_swish;          my $out_swish;
38    
         my $prefix = "";  
         if ($format =~ s/^([^\d]+)//) {  
                 $prefix = $1;  
         }  
   
39          my $display;          my $display;
40          my $swish;          my $swish;
41    
42          sub cnv_cp {          sub cnv_cp {
43                  my $tmp = shift;                  my $codepage = shift;
44                    my $tmp = shift || return;
45                  if ($codepage) {                  if ($codepage) {
46                          $tmp = $codepage->convert($tmp) || print STDERR "$1$2 = '$tmp' can't convert";                          $tmp = $codepage->convert($tmp) || print STDERR "iso: '$tmp' can't convert\n";
47                  }                  }
48                  return $tmp;                  return $tmp;
49          }          }
50    
51          while ($format) {          # if format doesn't exits, store it in cache
52  #print STDERR "\n#### $format";          if (! defined($cache->{format}->{$format})) {
53                  # this is EBSCO special to support numeric subfield in  #               print STDERR "parsing format for '$format'\n";
54                  # form of 856#3                  my @fmt;
55                  if ($format =~ s/^(\d\d\d)#*(\w?)//) {  
56                          my $tmp = get_sf($row,$1,$2,$i);                  my $f = $format;
57                          if ($tmp) {  
58                                  $display .= $prefix.cnv_cp($tmp);                  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                                    # still prefix?
91                                    if ($#fmt == 0) {
92                                            $fmt[0] .= $1;
93                                    } else {
94                                            push @fmt,$1;
95                                    }
96                            } elsif ($f =~ s/^([^\d]+\d{0,2})//) {
97                                    if ($#fmt == 0) {
98                                            $fmt[0] .= $1;
99                                    } else {
100                                            push @fmt,$1;
101                                    }
102                            } elsif ($f =~ s/^(\d{1,2})//) {
103                                    if ($#fmt == 0) {
104                                            $fmt[0] .= $1;
105                                    } else {
106                                            push @fmt,$1;
107                                    }
108                            } else {
109                                    print STDERR "unparsed format: $f\n";
110                                    $f = "";
111                            }
112                    }
113                    push @fmt,'' if ($#fmt % 3 != 0);       # add empty suffix
114                    $cache->{format}->{$format} = \@fmt;
115                    
116                    print STDERR "storing format for '$format': [",join("|",@fmt),"]\n";
117    #               print STDERR "storing format for '$format':",Dumper(@fmt),"\n";
118    #               print STDERR Dumper($cache->{format}->{$format});
119            }
120    
121            # now produce actual record
122            my $tmp = $cache->{format}->{$format} || die "no format cache for '$format'";
123            my @fmt = @{$tmp};
124    #       print STDERR "using format for '$format':",Dumper(@fmt),"\n";
125    #       print STDERR "tmp ",Dumper($tmp);
126    #       print STDERR "cache: ",Dumper($cache->{format}->{$format});
127    
128            # prefix
129            my $prefix = shift @fmt;
130            my $sufix;
131            while($#fmt > 1) {
132                    my $f = shift @fmt || die "BUG: field name can't be empty!";
133                    my $sf = shift @fmt;
134    
135                    if ($f eq 'mfn' && $i == 0) {
136                            $display .= $sufix if ($display);
137                            $display .= $row->{mfn};
138                    } else {
139                            my $val = &$func($row,$f,$sf,$i);
140                            if ($val) {
141    #                               print STDERR "val: $val\n";
142                                    my $tmp = cnv_cp($codepage,$val);
143                                    if ($display) {
144                                            $display .= $sufix.$tmp;
145                                    } else {
146                                            $display = $tmp;
147                                    }
148                                  $swish .= $tmp." ";                                  $swish .= $tmp." ";
 #print STDERR " == $tmp";  
149                          }                          }
150                          $prefix = "";                  }
151                  # this might be our local scpeciality -- fields 10 and 11                  $sufix = shift @fmt;
152                  # (as opposed to 010 and 011) so they are strictly listed          }
153                  # here          $display = $prefix.$display.$sufix if ($display);
154                  } elsif ($format =~ s/^(1[01])//) {          print STDERR "format left unused: [",join("|",@fmt),"]\n" if (@fmt);
155                          my $tmp = get_sf($row,$1,undef,$i);  
156                          if ($tmp) {          print STDERR "format: [",join("|",@{$tmp}),"]\n" if (@fmt);
157                                  $display .= $prefix.cnv_cp($tmp);  
158    #       print STDERR "display: $display swish: $swish\n";
159    
160            return ($swish,$display);
161    }
162    
163    #-------------------------------------------------------------
164    
165    sub parse_excel_format {
166            my $format = shift;
167            my $row = shift;
168            my $i = shift;
169            my $codepage = shift;
170    
171            return if ($i > 0);     # Excel doesn't support repeatable fields
172    
173            my $out;
174            my $out_swish;
175    
176            my $prefix = "";
177            if ($format =~ s/^([^A-Z\|]{1,3})//) {
178                    $prefix = $1;
179            }
180    
181            my $display;
182            my $swish;
183    
184            while ($format && length($format) > 0) {
185    #print STDERR "\n#### $format #";
186                    if ($format =~ s/^\|([A-Z]{1,2})\|//) {
187    #print STDERR "--$1-> $format -[",length($format),"] ";
188                            if ($row->{$1}) {
189                                    my $tmp = $row->{$1};
190                                    if ($codepage) {
191                                            $tmp = $codepage->convert($tmp) || warn "excel: $1 '$tmp' can't convert";
192                                    }
193                                    $display .= $prefix . $tmp;
194                                  $swish .= $tmp." ";                                  $swish .= $tmp." ";
195    #print STDERR " == $tmp";
196                          }                          }
197                          $prefix = "";                          $prefix = "";
198                  } elsif ($format =~ s/^mfn//i) {                  } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$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})//) {  
199                          $prefix .= $1 if ($display);                          $prefix .= $1 if ($display);
200                  } else {                  } else {
201                          print STDERR "unparsed format: $format\n";                          print STDERR "unparsed format: $format\n";
202                          $prefix .= $format;                          $prefix .= $format;
203                          $format = "";                          $format = "";
204                  }                  }
205    #print STDERR " display: $display swish: $swish [format: $format]";
206          }          }
207          # add suffix          # add suffix
208          $display .= $prefix if ($display);          $display .= $prefix if ($display);
# Line 96  sub parse_iso_format { Line 212  sub parse_iso_format {
212    
213  #-------------------------------------------------------------  #-------------------------------------------------------------
214    
215  sub parse_excel_format {  sub parse_feed_format {
216          my $format = shift;          my $format = shift;
217          my $row = shift;          my $data = shift;
218          my $i = shift;          my $i = shift;
219          my $codepage = shift;          my $codepage = shift;
220    
221            # XXX feed doesn't support repeatable fields, but they really
222            # should, This is a bug. It should be fixed!
223            return if ($i > 0);
224    
225          my $out;          my $out;
226          my $out_swish;          my $out_swish;
227    
228          my $prefix = "";          my $prefix = "";
229          if ($format =~ s/^([^A-Z\|]{1,3})//) {          if ($format =~ s/^([^\d\|]{1,3})//) {
230                  $prefix = $1;                  $prefix = $1;
231          }          }
232    
# Line 115  sub parse_excel_format { Line 235  sub parse_excel_format {
235    
236          while ($format && length($format) > 0) {          while ($format && length($format) > 0) {
237  #print STDERR "\n#### $format #";  #print STDERR "\n#### $format #";
238                  if ($format =~ s/^\|([A-Z]{1,2})\|//) {                  if ($format =~ s/^\|(\d+)\|//) {
239  #print STDERR "--$1-> $format -[",length($format),"] ";  #print STDERR "--$1-> $format -[",length($format),"] ";
240                          if ($row->{$1}) {                          if ($data->{$1}) {
241                                  my $tmp = $row->{$1};                                  my $tmp = $data->{$1};
242                                  if ($codepage) {                                  if ($codepage) {
243                                          $tmp = $codepage->convert($tmp) || warn "excel: $1 '$tmp' can't convert";                                          $tmp = $codepage->convert($tmp) || warn "feed: $1 '$tmp' can't convert\n";
244                                  }                                  }
245                                  $display .= $prefix . $tmp;                                  $display .= $prefix . $tmp;
246                                  $swish .= $tmp." ";                                  $swish .= $tmp." ";
247  #print STDERR " == $tmp";  #print STDERR " == $tmp";
248                          }                          }
249                          $prefix = "";                          $prefix = "";
250                  } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$2/) {                  } elsif ($format =~ s/^([^\d\|]+)(\|\d+\|)/$2/) {
251                          $prefix .= $1 if ($display);                          $prefix .= $1 if ($display);
252                  } else {                  } else {
253                          print STDERR "unparsed format: $format\n";                          print STDERR "unparsed format: $format\n";
# Line 142  sub parse_excel_format { Line 262  sub parse_excel_format {
262          return ($swish,$display);          return ($swish,$display);
263  }  }
264    
265    #-------------------------------------------------------------
266    
267  1;  1;

Legend:
Removed from v.62  
changed lines
  Added in v.176

  ViewVC Help
Powered by ViewVC 1.1.26