1 /* $Id: recgrs.c,v 1.62 2002-08-28 12:47:10 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];
229 sprintf (attr_tag_path_full, "@%s/%.*s",
230 xp->name, flen, tag_path_full);
234 wrd->string = attr_tag_path_full;
235 wrd->length = strlen(attr_tag_path_full);
240 wrd->string = xp->value;
241 wrd->length = strlen(xp->value);
247 wrd->string = attr_tag_path_full;
248 wrd->length = strlen(attr_tag_path_full);
256 static void index_termlist (data1_node *par, data1_node *n,
257 struct recExtractCtrl *p, int level, RecWord *wrd)
259 data1_termlist *tlist = 0;
260 data1_datatype dtype = DATA1K_string;
262 * cycle up towards the root until we find a tag with an att..
263 * this has the effect of indexing locally defined tags with
264 * the attribute of their ancestor in the record.
267 while (!par->u.tag.element)
268 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
270 if (!par || !(tlist = par->u.tag.element->termlists))
272 if (par->u.tag.element->tag)
273 dtype = par->u.tag.element->tag->kind;
275 for (; tlist; tlist = tlist->next)
278 /* consider source */
281 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
283 wrd->string = n->u.data.data;
284 wrd->length = n->u.data.len;
286 else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
288 wrd->string = n->u.tag.tag;
289 wrd->length = strlen(n->u.tag.tag);
291 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
292 n->which == DATA1N_tag)
294 data1_xattr *p = n->u.tag.attributes;
295 while (p && strcmp (p->name, xattr))
299 wrd->string = p->value;
300 wrd->length = strlen(p->value);
305 if (p->flagShowRecords)
308 printf("%*sIdx: [%s]", (level + 1) * 4, "",
310 printf("%s:%s [%d] %s",
311 tlist->att->parent->name,
312 tlist->att->name, tlist->att->value,
315 for (i = 0; i<wrd->length && i < 8; i++)
316 fputc (wrd->string[i], stdout);
320 fputc ('\n', stdout);
324 wrd->reg_type = *tlist->structure;
325 wrd->attrSet = (int) (tlist->att->parent->reference);
326 wrd->attrUse = tlist->att->locals->local;
333 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
336 for (; n; n = n->next)
338 if (p->flagShowRecords) /* display element description to user */
340 if (n->which == DATA1N_root)
342 printf("%*s", level * 4, "");
343 printf("Record type: '%s'\n", n->u.root.type);
345 else if (n->which == DATA1N_tag)
349 printf("%*s", level * 4, "");
350 if (!(e = n->u.tag.element))
351 printf("Local tag: '%s'\n", n->u.tag.tag);
354 printf("Elm: '%s' ", e->name);
357 data1_tag *t = e->tag;
359 printf("TagNam: '%s' ", t->names->name);
362 printf("%s[%d],", t->tagset->name, t->tagset->type);
365 if (t->which == DATA1T_numeric)
366 printf("%d)", t->value.numeric);
368 printf("'%s')", t->value.string);
375 if (n->which == DATA1N_tag)
377 index_termlist (n, n, p, level, wrd);
378 /* index start tag */
379 assert (n->root->u.root.absyn);
381 if (!n->root->u.root.absyn)
382 index_xpath (n, p, level, wrd, 1);
383 else if (n->root->u.root.absyn->enable_xpath_indexing)
384 index_xpath (n, p, level, wrd, 1);
388 if (dumpkeys(n->child, p, level + 1, wrd) < 0)
392 if (n->which == DATA1N_data)
394 data1_node *par = get_parent_tag(p->dh, n);
396 if (p->flagShowRecords)
398 printf("%*s", level * 4, "");
400 if (n->u.data.len > 256)
401 printf("'%.240s ... %.6s'\n", n->u.data.data,
402 n->u.data.data + n->u.data.len-6);
403 else if (n->u.data.len > 0)
404 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
410 index_termlist (par, n, p, level, wrd);
411 if (!n->root->u.root.absyn)
412 index_xpath (n, p, level, wrd, 1016);
413 else if (n->root->u.root.absyn->enable_xpath_indexing)
414 index_xpath (n, p, level, wrd, 1016);
417 if (n->which == DATA1N_tag)
420 if (!n->root->u.root.absyn)
421 index_xpath (n, p, level, wrd, 2);
422 else if (n->root->u.root.absyn->enable_xpath_indexing)
423 index_xpath (n, p, level, wrd, 2);
426 if (p->flagShowRecords && n->which == DATA1N_root)
428 printf("%*s-------------\n\n", level * 4, "");
434 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
437 int oidtmp[OID_SIZE];
440 oe.proto = PROTO_Z3950;
441 oe.oclass = CLASS_SCHEMA;
444 oe.value = n->u.root.absyn->reference;
446 if ((oid_ent_to_oid (&oe, oidtmp)))
447 (*p->schemaAdd)(p, oidtmp);
451 return dumpkeys(n, p, 0, &wrd);
454 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
458 struct grs_read_info gri;
460 int oidtmp[OID_SIZE];
463 gri.readf = p->readf;
464 gri.seekf = p->seekf;
465 gri.tellf = p->tellf;
468 gri.offset = p->offset;
472 if (read_grs_type (h, &gri, p->subType, &n))
473 return RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER;
475 return RECCTRL_EXTRACT_EOF;
476 oe.proto = PROTO_Z3950;
477 oe.oclass = CLASS_SCHEMA;
479 if (!n->u.root.absyn)
480 return RECCTRL_EXTRACT_ERROR;
484 oe.value = n->u.root.absyn->reference;
485 if ((oid_ent_to_oid (&oe, oidtmp)))
486 (*p->schemaAdd)(p, oidtmp);
489 /* ensure our data1 tree is UTF-8 */
490 data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
493 data1_pr_tree (p->dh, n, stdout);
497 if (dumpkeys(n, p, 0, &wrd) < 0)
499 data1_free_tree(p->dh, n);
500 return RECCTRL_EXTRACT_ERROR_GENERIC;
502 data1_free_tree(p->dh, n);
503 return RECCTRL_EXTRACT_OK;
506 static int grs_extract(void *clientData, struct recExtractCtrl *p)
509 NMEM mem = nmem_create ();
510 struct grs_handlers *h = (struct grs_handlers *) clientData;
512 ret = grs_extract_sub(h, p, mem);
518 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
520 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
522 data1_esetname *eset;
528 case Z_RecordComp_simple:
529 if (c->u.simple->which != Z_ElementSetNames_generic)
530 return 26; /* only generic form supported. Fix this later */
531 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
532 c->u.simple->u.generic)))
534 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
535 return 25; /* invalid esetname */
537 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
538 c->u.simple->u.generic);
541 case Z_RecordComp_complex:
542 if (c->u.complex->generic)
544 /* insert check for schema */
545 if ((p = c->u.complex->generic->elementSpec))
549 case Z_ElementSpec_elementSetName:
551 data1_getesetbyname(dh, n->u.root.absyn,
552 p->u.elementSetName)))
554 logf(LOG_LOG, "Unknown esetname '%s'",
555 p->u.elementSetName);
556 return 25; /* invalid esetname */
558 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
559 p->u.elementSetName);
562 case Z_ElementSpec_externalSpec:
563 if (p->u.externalSpec->which == Z_External_espec1)
565 logf(LOG_DEBUG, "Got Espec-1");
566 espec = p->u.externalSpec-> u.espec1;
570 logf(LOG_LOG, "Unknown external espec.");
571 return 25; /* bad. what is proper diagnostic? */
582 logf (LOG_DEBUG, "Element: Espec-1 match");
583 return data1_doespec1(dh, n, espec);
587 logf (LOG_DEBUG, "Element: all match");
592 static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
595 const char *idzebra_ns[7];
597 idzebra_ns[0] = "xmlns:idzebra";
598 idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
601 data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
603 data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
606 data1_mk_tag_data_int (p->dh, top, "idzebra:score",
609 data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
612 data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
616 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
618 data1_node *node = 0, *onode = 0, *top;
621 int res, selected = 0;
623 struct grs_read_info gri;
625 struct grs_handlers *h = (struct grs_handlers *) clientData;
626 int requested_schema = VAL_NONE;
627 data1_marctab *marctab;
631 gri.readf = p->readf;
632 gri.seekf = p->seekf;
633 gri.tellf = p->tellf;
640 logf (LOG_DEBUG, "grs_retrieve");
641 if (read_grs_type (h, &gri, p->subType, &node))
653 /* ensure our data1 tree is UTF-8 */
654 data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
657 data1_pr_tree (p->dh, node, stdout);
659 top = data1_get_root_tag (p->dh, node);
661 logf (LOG_DEBUG, "grs_retrieve: size");
662 if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
664 dnew->u.data.what = DATA1I_text;
665 dnew->u.data.data = dnew->lbuf;
666 sprintf(dnew->u.data.data, "%d", p->recordSize);
667 dnew->u.data.len = strlen(dnew->u.data.data);
670 tagname = res_get_def(p->res, "tagrank", "rank");
671 if (strcmp(tagname, "0") && p->score >= 0 &&
672 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
674 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
675 dnew->u.data.what = DATA1I_num;
676 dnew->u.data.data = dnew->lbuf;
677 sprintf(dnew->u.data.data, "%d", p->score);
678 dnew->u.data.len = strlen(dnew->u.data.data);
681 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
682 if (strcmp(tagname, "0") && p->localno > 0 &&
683 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
685 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
686 dnew->u.data.what = DATA1I_text;
687 dnew->u.data.data = dnew->lbuf;
689 sprintf(dnew->u.data.data, "%d", p->localno);
690 dnew->u.data.len = strlen(dnew->u.data.data);
693 data1_pr_tree (p->dh, node, stdout);
695 if (p->comp && p->comp->which == Z_RecordComp_complex &&
696 p->comp->u.complex->generic &&
697 p->comp->u.complex->generic->schema)
699 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
701 requested_schema = oe->value;
704 /* If schema has been specified, map if possible, then check that
705 * we got the right one
707 if (requested_schema != VAL_NONE)
709 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
710 for (map = node->u.root.absyn->maptabs; map; map = map->next)
712 if (map->target_absyn_ref == requested_schema)
715 if (!(node = data1_map_record(p->dh, onode, map, mem)))
724 if (node->u.root.absyn &&
725 requested_schema != node->u.root.absyn->reference)
733 * Does the requested format match a known syntax-mapping? (this reflects
734 * the overlap of schema and formatting which is inherent in the MARC
737 yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
738 if (node->u.root.absyn)
739 for (map = node->u.root.absyn->maptabs; map; map = map->next)
741 if (map->target_absyn_ref == p->input_format)
744 if (!(node = data1_map_record(p->dh, onode, map, mem)))
753 yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
754 if (node->u.root.absyn &&
755 node->u.root.absyn->reference != VAL_NONE &&
756 p->input_format == VAL_GRS1)
760 int oidtmp[OID_SIZE];
762 oe.proto = PROTO_Z3950;
763 oe.oclass = CLASS_SCHEMA;
764 oe.value = node->u.root.absyn->reference;
766 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
769 data1_handle dh = p->dh;
773 for (ii = oid; *ii >= 0; ii++)
777 sprintf(p, "%d", *ii);
782 if ((dnew = data1_mk_tag_data_wd(dh, node,
783 "schemaIdentifier", mem)))
785 dnew->u.data.what = DATA1I_oid;
786 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
787 memcpy(dnew->u.data.data, tmp, p - tmp);
788 dnew->u.data.len = p - tmp;
793 logf (LOG_DEBUG, "grs_retrieve: element spec");
794 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
798 data1_free_tree(p->dh, onode);
799 data1_free_tree(p->dh, node);
803 else if (p->comp && !res)
807 data1_pr_tree (p->dh, node, stdout);
809 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
810 switch (p->output_format = (p->input_format != VAL_NONE ?
811 p->input_format : VAL_SUTRS))
814 add_idzebra_info (p, top, mem);
817 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
819 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
824 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
825 memcpy (new_buf, p->rec_buf, p->rec_len);
826 p->rec_buf = new_buf;
831 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
833 p->diagnostic = 238; /* not available in requested syntax */
835 p->rec_len = (size_t) (-1);
838 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
842 p->rec_len = (size_t) (-1);
845 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
849 p->rec_len = (size_t) (-1);
853 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
854 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
859 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
860 memcpy (new_buf, p->rec_buf, p->rec_len);
861 p->rec_buf = new_buf;
865 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
870 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
871 memcpy (new_buf, p->rec_buf, p->rec_len);
872 p->rec_buf = new_buf;
876 if (!node->u.root.absyn)
881 for (marctab = node->u.root.absyn->marc; marctab;
882 marctab = marctab->next)
883 if (marctab->reference == p->input_format)
891 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
892 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
893 selected, &p->rec_len)))
897 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
898 memcpy (new_buf, p->rec_buf, p->rec_len);
899 p->rec_buf = new_buf;
903 data1_free_tree(p->dh, node);
905 data1_free_tree(p->dh, onode);
910 static struct recType grs_type =
919 RecType recTypeGrs = &grs_type;