1 /* $Id: recgrs.c,v 1.63 2002-08-28 19:52:29 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);
241 wrd->string = xp->value;
242 wrd->length = strlen(xp->value);
248 wrd->string = attr_tag_path_full;
249 wrd->length = strlen(attr_tag_path_full);
257 static void index_termlist (data1_node *par, data1_node *n,
258 struct recExtractCtrl *p, int level, RecWord *wrd)
260 data1_termlist *tlist = 0;
261 data1_datatype dtype = DATA1K_string;
263 * cycle up towards the root until we find a tag with an att..
264 * this has the effect of indexing locally defined tags with
265 * the attribute of their ancestor in the record.
268 while (!par->u.tag.element)
269 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
271 if (!par || !(tlist = par->u.tag.element->termlists))
273 if (par->u.tag.element->tag)
274 dtype = par->u.tag.element->tag->kind;
276 for (; tlist; tlist = tlist->next)
279 /* consider source */
282 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
284 wrd->string = n->u.data.data;
285 wrd->length = n->u.data.len;
287 else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
289 wrd->string = n->u.tag.tag;
290 wrd->length = strlen(n->u.tag.tag);
292 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
293 n->which == DATA1N_tag)
295 data1_xattr *p = n->u.tag.attributes;
296 while (p && strcmp (p->name, xattr))
300 wrd->string = p->value;
301 wrd->length = strlen(p->value);
306 if (p->flagShowRecords)
309 printf("%*sIdx: [%s]", (level + 1) * 4, "",
311 printf("%s:%s [%d] %s",
312 tlist->att->parent->name,
313 tlist->att->name, tlist->att->value,
316 for (i = 0; i<wrd->length && i < 8; i++)
317 fputc (wrd->string[i], stdout);
321 fputc ('\n', stdout);
325 wrd->reg_type = *tlist->structure;
326 wrd->attrSet = (int) (tlist->att->parent->reference);
327 wrd->attrUse = tlist->att->locals->local;
334 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
337 for (; n; n = n->next)
339 if (p->flagShowRecords) /* display element description to user */
341 if (n->which == DATA1N_root)
343 printf("%*s", level * 4, "");
344 printf("Record type: '%s'\n", n->u.root.type);
346 else if (n->which == DATA1N_tag)
350 printf("%*s", level * 4, "");
351 if (!(e = n->u.tag.element))
352 printf("Local tag: '%s'\n", n->u.tag.tag);
355 printf("Elm: '%s' ", e->name);
358 data1_tag *t = e->tag;
360 printf("TagNam: '%s' ", t->names->name);
363 printf("%s[%d],", t->tagset->name, t->tagset->type);
366 if (t->which == DATA1T_numeric)
367 printf("%d)", t->value.numeric);
369 printf("'%s')", t->value.string);
376 if (n->which == DATA1N_tag)
378 index_termlist (n, n, p, level, wrd);
379 /* index start tag */
380 assert (n->root->u.root.absyn);
382 if (!n->root->u.root.absyn)
383 index_xpath (n, p, level, wrd, 1);
384 else if (n->root->u.root.absyn->enable_xpath_indexing)
385 index_xpath (n, p, level, wrd, 1);
389 if (dumpkeys(n->child, p, level + 1, wrd) < 0)
393 if (n->which == DATA1N_data)
395 data1_node *par = get_parent_tag(p->dh, n);
397 if (p->flagShowRecords)
399 printf("%*s", level * 4, "");
401 if (n->u.data.len > 256)
402 printf("'%.240s ... %.6s'\n", n->u.data.data,
403 n->u.data.data + n->u.data.len-6);
404 else if (n->u.data.len > 0)
405 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
411 index_termlist (par, n, p, level, wrd);
412 if (!n->root->u.root.absyn)
413 index_xpath (n, p, level, wrd, 1016);
414 else if (n->root->u.root.absyn->enable_xpath_indexing)
415 index_xpath (n, p, level, wrd, 1016);
418 if (n->which == DATA1N_tag)
421 if (!n->root->u.root.absyn)
422 index_xpath (n, p, level, wrd, 2);
423 else if (n->root->u.root.absyn->enable_xpath_indexing)
424 index_xpath (n, p, level, wrd, 2);
427 if (p->flagShowRecords && n->which == DATA1N_root)
429 printf("%*s-------------\n\n", level * 4, "");
435 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
438 int oidtmp[OID_SIZE];
441 oe.proto = PROTO_Z3950;
442 oe.oclass = CLASS_SCHEMA;
445 oe.value = n->u.root.absyn->reference;
447 if ((oid_ent_to_oid (&oe, oidtmp)))
448 (*p->schemaAdd)(p, oidtmp);
452 return dumpkeys(n, p, 0, &wrd);
455 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
459 struct grs_read_info gri;
461 int oidtmp[OID_SIZE];
464 gri.readf = p->readf;
465 gri.seekf = p->seekf;
466 gri.tellf = p->tellf;
469 gri.offset = p->offset;
473 if (read_grs_type (h, &gri, p->subType, &n))
474 return RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER;
476 return RECCTRL_EXTRACT_EOF;
477 oe.proto = PROTO_Z3950;
478 oe.oclass = CLASS_SCHEMA;
480 if (!n->u.root.absyn)
481 return RECCTRL_EXTRACT_ERROR;
485 oe.value = n->u.root.absyn->reference;
486 if ((oid_ent_to_oid (&oe, oidtmp)))
487 (*p->schemaAdd)(p, oidtmp);
490 /* ensure our data1 tree is UTF-8 */
491 data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
494 data1_pr_tree (p->dh, n, stdout);
498 if (dumpkeys(n, p, 0, &wrd) < 0)
500 data1_free_tree(p->dh, n);
501 return RECCTRL_EXTRACT_ERROR_GENERIC;
503 data1_free_tree(p->dh, n);
504 return RECCTRL_EXTRACT_OK;
507 static int grs_extract(void *clientData, struct recExtractCtrl *p)
510 NMEM mem = nmem_create ();
511 struct grs_handlers *h = (struct grs_handlers *) clientData;
513 ret = grs_extract_sub(h, p, mem);
519 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
521 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
523 data1_esetname *eset;
529 case Z_RecordComp_simple:
530 if (c->u.simple->which != Z_ElementSetNames_generic)
531 return 26; /* only generic form supported. Fix this later */
532 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
533 c->u.simple->u.generic)))
535 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
536 return 25; /* invalid esetname */
538 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
539 c->u.simple->u.generic);
542 case Z_RecordComp_complex:
543 if (c->u.complex->generic)
545 /* insert check for schema */
546 if ((p = c->u.complex->generic->elementSpec))
550 case Z_ElementSpec_elementSetName:
552 data1_getesetbyname(dh, n->u.root.absyn,
553 p->u.elementSetName)))
555 logf(LOG_LOG, "Unknown esetname '%s'",
556 p->u.elementSetName);
557 return 25; /* invalid esetname */
559 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
560 p->u.elementSetName);
563 case Z_ElementSpec_externalSpec:
564 if (p->u.externalSpec->which == Z_External_espec1)
566 logf(LOG_DEBUG, "Got Espec-1");
567 espec = p->u.externalSpec-> u.espec1;
571 logf(LOG_LOG, "Unknown external espec.");
572 return 25; /* bad. what is proper diagnostic? */
583 logf (LOG_DEBUG, "Element: Espec-1 match");
584 return data1_doespec1(dh, n, espec);
588 logf (LOG_DEBUG, "Element: all match");
593 static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
596 const char *idzebra_ns[7];
598 idzebra_ns[0] = "xmlns:idzebra";
599 idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
602 data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
604 data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
607 data1_mk_tag_data_int (p->dh, top, "idzebra:score",
610 data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
613 data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
617 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
619 data1_node *node = 0, *onode = 0, *top;
622 int res, selected = 0;
624 struct grs_read_info gri;
626 struct grs_handlers *h = (struct grs_handlers *) clientData;
627 int requested_schema = VAL_NONE;
628 data1_marctab *marctab;
632 gri.readf = p->readf;
633 gri.seekf = p->seekf;
634 gri.tellf = p->tellf;
641 logf (LOG_DEBUG, "grs_retrieve");
642 if (read_grs_type (h, &gri, p->subType, &node))
654 /* ensure our data1 tree is UTF-8 */
655 data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
658 data1_pr_tree (p->dh, node, stdout);
660 top = data1_get_root_tag (p->dh, node);
662 logf (LOG_DEBUG, "grs_retrieve: size");
663 if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
665 dnew->u.data.what = DATA1I_text;
666 dnew->u.data.data = dnew->lbuf;
667 sprintf(dnew->u.data.data, "%d", p->recordSize);
668 dnew->u.data.len = strlen(dnew->u.data.data);
671 tagname = res_get_def(p->res, "tagrank", "rank");
672 if (strcmp(tagname, "0") && p->score >= 0 &&
673 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
675 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
676 dnew->u.data.what = DATA1I_num;
677 dnew->u.data.data = dnew->lbuf;
678 sprintf(dnew->u.data.data, "%d", p->score);
679 dnew->u.data.len = strlen(dnew->u.data.data);
682 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
683 if (strcmp(tagname, "0") && p->localno > 0 &&
684 (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
686 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
687 dnew->u.data.what = DATA1I_text;
688 dnew->u.data.data = dnew->lbuf;
690 sprintf(dnew->u.data.data, "%d", p->localno);
691 dnew->u.data.len = strlen(dnew->u.data.data);
694 data1_pr_tree (p->dh, node, stdout);
696 if (p->comp && p->comp->which == Z_RecordComp_complex &&
697 p->comp->u.complex->generic &&
698 p->comp->u.complex->generic->schema)
700 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
702 requested_schema = oe->value;
705 /* If schema has been specified, map if possible, then check that
706 * we got the right one
708 if (requested_schema != VAL_NONE)
710 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
711 for (map = node->u.root.absyn->maptabs; map; map = map->next)
713 if (map->target_absyn_ref == requested_schema)
716 if (!(node = data1_map_record(p->dh, onode, map, mem)))
725 if (node->u.root.absyn &&
726 requested_schema != node->u.root.absyn->reference)
734 * Does the requested format match a known syntax-mapping? (this reflects
735 * the overlap of schema and formatting which is inherent in the MARC
738 yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
739 if (node->u.root.absyn)
740 for (map = node->u.root.absyn->maptabs; map; map = map->next)
742 if (map->target_absyn_ref == p->input_format)
745 if (!(node = data1_map_record(p->dh, onode, map, mem)))
754 yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
755 if (node->u.root.absyn &&
756 node->u.root.absyn->reference != VAL_NONE &&
757 p->input_format == VAL_GRS1)
761 int oidtmp[OID_SIZE];
763 oe.proto = PROTO_Z3950;
764 oe.oclass = CLASS_SCHEMA;
765 oe.value = node->u.root.absyn->reference;
767 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
770 data1_handle dh = p->dh;
774 for (ii = oid; *ii >= 0; ii++)
778 sprintf(p, "%d", *ii);
783 if ((dnew = data1_mk_tag_data_wd(dh, node,
784 "schemaIdentifier", mem)))
786 dnew->u.data.what = DATA1I_oid;
787 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
788 memcpy(dnew->u.data.data, tmp, p - tmp);
789 dnew->u.data.len = p - tmp;
794 logf (LOG_DEBUG, "grs_retrieve: element spec");
795 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
799 data1_free_tree(p->dh, onode);
800 data1_free_tree(p->dh, node);
804 else if (p->comp && !res)
808 data1_pr_tree (p->dh, node, stdout);
810 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
811 switch (p->output_format = (p->input_format != VAL_NONE ?
812 p->input_format : VAL_SUTRS))
815 add_idzebra_info (p, top, mem);
818 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
820 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
825 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
826 memcpy (new_buf, p->rec_buf, p->rec_len);
827 p->rec_buf = new_buf;
832 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
834 p->diagnostic = 238; /* not available in requested syntax */
836 p->rec_len = (size_t) (-1);
839 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
843 p->rec_len = (size_t) (-1);
846 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
850 p->rec_len = (size_t) (-1);
854 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
855 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
860 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
861 memcpy (new_buf, p->rec_buf, p->rec_len);
862 p->rec_buf = new_buf;
866 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
871 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
872 memcpy (new_buf, p->rec_buf, p->rec_len);
873 p->rec_buf = new_buf;
877 if (!node->u.root.absyn)
882 for (marctab = node->u.root.absyn->marc; marctab;
883 marctab = marctab->next)
884 if (marctab->reference == p->input_format)
892 data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
893 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
894 selected, &p->rec_len)))
898 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
899 memcpy (new_buf, p->rec_buf, p->rec_len);
900 p->rec_buf = new_buf;
904 data1_free_tree(p->dh, node);
906 data1_free_tree(p->dh, onode);
911 static struct recType grs_type =
920 RecType recTypeGrs = &grs_type;