464767fa5843269b0ac6ad5f50ea99482d92017e
[webpac] / parse_format.pm
1 #-------------------------------------------------------------
2 #
3 # parse_format(...)
4 #
5
6
7 sub parse_format {
8         my $type = shift || die "parset_format must be called with type!";
9         my $format = shift || die "parse_format must be called with format!";
10         my $row = shift || die "parse_format must be called with row!";
11         my $i = shift || 0;     # isis repeatable number
12         my $codepage = shift || die "parse_format must be called with codepage!";
13         if ($type eq "isis") {
14                 return parse_iso_format($format,$row,$i,$codepage,'isis_sf');
15         } elsif ($type eq "excel") {
16                 return parse_excel_format($format,$row,$i,$codepage);
17         } elsif ($type eq "marc") {
18                 return parse_iso_format($format,$row,$i,$codepage,'marc_sf');
19         } elsif ($type eq "feed") {
20                 return parse_feed_format($format,$row,$i,$codepage);
21         }
22 }
23
24 #-------------------------------------------------------------
25
26 sub parse_iso_format {
27
28         my $format = shift;
29         my $row = shift;
30         my $i = shift;
31         my $codepage = shift;
32
33         my $func = shift || die "need to know which sub-field function to use";
34
35         require $func.".pm";
36
37         my $out;
38         my $out_swish;
39
40         my $prefix = "";
41         if ($format =~ s/^([^\d]+)//) {
42                 $prefix = $1;
43         }
44
45         my $display;
46         my $swish;
47
48         sub cnv_cp {
49                 my $tmp = shift;
50                 if ($codepage) {
51                         $tmp = $codepage->convert($tmp) || print STDERR "$1$2 = '$tmp' can't convert";
52                 }
53                 return $tmp;
54         }
55
56         while ($format) {
57 #print STDERR "\n#### $format";
58                 # this is EBSCO special to support numeric subfield in
59                 # form of 856#3
60                 if ($format =~ s/^(\d\d\d)#*(\w?)//) {
61                         my $tmp = get_sf($row,$1,$2,$i);
62                         if ($tmp) {
63                                 $display .= $prefix.cnv_cp($tmp);
64                                 $swish .= $tmp." ";
65 #print STDERR " == $tmp";
66                         }
67                         $prefix = "";
68                 # this might be our local scpeciality -- fields 10 and 11
69                 # (as opposed to 010 and 011) so they are strictly listed
70                 # here
71                 } elsif ($format =~ s/^(1[01])//) {
72                         my $tmp = get_sf($row,$1,undef,$i);
73                         if ($tmp) {
74                                 $display .= $prefix.cnv_cp($tmp);
75                                 $swish .= $tmp." ";
76                         }
77                         $prefix = "";
78                 } elsif ($format =~ s/^mfn//i) {
79                         $display .= $prefix . $row->{mfn};
80                         $prefix = "";
81                 } elsif ($format =~ s/^([^\d]+)(\d{0,3})/$2/) {
82                         $prefix .= $1 if ($display);
83                 } elsif ($format =~ s/^([^\d]+\d{0,2})//) {
84                         $prefix .= $1 if ($display);
85                 } elsif ($format =~ s/^(\d{1,2})//) {
86                         $prefix .= $1 if ($display);
87                 } else {
88                         print STDERR "unparsed format: $format\n";
89                         $prefix .= $format;
90                         $format = "";
91                 }
92         }
93         # add suffix
94         $display .= $prefix if ($display);
95
96         return ($swish,$display);
97 }
98
99 #-------------------------------------------------------------
100
101 sub parse_excel_format {
102         my $format = shift;
103         my $row = shift;
104         my $i = shift;
105         my $codepage = shift;
106
107         my $out;
108         my $out_swish;
109
110         my $prefix = "";
111         if ($format =~ s/^([^A-Z\|]{1,3})//) {
112                 $prefix = $1;
113         }
114
115         my $display;
116         my $swish;
117
118         while ($format && length($format) > 0) {
119 #print STDERR "\n#### $format #";
120                 if ($format =~ s/^\|([A-Z]{1,2})\|//) {
121 #print STDERR "--$1-> $format -[",length($format),"] ";
122                         if ($row->{$1}) {
123                                 my $tmp = $row->{$1};
124                                 if ($codepage) {
125                                         $tmp = $codepage->convert($tmp) || warn "excel: $1 '$tmp' can't convert";
126                                 }
127                                 $display .= $prefix . $tmp;
128                                 $swish .= $tmp." ";
129 #print STDERR " == $tmp";
130                         }
131                         $prefix = "";
132                 } elsif ($format =~ s/^([^A-Z\|]+)(\|[A-Z]{1,2}\|)/$2/) {
133                         $prefix .= $1 if ($display);
134                 } else {
135                         print STDERR "unparsed format: $format\n";
136                         $prefix .= $format;
137                         $format = "";
138                 }
139 #print STDERR " display: $display swish: $swish [format: $format]";
140         }
141         # add suffix
142         $display .= $prefix if ($display);
143
144         return ($swish,$display);
145 }
146
147 #-------------------------------------------------------------
148
149 sub parse_feed_format {
150         my $format = shift;
151         my $data = shift;
152         my $i = shift;
153         my $codepage = shift;
154
155         my $out;
156         my $out_swish;
157
158         my $prefix = "";
159         if ($format =~ s/^([^\d\|]{1,3})//) {
160                 $prefix = $1;
161         }
162
163         my $display;
164         my $swish;
165
166         while ($format && length($format) > 0) {
167 #print STDERR "\n#### $format #";
168                 if ($format =~ s/^\|(\d+)\|//) {
169 #print STDERR "--$1-> $format -[",length($format),"] ";
170                         if ($data->{$1}) {
171                                 my $tmp = $data->{$1};
172                                 if ($codepage) {
173                                         $tmp = $codepage->convert($tmp) || warn "feed: $1 '$tmp' can't convert\n";
174                                 }
175                                 $display .= $prefix . $tmp;
176                                 $swish .= $tmp." ";
177 #print STDERR " == $tmp";
178                         }
179                         $prefix = "";
180                 } elsif ($format =~ s/^([^\d\|]+)(\|\d+\|)/$2/) {
181                         $prefix .= $1 if ($display);
182                 } else {
183                         print STDERR "unparsed format: $format\n";
184                         $prefix .= $format;
185                         $format = "";
186                 }
187 #print STDERR " display: $display swish: $swish [format: $format]";
188         }
189         # add suffix
190         $display .= $prefix if ($display);
191
192         return ($swish,$display);
193 }
194
195 #-------------------------------------------------------------
196
197 1;