2dbbe131accc966ede26ee8c141fd0b96f8bec9e
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Utils.pm
1 # $Id: Utils.pm,v 1.33 2007-06-27 10:44:57 mike Exp $
2
3 package ZOOM::IRSpy::Utils;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use Exporter 'import';
10 our @EXPORT_OK = qw(utf8param
11                     isodate
12                     xml_encode 
13                     cql_quote
14                     cql_target
15                     irspy_xpath_context
16                     irspy_make_identifier
17                     irspy_record2identifier
18                     irspy_identifier2target
19                     modify_xml_document
20                     bib1_access_point
21                     render_record);
22
23 use XML::LibXML;
24 use XML::LibXML::XPathContext;
25 use Encode;
26 use Encode qw(is_utf8);
27
28
29 our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
30
31
32 # Utility functions follow, exported for use of web UI
33 sub utf8param {
34     my($r, $key, $value) = @_;
35     die "utf8param() called with value '$value'" if defined $value;
36
37     my $raw = $r->param($key);
38     return undef if !defined $raw;
39     my $cooked = decode_utf8($raw);
40     warn "converted '$raw' to '", $cooked, "'\n" if $cooked ne $raw;
41     return $cooked;
42 }
43
44
45 sub isodate {
46     my($time) = @_;
47
48     my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
49     return sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
50                    $year+1900, $mon+1, $mday, $hour, $min, $sec);
51 }
52
53
54 # I can't -- just can't, can't, can't -- believe that this function
55 # isn't provided by one of the core XML modules.  But the evidence all
56 # says that it's not: among other things, XML::Generator and
57 # Template::Plugin both roll their own.  So I will do likewise.  D'oh!
58 #
59 sub xml_encode {
60     my($text, $fallback, $opts) = @_;
61     if (!defined $opts && ref $fallback) {
62         # The second and third arguments are both optional
63         $opts = $fallback;
64         $fallback = undef;
65     }
66     $opts = {} if !defined $opts;
67
68     $text = $fallback if !defined $text;
69     use Carp;
70     confess "xml_encode(): text and fallback both undefined"
71         if !defined $text;
72
73     $text =~ s/&/&/g;
74     $text =~ s/</&lt;/g;
75     $text =~ s/>/&gt;/g;
76     # Internet Explorer can't display &apos; (!) so don't create it
77     #$text =~ s/['']/&apos;/g;
78     $text =~ s/[""]/&quot;/g;
79     $text =~ s/ /&nbsp;/g if $opts->{nbsp};
80
81     return $text;
82 }
83
84
85 # Quotes a term for use in a CQL query
86 sub cql_quote {
87     my($term) = @_;
88
89     $term =~ s/([""\\*?])/\\$1/g;
90     $term = qq["$term"] if $term =~ /[\s""\/]/;
91     return $term;
92 }
93
94
95 # Makes a CQL query that finds a specified target.  Arguments may be
96 # either an ID alone, or a (host, port, db) triple.
97 sub cql_target {
98     my($protocol, $host, $port, $db) = @_;
99
100     my $id;
101     if (defined $host) {
102         $id = irspy_make_identifier($protocol, $host, $port, $db);
103     } else {
104         $id = $protocol;
105     }
106
107     return "rec.id=" . cql_quote($id);
108 }
109
110
111 # PRIVATE to irspy_namespace() and irspy_xpath_context()
112 my %_namespaces = (
113                    e => 'http://explain.z3950.org/dtd/2.0/',
114                    i => $IRSPY_NS,
115                    );
116
117
118 sub irspy_namespace {
119     my($prefix) = @_;
120
121     use Carp;
122     confess "irspy_namespace(undef)" if !defined $prefix;
123     my $uri = $_namespaces{$prefix};
124     die "irspy_namespace(): no URI for namespace prefix '$prefix'"
125         if !defined $uri;
126
127     return $uri;
128 }
129
130
131 sub irspy_xpath_context {
132     my($record) = @_;
133
134     if (ref $record && $record->isa("ZOOM::Record")) {
135         $record = $record->render();
136     }
137
138     my $root;
139     if (ref $record) {
140         $root = $record;
141     } else {
142         my $parser = new XML::LibXML();
143         my $doc = $parser->parse_string($record);
144         $root = $doc->getDocumentElement();
145     }
146
147     my $xc = XML::LibXML::XPathContext->new($root);
148     foreach my $prefix (keys %_namespaces) {
149         $xc->registerNs($prefix, $_namespaces{$prefix});
150     }
151     return $xc;
152 }
153
154
155 # Construct an opaque identifier from its components.  Although it's
156 # trivial, this is needed in so many places that it really needs to be
157 # factored out.
158 #
159 # This is the converse of _parse_target_string() in IRSpy.pm, which
160 # should be renamed and moved into this package.
161 #
162 sub irspy_make_identifier {
163     my($protocol, $host, $port, $dbname) = @_;
164
165     die "irspy_make_identifier(" . join(", ", map { "'$_'" } @_).
166         "): wrong number of arguments" if @_ != 4;
167
168     die "irspy_make_identifier(): protocol undefined" if !defined $protocol;
169     die "irspy_make_identifier(): host undefined" if !defined $host;
170     die "irspy_make_identifier(): port undefined" if !defined $port;
171     die "irspy_make_identifier(): dbname undefined" if !defined $dbname;
172
173     return "$protocol:$host:$port/$dbname";
174 }
175
176
177 # Returns the opaque identifier of an IRSpy record based on the
178 # XPathContext'ed DOM object, as returned by irspy_xpath_context().
179 # This is doing the same thing as irspy_make_identifier() but from a
180 # record rather than a set of parameters.
181 #
182 sub irspy_record2identifier {
183     my($xc) = @_;
184
185     ### Must be kept the same as is used in ../../../zebra/*.xsl
186     return $xc->find("concat(e:serverInfo/\@protocol, ':',
187                              e:serverInfo/e:host, ':',
188                              e:serverInfo/e:port, '/',
189                              e:serverInfo/e:database)");
190 }
191
192
193 # Transforms an IRSpy opqaue identifier, as returned from
194 # irspy_make_identifier() or irspy_record2identifier(), into a YAZ
195 # target-string suitable for feeding to ZOOM.  Before we introduced
196 # the protocol element at the start of the identifier string, this was
197 # a null transform; now we have to be a bit cleverer.
198 #
199 sub irspy_identifier2target {
200     my $res = _irspy_identifier2target(@_);
201     #carp "converted ID '@_' to target '$res'";
202     return $res;
203 }
204
205 sub _irspy_identifier2target {
206     my($id) = @_;
207
208     my($protocol, $target) = ($id =~ /(.*?):(.*)/);
209     if (uc($protocol) eq "Z39.50") {
210         return "tcp:$target";
211     } elsif (uc($protocol) eq "SRU") {
212         return "sru=get,http:$target";
213     } elsif (uc($protocol) eq "SRW") {
214         return "sru=srw,http:$target";
215     }
216
217     warn "unrecognised protocol '$protocol' in ID $id";
218     return $target;
219 }
220
221
222 sub modify_xml_document {
223     my($xc, $fieldsByKey, $data) = @_;
224
225     my @changes = ();
226     foreach my $key (keys %$data) {
227         my $value = $data->{$key};
228         my $ref = $fieldsByKey->{$key} or die "no field '$key'";
229         my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
230         #print "Considering $key='$value' ($xpath)<br/>\n";
231         my @nodes = $xc->findnodes($xpath);
232         if (@nodes) {
233             warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
234             my $node = $nodes[0];
235
236             if ($node->isa("XML::LibXML::Attr")) {
237                 if ($value ne $node->getValue()) {
238                     $node->setValue($value);
239                     push @changes, $ref;
240                     #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
241                 }
242             } elsif ($node->isa("XML::LibXML::Element")) {
243                 # The contents could be any mixture of text and
244                 # comments and maybe even other crud such as processing
245                 # instructions.  The simplest thing is just to throw it all
246                 # away and start again, making a single Text node the
247                 # canonical representation.  But before we do that,
248                 # we'll check whether the element is already
249                 # canonical, to determine whether our change is a
250                 # no-op.
251                 my $old = "";
252                 my @children = $node->childNodes();
253                 if (@children == 1) {
254                     my $child = $node->firstChild();
255                     if (ref $child && ref $child eq "XML::LibXML::Text") {
256                         $old = $child->getData();
257                         print STDERR "child='$child', old=", _renderchars($old), "\n"
258                             if $key eq "title";
259                     }
260                 }
261                 next if $value eq $old;
262
263                 $node->removeChildNodes();
264                 my $child = new XML::LibXML::Text($value);
265                 $node->appendChild($child);
266                 push @changes, $ref;
267                 print STDERR "Elem $key ($xpath): ", _renderchars($old), " -> '", _renderchars($value), "\n";
268             } else {
269                 warn "unexpected node type $node";
270             }
271
272         } else {
273             next if !$value; # No need to create a new empty node
274             my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/;
275             dom_add_node($xc, $ppath, $selector, $value, @addAfter);
276             #print "New $key ($xpath) = '$value'<br/>\n";
277             push @changes, $ref;
278         }
279     }
280
281     return @changes;
282 }
283
284
285 sub _renderchars {
286     my($text) = @_;
287
288     return "'" . $text . "'", " (", join(" ", map {ord($_)} split //, $text), "), is_utf8=" , is_utf8($text);
289 }
290
291
292 sub dom_add_node {
293     my($xc, $ppath, $selector, $value, @addAfter) = @_;
294
295     #print "Adding $selector='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
296     my $node = find_or_make_node($xc, $ppath, 0);
297     die "couldn't find or make node '$node'" if !defined $node;
298
299     my $is_attr = ($selector =~ s/^@//);
300     my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
301     #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
302     if ($is_attr) {
303         if (defined $prefix) {
304             ### This seems to no-op (thank, DOM!) but I have have no
305             # idea, and it's not needed for IRSpy, so I am not going
306             # to debug it now.
307             $node->setAttributeNS(irspy_namespace($prefix),
308                                   $simpleSel, $value);
309         } else {
310             $node->setAttribute($simpleSel, $value);
311         }
312         return;
313     }
314
315     my $new = new XML::LibXML::Element($simpleSel);
316     $new->setNamespace(irspy_namespace($prefix), $prefix)
317         if defined $prefix;
318
319     $new->appendText($value);
320     foreach my $predecessor (reverse @addAfter) {
321         my($child) = $xc->findnodes($predecessor, $node);
322         if (defined $child) {
323             $node->insertAfter($new, $child);
324             #warn "Added after '$predecessor'";
325             return;
326         }
327     }
328
329     # Didn't find any of the nodes that are supposed to precede the
330     # new one, so we need to insert the new node as the first of the
331     # parent's children.  However *sigh* there is no prependChild()
332     # analogous to appendChild(), so we have to go the long way round.
333     my @children = $node->childNodes();
334     if (@children) {
335         $node->insertBefore($new, $children[0]);
336         #warn "Added new first child";
337     } else {
338         $node->appendChild($new);
339         #warn "Added new only child";
340     }
341
342     if (0) {
343         my $text = xml_encode(inheritance_tree($xc));
344         $text =~ s/\n/<br\/>$&/sg;
345         print "<pre>$text</pre>\n";
346     }
347 }
348
349
350 sub find_or_make_node {
351     my($xc, $path, $recursion_level) = @_;
352
353     die "deep recursion in find_or_make_node($path)"
354         if $recursion_level == 10;
355     $path = "." if $path eq "";
356
357     my @nodes = $xc->findnodes($path);
358     if (@nodes == 0) {
359         # Oh dear, the parent node doesn't exist.  We could make it,
360         my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
361         $ppath = "" if !defined $ppath;
362         #warn "path='$path', ppath='$ppath', element='$element'";
363         #warn "no node '$path': making it";
364         my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
365
366         my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
367         #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
368         my $new = new XML::LibXML::Element($nsElem);
369         if (defined $prefix) {
370             #warn "setNamespace($prefix)";
371             $new->setNamespace(irspy_namespace($prefix), $prefix);
372         }
373
374         $parent->appendChild($new);
375         return $new;
376     }
377     warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
378     return $nodes[0];
379 }
380
381
382 sub inheritance_tree {
383     my($type, $level) = @_;
384     $level = 0 if !defined $level;
385     return "Woah!  Too deep, man!\n" if $level > 20;
386
387     $type = ref $type if ref $type;
388     my $text = "";
389     $text = "--> " if $level == 0;
390     $text .= ("\t" x $level) . "$type\n";
391     my @ISA = eval "\@${type}::ISA";
392     foreach my $superclass (@ISA) {
393         $text .= inheritance_tree($superclass, $level+1);
394     }
395
396     return $text;
397 }
398
399
400 # This function is made available in xslt using the register_function call
401 sub xslt_strcmp {
402     my ($arg1, $arg2) = @_;
403     return ($arg1->to_literal()) cmp ($arg2->to_literal());
404 }
405
406
407 ### It feels like this should be in YAZ, exported via ZOOM-Perl.
408 my %_bib1_access_point = (
409         1 =>    "Personal name",
410         2 =>    "Corporate name",
411         3 =>    "Conference name",
412         4 =>    "Title",
413         5 =>    "Title series",
414         6 =>    "Title uniform",
415         7 =>    "ISBN",
416         8 =>    "ISSN",
417         9 =>    "LC card number",
418         10 =>   "BNB card no.",
419         11 =>   "BGF number",
420         12 =>   "Local number",
421         13 =>   "Dewey classification",
422         14 =>   "UDC classification",
423         15 =>   "Bliss classification",
424         16 =>   "LC call number",
425         17 =>   "NLM call number",
426         18 =>   "NAL call number",
427         19 =>   "MOS call number",
428         20 =>   "Local classification",
429         21 =>   "Subject heading",
430         22 =>   "Subject Rameau",
431         23 =>   "BDI index subject",
432         24 =>   "INSPEC subject",
433         25 =>   "MESH subject",
434         26 =>   "PA subject",
435         27 =>   "LC subject heading",
436         28 =>   "RVM subject heading",
437         29 =>   "Local subject index",
438         30 =>   "Date",
439         31 =>   "Date of publication",
440         32 =>   "Date of acquisition",
441         33 =>   "Title key",
442         34 =>   "Title collective",
443         35 =>   "Title parallel",
444         36 =>   "Title cover",
445         37 =>   "Title added title page",
446         38 =>   "Title caption",
447         39 =>   "Title running",
448         40 =>   "Title spine",
449         41 =>   "Title other variant",
450         42 =>   "Title former",
451         43 =>   "Title abbreviated",
452         44 =>   "Title expanded",
453         45 =>   "Subject precis",
454         46 =>   "Subject rswk",
455         47 =>   "Subject subdivision",
456         48 =>   "No. nat'l biblio.",
457         49 =>   "No. legal deposit",
458         50 =>   "No. govt pub.",
459         51 =>   "No. music publisher",
460         52 =>   "Number db",
461         53 =>   "Number local call",
462         54 =>   "Code--language",
463         55 =>   "Code--geographic area",
464         56 =>   "Code--institution",
465         57 =>   "Name and title *",
466         58 =>   "Name geographic",
467         59 =>   "Place publication",
468         60 =>   "CODEN",
469         61 =>   "Microform generation",
470         62 =>   "Abstract",
471         63 =>   "Note",
472         1000 => "Author-title",
473         1001 => "Record type",
474         1002 => "Name",
475         1003 => "Author",
476         1004 => "Author-name personal",
477         1005 => "Author-name corporate",
478         1006 => "Author-name conference",
479         1007 => "Identifier--standard",
480         1008 => "Subject--LC children's",
481         1009 => "Subject name -- personal",
482         1010 => "Body of text",
483         1011 => "Date/time added to db",
484         1012 => "Date/time last modified",
485         1013 => "Authority/format id",
486         1014 => "Concept-text",
487         1015 => "Concept-reference",
488         1016 => "Any",
489         1017 => "Server-choice",
490         1018 => "Publisher",
491         1019 => "Record-source",
492         1020 => "Editor",
493         1021 => "Bib-level",
494         1022 => "Geographic-class",
495         1023 => "Indexed-by",
496         1024 => "Map-scale",
497         1025 => "Music-key",
498         1026 => "Related-periodical",
499         1027 => "Report-number",
500         1028 => "Stock-number",
501         1030 => "Thematic-number",
502         1031 => "Material-type",
503         1032 => "Doc-id",
504         1033 => "Host-item",
505         1034 => "Content-type",
506         1035 => "Anywhere",
507         1036 => "Author-Title-Subject",
508         1032 => "Doc-id (semantic definition change)",
509         1037 => "SICI",
510         1038 => "Abstract-language",
511         1039 => "Application-kind",
512         1040 => "Classification",
513         1041 => "Classification-basic",
514         1042 => "Classification-local-record",
515         1043 => "Enzyme",
516         1044 => "Possessing-institution",
517         1045 => "Record-linking",
518         1046 => "Record-status",
519         1047 => "Treatment",
520         1048 => "Control-number-GKD",
521         1049 => "Control-number-linking",
522         1050 => "Control-number-PND",
523         1051 => "Control-number-SWD",
524         1052 => "Control-number-ZDB",
525         1053 => "Country-publication (country of Publication)",
526         1054 => "Date-conference (meeting date)",
527         1055 => "Date-record-status",
528         1056 => "Dissertation-information",
529         1057 => "Meeting-organizer",
530         1058 => "Note-availability",
531         1059 => "Number-CAS-registry (CAS registry number)",
532         1060 => "Number-document (document number)",
533         1061 => "Number-local-accounting",
534         1062 => "Number-local-acquisition",
535         1063 => "Number-local-call-copy-specific",
536         1064 => "Number-of-reference (reference count)",
537         1065 => "Number-norm",
538         1066 => "Number-volume",
539         1067 => "Place-conference (meeting location)",
540         1068 => "Reference (references and footnotes)",
541         1069 => "Referenced-journal (reference work)",
542         1070 => "Section-code",
543         1071 => "Section-heading",
544         1072 => "Subject-GOO",
545         1073 => "Subject-name-conference",
546         1074 => "Subject-name-corporate",
547         1075 => "Subject-genre/form",
548         1076 => "Subject-name-geographical",
549         1077 => "Subject--chronological",
550         1078 => "Subject--title",
551         1079 => "Subject--topical",
552         1080 => "Subject-uncontrolled",
553         1081 => "Terminology-chemical (chemical name)",
554         1082 => "Title-translated",
555         1083 => "Year-of-beginning",
556         1084 => "Year-of-ending",
557         1085 => "Subject-AGROVOC",
558         1086 => "Subject-COMPASS",
559         1087 => "Subject-EPT",
560         1088 => "Subject-NAL",
561         1089 => "Classification-BCM",
562         1090 => "Classification-DB",
563         1091 => "Identifier-ISRC",
564         1092 => "Identifier-ISMN",
565         1093 => "Identifier-ISRN",
566         1094 => "Identifier-DOI",
567         1095 => "Code-language-original",
568         1096 => "Title-later",
569         1097 => "DC-Title",
570         1098 => "DC-Creator",
571         1099 => "DC-Subject",
572         1100 => "DC-Description",
573         1101 => "DC-Publisher",
574         1102 => "DC-Date",
575         1103 => "DC-ResourceType",
576         1104 => "DC-ResourceIdentifier",
577         1105 => "DC-Language",
578         1106 => "DC-OtherContributor",
579         1107 => "DC-Format",
580         1108 => "DC-Source",
581         1109 => "DC-Relation",
582         1110 => "DC-Coverage",
583         1111 => "DC-RightsManagement",
584         1112 => "Controlled Subject Index",
585         1113 => "Subject Thesaurus",
586         1114 => "Index Terms -- Controlled",
587         1115 => "Controlled Term",
588         1116 => "Spatial Domain",
589         1117 => "Bounding Coordinates",
590         1118 => "West Bounding Coordinate",
591         1119 => "East Bounding Coordinate",
592         1120 => "North Bounding Coordinate",
593         1121 => "South Bounding Coordinate",
594         1122 => "Place",
595         1123 => "Place Keyword Thesaurus",
596         1124 => "Place Keyword",
597         1125 => "Time Period",
598         1126 => "Time Period Textual",
599         1127 => "Time Period Structured",
600         1128 => "Beginning Date",
601         1129 => "Ending Date",
602         1130 => "Availability",
603         1131 => "Distributor",
604         1132 => "Distributor Name",
605         1133 => "Distributor Organization",
606         1134 => "Distributor Street Address",
607         1135 => "Distributor City",
608         1136 => "Distributor State or Province",
609         1137 => "Distributor Zip or Postal Code",
610         1138 => "Distributor Country",
611         1139 => "Distributor Network Address",
612         1140 => "Distributor Hours of Service",
613         1141 => "Distributor Telephone",
614         1142 => "Distributor Fax",
615         1143 => "Resource Description",
616         1144 => "Order Process",
617         1145 => "Order Information",
618         1146 => "Cost",
619         1147 => "Cost Information",
620         1148 => "Technical Prerequisites",
621         1149 => "Available Time Period",
622         1150 => "Available Time Textual",
623         1151 => "Available Time Structured",
624         1152 => "Available Linkage",
625         1153 => "Linkage Type",
626         1154 => "Linkage",
627         1155 => "Sources of Data",
628         1156 => "Methodology",
629         1157 => "Access Constraints",
630         1158 => "General Access Constraints",
631         1159 => "Originator Dissemination Control",
632         1160 => "Security Classification Control",
633         1161 => "Use Constraints",
634         1162 => "Point of Contact",
635         1163 => "Contact Name",
636         1164 => "Contact Organization",
637         1165 => "Contact Street Address",
638         1166 => "Contact City",
639         1167 => "Contact State or Province",
640         1168 => "Contact Zip or Postal Code",
641         1169 => "Contact Country",
642         1170 => "Contact Network Address",
643         1171 => "Contact Hours of Service",
644         1172 => "Contact Telephone",
645         1173 => "Contact Fax",
646         1174 => "Supplemental Information",
647         1175 => "Purpose",
648         1176 => "Agency Program",
649         1177 => "Cross Reference",
650         1178 => "Cross Reference Title",
651         1179 => "Cross Reference Relationship",
652         1180 => "Cross Reference Linkage",
653         1181 => "Schedule Number",
654         1182 => "Original Control Identifier",
655         1183 => "Language of Record",
656         1184 => "Record Review Date",
657         1185 => "Performer",
658         1186 => "Performer-Individual",
659         1187 => "Performer-Group",
660         1188 => "Instrumentation",
661         1189 => "Instrumentation-Original",
662         1190 => "Instrumentation-Current",
663         1191 => "Arrangement",
664         1192 => "Arrangement-Original",
665         1193 => "Arrangement-Current",
666         1194 => "Musical Key-Original",
667         1195 => "Musical Key-Current",
668         1196 => "Date-Composition",
669         1197 => "Date-Recording",
670         1198 => "Place-Recording",
671         1199 => "Country-Recording",
672         1200 => "Number-ISWC",
673         1201 => "Number-Matrix",
674         1202 => "Number-Plate",
675         1203 => "Classification-McColvin",
676         1204 => "Duration",
677         1205 => "Number-Copies",
678         1206 => "Musical Theme",
679         1207 => "Instruments - total number",
680         1208 => "Instruments - distinct number",
681         1209 => "Identifier - URN",
682         1210 => "Sears Subject Heading",
683         1211 => "OCLC Number",
684         1212 => "Composition",
685         1213 => "Intellectual level",
686         1214 => "EAN",
687         1215 => "NLC",
688         1216 => "CRCS",
689         1217 => "Nationality",
690         1218 => "Equinox",
691         1219 => "Compression",
692         1220 => "Format",
693         1221 => "Subject - occupation",
694         1222 => "Subject - function",
695         1223 => "Edition",
696 );
697
698 sub bib1_access_point {
699     my($ap) = @_;
700
701     return $_bib1_access_point{$ap} ||
702         "unknown BIB-1 attribute '$ap'";
703 }
704
705
706 sub render_record {
707     my($rs, $which, $elementSetName) = @_;
708
709     # There is a slight race condition here on the element-set name,
710     # but it shouldn't be a problem as this is (currently) only called
711     # from parts of the program that run single-threaded.
712     my $old = $rs->option(elementSetName => $elementSetName);
713     my $rec = $rs->record($which);
714     $rs->option(elementSetName => $old);
715
716     return $rec->render();
717 }
718
719
720 1;