1 /* $Id: recgrs.c,v 1.68 2002-10-08 22:17:44 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include <sys/types.h>
36 #define GRS_MAX_WORD 512
42 struct grs_handler *next;
46 struct grs_handler *handlers;
49 static int read_grs_type (struct grs_handlers *h,
50 struct grs_read_info *p, const char *type,
53 struct grs_handler *gh = h->handlers;
54 const char *cp = strchr (type, '.');
56 if (cp == NULL || cp == type)
58 cp = strlen(type) + type;
62 strcpy (p->type, cp+1);
63 for (gh = h->handlers; gh; gh = gh->next)
65 if (!memcmp (type, gh->type->type, cp-type))
70 gh->clientData = (*gh->type->init)();
72 p->clientData = gh->clientData;
73 *root = (gh->type->read)(p);
74 gh->clientData = p->clientData;
81 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
83 struct grs_handler *gh = (struct grs_handler *) xmalloc (sizeof(*gh));
84 gh->next = h->handlers;
91 static void *grs_init(RecType recType)
93 struct grs_handlers *h = (struct grs_handlers *) xmalloc (sizeof(*h));
96 grs_add_handler (h, recTypeGrs_sgml);
97 grs_add_handler (h, recTypeGrs_regx);
99 grs_add_handler (h, recTypeGrs_tcl);
101 grs_add_handler (h, recTypeGrs_marc);
103 grs_add_handler (h, recTypeGrs_xml);
108 static void grs_destroy(void *clientData)
110 struct grs_handlers *h = (struct grs_handlers *) clientData;
111 struct grs_handler *gh = h->handlers, *gh_next;
116 (*gh->type->destroy)(gh->clientData);
124 1 start element (tag)
126 3 start attr (and attr-exact)
133 static void index_xpath (data1_node *n, struct recExtractCtrl *p,
134 int level, RecWord *wrd, int use)
137 char tag_path_full[1024];
145 wrd->string = n->u.data.data;
146 wrd->length = n->u.data.len;
147 wrd->attrSet = VAL_IDXPATH,
149 if (p->flagShowRecords)
151 printf("%*s data=", (level + 1) * 4, "");
152 for (i = 0; i<wrd->length && i < 8; i++)
153 fputc (wrd->string[i], stdout);
162 for (nn = n; nn; nn = nn->parent)
164 if (nn->which == DATA1N_tag)
166 size_t tlen = strlen(nn->u.tag.tag);
167 if (tlen + flen > (sizeof(tag_path_full)-2))
169 memcpy (tag_path_full + flen, nn->u.tag.tag, tlen);
171 tag_path_full[flen++] = '/';
173 else if (nn->which == DATA1N_root)
177 wrd->string = tag_path_full;
179 wrd->attrSet = VAL_IDXPATH;
181 if (p->flagShowRecords)
183 printf("%*s tag=", (level + 1) * 4, "");
184 for (i = 0; i<wrd->length && i < 40; i++)
185 fputc (wrd->string[i], stdout);
193 (*p->tokenAdd)(wrd); /* index element pag (AKA tag path) */
196 for (xp = n->u.tag.attributes; xp; xp = xp->next)
199 /* attribute (no value) */
202 wrd->string = xp->name;
203 wrd->length = strlen(xp->name);
209 strlen(xp->name) + strlen(xp->value) < sizeof(comb)-2)
211 /* attribute value exact */
212 strcpy (comb, xp->name);
214 strcat (comb, xp->value);
219 wrd->length = strlen(comb);
225 for (xp = n->u.tag.attributes; xp; xp = xp->next)
227 char attr_tag_path_full[1024];
230 sprintf (attr_tag_path_full, "@%s/%.*s",
231 xp->name, int_len, tag_path_full);
235 wrd->string = attr_tag_path_full;
236 wrd->length = strlen(attr_tag_path_full);
243 wrd->string = xp->value;
244 wrd->length = strlen(xp->value);
250 wrd->string = attr_tag_path_full;
251 wrd->length = strlen(attr_tag_path_full);
259 static void index_termlist (data1_node *par, data1_node *n,
260 struct recExtractCtrl *p, int level, RecWord *wrd)
262 data1_termlist *tlist = 0;
263 data1_datatype dtype = DATA1K_string;
265 * cycle up towards the root until we find a tag with an att..
266 * this has the effect of indexing locally defined tags with
267 * the attribute of their ancestor in the record.
270 while (!par->u.tag.element)
271 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
273 if (!par || !(tlist = par->u.tag.element->termlists))
275 if (par->u.tag.element->tag)
276 dtype = par->u.tag.element->tag->kind;
278 for (; tlist; tlist = tlist->next)
281 /* consider source */
284 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
286 wrd->string = n->u.data.data;
287 wrd->length = n->u.data.len;
289 else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
291 wrd->string = n->u.tag.tag;
292 wrd->length = strlen(n->u.tag.tag);
294 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
295 n->which == DATA1N_tag)
297 data1_xattr *p = n->u.tag.attributes;
298 while (p && strcmp (p->name, xattr))
302 wrd->string = p->value;
303 wrd->length = strlen(p->value);
308 if (p->flagShowRecords)
311 printf("%*sIdx: [%s]", (level + 1) * 4, "",
313 printf("%s:%s [%d] %s",
314 tlist->att->parent->name,
315 tlist->att->name, tlist->att->value,
318 for (i = 0; i<wrd->length && i < 8; i++)
319 fputc (wrd->string[i], stdout);
323 fputc ('\n', stdout);
327 wrd->reg_type = *tlist->structure;
328 wrd->attrSet = (int) (tlist->att->parent->reference);
329 wrd->attrUse = tlist->att->locals->local;
336 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
339 for (; n; n = n->next)
341 if (p->flagShowRecords) /* display element description to user */
343 if (n->which == DATA1N_root)
345 printf("%*s", level * 4, "");
346 printf("Record type: '%s'\n", n->u.root.type);
348 else if (n->which == DATA1N_tag)
352 printf("%*s", level * 4, "");
353 if (!(e = n->u.tag.element))
354 printf("Local tag: '%s'\n", n->u.tag.tag);
357 printf("Elm: '%s' ", e->name);
360 data1_tag *t = e->tag;
362 printf("TagNam: '%s' ", t->names->name);
365 printf("%s[%d],", t->tagset->name, t->tagset->type);
368 if (t->which == DATA1T_numeric)
369 printf("%d)", t->value.numeric);
371 printf("'%s')", t->value.string);
378 if (n->which == DATA1N_tag)
380 index_termlist (n, n, p, level, wrd);
381 /* index start tag */
382 assert (n->root->u.root.absyn);
384 if (!n->root->u.root.absyn)
385 index_xpath (n, p, level, wrd, 1);
386 else if (n->root->u.root.absyn->enable_xpath_indexing)
387 index_xpath (n, p, level, wrd, 1);
391 if (dumpkeys(n->child, p, level + 1, wrd) < 0)
395 if (n->which == DATA1N_data)
397 data1_node *par = get_parent_tag(p->dh, n);
399 if (p->flagShowRecords)
401 printf("%*s", level * 4, "");
403 if (n->u.data.len > 256)
404 printf("'%.240s ... %.6s'\n", n->u.data.data,
405 n->u.data.data + n->u.data.len-6);
406 else if (n->u.data.len > 0)
407 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
413 index_termlist (par, n, p, level, wrd);
414 if (!n->root->u.root.absyn)
415 index_xpath (n, p, level, wrd, 1016);
416 else if (n->root->u.root.absyn->enable_xpath_indexing)
417 index_xpath (n, p, level, wrd, 1016);
420 if (n->which == DATA1N_tag)
423 if (!n->root->u.root.absyn)
424 index_xpath (n, p, level, wrd, 2);
425 else if (n->root->u.root.absyn->enable_xpath_indexing)
426 index_xpath (n, p, level, wrd, 2);
429 if (p->flagShowRecords && n->which == DATA1N_root)
431 printf("%*s-------------\n\n", level * 4, "");
437 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
440 int oidtmp[OID_SIZE];
443 oe.proto = PROTO_Z3950;
444 oe.oclass = CLASS_SCHEMA;
447 oe.value = n->u.root.absyn->reference;
449 if ((oid_ent_to_oid (&oe, oidtmp)))
450 (*p->schemaAdd)(p, oidtmp);
454 return dumpkeys(n, p, 0, &wrd);
457 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
461 struct grs_read_info gri;
463 int oidtmp[OID_SIZE];
466 gri.readf = p->readf;
467 gri.seekf = p->seekf;
468 gri.tellf = p->tellf;
471 gri.offset = p->offset;
475 if (read_grs_type (h, &gri, p->subType, &n))
476 return RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER;
478 return RECCTRL_EXTRACT_EOF;
479 oe.proto = PROTO_Z3950;
480 oe.oclass = CLASS_SCHEMA;
482 if (!n->u.root.absyn)
483 return RECCTRL_EXTRACT_ERROR;
487 oe.value = n->u.root.absyn->reference;
488 if ((oid_ent_to_oid (&oe, oidtmp)))
489 (*p->schemaAdd)(p, oidtmp);
492 /* ensure our data1 tree is UTF-8 */
493 data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
496 data1_pr_tree (p->dh, n, stdout);
500 if (dumpkeys(n, p, 0, &wrd) < 0)
502 data1_free_tree(p->dh, n);
503 return RECCTRL_EXTRACT_ERROR_GENERIC;
505 data1_free_tree(p->dh, n);
506 return RECCTRL_EXTRACT_OK;
509 static int grs_extract(void *clientData, struct recExtractCtrl *p)
512 NMEM mem = nmem_create ();
513 struct grs_handlers *h = (struct grs_handlers *) clientData;
515 ret = grs_extract_sub(h, p, mem);
521 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
523 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
525 data1_esetname *eset;
531 case Z_RecordComp_simple:
532 if (c->u.simple->which != Z_ElementSetNames_generic)
533 return 26; /* only generic form supported. Fix this later */
534 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
535 c->u.simple->u.generic)))
537 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
538 return 25; /* invalid esetname */
540 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
541 c->u.simple->u.generic);
544 case Z_RecordComp_complex:
545 if (c->u.complex->generic)
547 /* insert check for schema */
548 if ((p = c->u.complex->generic->elementSpec))
552 case Z_ElementSpec_elementSetName:
554 data1_getesetbyname(dh, n->u.root.absyn,
555 p->u.elementSetName)))
557 logf(LOG_LOG, "Unknown esetname '%s'",
558 p->u.elementSetName);
559 return 25; /* invalid esetname */
561 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
562 p->u.elementSetName);
565 case Z_ElementSpec_externalSpec:
566 if (p->u.externalSpec->which == Z_External_espec1)
568 logf(LOG_DEBUG, "Got Espec-1");
569 espec = p->u.externalSpec-> u.espec1;
573 logf(LOG_LOG, "Unknown external espec.");
574 return 25; /* bad. what is proper diagnostic? */
585 logf (LOG_DEBUG, "Element: Espec-1 match");
586 return data1_doespec1(dh, n, espec);
590 logf (LOG_DEBUG, "Element: all match");
595 /* Add Zebra info in separate namespace ...
598 <metadata xmlns="http://www.indexdata.dk/zebra/">
600 <localnumber>447</localnumber>
601 <filename>records/genera.xml</filename>
606 static void zebra_xml_metadata (struct recRetrieveCtrl *p, data1_node *top,
609 const char *idzebra_ns[3];
610 const char *i2 = "\n ";
611 const char *i4 = "\n ";
614 idzebra_ns[0] = "xmlns";
615 idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
618 data1_mk_text (p->dh, mem, i2, top);
620 n = data1_mk_tag (p->dh, mem, "idzebra", idzebra_ns, top);
622 data1_mk_text (p->dh, mem, "\n", top);
624 data1_mk_text (p->dh, mem, i4, n);
626 data1_mk_tag_data_int (p->dh, n, "size", p->recordSize, mem);
630 data1_mk_text (p->dh, mem, i4, n);
631 data1_mk_tag_data_int (p->dh, n, "score", p->score, mem);
633 data1_mk_text (p->dh, mem, i4, n);
634 data1_mk_tag_data_int (p->dh, n, "localnumber", p->localno, mem);
637 data1_mk_text (p->dh, mem, i4, n);
638 data1_mk_tag_data_text(p->dh, n, "filename", p->fname, mem);
640 data1_mk_text (p->dh, mem, i2, n);
643 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
645 data1_node *node = 0, *onode = 0, *top;
648 int res, selected = 0;
650 struct grs_read_info gri;
652 struct grs_handlers *h = (struct grs_handlers *) clientData;
653 int requested_schema = VAL_NONE;
654 data1_marctab *marctab;
658 gri.readf = p->readf;
659 gri.seekf = p->seekf;
660 gri.tellf = p->tellf;
667 logf (LOG_DEBUG, "grs_retrieve");
668 if (read_grs_type (h, &gri, p->subType, &node))
680 /* ensure our data1 tree is UTF-8 */
681 data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
684 data1_pr_tree (p->dh, node, stdout);
686 top = data1_get_root_tag (p->dh, node);
688 logf (LOG_DEBUG, "grs_retrieve: size");
689 if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
691 dnew->u.data.what = DATA1I_text;
692 dnew->u.data.data = dnew->lbuf;
693 sprintf(dnew->u.data.data, "%d", p->recordSize);
694 dnew->u.data.len = strlen(dnew->u.data.data);
697 tagname = res_get_def(p->res, "tagrank", "rank");
698 if (strcmp(tagname, "0") && p->score >= 0 &&
699 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
701 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
702 dnew->u.data.what = DATA1I_num;
703 dnew->u.data.data = dnew->lbuf;
704 sprintf(dnew->u.data.data, "%d", p->score);
705 dnew->u.data.len = strlen(dnew->u.data.data);
708 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
709 if (strcmp(tagname, "0") && p->localno > 0 &&
710 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
712 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
713 dnew->u.data.what = DATA1I_text;
714 dnew->u.data.data = dnew->lbuf;
716 sprintf(dnew->u.data.data, "%d", p->localno);
717 dnew->u.data.len = strlen(dnew->u.data.data);
720 data1_pr_tree (p->dh, node, stdout);
722 if (p->comp && p->comp->which == Z_RecordComp_complex &&
723 p->comp->u.complex->generic &&
724 p->comp->u.complex->generic->schema)
726 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
728 requested_schema = oe->value;
731 /* If schema has been specified, map if possible, then check that
732 * we got the right one
734 if (requested_schema != VAL_NONE)
736 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
737 for (map = node->u.root.absyn->maptabs; map; map = map->next)
739 if (map->target_absyn_ref == requested_schema)
742 if (!(node = data1_map_record(p->dh, onode, map, mem)))
751 if (node->u.root.absyn &&
752 requested_schema != node->u.root.absyn->reference)
760 * Does the requested format match a known syntax-mapping? (this reflects
761 * the overlap of schema and formatting which is inherent in the MARC
764 yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
765 if (node->u.root.absyn)
766 for (map = node->u.root.absyn->maptabs; map; map = map->next)
768 if (map->target_absyn_ref == p->input_format)
771 if (!(node = data1_map_record(p->dh, onode, map, mem)))
780 yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
781 if (node->u.root.absyn &&
782 node->u.root.absyn->reference != VAL_NONE &&
783 p->input_format == VAL_GRS1)
787 int oidtmp[OID_SIZE];
789 oe.proto = PROTO_Z3950;
790 oe.oclass = CLASS_SCHEMA;
791 oe.value = node->u.root.absyn->reference;
793 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
796 data1_handle dh = p->dh;
800 for (ii = oid; *ii >= 0; ii++)
804 sprintf(p, "%d", *ii);
807 if ((dnew = data1_mk_tag_data_wd(dh, top,
808 "schemaIdentifier", mem)))
810 dnew->u.data.what = DATA1I_oid;
811 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
812 memcpy(dnew->u.data.data, tmp, p - tmp);
813 dnew->u.data.len = p - tmp;
818 logf (LOG_DEBUG, "grs_retrieve: element spec");
819 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
823 data1_free_tree(p->dh, onode);
824 data1_free_tree(p->dh, node);
828 else if (p->comp && !res)
832 data1_pr_tree (p->dh, node, stdout);
834 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
835 switch (p->output_format = (p->input_format != VAL_NONE ?
836 p->input_format : VAL_SUTRS))
839 zebra_xml_metadata (p, top, mem);
842 data1_pr_tree (p->dh, node, stdout);
846 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
848 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
853 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
854 memcpy (new_buf, p->rec_buf, p->rec_len);
855 p->rec_buf = new_buf;
860 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
862 p->diagnostic = 238; /* not available in requested syntax */
864 p->rec_len = (size_t) (-1);
867 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
871 p->rec_len = (size_t) (-1);
874 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
878 p->rec_len = (size_t) (-1);
882 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
883 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
888 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
889 memcpy (new_buf, p->rec_buf, p->rec_len);
890 p->rec_buf = new_buf;
894 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
899 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
900 memcpy (new_buf, p->rec_buf, p->rec_len);
901 p->rec_buf = new_buf;
905 if (!node->u.root.absyn)
910 for (marctab = node->u.root.absyn->marc; marctab;
911 marctab = marctab->next)
912 if (marctab->reference == p->input_format)
920 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
921 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
922 selected, &p->rec_len)))
926 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
927 memcpy (new_buf, p->rec_buf, p->rec_len);
928 p->rec_buf = new_buf;
932 data1_free_tree(p->dh, node);
934 data1_free_tree(p->dh, onode);
939 static struct recType grs_type =
948 RecType recTypeGrs = &grs_type;