X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fsrw.c;h=dfd3aadb16616f24e29b60c5e3a59b653966ad20;hp=93ff77c52929c0a30782ec15156f28ba55b5e8be;hb=47d9d5ac3241f8db09ba19a1a5c1fdd5e140e75f;hpb=87db97bc97dd4eb54a4d474f35fb585749135ff9 diff --git a/src/srw.c b/src/srw.c index 93ff77c..dfd3aad 100644 --- a/src/srw.c +++ b/src/srw.c @@ -1,18 +1,26 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) 1995-2011 Index Data * See the file LICENSE for details. */ /** * \file srw.c * \brief Implements SRW/SRU package encoding and decoding */ +#if HAVE_CONFIG_H +#include +#endif + +#include #include +#include #if YAZ_HAVE_XML2 #include #include #include +#include "sru-p.h" + static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len, xmlNsPtr ns_ptr) { @@ -390,7 +398,7 @@ static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec, } } else if (o->direction == ODR_ENCODE) - { + { xmlNodePtr ptr = pptr; add_xsd_string(ptr, "versionType", rec->versionType); add_xsd_string(ptr, "versionValue", rec->versionValue); @@ -430,7 +438,7 @@ static int yaz_srw_versions(ODR o, xmlNodePtr pptr, { int i; for (i = 0; i < *num; i++) - { + { xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version", 0); yaz_srw_version(o, rptr, (*vers)+i, client_data, ns); @@ -439,30 +447,114 @@ static int yaz_srw_versions(ODR o, xmlNodePtr pptr, return 0; } +Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node) +{ + int freq; + xmlNodePtr child; + WRBUF wrbuf = wrbuf_alloc(); + const char *freq_string = yaz_element_attribute_value_get(node, "facetvalue", "est_representation"); + Z_Term *term; + if (freq_string) + freq = atoi(freq_string); + else + freq = -1; + + for (child = node->children; child ; child = child->next) { + if (child->type == XML_TEXT_NODE) + wrbuf_puts(wrbuf, (const char *) child->content); + } + term = term_create(odr, wrbuf_cstr(wrbuf)); + yaz_log(YLOG_DEBUG, "sru-proxy facet: %s %d", wrbuf_cstr(wrbuf), freq); + wrbuf_destroy(wrbuf); + return facet_term_create(odr, term, freq); +}; + +static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr) +{ + Z_AttributeList *list; + Z_FacetField *facet_field; + int num_terms = 0; + int index = 0; + xmlNodePtr node; + /* USE attribute */ + const char* name = yaz_element_attribute_value_get(ptr, "facet", "code"); + yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name); + + list = yaz_use_atttribute_create(odr, name); + for (node = ptr->children; node; node = node->next) { + if (match_element(node, "facetvalue")) + num_terms++; + } + facet_field = facet_field_create(odr, list, num_terms); + index = 0; + for (node = ptr->children; node; node = node->next) + { + if (match_element(node, "facetvalue")) + { + facet_field_term_set(odr, facet_field, yaz_sru_proxy_get_facet_term_count(odr, node), index); + index++; + } + } + return facet_field; +} + +static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root, + Z_FacetList **facetList) +{ + xmlNodePtr ptr; + + for (ptr = root->children; ptr; ptr = ptr->next) + { + if (match_element(ptr, "facets")) + { + xmlNodePtr node; + Z_FacetList *facet_list; + int num_facets = 0; + for (node = ptr->children; node; node= node->next) + { + if (node->type == XML_ELEMENT_NODE) + num_facets++; + } + facet_list = facet_list_create(o, num_facets); + num_facets = 0; + for (node = ptr->children; node; node= node->next) + { + if (match_element(node, "facet")) { + facet_list_field_set(o, facet_list, yaz_sru_proxy_decode_facet_field(o, node), num_facets); + num_facets++; + } + } + *facetList = facet_list; + break; + } + } + return 0; +} + + static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs, int *num, void *client_data, const char *ns) - { int i; xmlNodePtr ptr; *num = 0; for (ptr = pptr; ptr; ptr = ptr->next) { - if (ptr->type == XML_ELEMENT_NODE && - !xmlStrcmp(ptr->name, BAD_CAST "diagnostic")) - (*num)++; + if (ptr->type == XML_ELEMENT_NODE && + !xmlStrcmp(ptr->name, BAD_CAST "diagnostic")) + (*num)++; } if (!*num) return 1; *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs)); for (i = 0; i < *num; i++) - { - (*recs)[i].uri = 0; - (*recs)[i].details = 0; - (*recs)[i].message = 0; - } + { + (*recs)[i].uri = 0; + (*recs)[i].details = 0; + (*recs)[i].message = 0; + } for (i = 0, ptr = pptr; ptr; ptr = ptr->next) { if (ptr->type == XML_ELEMENT_NODE && @@ -571,7 +663,7 @@ static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term, if (match_xsd_string(ptr, "value", o, &term->value)) ; else if (match_xsd_integer(ptr, "numberOfRecords", o, - &term->numberOfRecords)) + &term->numberOfRecords)) ; else if (match_xsd_string(ptr, "displayTerm", o, &term->displayTerm)) @@ -675,13 +767,13 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, &(*p)->srw_version)) ; else if (match_xsd_string(ptr, "query", o, - &req->query.cql)) + &req->query.cql)) req->query_type = Z_SRW_query_type_cql; else if (match_xsd_string(ptr, "pQuery", o, - &req->query.pqf)) + &req->query.pqf)) req->query_type = Z_SRW_query_type_pqf; else if (match_xsd_string(ptr, "xQuery", o, - &req->query.xcql)) + &req->query.xcql)) req->query_type = Z_SRW_query_type_xcql; else if (match_xsd_integer(ptr, "startRecord", o, &req->startRecord)) @@ -705,10 +797,10 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, &req->sort.sortKeys)) req->sort_type = Z_SRW_sort_type_sort; else if (match_xsd_string(ptr, "stylesheet", o, - &req->stylesheet)) + &req->stylesheet)) ; else if (match_xsd_string(ptr, "database", o, - &req->database)) + &req->database)) ; } if (!req->query.cql && !req->query.pqf && !req->query.xcql) @@ -746,7 +838,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, &(*p)->extraResponseData_len)) ; else if (match_xsd_integer(ptr, "numberOfRecords", o, - &res->numberOfRecords)) + &res->numberOfRecords)) ; else if (match_xsd_string(ptr, "resultSetId", o, &res->resultSetId)) @@ -765,6 +857,8 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, yaz_srw_diagnostics(o, ptr, &res->diagnostics, &res->num_diagnostics, client_data, ns); + else if (match_element(ptr, "facet_analysis")) + yaz_sru_proxy_decode_facets(o, ptr, &res->facetList); } } else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest")) @@ -781,7 +875,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, for (; ptr; ptr = ptr->next) { if (match_xsd_string(ptr, "version", o, - &(*p)->srw_version)) + &(*p)->srw_version)) ; else if (match_xsd_XML_n(ptr, "extraResponseData", o, &(*p)->extraResponseData_buf, @@ -791,10 +885,10 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, &req->stylesheet)) ; else if (match_xsd_string(ptr, "recordPacking", o, - &req->recordPacking)) + &req->recordPacking)) ; else if (match_xsd_string(ptr, "database", o, - &req->database)) + &req->database)) ; } } @@ -816,7 +910,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, for (; ptr; ptr = ptr->next) { if (match_xsd_string(ptr, "version", o, - &(*p)->srw_version)) + &(*p)->srw_version)) ; else if (match_xsd_XML_n(ptr, "extraResponseData", o, &(*p)->extraResponseData_buf, @@ -857,7 +951,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, &(*p)->extraResponseData_len)) ; else if (match_xsd_string(ptr, "scanClause", o, - &req->scanClause.cql)) + &req->scanClause.cql)) ; else if (match_xsd_string(ptr, "pScanClause", o, &req->scanClause.pqf)) @@ -1156,10 +1250,10 @@ int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, client_data, ns_ucp_str); } else if (match_xsd_string(ptr, "stylesheet", o, - &req->stylesheet)) + &req->stylesheet)) ; else if (match_xsd_string(ptr, "database", o, - &req->database)) + &req->database)) ; } } @@ -1189,7 +1283,7 @@ int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data, &(*p)->srw_version)) ; else if (match_xsd_string(ptr, "operationStatus", o, - &res->operationStatus )) + &res->operationStatus )) ; else if (match_xsd_string(ptr, "recordIdentifier", o, &res->recordId))