1 /* $Id: d1_absyn.c,v 1.9.2.8 2006-08-14 10:38:51 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <zebra_xpath.h>
33 #define D1_MAX_NESTING 128
38 struct data1_systag *next;
41 struct data1_absyn_cache_info
45 data1_absyn_cache next;
48 struct data1_attset_cache_info
52 data1_attset_cache next;
55 data1_absyn *data1_absyn_search (data1_handle dh, const char *name)
57 data1_absyn_cache p = *data1_absyn_cache_get (dh);
61 if (!strcmp (name, p->name))
68 We need to destroy DFAs, in xp_element (xelm) definitions
72 void data1_absyn_destroy (data1_handle dh)
74 data1_absyn_cache p = *data1_absyn_cache_get (dh);
78 data1_absyn *abs = p->absyn;
81 data1_xpelement *xpe = abs->xp_elements;
83 logf (LOG_DEBUG,"Destroy xp element %s",xpe->xpath_expr);
84 if (xpe->dfa) { dfa_delete (&xpe->dfa); }
93 void data1_absyn_trav (data1_handle dh, void *handle,
94 void (*fh)(data1_handle dh, void *h, data1_absyn *a))
96 data1_absyn_cache p = *data1_absyn_cache_get (dh);
100 (*fh)(dh, handle, p->absyn);
105 data1_absyn *data1_absyn_add (data1_handle dh, const char *name)
108 NMEM mem = data1_nmem_get (dh);
110 data1_absyn_cache p = (data1_absyn_cache)nmem_malloc (mem, sizeof(*p));
111 data1_absyn_cache *pp = data1_absyn_cache_get (dh);
113 sprintf(fname, "%s.abs", name);
114 p->absyn = data1_read_absyn (dh, fname, 0);
115 p->name = nmem_strdup (mem, name);
121 data1_absyn *data1_get_absyn (data1_handle dh, const char *name)
125 if (!(absyn = data1_absyn_search (dh, name)))
126 absyn = data1_absyn_add (dh, name);
130 data1_attset *data1_attset_search_name (data1_handle dh, const char *name)
132 data1_attset_cache p = *data1_attset_cache_get (dh);
136 if (!strcmp (name, p->name))
143 data1_attset *data1_attset_search_id (data1_handle dh, int id)
145 data1_attset_cache p = *data1_attset_cache_get (dh);
149 if (id == p->attset->reference)
156 data1_attset *data1_attset_add (data1_handle dh, const char *name)
158 char fname[512], aname[512];
159 NMEM mem = data1_nmem_get (dh);
160 data1_attset *attset;
162 strcpy (aname, name);
163 sprintf(fname, "%s.att", name);
164 attset = data1_read_attset (dh, fname);
168 attset = data1_read_attset (dh, name);
169 if (attset && (cp = strrchr (aname, '.')))
173 yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
176 data1_attset_cache p = (data1_attset_cache)
177 nmem_malloc (mem, sizeof(*p));
178 data1_attset_cache *pp = data1_attset_cache_get (dh);
180 attset->name = p->name = nmem_strdup (mem, aname);
188 data1_attset *data1_get_attset (data1_handle dh, const char *name)
190 data1_attset *attset;
192 if (!(attset = data1_attset_search_name (dh, name)))
193 attset = data1_attset_add (dh, name);
197 data1_esetname *data1_getesetbyname(data1_handle dh, data1_absyn *a,
202 for (r = a->esetnames; r; r = r->next)
203 if (!data1_matchstr(r->name, name))
208 data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
209 data1_element *parent,
214 /* It's now possible to have a data1 tree with no abstract syntax */
219 r = abs->main_elements;
221 r = parent->children;
223 for (; r; r = r->next)
227 for (n = r->tag->names; n; n = n->next)
228 if (!data1_matchstr(tagname, n->name))
234 data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
239 /* It's now possible to have a data1 tree with no abstract syntax */
242 for (r = absyn->main_elements; r; r = r->next)
243 if (!data1_matchstr(r->name, name))
249 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
251 /* It's now possible to have a data1 tree with no abstract syntax */
255 for (; e; e = e->next)
260 fix_element_ref (dh, absyn, e->children);
264 data1_sub_elements *sub_e = absyn->sub_elements;
265 while (sub_e && strcmp (e->sub_name, sub_e->name))
268 e->children = sub_e->elements;
270 yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
277 New function, a bit dummy now... I've seen it in zrpn.c... We should build
278 more clever regexps...
290 Now [] predicates are supported
296 static const char * mk_xpath_regexp (data1_handle dh, const char *expr)
298 const char *p = expr;
302 char *res_p, *res = 0;
315 int is_predicate = 0;
318 for (i = 0; *p && !strchr("/",*p); i++, p++)
320 res_size += (i+3); /* we'll add / between later .. */
321 stack[e] = (char *) nmem_malloc(data1_nmem_get(dh), i+1);
323 for (j = 0; j < i; j++)
325 const char *pp = p-i+j;
344 res_p = res = nmem_malloc(data1_nmem_get(dh), res_size + 10);
347 if (stack[e-1][0] == '@') /* path/@attr spec (leaf is attribute) */
350 strcpy(res_p, "[^@]*/"); /* path .. (index all cdata below it) */
351 res_p = res_p + strlen(res_p);
353 sprintf(res_p, "%s/", stack[e]);
354 res_p += strlen(stack[e]) + 1;
358 sprintf(res_p, ".*");
361 sprintf (res_p, "$");
363 yaz_log(LOG_DEBUG, "Got regexp: %s", res);
369 added arg xpelement... when called from xelm context, it's 1, saying
370 that ! means xpath, not element name as attribute name...
374 static int parse_termlists (data1_handle dh, data1_termlist ***tpp,
375 char *cp, const char *file, int lineno,
376 const char *element_name, data1_absyn *res,
379 data1_termlist **tp = *tpp;
382 char attname[512], structure[512];
387 for (i = 0; cp[i] && i<sizeof(attname)-1; i++)
388 if (strchr(":,", cp[i]))
396 "%s:%d: Syntax error in termlistspec '%s'",
406 for (i = 0; cp[i] && i<sizeof(structure)-1; i++)
407 if (level == 0 && strchr(",", cp[i]))
411 structure[i] = cp[i];
414 else if (cp[i] == ')')
424 *tp = (data1_termlist *)
425 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
430 strcpy(attname, element_name);
432 if (!((*tp)->att = data1_getattbyname(dh, res->attset,
434 if ((!xpelement) || (*attname != '!')) {
436 "%s:%d: Couldn't find att '%s' in attset",
437 file, lineno, attname);
444 if (r == 2 && (source = strchr(structure, ':')))
445 *source++ = '\0'; /* cut off structure .. */
447 source = "data"; /* ok: default is leaf data */
448 (*tp)->source = (char *)
449 nmem_strdup (data1_nmem_get (dh), source);
451 if (r < 2) /* is the structure qualified? */
452 (*tp)->structure = "w";
454 (*tp)->structure = (char *)
455 nmem_strdup (data1_nmem_get (dh), structure);
464 * Converts a 'melm' field[$subfield] pattern to a simple xpath
466 static int melm2xpath(char *melm, char *buf)
472 if ((dollar = strchr(melm, '$'))) {
477 if (field[0] == '0' && field[1] == '0')
478 fieldtype = "controlfield";
480 fieldtype = "datafield";
481 sprintf(buf, "/*/%s[@tag=\"%s\"]", fieldtype, field);
483 sprintf(buf + strlen(buf), "/subfield[@code=\"%s\"]", subfield);
484 else if (field[0] != '0' || field[1] != '0')
485 strcat(buf, "/subfield");
486 yaz_log(LOG_DEBUG, "Created xpath: '%s'", buf);
490 const char *data1_systag_lookup(data1_absyn *absyn, const char *tag,
491 const char *default_value)
493 struct data1_systag *p = absyn->systags;
494 for (; p; p = p->next)
495 if (!strcmp(p->name, tag))
497 return default_value;
500 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n' || (c) == '\r')
502 int read_absyn_line(FILE *f, int *lineno, char *line, int len,
503 char *argv[], int num)
509 while ((p = fgets(line, len, f)))
512 while (*p && l_isspace(*p))
520 for (argc = 0; *p ; argc++)
522 if (*p == '#') /* trailing comment */
525 while (*p && !(l_isspace(*p) && !quoted)) {
526 if (*p =='"') quoted = 1 - quoted;
527 if (*p =='[') quoted = 1;
528 if (*p ==']') quoted = 0;
534 while (*p && l_isspace(*p))
542 data1_absyn *data1_read_absyn (data1_handle dh, const char *file,
545 data1_sub_elements *cur_elements = NULL;
546 data1_xpelement *cur_xpelement = NULL;
548 data1_absyn *res = 0;
550 data1_element **ppl[D1_MAX_NESTING];
551 data1_esetname **esetpp;
552 data1_maptab **maptabp;
553 data1_marctab **marcp;
554 data1_termlist *all = 0;
555 data1_attset_child **attset_childp;
556 data1_tagset **tagset_childp;
557 struct data1_systag **systagsp;
561 char *argv[50], line[512];
563 if (!(f = data1_path_fopen(dh, file, "r")))
565 yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
570 res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
572 res->reference = VAL_NONE;
575 res->enable_xpath_indexing = (f ? 0 : 1);
577 systagsp = &res->systags;
578 tagset_childp = &res->tagset;
580 res->attset = data1_empty_attset (dh);
581 attset_childp = &res->attset->children;
585 esetpp = &res->esetnames;
587 maptabp = &res->maptabs;
590 res->sub_elements = NULL;
591 res->main_elements = NULL;
592 res->xp_elements = NULL;
594 while (f && (argc = read_absyn_line(f, &lineno, line, 512, argv, 50)))
597 if (!strcmp(cmd, "elm") || !strcmp(cmd, "element"))
599 data1_element *new_element;
601 char *p, *sub_p, *path, *name, *termlists;
607 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
616 cur_elements = (data1_sub_elements *)
617 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
618 cur_elements->next = res->sub_elements;
619 cur_elements->elements = NULL;
620 cur_elements->name = "main";
621 res->sub_elements = cur_elements;
624 ppl[level] = &cur_elements->elements;
631 if ((e = strchr(p, '/')))
638 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
643 new_element = *ppl[level-1] = (data1_element *)
644 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
645 new_element->next = new_element->children = 0;
646 new_element->tag = 0;
647 new_element->termlists = 0;
648 new_element->sub_name = 0;
650 tp = &new_element->termlists;
651 ppl[level-1] = &new_element->next;
652 ppl[level] = &new_element->children;
654 /* consider subtree (if any) ... */
655 if ((sub_p = strchr (p, ':')) && sub_p[1])
658 new_element->sub_name =
659 nmem_strdup (data1_nmem_get(dh), sub_p);
661 /* well-defined tag */
662 if (sscanf(p, "(%d,%d)", &type, &value) == 2)
666 yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
670 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
673 yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
683 new_element->tag = (data1_tag *)
684 nmem_malloc(data1_nmem_get (dh),
685 sizeof(*new_element->tag));
686 nt->which = DATA1T_string;
687 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
688 nt->names = (data1_name *)
689 nmem_malloc(data1_nmem_get(dh),
690 sizeof(*new_element->tag->names));
691 nt->names->name = nt->value.string;
693 nt->kind = DATA1K_string;
699 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
703 /* parse termList definitions */
707 assert (res->attset);
709 if (parse_termlists (dh, &tp, p, file, lineno, name, res, 0))
714 *tp = all; /* append any ALL entries to the list */
716 new_element->name = nmem_strdup(data1_nmem_get (dh), name);
719 New code to support xelm directive
720 for each xelm a dfa is built. xelms are stored in res->xp_elements
722 maybe we should use a simple sscanf instead of dfa?
726 Now [] predicates are supported. regexps and xpath structure is
727 a bit redundant, however it's comfortable later...
732 else if (!strcmp(cmd, "xelm") || !strcmp(cmd, "melm")) {
735 char *p, *xpath_expr, *termlists;
737 struct DFA *dfa = dfa = dfa_init();
739 char melm_xpath[128];
743 yaz_log(LOG_WARN, "%s:%d: Bad # of args to xelm", file, lineno);
747 if (!strcmp(cmd, "melm")) {
748 if (melm2xpath(argv[1], melm_xpath) < 0)
750 xpath_expr = melm_xpath;
752 xpath_expr = argv[1];
755 regexp = mk_xpath_regexp(dh, xpath_expr);
756 i = dfa_parse (dfa, ®exp);
758 yaz_log(LOG_WARN, "%s:%d: Bad xpath to xelm", file, lineno);
765 cur_xpelement = (data1_xpelement *)
766 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
767 res->xp_elements = cur_xpelement;
769 cur_xpelement->next = (data1_xpelement *)
770 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
771 cur_xpelement = cur_xpelement->next;
773 cur_xpelement->next = NULL;
774 cur_xpelement->xpath_expr = nmem_strdup(data1_nmem_get (dh),
778 cur_xpelement->dfa = dfa;
781 cur_xpelement->xpath_len =
782 zebra_parse_xpath_str(xpath_expr,
783 cur_xpelement->xpath, XPATH_STEP_COUNT,
787 dump_xp_steps(cur_xpelement->xpath,cur_xpelement->xpath_len);
790 cur_xpelement->termlists = 0;
791 tp = &cur_xpelement->termlists;
793 /* parse termList definitions */
797 assert (res->attset);
799 if (parse_termlists (dh, &tp, p, file, lineno,
805 *tp = all; /* append any ALL entries to the list */
808 else if (!strcmp(cmd, "section"))
814 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
820 cur_elements = (data1_sub_elements *)
821 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
822 cur_elements->next = res->sub_elements;
823 cur_elements->elements = NULL;
824 cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
825 res->sub_elements = cur_elements;
828 ppl[level] = &cur_elements->elements;
830 else if (!strcmp(cmd, "xpath"))
834 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'xpath' directive",
838 if (!strcmp(argv[1], "enable"))
839 res->enable_xpath_indexing = 1;
840 else if (!strcmp (argv[1], "disable"))
841 res->enable_xpath_indexing = 0;
844 yaz_log(LOG_WARN, "%s:%d: Expecting disable/enable "
845 "after 'xpath' directive", file, lineno);
848 else if (!strcmp(cmd, "all"))
850 data1_termlist **tp = &all;
853 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
859 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
863 if (parse_termlists (dh, &tp, argv[1], file, lineno, 0, res, 0))
869 else if (!strcmp(cmd, "name"))
873 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
877 res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
879 else if (!strcmp(cmd, "reference"))
885 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
890 if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
892 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'",
897 else if (!strcmp(cmd, "attset"))
900 data1_attset *attset;
904 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
909 if (!(attset = data1_get_attset (dh, name)))
911 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset %s",
915 *attset_childp = (data1_attset_child *)
916 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
917 (*attset_childp)->child = attset;
918 (*attset_childp)->next = 0;
919 attset_childp = &(*attset_childp)->next;
921 else if (!strcmp(cmd, "tagset"))
927 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
933 type = atoi(argv[2]);
934 *tagset_childp = data1_read_tagset (dh, name, type);
935 if (!(*tagset_childp))
937 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
941 tagset_childp = &(*tagset_childp)->next;
943 else if (!strcmp(cmd, "varset"))
949 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
954 if (!(res->varset = data1_read_varset (dh, name)))
956 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
961 else if (!strcmp(cmd, "esetname"))
967 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
974 *esetpp = (data1_esetname *)
975 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
976 (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
980 else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
982 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
983 file, lineno, fname);
986 esetpp = &(*esetpp)->next;
988 else if (!strcmp(cmd, "maptab"))
994 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
999 if (!(*maptabp = data1_read_maptab (dh, name)))
1001 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
1002 file, lineno, name);
1005 maptabp = &(*maptabp)->next;
1007 else if (!strcmp(cmd, "marc"))
1013 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
1018 if (!(*marcp = data1_read_marctab (dh, name)))
1020 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
1021 file, lineno, name);
1024 marcp = &(*marcp)->next;
1026 else if (!strcmp(cmd, "encoding"))
1030 yaz_log(LOG_WARN, "%s:%d: Bad # or args for encoding",
1034 res->encoding = nmem_strdup (data1_nmem_get(dh), argv[1]);
1036 else if (!strcmp(cmd, "systag"))
1040 yaz_log(LOG_WARN, "%s:%d: Bad # or args for systag",
1044 *systagsp = nmem_malloc (data1_nmem_get(dh), sizeof(**systagsp));
1046 (*systagsp)->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
1047 (*systagsp)->value = nmem_strdup(data1_nmem_get(dh), argv[2]);
1048 systagsp = &(*systagsp)->next;
1052 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file,
1060 for (cur_elements = res->sub_elements; cur_elements;
1061 cur_elements = cur_elements->next)
1063 if (!strcmp (cur_elements->name, "main"))
1064 res->main_elements = cur_elements->elements;
1065 fix_element_ref (dh, res, cur_elements->elements);
1068 yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);