1 /* $Id: recgrs.c,v 1.59 2002-08-02 19:26:56 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
27 #include <sys/types.h>
38 #define GRS_MAX_WORD 512
44 struct grs_handler *next;
48 struct grs_handler *handlers;
51 static int read_grs_type (struct grs_handlers *h,
52 struct grs_read_info *p, const char *type,
55 struct grs_handler *gh = h->handlers;
56 const char *cp = strchr (type, '.');
58 if (cp == NULL || cp == type)
60 cp = strlen(type) + type;
64 strcpy (p->type, cp+1);
65 for (gh = h->handlers; gh; gh = gh->next)
67 if (!memcmp (type, gh->type->type, cp-type))
72 gh->clientData = (*gh->type->init)();
74 p->clientData = gh->clientData;
75 *root = (gh->type->read)(p);
76 gh->clientData = p->clientData;
83 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
85 struct grs_handler *gh = (struct grs_handler *) xmalloc (sizeof(*gh));
86 gh->next = h->handlers;
93 static void *grs_init(RecType recType)
95 struct grs_handlers *h = (struct grs_handlers *) xmalloc (sizeof(*h));
98 grs_add_handler (h, recTypeGrs_sgml);
99 grs_add_handler (h, recTypeGrs_regx);
101 grs_add_handler (h, recTypeGrs_tcl);
103 grs_add_handler (h, recTypeGrs_marc);
105 grs_add_handler (h, recTypeGrs_xml);
110 static void grs_destroy(void *clientData)
112 struct grs_handlers *h = (struct grs_handlers *) clientData;
113 struct grs_handler *gh = h->handlers, *gh_next;
118 (*gh->type->destroy)(gh->clientData);
126 1 start element (tag)
128 3 start attr (and attr-exact)
135 static void index_xpath (data1_node *n, struct recExtractCtrl *p,
136 int level, RecWord *wrd, int use)
139 char tag_path_full[1024];
147 wrd->string = n->u.data.data;
148 wrd->length = n->u.data.len;
149 wrd->attrSet = VAL_IDXPATH,
151 if (p->flagShowRecords)
153 printf("%*s data=", (level + 1) * 4, "");
154 for (i = 0; i<wrd->length && i < 8; i++)
155 fputc (wrd->string[i], stdout);
164 for (nn = n; nn; nn = nn->parent)
166 if (nn->which == DATA1N_tag)
168 size_t tlen = strlen(nn->u.tag.tag);
169 if (tlen + flen > (sizeof(tag_path_full)-2))
171 memcpy (tag_path_full + flen, nn->u.tag.tag, tlen);
173 tag_path_full[flen++] = '/';
175 else if (nn->which == DATA1N_root)
179 wrd->string = tag_path_full;
181 wrd->attrSet = VAL_IDXPATH;
183 if (p->flagShowRecords)
185 printf("%*s tag=", (level + 1) * 4, "");
186 for (i = 0; i<wrd->length && i < 40; i++)
187 fputc (wrd->string[i], stdout);
195 (*p->tokenAdd)(wrd); /* index element pag (AKA tag path) */
198 for (xp = n->u.tag.attributes; xp; xp = xp->next)
201 /* attribute (no value) */
204 wrd->string = xp->name;
205 wrd->length = strlen(xp->name);
211 strlen(xp->name) + strlen(xp->value) < sizeof(comb)-2)
213 /* attribute value exact */
214 strcpy (comb, xp->name);
216 strcat (comb, xp->value);
221 wrd->length = strlen(comb);
227 for (xp = n->u.tag.attributes; xp; xp = xp->next)
229 char attr_tag_path_full[1024];
231 sprintf (attr_tag_path_full, "@%s/%.*s",
232 xp->name, flen, tag_path_full);
236 wrd->string = attr_tag_path_full;
237 wrd->length = strlen(attr_tag_path_full);
242 wrd->string = xp->value;
243 wrd->length = strlen(xp->value);
249 wrd->string = attr_tag_path_full;
250 wrd->length = strlen(attr_tag_path_full);
258 static void index_termlist (data1_node *par, data1_node *n,
259 struct recExtractCtrl *p, int level, RecWord *wrd)
261 data1_termlist *tlist = 0;
262 data1_datatype dtype = DATA1K_string;
264 * cycle up towards the root until we find a tag with an att..
265 * this has the effect of indexing locally defined tags with
266 * the attribute of their ancestor in the record.
269 while (!par->u.tag.element)
270 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
272 if (!par || !(tlist = par->u.tag.element->termlists))
274 if (par->u.tag.element->tag)
275 dtype = par->u.tag.element->tag->kind;
277 for (; tlist; tlist = tlist->next)
280 /* consider source */
283 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
285 wrd->string = n->u.data.data;
286 wrd->length = n->u.data.len;
288 else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
290 wrd->string = n->u.tag.tag;
291 wrd->length = strlen(n->u.tag.tag);
293 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
294 n->which == DATA1N_tag)
296 data1_xattr *p = n->u.tag.attributes;
297 while (p && strcmp (p->name, xattr))
301 wrd->string = p->value;
302 wrd->length = strlen(p->value);
307 if (p->flagShowRecords)
310 printf("%*sIdx: [%s]", (level + 1) * 4, "",
312 printf("%s:%s [%d] %s",
313 tlist->att->parent->name,
314 tlist->att->name, tlist->att->value,
317 for (i = 0; i<wrd->length && i < 8; i++)
318 fputc (wrd->string[i], stdout);
322 fputc ('\n', stdout);
326 wrd->reg_type = *tlist->structure;
327 wrd->attrSet = (int) (tlist->att->parent->reference);
328 wrd->attrUse = tlist->att->locals->local;
335 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
338 for (; n; n = n->next)
340 if (p->flagShowRecords) /* display element description to user */
342 if (n->which == DATA1N_root)
344 printf("%*s", level * 4, "");
345 printf("Record type: '%s'\n", n->u.root.type);
347 else if (n->which == DATA1N_tag)
351 printf("%*s", level * 4, "");
352 if (!(e = n->u.tag.element))
353 printf("Local tag: '%s'\n", n->u.tag.tag);
356 printf("Elm: '%s' ", e->name);
359 data1_tag *t = e->tag;
361 printf("TagNam: '%s' ", t->names->name);
364 printf("%s[%d],", t->tagset->name, t->tagset->type);
367 if (t->which == DATA1T_numeric)
368 printf("%d)", t->value.numeric);
370 printf("'%s')", t->value.string);
377 if (n->which == DATA1N_tag)
379 index_termlist (n, n, p, level, wrd);
380 /* index start tag */
381 if (!n->root->u.root.absyn)
382 index_xpath (n, p, level, wrd, 1);
386 if (dumpkeys(n->child, p, level + 1, wrd) < 0)
390 if (n->which == DATA1N_data)
392 data1_node *par = get_parent_tag(p->dh, n);
394 if (p->flagShowRecords)
396 printf("%*s", level * 4, "");
398 if (n->u.data.len > 32)
399 printf("'%.24s ... %.6s'\n", n->u.data.data,
400 n->u.data.data + n->u.data.len-6);
401 else if (n->u.data.len > 0)
402 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
408 index_termlist (par, n, p, level, wrd);
409 if (!n->root->u.root.absyn)
410 index_xpath (n, p, level, wrd, 1016);
414 if (n->which == DATA1N_tag)
417 if (!n->root->u.root.absyn)
418 index_xpath (n, p, level, wrd, 2);
422 if (p->flagShowRecords && n->which == DATA1N_root)
424 printf("%*s-------------\n\n", level * 4, "");
430 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
433 int oidtmp[OID_SIZE];
436 oe.proto = PROTO_Z3950;
437 oe.oclass = CLASS_SCHEMA;
440 oe.value = n->u.root.absyn->reference;
442 if ((oid_ent_to_oid (&oe, oidtmp)))
443 (*p->schemaAdd)(p, oidtmp);
447 return dumpkeys(n, p, 0, &wrd);
450 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
454 struct grs_read_info gri;
456 int oidtmp[OID_SIZE];
459 gri.readf = p->readf;
460 gri.seekf = p->seekf;
461 gri.tellf = p->tellf;
464 gri.offset = p->offset;
468 if (read_grs_type (h, &gri, p->subType, &n))
469 return RECCTRL_EXTRACT_ERROR;
471 return RECCTRL_EXTRACT_EOF;
472 oe.proto = PROTO_Z3950;
473 oe.oclass = CLASS_SCHEMA;
475 if (!n->u.root.absyn)
476 return RECCTRL_EXTRACT_ERROR;
480 oe.value = n->u.root.absyn->reference;
481 if ((oid_ent_to_oid (&oe, oidtmp)))
482 (*p->schemaAdd)(p, oidtmp);
485 /* ensure our data1 tree is UTF-8 */
486 data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
489 data1_pr_tree (p->dh, n, stdout);
493 if (dumpkeys(n, p, 0, &wrd) < 0)
495 data1_free_tree(p->dh, n);
496 return RECCTRL_EXTRACT_ERROR;
498 data1_free_tree(p->dh, n);
499 return RECCTRL_EXTRACT_OK;
502 static int grs_extract(void *clientData, struct recExtractCtrl *p)
505 NMEM mem = nmem_create ();
506 struct grs_handlers *h = (struct grs_handlers *) clientData;
508 ret = grs_extract_sub(h, p, mem);
514 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
516 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
518 data1_esetname *eset;
524 case Z_RecordComp_simple:
525 if (c->u.simple->which != Z_ElementSetNames_generic)
526 return 26; /* only generic form supported. Fix this later */
527 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
528 c->u.simple->u.generic)))
530 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
531 return 25; /* invalid esetname */
533 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
534 c->u.simple->u.generic);
537 case Z_RecordComp_complex:
538 if (c->u.complex->generic)
540 /* insert check for schema */
541 if ((p = c->u.complex->generic->elementSpec))
545 case Z_ElementSpec_elementSetName:
547 data1_getesetbyname(dh, n->u.root.absyn,
548 p->u.elementSetName)))
550 logf(LOG_LOG, "Unknown esetname '%s'",
551 p->u.elementSetName);
552 return 25; /* invalid esetname */
554 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
555 p->u.elementSetName);
558 case Z_ElementSpec_externalSpec:
559 if (p->u.externalSpec->which == Z_External_espec1)
561 logf(LOG_DEBUG, "Got Espec-1");
562 espec = p->u.externalSpec-> u.espec1;
566 logf(LOG_LOG, "Unknown external espec.");
567 return 25; /* bad. what is proper diagnostic? */
578 logf (LOG_DEBUG, "Element: Espec-1 match");
579 return data1_doespec1(dh, n, espec);
583 logf (LOG_DEBUG, "Element: all match");
588 static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
591 const char *idzebra_ns[7];
593 idzebra_ns[0] = "xmlns:idzebra";
594 idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
597 data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
599 data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
602 data1_mk_tag_data_int (p->dh, top, "idzebra:score",
605 data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
608 data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
612 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
614 data1_node *node = 0, *onode = 0, *top;
617 int res, selected = 0;
619 struct grs_read_info gri;
621 struct grs_handlers *h = (struct grs_handlers *) clientData;
622 int requested_schema = VAL_NONE;
623 data1_marctab *marctab;
627 gri.readf = p->readf;
628 gri.seekf = p->seekf;
629 gri.tellf = p->tellf;
636 logf (LOG_DEBUG, "grs_retrieve");
637 if (read_grs_type (h, &gri, p->subType, &node))
649 /* ensure our data1 tree is UTF-8 */
650 data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
653 data1_pr_tree (p->dh, node, stdout);
655 top = data1_get_root_tag (p->dh, node);
657 logf (LOG_DEBUG, "grs_retrieve: size");
658 if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
660 dnew->u.data.what = DATA1I_text;
661 dnew->u.data.data = dnew->lbuf;
662 sprintf(dnew->u.data.data, "%d", p->recordSize);
663 dnew->u.data.len = strlen(dnew->u.data.data);
666 tagname = res_get_def(p->res, "tagrank", "rank");
667 if (strcmp(tagname, "0") && p->score >= 0 &&
668 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
670 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
671 dnew->u.data.what = DATA1I_num;
672 dnew->u.data.data = dnew->lbuf;
673 sprintf(dnew->u.data.data, "%d", p->score);
674 dnew->u.data.len = strlen(dnew->u.data.data);
677 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
678 if (strcmp(tagname, "0") && p->localno > 0 &&
679 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
681 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
682 dnew->u.data.what = DATA1I_text;
683 dnew->u.data.data = dnew->lbuf;
685 sprintf(dnew->u.data.data, "%d", p->localno);
686 dnew->u.data.len = strlen(dnew->u.data.data);
689 data1_pr_tree (p->dh, node, stdout);
691 if (p->comp && p->comp->which == Z_RecordComp_complex &&
692 p->comp->u.complex->generic &&
693 p->comp->u.complex->generic->schema)
695 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
697 requested_schema = oe->value;
700 /* If schema has been specified, map if possible, then check that
701 * we got the right one
703 if (requested_schema != VAL_NONE)
705 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
706 for (map = node->u.root.absyn->maptabs; map; map = map->next)
708 if (map->target_absyn_ref == requested_schema)
711 if (!(node = data1_map_record(p->dh, onode, map, mem)))
720 if (node->u.root.absyn &&
721 requested_schema != node->u.root.absyn->reference)
729 * Does the requested format match a known syntax-mapping? (this reflects
730 * the overlap of schema and formatting which is inherent in the MARC
733 yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
734 if (node->u.root.absyn)
735 for (map = node->u.root.absyn->maptabs; map; map = map->next)
737 if (map->target_absyn_ref == p->input_format)
740 if (!(node = data1_map_record(p->dh, onode, map, mem)))
749 yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
750 if (node->u.root.absyn &&
751 node->u.root.absyn->reference != VAL_NONE &&
752 p->input_format == VAL_GRS1)
756 int oidtmp[OID_SIZE];
758 oe.proto = PROTO_Z3950;
759 oe.oclass = CLASS_SCHEMA;
760 oe.value = node->u.root.absyn->reference;
762 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
765 data1_handle dh = p->dh;
769 for (ii = oid; *ii >= 0; ii++)
773 sprintf(p, "%d", *ii);
778 if ((dnew = data1_mk_tag_data_wd(dh, node,
779 "schemaIdentifier", mem)))
781 dnew->u.data.what = DATA1I_oid;
782 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
783 memcpy(dnew->u.data.data, tmp, p - tmp);
784 dnew->u.data.len = p - tmp;
789 logf (LOG_DEBUG, "grs_retrieve: element spec");
790 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
794 data1_free_tree(p->dh, onode);
795 data1_free_tree(p->dh, node);
799 else if (p->comp && !res)
803 data1_pr_tree (p->dh, node, stdout);
805 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
806 switch (p->output_format = (p->input_format != VAL_NONE ?
807 p->input_format : VAL_SUTRS))
810 add_idzebra_info (p, top, mem);
813 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
815 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
820 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
821 memcpy (new_buf, p->rec_buf, p->rec_len);
822 p->rec_buf = new_buf;
827 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
829 p->diagnostic = 238; /* not available in requested syntax */
831 p->rec_len = (size_t) (-1);
834 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
838 p->rec_len = (size_t) (-1);
841 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
845 p->rec_len = (size_t) (-1);
849 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
850 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
855 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
856 memcpy (new_buf, p->rec_buf, p->rec_len);
857 p->rec_buf = new_buf;
861 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
866 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
867 memcpy (new_buf, p->rec_buf, p->rec_len);
868 p->rec_buf = new_buf;
872 if (!node->u.root.absyn)
877 for (marctab = node->u.root.absyn->marc; marctab;
878 marctab = marctab->next)
879 if (marctab->reference == p->input_format)
887 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
888 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
889 selected, &p->rec_len)))
893 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
894 memcpy (new_buf, p->rec_buf, p->rec_len);
895 p->rec_buf = new_buf;
899 data1_free_tree(p->dh, node);
901 data1_free_tree(p->dh, onode);
906 static struct recType grs_type =
915 RecType recTypeGrs = &grs_type;