Refactor yaz_match-routines to separate source
[yaz-moved-to-github.git] / src / srw.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file srw.c
7  * \brief Implements SRW/SRU package encoding and decoding
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14
15 #include <yaz/srw.h>
16 #include <yaz/wrbuf.h>
17 #if YAZ_HAVE_XML2
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
20 #include <assert.h>
21 #include <yaz/facet.h>
22 #include "sru-p.h"
23
24 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len,
25                       xmlNsPtr ns_ptr)
26 {
27     if (val)
28     {
29         xmlDocPtr doc = xmlParseMemory(val,len);
30         if (doc)
31         {
32             xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
33             xmlNodePtr t = xmlDocGetRootElement(doc);
34             xmlAddChild(c, xmlCopyNode(t,1));
35             xmlFreeDoc(doc);
36         }
37     }
38 }
39
40 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, const char *val,
41                             int len)
42 {
43     if (val)
44     {
45         xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
46         xmlNodePtr t = xmlNewTextLen(BAD_CAST val, len);
47         xmlAddChild(c, t);
48         return t;
49     }
50     return 0;
51 }
52
53 xmlNodePtr add_xsd_string_ns(xmlNodePtr ptr, const char *elem, const char *val,
54                              xmlNsPtr ns_ptr)
55 {
56     if (val)
57     {
58         xmlNodePtr c = xmlNewChild(ptr, ns_ptr, BAD_CAST elem, 0);
59         xmlNodePtr t = xmlNewText(BAD_CAST val);
60         xmlAddChild(c, t);
61         return t;
62     }
63     return 0;
64 }
65
66 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
67 {
68     return add_xsd_string_ns(ptr, elem, val, 0);
69 }
70
71 void add_xsd_integer(xmlNodePtr ptr, const char *elem,
72                             const Odr_int *val)
73 {
74     if (val)
75     {
76         char str[40];
77         sprintf(str, ODR_INT_PRINTF, *val);
78         xmlNewTextChild(ptr, 0, BAD_CAST elem, BAD_CAST str);
79     }
80 }
81
82 char *yaz_negotiate_sru_version(char *input_ver)
83 {
84     if (!input_ver)
85         return "2.0";
86     if (!strcmp(input_ver, "1.1"))
87         return "1.1";
88     if (!strncmp(input_ver, "1.", 2))
89         return "1.2";
90     if (!strncmp(input_ver, "2.", 2))
91         return "2.0";
92     return 0;
93 }
94
95 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
96                           Z_SRW_extra_record **extra,
97                           void *client_data, int version2)
98 {
99     if (o->direction == ODR_DECODE)
100     {
101         Z_SRW_extra_record ex;
102
103         char *spack = 0;
104         xmlNodePtr ptr;
105 #ifdef Z_SRW_packed
106         rec->packing = 0;
107 #endif
108         rec->recordSchema = 0;
109         rec->recordData_buf = 0;
110         rec->recordData_len = 0;
111         rec->recordPosition = 0;
112         *extra = 0;
113
114         ex.extraRecordData_buf = 0;
115         ex.extraRecordData_len = 0;
116         ex.recordIdentifier = 0;
117
118         for (ptr = pptr->children; ptr; ptr = ptr->next)
119         {
120
121             if (yaz_match_xsd_string(ptr, "recordSchema", o,
122                                      &rec->recordSchema))
123                 ;
124             else if (yaz_match_xsd_string(ptr, "recordPacking", o, &spack))
125                 ;  /* can't rely on it: in SRU 2.0 it's different semantics */
126             else if (yaz_match_xsd_integer(ptr, "recordPosition", o,
127                                            &rec->recordPosition))
128                 ;
129             else if (yaz_match_xsd_element(ptr, "recordData"))
130             {
131                 /* we assume XML packing, if any element nodes exist below
132                    recordData. Unfortunately, in SRU 2.0 recordPacking
133                    means something different */
134                 xmlNode *p = ptr->children;
135                 for (; p; p = p->next)
136                     if (p->type == XML_ELEMENT_NODE)
137                         break;
138                 if (p)
139                 {
140                     yaz_match_xsd_XML_n2(
141                         ptr, "recordData", o,
142                         &rec->recordData_buf, &rec->recordData_len, 1);
143                     rec->recordPacking = Z_SRW_recordPacking_XML;
144                 }
145                 else
146                 {
147                     yaz_match_xsd_string_n(
148                         ptr, "recordData", o,
149                         &rec->recordData_buf, &rec->recordData_len);
150                     rec->recordPacking = Z_SRW_recordPacking_string;
151                 }
152             }
153             else if (yaz_match_xsd_XML_n(ptr, "extraRecordData", o,
154                                          &ex.extraRecordData_buf,
155                                          &ex.extraRecordData_len) )
156                 ;
157             else
158                 yaz_match_xsd_string(ptr, "recordIdentifier", o,
159                                      &ex.recordIdentifier);
160         }
161         if (ex.extraRecordData_buf || ex.recordIdentifier)
162         {
163             *extra = (Z_SRW_extra_record *)
164                 odr_malloc(o, sizeof(Z_SRW_extra_record));
165             memcpy(*extra, &ex, sizeof(Z_SRW_extra_record));
166         }
167     }
168     else if (o->direction == ODR_ENCODE)
169     {
170         xmlNodePtr ptr = pptr;
171         int pack = rec->recordPacking;
172         const char *spack = yaz_srw_pack_to_str(pack);
173
174         add_xsd_string(ptr, "recordSchema", rec->recordSchema);
175         if (spack)
176         {
177             if (version2)
178                 add_xsd_string(ptr, "recordXMLEscaping", spack);
179             else
180                 add_xsd_string(ptr, "recordPacking", spack);
181         }
182         switch (pack)
183         {
184         case Z_SRW_recordPacking_string:
185             add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
186                              rec->recordData_len);
187             break;
188         case Z_SRW_recordPacking_XML:
189             add_XML_n(ptr, "recordData", rec->recordData_buf,
190                       rec->recordData_len, 0);
191             break;
192         case Z_SRW_recordPacking_URL:
193             add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
194                              rec->recordData_len);
195             break;
196         }
197         if (rec->recordPosition)
198             add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
199         if (extra && *extra)
200         {
201             if ((*extra)->recordIdentifier)
202                 add_xsd_string(ptr, "recordIdentifier",
203                                (*extra)->recordIdentifier);
204             if ((*extra)->extraRecordData_buf)
205                 add_XML_n(ptr, "extraRecordData",
206                           (*extra)->extraRecordData_buf,
207                           (*extra)->extraRecordData_len, 0);
208         }
209     }
210     return 0;
211 }
212
213 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
214                            Z_SRW_extra_record ***extra,
215                            int *num, void *client_data, int version2)
216 {
217     if (o->direction == ODR_DECODE)
218     {
219         int i;
220         xmlNodePtr ptr;
221         *num = 0;
222         for (ptr = pptr->children; ptr; ptr = ptr->next)
223         {
224             if (ptr->type == XML_ELEMENT_NODE &&
225                 !xmlStrcmp(ptr->name, BAD_CAST "record"))
226                 (*num)++;
227         }
228         if (!*num)
229             return 1;
230         *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
231         *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
232         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
233         {
234             if (ptr->type == XML_ELEMENT_NODE &&
235                 !xmlStrcmp(ptr->name, BAD_CAST "record"))
236             {
237                 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, 0);
238                 i++;
239             }
240         }
241     }
242     else if (o->direction == ODR_ENCODE)
243     {
244         int i;
245         for (i = 0; i < *num; i++)
246         {
247             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
248                                           0);
249             yaz_srw_record(o, rptr, (*recs)+i, (*extra ? *extra + i : 0),
250                            client_data, version2);
251         }
252     }
253     return 0;
254 }
255
256 static int yaz_srw_version(ODR o, xmlNodePtr pptr, Z_SRW_recordVersion *rec,
257                            void *client_data, const char *ns)
258 {
259     if (o->direction == ODR_DECODE)
260     {
261         xmlNodePtr ptr;
262         rec->versionType = 0;
263         rec->versionValue = 0;
264         for (ptr = pptr->children; ptr; ptr = ptr->next)
265         {
266
267             if (yaz_match_xsd_string(ptr, "versionType", o,
268                                      &rec->versionType))
269                 ;
270             else
271                 yaz_match_xsd_string(ptr, "versionValue", o,
272                                      &rec->versionValue);
273         }
274     }
275     else if (o->direction == ODR_ENCODE)
276     {
277         xmlNodePtr ptr = pptr;
278         add_xsd_string(ptr, "versionType", rec->versionType);
279         add_xsd_string(ptr, "versionValue", rec->versionValue);
280     }
281     return 0;
282 }
283
284 static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
285                             Z_SRW_recordVersion **vers,
286                             int *num, void *client_data, const char *ns)
287 {
288     if (o->direction == ODR_DECODE)
289     {
290         int i;
291         xmlNodePtr ptr;
292         *num = 0;
293         for (ptr = pptr->children; ptr; ptr = ptr->next)
294         {
295             if (ptr->type == XML_ELEMENT_NODE &&
296                 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
297                 (*num)++;
298         }
299         if (!*num)
300             return 1;
301         *vers = (Z_SRW_recordVersion *) odr_malloc(o, *num * sizeof(**vers));
302         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
303         {
304             if (ptr->type == XML_ELEMENT_NODE &&
305                 !xmlStrcmp(ptr->name, BAD_CAST "recordVersion"))
306             {
307                 yaz_srw_version(o, ptr, *vers + i, client_data, ns);
308                 i++;
309             }
310         }
311     }
312     else if (o->direction == ODR_ENCODE)
313     {
314         int i;
315         for (i = 0; i < *num; i++)
316         {
317             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "version",
318                                           0);
319             yaz_srw_version(o, rptr, (*vers)+i, client_data, ns);
320         }
321     }
322     return 0;
323 }
324
325 Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node)
326 {
327     Odr_int freq;
328     xmlNodePtr child;
329     WRBUF wrbuf = wrbuf_alloc();
330     Z_FacetTerm *facet_term;
331     const char *freq_string = yaz_element_attribute_value_get(
332         node, "facetvalue", "est_representation");
333     if (freq_string)
334         freq = odr_atoi(freq_string);
335     else
336         freq = -1;
337
338     for (child = node->children; child ; child = child->next)
339     {
340         if (child->type == XML_TEXT_NODE)
341             wrbuf_puts(wrbuf, (const char *) child->content);
342     }
343     facet_term = facet_term_create_cstr(odr, wrbuf_cstr(wrbuf), freq);
344     wrbuf_destroy(wrbuf);
345     return facet_term;
346 }
347
348 static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr)
349 {
350     Z_AttributeList *list;
351     Z_FacetField *facet_field;
352     int num_terms = 0;
353     int index = 0;
354     xmlNodePtr node;
355     /* USE attribute */
356     const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
357     yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
358
359     list = yaz_use_attribute_create(odr, name);
360     for (node = ptr->children; node; node = node->next) {
361         if (yaz_match_xsd_element(node, "facetvalue"))
362             num_terms++;
363     }
364     facet_field = facet_field_create(odr, list, num_terms);
365     index = 0;
366     for (node = ptr->children; node; node = node->next)
367     {
368         if (yaz_match_xsd_element(node, "facetvalue"))
369         {
370             facet_field_term_set(odr, facet_field,
371                                  yaz_sru_proxy_get_facet_term_count(odr, node),
372                                  index);
373             index++;
374         }
375     }
376     return facet_field;
377 }
378
379 static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root,
380                                        Z_FacetList **facetList)
381 {
382     xmlNodePtr ptr;
383
384     for (ptr = root->children; ptr; ptr = ptr->next)
385     {
386         if (yaz_match_xsd_element(ptr, "facets"))
387         {
388             xmlNodePtr node;
389             Z_FacetList *facet_list;
390             int num_facets = 0;
391             for (node = ptr->children; node; node= node->next)
392             {
393                 if (node->type == XML_ELEMENT_NODE)
394                     num_facets++;
395             }
396             facet_list = facet_list_create(o, num_facets);
397             num_facets = 0;
398             for (node = ptr->children; node; node= node->next)
399             {
400                 if (yaz_match_xsd_element(node, "facet"))
401                 {
402                     facet_list_field_set(
403                         o, facet_list,
404                         yaz_sru_proxy_decode_facet_field(o, node), num_facets);
405                     num_facets++;
406                 }
407             }
408             *facetList = facet_list;
409             break;
410         }
411     }
412     return 0;
413 }
414
415
416
417 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
418                                       Z_SRW_diagnostic **recs, int *num,
419                                       void *client_data, const char *ns)
420 {
421     int i;
422     xmlNodePtr ptr;
423     *num = 0;
424     for (ptr = pptr; ptr; ptr = ptr->next)
425     {
426         if (ptr->type == XML_ELEMENT_NODE &&
427             !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
428             (*num)++;
429     }
430     if (!*num)
431         return 1;
432     *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
433     for (i = 0; i < *num; i++)
434     {
435         (*recs)[i].uri = 0;
436         (*recs)[i].details = 0;
437         (*recs)[i].message = 0;
438     }
439     for (i = 0, ptr = pptr; ptr; ptr = ptr->next)
440     {
441         if (ptr->type == XML_ELEMENT_NODE &&
442             !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
443         {
444             xmlNodePtr rptr;
445             (*recs)[i].uri = 0;
446             (*recs)[i].details = 0;
447             (*recs)[i].message = 0;
448             for (rptr = ptr->children; rptr; rptr = rptr->next)
449             {
450                 if (yaz_match_xsd_string(rptr, "uri", o, &(*recs)[i].uri))
451                     ;
452                 else if (yaz_match_xsd_string(rptr, "details", o,
453                                               &(*recs)[i].details))
454                     ;
455                 else
456                     yaz_match_xsd_string(rptr, "message", o,
457                                          &(*recs)[i].message);
458             }
459             i++;
460         }
461     }
462     return 0;
463 }
464
465 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
466                                      Z_SRW_diagnostic **diag,
467                                      int *num, ODR odr)
468 {
469     int ret = 0;
470     xmlDocPtr doc = xmlParseMemory(buf, len);
471     if (doc)
472     {
473         xmlNodePtr ptr = xmlDocGetRootElement(doc);
474         while (ptr && ptr->type != XML_ELEMENT_NODE)
475             ptr = ptr->next;
476         if (ptr && ptr->ns
477             && !xmlStrcmp(ptr->ns->href,
478                           BAD_CAST "http://www.loc.gov/zing/srw/diagnostic/"))
479         {
480             ret = yaz_srw_decode_diagnostics(odr, ptr, diag, num, 0, 0);
481         }
482         xmlFreeDoc(doc);
483     }
484     return ret;
485 }
486
487 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
488                                int *num, void *client_data, const char *ns)
489 {
490     if (o->direction == ODR_DECODE)
491     {
492         return yaz_srw_decode_diagnostics(o, pptr->children, recs, num, client_data, ns);
493     }
494     else if (o->direction == ODR_ENCODE)
495     {
496         int i;
497         xmlNsPtr ns_diag =
498             xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1, BAD_CAST "diag" );
499         for (i = 0; i < *num; i++)
500         {
501             const char *std_diag = "info:srw/diagnostic/1/";
502             const char *ucp_diag = "info:srw/diagnostic/12/";
503             xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
504                                           BAD_CAST "diagnostic", 0);
505             add_xsd_string(rptr, "uri", (*recs)[i].uri);
506             if ((*recs)[i].message)
507                 add_xsd_string(rptr, "message", (*recs)[i].message);
508             else if ((*recs)[i].uri )
509             {
510                 if (!strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
511                 {
512                     int no = atoi((*recs)[i].uri + strlen(std_diag));
513                     const char *message = yaz_diag_srw_str(no);
514                     if (message)
515                         add_xsd_string(rptr, "message", message);
516                 }
517                 else if (!strncmp((*recs)[i].uri, ucp_diag, strlen(ucp_diag)))
518                 {
519                     int no = atoi((*recs)[i].uri + strlen(ucp_diag));
520                     const char *message = yaz_diag_sru_update_str(no);
521                     if (message)
522                         add_xsd_string(rptr, "message", message);
523                 }
524             }
525             add_xsd_string(rptr, "details", (*recs)[i].details);
526         }
527     }
528     return 0;
529 }
530
531 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
532                         void *client_data, const char *ns)
533 {
534     if (o->direction == ODR_DECODE)
535     {
536         xmlNodePtr ptr;
537         term->value = 0;
538         term->numberOfRecords = 0;
539         term->displayTerm = 0;
540         term->whereInList = 0;
541         for (ptr = pptr->children; ptr; ptr = ptr->next)
542         {
543             if (yaz_match_xsd_string(ptr, "value", o,  &term->value))
544                 ;
545             else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
546                                            &term->numberOfRecords))
547                 ;
548             else if (yaz_match_xsd_string(ptr, "displayTerm", o,
549                                           &term->displayTerm))
550                 ;
551             else
552                 yaz_match_xsd_string(ptr, "whereInList", o, &term->whereInList);
553         }
554     }
555     else if (o->direction == ODR_ENCODE)
556     {
557         xmlNodePtr ptr = pptr;
558         add_xsd_string(ptr, "value", term->value);
559         add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
560         add_xsd_string(ptr, "displayTerm", term->displayTerm);
561         add_xsd_string(ptr, "whereInList", term->whereInList);
562     }
563     return 0;
564 }
565
566 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
567                          int *num, void *client_data, const char *ns)
568 {
569     if (o->direction == ODR_DECODE)
570     {
571         int i;
572         xmlNodePtr ptr;
573         *num = 0;
574         for (ptr = pptr->children; ptr; ptr = ptr->next)
575         {
576             if (ptr->type == XML_ELEMENT_NODE &&
577                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
578                 (*num)++;
579         }
580         if (!*num)
581             return 1;
582         *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
583         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
584         {
585             if (ptr->type == XML_ELEMENT_NODE &&
586                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
587                 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
588         }
589     }
590     else if (o->direction == ODR_ENCODE)
591     {
592         int i;
593         for (i = 0; i < *num; i++)
594         {
595             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
596             yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
597         }
598     }
599     return 0;
600 }
601
602 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
603                   void *client_data, const char *ns)
604 {
605     xmlNodePtr pptr = (xmlNodePtr) vptr;
606     if (o->direction == ODR_DECODE)
607     {
608         Z_SRW_PDU **p = handler_data;
609         xmlNodePtr method = pptr->children;
610         char *neg_version;
611
612         while (method && method->type == XML_TEXT_NODE)
613             method = method->next;
614
615         if (!method)
616             return -1;
617         if (method->type != XML_ELEMENT_NODE)
618             return -1;
619
620         *p = yaz_srw_get_core_v_2_0(o);
621
622         if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
623         {
624             xmlNodePtr ptr = method->children;
625             Z_SRW_searchRetrieveRequest *req;
626             char *recordPacking = 0;
627             char *recordXMLEscaping = 0;
628             const char *facetLimit = 0;
629
630             (*p)->which = Z_SRW_searchRetrieve_request;
631             req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
632                 odr_malloc(o, sizeof(*req));
633             req->queryType = "cql";
634             req->query = 0;
635             req->sort_type = Z_SRW_sort_type_none;
636             req->sort.none = 0;
637             req->startRecord = 0;
638             req->maximumRecords = 0;
639             req->recordSchema = 0;
640             req->recordPacking = 0;
641             req->packing = 0;
642             req->recordXPath = 0;
643             req->resultSetTTL = 0;
644             req->stylesheet = 0;
645             req->database = 0;
646
647             for (; ptr; ptr = ptr->next)
648             {
649                 if (yaz_match_xsd_string(ptr, "version", o,
650                                      &(*p)->srw_version))
651                     ;
652                 else if (yaz_match_xsd_string(ptr, "queryType", o,
653                                           &req->queryType))
654                     ;
655                 else if (yaz_match_xsd_string(ptr, "query", o,
656                                           &req->query))
657                     ;
658                 else if (yaz_match_xsd_string(ptr, "pQuery", o,
659                                           &req->query))
660                     req->queryType = "pqf";
661                 else if (yaz_match_xsd_string(ptr, "xQuery", o,
662                                           &req->query))
663                     req->queryType = "xcql";
664                 else if (yaz_match_xsd_integer(ptr, "startRecord", o,
665                                            &req->startRecord))
666                     ;
667                 else if (yaz_match_xsd_integer(ptr, "maximumRecords", o,
668                                            &req->maximumRecords))
669                     ;
670                 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
671                                           &recordPacking))
672                     ;
673                 else if (yaz_match_xsd_string(ptr, "recordXMLEscaping", o,
674                                           &recordXMLEscaping))
675                     ;
676                 else if (yaz_match_xsd_string(ptr, "recordSchema", o,
677                                           &req->recordSchema))
678                     ;
679                 else if (yaz_match_xsd_string(ptr, "recordXPath", o,
680                                           &req->recordXPath))
681                     ;
682                 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
683                                            &req->resultSetTTL))
684                     ;
685                 else if (yaz_match_xsd_string(ptr, "sortKeys", o,
686                                           &req->sort.sortKeys))
687                     req->sort_type = Z_SRW_sort_type_sort;
688                 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
689                                           &req->stylesheet))
690                     ;
691                 else if (yaz_match_xsd_string(ptr, "database", o, &req->database))
692                     ;
693                 else if (yaz_match_xsd_string(ptr, "facetLimit", o,
694                                           (char**) &facetLimit))
695                     ;
696             }
697             if (!req->query)
698             {
699                 /* should put proper diagnostic here */
700                 return -1;
701             }
702             if (!strcmp((*p)->srw_version, "2.0"))
703             {
704                 req->recordPacking = recordXMLEscaping;
705                 req->packing = recordPacking;
706             }
707             else
708             {
709                 req->recordPacking = recordPacking;
710             }
711             yaz_sru_facet_request(o, &req->facetList, &facetLimit);
712         }
713         else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
714         {
715             xmlNodePtr ptr = method->children;
716             Z_SRW_searchRetrieveResponse *res;
717
718             (*p)->which = Z_SRW_searchRetrieve_response;
719             res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
720                 odr_malloc(o, sizeof(*res));
721
722             res->numberOfRecords = 0;
723             res->resultCountPrecision = 0;
724             res->resultSetId = 0;
725             res->resultSetIdleTime = 0;
726             res->records = 0;
727             res->num_records = 0;
728             res->diagnostics = 0;
729             res->num_diagnostics = 0;
730             res->nextRecordPosition = 0;
731             res->facetList = 0;
732             res->suggestions = 0;
733
734             for (; ptr; ptr = ptr->next)
735             {
736                 if (yaz_match_xsd_string(ptr, "version", o,
737                                      &(*p)->srw_version))
738                     ;
739                 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
740                                          &(*p)->extraResponseData_buf,
741                                          &(*p)->extraResponseData_len))
742                     ;
743                 else if (yaz_match_xsd_integer(ptr, "numberOfRecords", o,
744                                            &res->numberOfRecords))
745                     ;
746                 else if (yaz_match_xsd_string(ptr, "resultCountPrecision", o,
747                                            &res->resultCountPrecision))
748                     ;
749                 else if (yaz_match_xsd_string(ptr, "resultSetId", o,
750                                           &res->resultSetId))
751                     ;
752                 else if (yaz_match_xsd_integer(ptr, "resultSetIdleTime", o,
753                                            &res->resultSetIdleTime))
754                     ;
755                 else if (yaz_match_xsd_integer(ptr, "resultSetTTL", o,
756                                            &res->resultSetIdleTime))
757                     ;
758                 else if (yaz_match_xsd_element(ptr, "records"))
759                     yaz_srw_records(o, ptr, &res->records,
760                                     &res->extra_records,
761                                     &res->num_records, client_data, 0);
762                 else if (yaz_match_xsd_integer(ptr, "nextRecordPosition", o,
763                                            &res->nextRecordPosition))
764                     ;
765                 else if (yaz_match_xsd_element(ptr, "diagnostics"))
766                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
767                                         &res->num_diagnostics,
768                                         client_data, ns);
769                 else if (yaz_match_xsd_element(ptr, "facet_analysis"))
770                     yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
771                 else if (yaz_match_xsd_element(ptr, "facetedResults"))
772                     yaz_sru_facet_response(o, &res->facetList, ptr);
773             }
774         }
775         else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
776         {
777             Z_SRW_explainRequest *req;
778             xmlNodePtr ptr = method->children;
779
780             (*p)->which = Z_SRW_explain_request;
781             req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
782                 odr_malloc(o, sizeof(*req));
783             req->recordPacking = 0;
784             req->packing = 0;
785             req->database = 0;
786             req->stylesheet = 0;
787             for (; ptr; ptr = ptr->next)
788             {
789                 if (yaz_match_xsd_string(ptr, "version", o,
790                                      &(*p)->srw_version))
791                     ;
792                 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
793                                          &(*p)->extraResponseData_buf,
794                                          &(*p)->extraResponseData_len))
795                     ;
796                 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
797                                           &req->stylesheet))
798                     ;
799                 else if (yaz_match_xsd_string(ptr, "recordPacking", o,
800                                           &req->recordPacking))
801                     ;
802                 else
803                     yaz_match_xsd_string(ptr, "database", o, &req->database);
804             }
805         }
806         else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
807         {
808             Z_SRW_explainResponse *res;
809             xmlNodePtr ptr = method->children;
810
811             (*p)->which = Z_SRW_explain_response;
812             res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
813                 odr_malloc(o, sizeof(*res));
814             res->diagnostics = 0;
815             res->num_diagnostics = 0;
816             res->record.recordSchema = 0;
817             res->record.recordData_buf = 0;
818             res->record.recordData_len = 0;
819             res->record.recordPosition = 0;
820
821             for (; ptr; ptr = ptr->next)
822             {
823                 if (yaz_match_xsd_string(ptr, "version", o,
824                                      &(*p)->srw_version))
825                     ;
826                 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
827                                          &(*p)->extraResponseData_buf,
828                                          &(*p)->extraResponseData_len))
829                     ;
830                 else if (yaz_match_xsd_element(ptr, "record"))
831                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
832                                    client_data, 0);
833                 else if (yaz_match_xsd_element(ptr, "diagnostics"))
834                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
835                                         &res->num_diagnostics,
836                                         client_data, ns);
837                 ;
838             }
839         }
840         else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
841         {
842             Z_SRW_scanRequest *req;
843             xmlNodePtr ptr = method->children;
844
845             (*p)->which = Z_SRW_scan_request;
846             req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
847                 odr_malloc(o, sizeof(*req));
848             req->queryType = "cql";
849             req->scanClause = 0;
850             req->responsePosition = 0;
851             req->maximumTerms = 0;
852             req->stylesheet = 0;
853             req->database = 0;
854
855             for (; ptr; ptr = ptr->next)
856             {
857                 if (yaz_match_xsd_string(ptr, "version", o,
858                                      &(*p)->srw_version))
859                     ;
860                 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
861                                          &(*p)->extraResponseData_buf,
862                                          &(*p)->extraResponseData_len))
863                     ;
864                 else if (yaz_match_xsd_string(ptr, "scanClause", o,
865                                           &req->scanClause))
866                     ;
867                 else if (yaz_match_xsd_string(ptr, "pScanClause", o,
868                                           &req->scanClause))
869                 {
870                     req->queryType = "pqf";
871                 }
872                 else if (yaz_match_xsd_integer(ptr, "responsePosition", o,
873                                            &req->responsePosition))
874                     ;
875                 else if (yaz_match_xsd_integer(ptr, "maximumTerms", o,
876                                            &req->maximumTerms))
877                     ;
878                 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
879                                           &req->stylesheet))
880                     ;
881                 else
882                     yaz_match_xsd_string(ptr, "database", o, &req->database);
883             }
884         }
885         else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
886         {
887             Z_SRW_scanResponse *res;
888             xmlNodePtr ptr = method->children;
889
890             (*p)->which = Z_SRW_scan_response;
891             res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
892                 odr_malloc(o, sizeof(*res));
893             res->terms = 0;
894             res->num_terms = 0;
895             res->diagnostics = 0;
896             res->num_diagnostics = 0;
897
898             for (; ptr; ptr = ptr->next)
899             {
900                 if (yaz_match_xsd_string(ptr, "version", o,
901                                      &(*p)->srw_version))
902                     ;
903                 else if (yaz_match_xsd_XML_n(ptr, "extraResponseData", o,
904                                          &(*p)->extraResponseData_buf,
905                                          &(*p)->extraResponseData_len))
906                     ;
907                 else if (yaz_match_xsd_element(ptr, "terms"))
908                     yaz_srw_terms(o, ptr, &res->terms,
909                                   &res->num_terms, client_data,
910                                   ns);
911                 else if (yaz_match_xsd_element(ptr, "diagnostics"))
912                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
913                                         &res->num_diagnostics,
914                                         client_data, ns);
915             }
916         }
917         else
918         {
919             *p = 0;
920             return -1;
921         }
922         neg_version = yaz_negotiate_sru_version((*p)->srw_version);
923         if (neg_version)
924             (*p)->srw_version = neg_version;
925     }
926     else if (o->direction == ODR_ENCODE)
927     {
928         Z_SRW_PDU **p = handler_data;
929         xmlNsPtr ns_srw;
930         xmlNodePtr ptr = 0;
931         int version2 = !(*p)->srw_version ||
932             strcmp((*p)->srw_version, "2.") > 0;
933         if ((*p)->which == Z_SRW_searchRetrieve_request)
934         {
935             Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
936             const char *queryType = req->queryType;
937             if (version2)
938                 ns = "http://docs.oasis-open.org/ns/search-ws/sruRequest";
939             ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
940             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
941             xmlSetNs(ptr, ns_srw);
942
943             add_xsd_string(ptr, "version", (*p)->srw_version);
944             if (version2)
945             {
946                 if (queryType)
947                     add_xsd_string(ptr, "queryType", queryType);
948                 add_xsd_string(ptr, "query", req->query);
949             }
950             else
951             {
952                 if (!queryType || !strcmp(queryType, "cql"))
953                     add_xsd_string(ptr, "query", req->query);
954                 else if (!strcmp(queryType, "xcql"))
955                     add_xsd_string(ptr, "xQuery", req->query);
956                 else if (!strcmp(queryType, "pqf"))
957                     add_xsd_string(ptr, "pQuery", req->query);
958             }
959             add_xsd_integer(ptr, "startRecord", req->startRecord);
960             add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
961             if (version2)
962             {
963                 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
964                 add_xsd_string(ptr, "recordPacking", req->packing);
965             }
966             else
967                 add_xsd_string(ptr, "recordPacking", req->recordPacking);
968             add_xsd_string(ptr, "recordSchema", req->recordSchema);
969             add_xsd_string(ptr, "recordXPath", req->recordXPath);
970             add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
971             switch (req->sort_type)
972             {
973             case Z_SRW_sort_type_none:
974                 break;
975             case Z_SRW_sort_type_sort:
976                 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
977                 break;
978             case Z_SRW_sort_type_xSort:
979                 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
980                 break;
981             }
982             add_xsd_string(ptr, "stylesheet", req->stylesheet);
983             add_xsd_string(ptr, "database", req->database);
984             {
985                 const char *limit = 0;
986                 yaz_sru_facet_request(o, &req->facetList, &limit);
987                 add_xsd_string(ptr, "facetLimit", limit);
988             }
989         }
990         else if ((*p)->which == Z_SRW_searchRetrieve_response)
991         {
992             Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
993             if (version2)
994                 ns = "http://docs.oasis-open.org/ns/search-ws/sruResponse";
995             ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
996             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
997             xmlSetNs(ptr, ns_srw);
998
999             add_xsd_string(ptr, "version", (*p)->srw_version);
1000             add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
1001             add_xsd_string(ptr, "resultSetId", res->resultSetId);
1002             add_xsd_integer(ptr,
1003                             version2 ? "resultSetTTL" : "resultSetIdleTime" ,
1004                             res->resultSetIdleTime);
1005             if (res->num_records)
1006             {
1007                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
1008                 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
1009                                 &res->num_records,
1010                                 client_data, version2);
1011             }
1012             add_xsd_integer(ptr, "nextRecordPosition",
1013                             res->nextRecordPosition);
1014             if (res->num_diagnostics)
1015             {
1016                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1017                                               0);
1018                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1019                                     &res->num_diagnostics, client_data, ns);
1020             }
1021             if (res->resultCountPrecision)
1022                 add_xsd_string(ptr, "resultCountPrecision",
1023                                res->resultCountPrecision);
1024             yaz_sru_facet_response(o, &res->facetList, ptr);
1025         }
1026         else if ((*p)->which == Z_SRW_explain_request)
1027         {
1028             Z_SRW_explainRequest *req = (*p)->u.explain_request;
1029             ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
1030             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1031             xmlSetNs(ptr, ns_srw);
1032
1033             add_xsd_string(ptr, "version", (*p)->srw_version);
1034             if (version2)
1035             {
1036                 add_xsd_string(ptr, "recordXMLEscaping", req->recordPacking);
1037                 add_xsd_string(ptr, "recordPacking", req->packing);
1038             }
1039             else
1040                 add_xsd_string(ptr, "recordPacking", req->recordPacking);
1041             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1042             add_xsd_string(ptr, "database", req->database);
1043         }
1044         else if ((*p)->which == Z_SRW_explain_response)
1045         {
1046             Z_SRW_explainResponse *res = (*p)->u.explain_response;
1047             ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
1048             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1049             xmlSetNs(ptr, ns_srw);
1050
1051             add_xsd_string(ptr, "version", (*p)->srw_version);
1052             if (1)
1053             {
1054                 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1055                 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
1056                                client_data, version2);
1057             }
1058             if (res->num_diagnostics)
1059             {
1060                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1061                                               0);
1062                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1063                                     &res->num_diagnostics, client_data, ns);
1064             }
1065         }
1066         else if ((*p)->which == Z_SRW_scan_request)
1067         {
1068             Z_SRW_scanRequest *req = (*p)->u.scan_request;
1069             const char *queryType = req->queryType;
1070             if (version2)
1071                 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1072             ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
1073             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1074             xmlSetNs(ptr, ns_srw);
1075
1076             add_xsd_string(ptr, "version", (*p)->srw_version);
1077
1078             if (version2)
1079             {
1080                 if (queryType && strcmp(queryType, "cql"))
1081                     add_xsd_string(ptr, "queryType", queryType);
1082                 add_xsd_string(ptr, "scanClause", req->scanClause);
1083             }
1084             else
1085             {
1086                 if (!queryType || !strcmp(queryType, "cql"))
1087                     add_xsd_string(ptr, "scanClause", req->scanClause);
1088                 else if (!strcmp(queryType, "pqf"))
1089                     add_xsd_string(ptr, "pScanClause", req->scanClause);
1090             }
1091             add_xsd_integer(ptr, "responsePosition", req->responsePosition);
1092             add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
1093             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1094             add_xsd_string(ptr, "database", req->database);
1095         }
1096         else if ((*p)->which == Z_SRW_scan_response)
1097         {
1098             Z_SRW_scanResponse *res = (*p)->u.scan_response;
1099             if (version2)
1100                 ns = "http://docs.oasis-open.org/ns/search-ws/scan";
1101             ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
1102             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
1103             xmlSetNs(ptr, ns_srw);
1104
1105             add_xsd_string(ptr, "version", (*p)->srw_version);
1106             if (res->num_terms)
1107             {
1108                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
1109                 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
1110                               client_data, ns);
1111             }
1112             if (res->num_diagnostics)
1113             {
1114                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
1115                                               0);
1116                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1117                                     &res->num_diagnostics, client_data, ns);
1118             }
1119         }
1120         else
1121             return -1;
1122         if (ptr && (*p)->extraResponseData_len)
1123             add_XML_n(ptr, "extraResponseData",
1124                       (*p)->extraResponseData_buf,
1125                       (*p)->extraResponseData_len, ns_srw);
1126
1127
1128     }
1129     return 0;
1130 }
1131
1132 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
1133                   void *client_data, const char *ns_ucp_str)
1134 {
1135     xmlNodePtr pptr = (xmlNodePtr) vptr;
1136     const char *ns_srw_str = YAZ_XMLNS_SRU_v1_1;
1137     if (o->direction == ODR_DECODE)
1138     {
1139         Z_SRW_PDU **p = handler_data;
1140         xmlNodePtr method = pptr->children;
1141
1142         while (method && method->type == XML_TEXT_NODE)
1143             method = method->next;
1144
1145         if (!method)
1146             return -1;
1147         if (method->type != XML_ELEMENT_NODE)
1148             return -1;
1149
1150         *p = yaz_srw_get_core_v_2_0(o);
1151
1152         if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
1153         {
1154             xmlNodePtr ptr = method->children;
1155             Z_SRW_updateRequest *req;
1156             char *oper = 0;
1157
1158             (*p)->which = Z_SRW_update_request;
1159             req = (*p)->u.update_request = (Z_SRW_updateRequest *)
1160                 odr_malloc(o, sizeof(*req));
1161             req->database = 0;
1162             req->operation = 0;
1163             req->recordId = 0;
1164             req->recordVersions = 0;
1165             req->num_recordVersions = 0;
1166             req->record = 0;
1167             req->extra_record = 0;
1168             req->extraRequestData_buf = 0;
1169             req->extraRequestData_len = 0;
1170             req->stylesheet = 0;
1171
1172             for (; ptr; ptr = ptr->next)
1173             {
1174                 if (yaz_match_xsd_string(ptr, "version", o,
1175                                      &(*p)->srw_version))
1176                     ;
1177                 else if (yaz_match_xsd_string(ptr, "action", o,
1178                                           &oper)){
1179                     if (oper)
1180                     {
1181                         if (!strcmp(oper, "info:srw/action/1/delete"))
1182                             req->operation = "delete";
1183                         else if (!strcmp(oper,"info:srw/action/1/replace" ))
1184                             req->operation = "replace";
1185                         else if (!strcmp(oper, "info:srw/action/1/create"))
1186                             req->operation = "insert";
1187                     }
1188                 }
1189                 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1190                                           &req->recordId))
1191                     ;
1192                 else if (yaz_match_xsd_element(ptr, "recordVersions" ) )
1193                     yaz_srw_versions( o, ptr, &req->recordVersions,
1194                                       &req->num_recordVersions, client_data,
1195                                       ns_ucp_str);
1196                 else if (yaz_match_xsd_element(ptr, "record"))
1197                 {
1198                     req->record = yaz_srw_get_record(o);
1199                     yaz_srw_record(o, ptr, req->record, &req->extra_record,
1200                                    client_data, 0);
1201                 }
1202                 else if (yaz_match_xsd_string(ptr, "stylesheet", o,
1203                                           &req->stylesheet))
1204                     ;
1205                 else
1206                     yaz_match_xsd_string(ptr, "database", o, &req->database);
1207             }
1208         }
1209         else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1210         {
1211             xmlNodePtr ptr = method->children;
1212             Z_SRW_updateResponse *res;
1213
1214             (*p)->which = Z_SRW_update_response;
1215             res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1216                 odr_malloc(o, sizeof(*res));
1217
1218             res->operationStatus = 0;
1219             res->recordId = 0;
1220             res->recordVersions = 0;
1221             res->num_recordVersions = 0;
1222             res->diagnostics = 0;
1223             res->num_diagnostics = 0;
1224             res->record = 0;
1225             res->extra_record = 0;
1226             res->extraResponseData_buf = 0;
1227             res->extraResponseData_len = 0;
1228
1229             for (; ptr; ptr = ptr->next)
1230             {
1231                 if (yaz_match_xsd_string(ptr, "version", o,
1232                                      &(*p)->srw_version))
1233                     ;
1234                 else if (yaz_match_xsd_string(ptr, "operationStatus", o,
1235                                           &res->operationStatus ))
1236                     ;
1237                 else if (yaz_match_xsd_string(ptr, "recordIdentifier", o,
1238                                           &res->recordId))
1239                     ;
1240                 else if (yaz_match_xsd_element(ptr, "recordVersions" ))
1241                     yaz_srw_versions(o, ptr, &res->recordVersions,
1242                                      &res->num_recordVersions,
1243                                      client_data, ns_ucp_str);
1244                 else if (yaz_match_xsd_element(ptr, "record"))
1245                 {
1246                     res->record = yaz_srw_get_record(o);
1247                     yaz_srw_record(o, ptr, res->record, &res->extra_record,
1248                                    client_data, 0);
1249                 }
1250                 else if (yaz_match_xsd_element(ptr, "diagnostics"))
1251                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1252                                         &res->num_diagnostics,
1253                                         client_data, ns_ucp_str);
1254             }
1255         }
1256         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1257         {
1258         }
1259         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1260         {
1261         }
1262         else
1263         {
1264             *p = 0;
1265             return -1;
1266         }
1267     }
1268     else if (o->direction == ODR_ENCODE)
1269     {
1270         Z_SRW_PDU **p = handler_data;
1271         xmlNsPtr ns_ucp, ns_srw;
1272
1273         if ((*p)->which == Z_SRW_update_request)
1274         {
1275             Z_SRW_updateRequest *req = (*p)->u.update_request;
1276             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "updateRequest", 0);
1277             ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1278             xmlSetNs(ptr, ns_ucp);
1279             ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1280
1281             add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1282             add_xsd_string(ptr, "action", req->operation);
1283             add_xsd_string(ptr, "recordIdentifier", req->recordId );
1284             if (req->recordVersions)
1285                 yaz_srw_versions( o, ptr, &req->recordVersions,
1286                                   &req->num_recordVersions,
1287                                   client_data, ns_ucp_str);
1288             if (req->record && req->record->recordData_len)
1289             {
1290                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1291                 xmlSetNs(rptr, ns_srw);
1292                 yaz_srw_record(o, rptr, req->record, &req->extra_record,
1293                                client_data, 0);
1294             }
1295             if (req->extraRequestData_len)
1296             {
1297                 add_XML_n(ptr, "extraRequestData",
1298                           req->extraRequestData_buf,
1299                           req->extraRequestData_len, ns_srw);
1300             }
1301             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1302             add_xsd_string(ptr, "database", req->database);
1303         }
1304         else if ((*p)->which == Z_SRW_update_response)
1305         {
1306             Z_SRW_updateResponse *res = (*p)->u.update_response;
1307             xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *)
1308                                          "updateResponse", 0);
1309             ns_ucp = xmlNewNs(ptr, BAD_CAST ns_ucp_str, BAD_CAST "zu");
1310             xmlSetNs(ptr, ns_ucp);
1311             ns_srw = xmlNewNs(ptr, BAD_CAST ns_srw_str, BAD_CAST "zs");
1312
1313             add_xsd_string_ns(ptr, "version", (*p)->srw_version, ns_srw);
1314             add_xsd_string(ptr, "operationStatus", res->operationStatus );
1315             add_xsd_string(ptr, "recordIdentifier", res->recordId );
1316             if (res->recordVersions)
1317                 yaz_srw_versions(o, ptr, &res->recordVersions,
1318                                  &res->num_recordVersions,
1319                                  client_data, ns_ucp_str);
1320             if (res->record && res->record->recordData_len)
1321             {
1322                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
1323                 xmlSetNs(rptr, ns_srw);
1324                 yaz_srw_record(o, rptr, res->record, &res->extra_record,
1325                                client_data, 0);
1326             }
1327             if (res->num_diagnostics)
1328             {
1329                 xmlNsPtr ns_diag =
1330                     xmlNewNs(pptr, BAD_CAST YAZ_XMLNS_DIAG_v1_1,
1331                              BAD_CAST "diag" );
1332
1333                 xmlNodePtr rptr = xmlNewChild(ptr, ns_diag, BAD_CAST "diagnostics", 0);
1334                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1335                                     &res->num_diagnostics, client_data,
1336                                     ns_ucp_str);
1337             }
1338             if (res->extraResponseData_len)
1339                 add_XML_n(ptr, "extraResponseData",
1340                           res->extraResponseData_buf,
1341                           res->extraResponseData_len, ns_srw);
1342         }
1343         else
1344             return -1;
1345     }
1346     return 0;
1347 }
1348
1349 #endif
1350
1351
1352 /*
1353  * Local variables:
1354  * c-basic-offset: 4
1355  * c-file-style: "Stroustrup"
1356  * indent-tabs-mode: nil
1357  * End:
1358  * vim: shiftwidth=4 tabstop=8 expandtab
1359  */
1360