1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SRU 2.0 facets
16 #include <yaz/wrbuf.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
24 #include <yaz/pquery.h>
25 #include <yaz/facet.h>
27 void yaz_sru_facet_request(ODR o, Z_FacetList **facetList, const char **limit)
29 if (o->direction == ODR_ENCODE)
31 Z_FacetList *fl = *facetList;
35 WRBUF w = wrbuf_alloc();
36 for (i = 0; i < fl->num; i++)
38 struct yaz_facet_attr av;
39 yaz_facet_attr_init(&av);
40 yaz_facet_attr_get_z_attributes(fl->elements[i]->attributes,
44 wrbuf_printf(w, "%d", av.limit ? av.limit : -1);
46 wrbuf_printf(w, ":%s,", av.useattr);
47 /* av.relation not considered yet */
52 wrbuf_cut_right(w, 1); /* remove , */
53 *limit = odr_strdup(o, wrbuf_cstr(w));
58 else if (o->direction == ODR_DECODE)
60 const char *cp = *limit;
66 WRBUF w = wrbuf_alloc();
67 while (sscanf(cp, "%d%n", &limit_val, &nor) >= 1 && nor > 0)
72 if (*cp == ':') /* field name follows */
74 wrbuf_puts(w, "@attr 1=");
75 while (*++cp && *cp != ',')
80 wrbuf_printf(w, "@attr 3=%d", limit_val);
86 *facetList = yaz_pqf_parse_facet_list(o, wrbuf_cstr(w));
93 void yaz_sru_facet_response(ODR o, Z_FacetList **facetList, xmlNodePtr n)
95 if (o->direction == ODR_ENCODE)
97 Z_FacetList *fl = *facetList;
102 "http://docs.oasis-open.org/ns/search-ws/facetedResults";
103 xmlNode *p1 = xmlNewChild(n, 0, BAD_CAST "facetedResults", 0);
104 xmlNsPtr ns_fr = xmlNewNs(p1, BAD_CAST ns, BAD_CAST "fr");
106 for (i = 0; i < fl->num; i++)
108 Z_FacetField *ff = fl->elements[i];
109 xmlNode *p2 = xmlNewChild(p1, 0, BAD_CAST "facet", 0);
112 struct yaz_facet_attr av;
113 yaz_facet_attr_init(&av);
114 yaz_facet_attr_get_z_attributes(ff->attributes, &av);
115 add_xsd_string(p2, "index", av.useattr);
116 p3 = xmlNewChild(p2, 0, BAD_CAST "terms", 0);
117 for (j = 0; j < ff->num_terms; j++)
119 Z_FacetTerm *ft = ff->terms[j];
120 Z_Term *zt = ft->term;
121 xmlNode *p4 = xmlNewChild(p3, 0, BAD_CAST "term", 0);
122 if (zt->which == Z_Term_general)
123 add_xsd_string_n(p4, "actualTerm",
124 (char *) zt->u.general->buf,
127 add_xsd_integer(p4, "count", ft->count);
132 else if (o->direction == ODR_DECODE)
134 Z_FacetList *fl = (Z_FacetList *) odr_malloc(o, sizeof(*fl));
138 for (p1 = n->children; p1; p1 = p1->next)
139 if (match_element(p1, "facet"))
145 fl->elements = (Z_FacetField **)
146 odr_malloc(o, sizeof(*fl->elements) * fl->num);
147 for (p1 = n->children; p1; p1 = p1->next)
148 if (match_element(p1, "facet"))
150 char *index_name = 0;
151 xmlNode *p_terms = 0;
152 xmlNode *p2 = p1->children;
153 Z_FacetField *ff = (Z_FacetField *)
154 odr_malloc(o, sizeof(*ff));
155 fl->elements[i++] = ff;
159 for (; p2; p2 = p2->next)
161 if (match_xsd_string(p2, "index", o, &index_name))
163 else if (match_element(p2, "terms"))
168 Z_AttributeList *al =
169 (Z_AttributeList*) odr_malloc(o, sizeof(*al));
170 Z_ComplexAttribute *ca =
171 (Z_ComplexAttribute *) odr_malloc(o, sizeof(*ca));
172 Z_AttributeElement *ae =
173 (Z_AttributeElement *) odr_malloc(o, sizeof(*ae));
174 al->num_attributes = 1;
175 al->attributes = (Z_AttributeElement **)
176 odr_malloc(o, sizeof(*al->attributes));
177 al->attributes[0] = ae;
178 ae->attributeSet = 0;
179 ae->attributeType = odr_intdup(o, 1);
180 ae->which = Z_AttributeValue_complex;
181 ae->value.complex = ca;
182 ca->num_semanticAction = 0;
183 ca->semanticAction = 0;
185 ca->list = (Z_StringOrNumeric **)
186 odr_malloc(o, sizeof(*ca->list));
187 ca->list[0] = (Z_StringOrNumeric *)
188 odr_malloc(o, sizeof(**ca->list));
189 ca->list[0]->which = Z_StringOrNumeric_string;
190 ca->list[0]->u.string = index_name;
197 for (p = p_terms->children; p; p = p->next)
199 if (match_element(p, "term"))
203 ff->terms = (Z_FacetTerm **)
205 sizeof(*ff->terms) * ff->num_terms);
206 for (p = p_terms->children; p; p = p->next)
208 if (match_element(p, "term"))
212 xmlNode *p2 = p->children;
213 for (; p2; p2 = p2->next)
215 if (match_xsd_string(p2, "actualTerm", o,
218 else if (match_xsd_integer(p2, "count", o,
225 facet_term_create_cstr(o, cstr, *count);
230 if (ff->num_terms == 0)
245 * c-file-style: "Stroustrup"
246 * indent-tabs-mode: nil
248 * vim: shiftwidth=4 tabstop=8 expandtab