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!"; |
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 |
$swish .= $tmp." "; |
if ($f) { # there is more to parse |
60 |
#print STDERR " == $tmp"; |
push @fmt,$1; |
61 |
|
} else { |
62 |
|
@fmt = ('',$1,undef,''); |
63 |
|
#print STDERR "just one field: $1\n"; |
64 |
} |
} |
65 |
$prefix = ""; |
} else { |
66 |
# this might be our local scpeciality -- fields 10 and 11 |
push @fmt,''; |
67 |
# (as opposed to 010 and 011) so they are strictly listed |
} |
68 |
# here |
|
69 |
} elsif ($format =~ s/^(1[01])//) { |
while ($f) { |
70 |
my $tmp = get_sf($row,$1,undef,$i); |
# print STDERR "\n#### $f"; |
71 |
if ($tmp) { |
# this is EBSCO special to support numeric subfield in |
72 |
$display .= $prefix.cnv_cp($tmp); |
# form of 856#3 |
73 |
$swish .= $tmp." "; |
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 |
$prefix = ""; |
} |
100 |
} elsif ($format =~ s/^mfn//i) { |
push @fmt,'' if ($#fmt % 3 != 0); # add empty suffix |
101 |
$display .= $prefix . $row->{mfn}; |
$cache->{format}->{$format} = \@fmt; |
102 |
$prefix = ""; |
|
103 |
} elsif ($format =~ s/^([^\d]+)(\d{0,3})/$2/) { |
# print STDERR "storing format for '$format': [",join("|",@fmt),"]\n"; |
104 |
$prefix .= $1 if ($display); |
# print STDERR "storing format for '$format':",Dumper(@fmt),"\n"; |
105 |
} elsif ($format =~ s/^([^\d]+\d{0,2})//) { |
# print STDERR Dumper($cache->{format}->{$format}); |
106 |
$prefix .= $1 if ($display); |
} |
107 |
} elsif ($format =~ s/^(\d{1,2})//) { |
|
108 |
$prefix .= $1 if ($display); |
# 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 { |
} else { |
126 |
print STDERR "unparsed format: $format\n"; |
my $val = &$func($row,$f,$sf,$i); |
127 |
$prefix .= $format; |
if ($val) { |
128 |
$format = ""; |
# 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 |
# add suffix |
$display = $prefix.$display.$sufix if ($display); |
141 |
$display .= $prefix if ($display); |
print STDERR "format left unused: [",join("|",@fmt),"]\n" if (@fmt); |
142 |
|
|
143 |
|
# print STDERR "display: $display swish: $swish\n"; |
144 |
|
|
145 |
return ($swish,$display); |
return ($swish,$display); |
146 |
} |
} |
153 |
my $i = shift; |
my $i = shift; |
154 |
my $codepage = shift; |
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 |
|
|
203 |
my $i = shift; |
my $i = shift; |
204 |
my $codepage = shift; |
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; |
my $out; |
211 |
my $out_swish; |
my $out_swish; |
212 |
|
|