Fix bug introduced in srw.c revision 1.37
[yaz-moved-to-github.git] / src / srw.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: srw.c,v 1.40 2005-11-11 22:07:11 adam Exp $
6  */
7 /**
8  * \file srw.c
9  * \brief Implements SRW/SRU package encoding and decoding
10  */
11
12 #include <yaz/srw.h>
13 #if HAVE_XML2
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16
17 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len)
18 {
19     if (val)
20     {
21         xmlDocPtr doc = xmlParseMemory(val,len);
22         if (doc)
23         {
24             xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
25             xmlNodePtr t = xmlDocGetRootElement(doc);
26             xmlAddChild(c, xmlCopyNode(t,1));
27             xmlFreeDoc(doc);
28         }
29     }
30 }
31
32 xmlNodePtr add_xsd_string_n(xmlNodePtr ptr, const char *elem, const char *val,
33                             int len)
34 {
35     if (val)
36     {
37         xmlNodePtr c = xmlNewChild(ptr, 0, BAD_CAST elem, 0);
38         xmlNodePtr t = xmlNewTextLen(BAD_CAST val, len);
39         xmlAddChild(c, t);
40         return t;
41     }
42     return 0;
43 }
44
45 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
46 {
47     if (val)
48         return xmlNewTextChild(ptr, 0, BAD_CAST elem,
49                                BAD_CAST val);
50     return 0;
51 }
52
53 static void add_xsd_integer(xmlNodePtr ptr, const char *elem, const int *val)
54 {
55     if (val)
56     {
57         char str[30];
58         sprintf(str, "%d", *val);
59         xmlNewTextChild(ptr, 0, BAD_CAST elem, BAD_CAST str);
60     }
61 }
62
63 static int match_element(xmlNodePtr ptr, const char *elem)
64 {
65     if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
66         return 1;
67     return 0;
68 }
69
70 #define CHECK_TYPE 0
71
72 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
73                               char **val, int *len)
74 {
75 #if CHECK_TYPE
76     struct _xmlAttr *attr;
77 #endif
78     if (!match_element(ptr, elem))
79         return 0;
80 #if CHECK_TYPE
81     for (attr = ptr->properties; attr; attr = attr->next)
82         if (!strcmp(attr->name, "type") &&
83             attr->children && attr->children->type == XML_TEXT_NODE)
84         {
85             const char *t = strchr(attr->children->content, ':');
86             if (t)
87                 t = t + 1;
88             else
89                 t = attr->children->content;
90             if (!strcmp(t, "string"))
91                 break;
92         }
93     if (!attr)
94         return 0;
95 #endif
96     ptr = ptr->children;
97     if (!ptr || ptr->type != XML_TEXT_NODE)
98     {
99         *val = "";
100         return 1;
101     }
102     *val = odr_strdup(o, (const char *) ptr->content);
103     if (len)
104         *len = xmlStrlen(ptr->content);
105     return 1;
106 }
107
108
109 static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
110                             char **val)
111 {
112     return match_xsd_string_n(ptr, elem, o, val, 0);
113 }
114
115 static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
116                            char **val, int *len)
117 {
118     xmlBufferPtr buf;
119
120     if (!match_element(ptr, elem))
121         return 0;
122     ptr = ptr->children;
123     while (ptr && (ptr->type == XML_TEXT_NODE || ptr->type == XML_COMMENT_NODE))
124         ptr = ptr->next;
125     if (!ptr)
126         return 0;
127     buf = xmlBufferCreate();
128     
129     xmlNodeDump(buf, ptr->doc, ptr, 0, 0);
130     
131     *val = odr_malloc(o, buf->use+1);
132     memcpy (*val, buf->content, buf->use);
133     (*val)[buf->use] = '\0';
134
135     if (len)
136         *len = buf->use;
137
138     xmlBufferFree(buf);
139
140     return 1;
141 }
142                      
143 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
144 {
145 #if CHECK_TYPE
146     struct _xmlAttr *attr;
147 #endif
148     if (!match_element(ptr, elem))
149         return 0;
150 #if CHECK_TYPE
151     for (attr = ptr->properties; attr; attr = attr->next)
152         if (!strcmp(attr->name, "type") &&
153             attr->children && attr->children->type == XML_TEXT_NODE)
154         {
155             const char *t = strchr(attr->children->content, ':');
156             if (t)
157                 t = t + 1;
158             else
159                 t = attr->children->content;
160             if (!strcmp(t, "integer"))
161                 break;
162         }
163     if (!attr)
164         return 0;
165 #endif
166     ptr = ptr->children;
167     if (!ptr || ptr->type != XML_TEXT_NODE)
168         return 0;
169     *val = odr_intdup(o, atoi((const char *) ptr->content));
170     return 1;
171 }
172
173 static int yaz_srw_extra_record(ODR o, xmlNodePtr pptr,
174                                 Z_SRW_extra_record *rec,
175                                 void *client_data, const char *ns)
176 {
177     if (o->direction == ODR_DECODE)
178     {
179         xmlNodePtr ptr;
180         rec->type = 1;
181         rec->recordId         = 0;
182         rec->recordReviewCode = 0;
183         rec->recordReviewNote = 0;
184         rec->recordLockStatus = 0;
185         rec->recordOldVersion = 0;
186         rec->nonDupRecordId   = 0;
187         for (ptr = pptr->children; ptr; ptr = ptr->next)
188         {
189             if (match_xsd_string(ptr, "recordId", o, 
190                                  &rec->recordId ))
191                 ;
192             else if (match_xsd_string(ptr, "recordReviewCode", o, 
193                                       &rec->recordReviewCode ))
194               ;
195             else if (match_xsd_string(ptr, "recordReviewNote", o, 
196                                        &rec->recordReviewNote ))
197                 ;
198             else if (match_xsd_string(ptr, "nonDupRecordId", o, 
199                                       &rec->nonDupRecordId ))
200               ;
201             else if (match_xsd_string(ptr, "recordLockStatus", o, 
202                                       &rec->recordLockStatus ))
203               ;
204             else if (match_xsd_string(ptr, "recordOldVersion", o, 
205                                       &rec->recordOldVersion ))
206               ;
207         }
208     }
209     else if (o->direction == ODR_ENCODE)
210     {
211         xmlNodePtr ptr = pptr;
212         if ( rec->recordId )
213             add_xsd_string(ptr, "recordId", rec->recordId);
214         if ( rec->recordReviewCode )
215             add_xsd_string(ptr, "recordReviewCode", rec->recordReviewCode);
216         if (  rec->recordReviewNote )
217             add_xsd_string(ptr, "recordReviewNote", rec->recordReviewNote);
218         if ( rec->nonDupRecordId ) 
219             add_xsd_string(ptr, "nonDupRecordId", rec->nonDupRecordId);
220         if ( rec->recordLockStatus ) 
221             add_xsd_string(ptr, "recordLockStatus", rec->recordLockStatus);
222         if (  rec->recordOldVersion )
223             add_xsd_string(ptr, "recordOldVersion", rec->recordOldVersion);
224     }
225     return 0;
226 }
227
228 static int yaz_srw_record(ODR o, xmlNodePtr pptr, Z_SRW_record *rec,
229                           Z_SRW_extra_record **extra,
230                           void *client_data, const char *ns)
231 {
232     if (o->direction == ODR_DECODE)
233     {
234         char *spack = 0;
235         int pack = Z_SRW_recordPacking_string;
236         xmlNodePtr ptr;
237         xmlNodePtr data_ptr = 0;
238         rec->recordSchema = 0;
239         rec->recordData_buf = 0;
240         rec->recordData_len = 0;
241         rec->recordPosition = 0;
242         *extra = 0;
243         for (ptr = pptr->children; ptr; ptr = ptr->next)
244         {
245             
246             if (match_xsd_string(ptr, "recordSchema", o, 
247                                  &rec->recordSchema))
248                 ;
249             else if (match_xsd_string(ptr, "recordPacking", o, &spack))
250             {
251                 if (spack && !strcmp(spack, "xml"))
252                     pack = Z_SRW_recordPacking_XML;
253                 if (spack && !strcmp(spack, "url"))
254                     pack = Z_SRW_recordPacking_URL;
255                 if (spack && !strcmp(spack, "string"))
256                     pack = Z_SRW_recordPacking_string;
257             }
258             else if (match_xsd_integer(ptr, "recordPosition", o, 
259                                        &rec->recordPosition))
260                 ;
261             else if (match_element(ptr, "recordData"))
262             {
263                 /* save position of Data until after the loop
264                    then we will know the packing (hopefully), and
265                    unpacking is done once
266                 */
267                 data_ptr = ptr;
268             }
269             else if (match_element(ptr, "extraRecordData"))
270             {
271                 *extra = (Z_SRW_extra_record *)
272                     odr_malloc(o, sizeof(Z_SRW_extra_record));
273                 yaz_srw_extra_record(o, ptr, *extra, client_data, ns);
274             }
275         }
276         if (data_ptr)
277         {
278             switch(pack)
279             {
280             case Z_SRW_recordPacking_XML:
281                 match_xsd_XML_n(data_ptr, "recordData", o, 
282                                 &rec->recordData_buf, &rec->recordData_len);
283                 break;
284             case Z_SRW_recordPacking_URL:
285                 /* just store it as a string.
286                    leave it to the backend to collect the document */
287                 match_xsd_string_n(data_ptr, "recordData", o, 
288                                    &rec->recordData_buf, &rec->recordData_len);
289                 break;
290             case Z_SRW_recordPacking_string:
291                 match_xsd_string_n(data_ptr, "recordData", o, 
292                                    &rec->recordData_buf, &rec->recordData_len);
293                 break;
294             }
295         }
296         rec->recordPacking = pack;
297     }
298     else if (o->direction == ODR_ENCODE)
299     {
300         xmlNodePtr ptr = pptr;
301         int pack = rec->recordPacking;
302         add_xsd_string(ptr, "recordSchema", rec->recordSchema);
303
304         switch(pack)
305         {
306         case Z_SRW_recordPacking_string:
307             add_xsd_string(ptr, "recordPacking", "string");
308             add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
309                              rec->recordData_len);
310             break;
311         case Z_SRW_recordPacking_XML:
312             add_xsd_string(ptr, "recordPacking", "xml");
313             add_XML_n(ptr, "recordData", rec->recordData_buf,
314                       rec->recordData_len);
315             break;
316         case Z_SRW_recordPacking_URL:
317             add_xsd_string(ptr, "recordPacking", "url");
318             add_xsd_string_n(ptr, "recordData", rec->recordData_buf,
319                              rec->recordData_len);
320             break;
321         }
322         if (rec->recordPosition)
323             add_xsd_integer(ptr, "recordPosition", rec->recordPosition );
324         if (*extra)
325         {
326             xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "extraRecordData",
327                                           0);
328             yaz_srw_extra_record(o, rptr, *extra, client_data, ns);
329         }
330     }
331     return 0;
332 }
333
334 static int yaz_srw_records(ODR o, xmlNodePtr pptr, Z_SRW_record **recs,
335                            Z_SRW_extra_record ***extra,
336                            int *num, void *client_data, const char *ns)
337 {
338     if (o->direction == ODR_DECODE)
339     {
340         int i;
341         xmlNodePtr ptr;
342         *num = 0;
343         for (ptr = pptr->children; ptr; ptr = ptr->next)
344         {
345             if (ptr->type == XML_ELEMENT_NODE &&
346                 !xmlStrcmp(ptr->name, BAD_CAST "record"))
347                 (*num)++;
348         }
349         if (!*num)
350             return 1;
351         *recs = (Z_SRW_record *) odr_malloc(o, *num * sizeof(**recs));
352         *extra = (Z_SRW_extra_record **) odr_malloc(o, *num * sizeof(**extra));
353         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
354         {
355             if (ptr->type == XML_ELEMENT_NODE &&
356                 !xmlStrcmp(ptr->name, BAD_CAST "record"))
357             {
358                 yaz_srw_record(o, ptr, *recs + i, *extra + i, client_data, ns);
359                 i++;
360             }
361         }
362     }
363     else if (o->direction == ODR_ENCODE)
364     {
365         int i;
366         for (i = 0; i < *num; i++)
367         {
368             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "record",
369                                           0);
370             yaz_srw_record(o, rptr, (*recs)+i, *extra + i, client_data, ns);
371         }
372     }
373     return 0;
374 }
375
376 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
377                                int *num, void *client_data, const char *ns)
378 {
379     if (o->direction == ODR_DECODE)
380     {
381         int i;
382         xmlNodePtr ptr;
383         *num = 0;
384         for (ptr = pptr->children; ptr; ptr = ptr->next)
385         {
386             if (ptr->type == XML_ELEMENT_NODE &&
387                 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
388                 (*num)++;
389         }
390         if (!*num)
391             return 1;
392         *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
393         for (i = 0; i < *num; i++)
394         {
395             (*recs)[i].uri = 0;
396             (*recs)[i].details = 0;
397             (*recs)[i].message = 0;
398         } 
399         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
400         {
401             if (ptr->type == XML_ELEMENT_NODE &&
402                 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
403             {
404                 xmlNodePtr rptr;
405                 (*recs)[i].uri = 0;
406                 (*recs)[i].details = 0;
407                 (*recs)[i].message = 0;
408                 for (rptr = ptr->children; rptr; rptr = rptr->next)
409                 {
410                     if (match_xsd_string(rptr, "uri", o, 
411                                          &(*recs)[i].uri))
412                         ;
413                     else if (match_xsd_string(rptr, "details", o, 
414                                               &(*recs)[i].details))
415                         ;
416                     else if (match_xsd_string(rptr, "message", o, 
417                                               &(*recs)[i].message))
418                         ;
419                 }
420                 i++;
421             }
422         }
423     }
424     else if (o->direction == ODR_ENCODE)
425     {
426         int i;
427         xmlNsPtr ns_diag =
428             xmlNewNs(pptr, BAD_CAST
429                      "http://www.loc.gov/zing/srw/diagnostic/", 0);
430         for (i = 0; i < *num; i++)
431         {
432             const char *std_diag = "info:srw/diagnostic/1/";
433             xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
434                                           BAD_CAST "diagnostic", 0);
435             add_xsd_string(rptr, "uri", (*recs)[i].uri);
436             if ((*recs)[i].message)
437                 add_xsd_string(rptr, "message", (*recs)[i].message);
438             else if ((*recs)[i].uri && 
439                      !strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
440             {
441                 int no = atoi((*recs)[i].uri + strlen(std_diag));
442                 const char *message = yaz_diag_srw_str(no);
443                 if (message)
444                     add_xsd_string(rptr, "message", message);
445             }
446             add_xsd_string(rptr, "details", (*recs)[i].details);
447         }
448     }
449     return 0;
450 }
451
452 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
453                         void *client_data, const char *ns)
454 {
455     if (o->direction == ODR_DECODE)
456     {
457         xmlNodePtr ptr;
458         term->value = 0;
459         term->numberOfRecords = 0;
460         term->displayTerm = 0;
461         term->whereInList = 0;
462         for (ptr = pptr->children; ptr; ptr = ptr->next)
463         {
464             if (match_xsd_string(ptr, "value", o,  &term->value))
465                 ;
466             else if (match_xsd_integer(ptr, "numberOfRecords", o, 
467                                    &term->numberOfRecords))
468                 ;
469             else if (match_xsd_string(ptr, "displayTerm", o, 
470                                       &term->displayTerm))
471                 ;
472             else if (match_xsd_string(ptr, "whereInList", o, 
473                                       &term->whereInList))
474                 ;
475         }
476     }
477     else if (o->direction == ODR_ENCODE)
478     {
479         xmlNodePtr ptr = pptr;
480         add_xsd_string(ptr, "value", term->value);
481         add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
482         add_xsd_string(ptr, "displayTerm", term->displayTerm);
483         add_xsd_string(ptr, "whereInList", term->whereInList);
484     }
485     return 0;
486 }
487
488 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
489                          int *num, void *client_data, const char *ns)
490 {
491     if (o->direction == ODR_DECODE)
492     {
493         int i;
494         xmlNodePtr ptr;
495         *num = 0;
496         for (ptr = pptr->children; ptr; ptr = ptr->next)
497         {
498             if (ptr->type == XML_ELEMENT_NODE &&
499                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
500                 (*num)++;
501         }
502         if (!*num)
503             return 1;
504         *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
505         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
506         {
507             if (ptr->type == XML_ELEMENT_NODE &&
508                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
509                 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
510         }
511     }
512     else if (o->direction == ODR_ENCODE)
513     {
514         int i;
515         for (i = 0; i < *num; i++)
516         {
517             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
518             yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
519         }
520     }
521     return 0;
522 }
523
524 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
525                   void *client_data, const char *ns)
526 {
527     xmlNodePtr pptr = (xmlNodePtr) vptr;
528     if (o->direction == ODR_DECODE)
529     {
530         Z_SRW_PDU **p = handler_data;
531         xmlNodePtr method = pptr->children;
532
533         while (method && method->type == XML_TEXT_NODE)
534             method = method->next;
535         
536         if (!method)
537             return -1;
538         if (method->type != XML_ELEMENT_NODE)
539             return -1;
540
541         *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(**p));
542         (*p)->srw_version = odr_strdup(o, "1.1");
543         
544         if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
545         {
546             xmlNodePtr ptr = method->children;
547             Z_SRW_searchRetrieveRequest *req;
548
549             (*p)->which = Z_SRW_searchRetrieve_request;
550             req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
551                 odr_malloc(o, sizeof(*req));
552             req->query_type = Z_SRW_query_type_cql;
553             req->query.cql = 0;
554             req->sort_type = Z_SRW_sort_type_none;
555             req->sort.none = 0;
556             req->startRecord = 0;
557             req->maximumRecords = 0;
558             req->recordSchema = 0;
559             req->recordPacking = 0;
560             req->recordXPath = 0;
561             req->resultSetTTL = 0;
562             req->stylesheet = 0;
563             req->database = 0;
564
565             for (; ptr; ptr = ptr->next)
566             {
567                 if (match_xsd_string(ptr, "version", o,
568                                      &(*p)->srw_version))
569                     ;
570                 else if (match_xsd_string(ptr, "query", o, 
571                                      &req->query.cql))
572                     req->query_type = Z_SRW_query_type_cql;
573                 else if (match_xsd_string(ptr, "pQuery", o, 
574                                      &req->query.pqf))
575                     req->query_type = Z_SRW_query_type_pqf;
576                 else if (match_xsd_string(ptr, "xQuery", o, 
577                                      &req->query.xcql))
578                     req->query_type = Z_SRW_query_type_xcql;
579                 else if (match_xsd_integer(ptr, "startRecord", o,
580                                            &req->startRecord))
581                     ;
582                 else if (match_xsd_integer(ptr, "maximumRecords", o,
583                                            &req->maximumRecords))
584                     ;
585                 else if (match_xsd_string(ptr, "recordPacking", o,
586                                           &req->recordPacking))
587                     ;
588                 else if (match_xsd_string(ptr, "recordSchema", o, 
589                                           &req->recordSchema))
590                     ;
591                 else if (match_xsd_string(ptr, "recordXPath", o,
592                                           &req->recordXPath))
593                     ;
594                 else if (match_xsd_string(ptr, "resultSetTTL", o,
595                                            &req->database))
596                     ;
597                 else if (match_xsd_string(ptr, "sortKeys", o, 
598                                           &req->sort.sortKeys))
599                     req->sort_type = Z_SRW_sort_type_sort;
600                 else if (match_xsd_string(ptr, "stylesheet", o,
601                                            &req->stylesheet))
602                     ;
603                 else if (match_xsd_string(ptr, "database", o,
604                                            &req->database))
605                     ;
606                 /* missing is xQuery, xSortKeys .. */
607             }
608         }
609         else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
610         {
611             xmlNodePtr ptr = method->children;
612             Z_SRW_searchRetrieveResponse *res;
613
614             (*p)->which = Z_SRW_searchRetrieve_response;
615             res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
616                 odr_malloc(o, sizeof(*res));
617
618             res->numberOfRecords = 0;
619             res->resultSetId = 0;
620             res->resultSetIdleTime = 0;
621             res->records = 0;
622             res->num_records = 0;
623             res->diagnostics = 0;
624             res->num_diagnostics = 0;
625             res->nextRecordPosition = 0;
626
627             for (; ptr; ptr = ptr->next)
628             {
629                 if (match_xsd_string(ptr, "version", o,
630                                      &(*p)->srw_version))
631                     ;
632                 else if (match_xsd_integer(ptr, "numberOfRecords", o, 
633                                       &res->numberOfRecords))
634                     ;
635                 else if (match_xsd_string(ptr, "resultSetId", o, 
636                                           &res->resultSetId))
637                     ;
638                 else if (match_xsd_integer(ptr, "resultSetIdleTime", o, 
639                                            &res->resultSetIdleTime))
640                     ;
641                 else if (match_element(ptr, "records"))
642                     yaz_srw_records(o, ptr, &res->records,
643                                     &res->extra_records, 
644                                     &res->num_records, client_data, ns);
645                 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
646                                            &res->nextRecordPosition))
647                     ;
648                 else if (match_element(ptr, "diagnostics"))
649                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
650                                         &res->num_diagnostics,
651                                         client_data, ns);
652             }
653         }
654         else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
655         {
656             Z_SRW_explainRequest *req;
657             xmlNodePtr ptr = method->children;
658             
659             (*p)->which = Z_SRW_explain_request;
660             req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
661                 odr_malloc(o, sizeof(*req));
662             req->recordPacking = 0;
663             req->database = 0;
664             req->stylesheet = 0;
665             for (; ptr; ptr = ptr->next)
666             {
667                 if (match_xsd_string(ptr, "version", o,
668                                            &(*p)->srw_version))
669                     ;
670                 else if (match_xsd_string(ptr, "stylesheet", o,
671                                           &req->stylesheet))
672                     ;
673                 else if (match_xsd_string(ptr, "recordPacking", o,
674                                      &req->recordPacking))
675                     ;
676                 else if (match_xsd_string(ptr, "database", o,
677                                      &req->database))
678                     ;
679             }
680         }
681         else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
682         {
683             Z_SRW_explainResponse *res;
684             xmlNodePtr ptr = method->children;
685
686             (*p)->which = Z_SRW_explain_response;
687             res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
688                 odr_malloc(o, sizeof(*res));
689             res->diagnostics = 0;
690             res->num_diagnostics = 0;
691             res->record.recordSchema = 0;
692             res->record.recordData_buf = 0;
693             res->record.recordData_len = 0;
694             res->record.recordPosition = 0;
695
696             for (; ptr; ptr = ptr->next)
697             {
698                 if (match_xsd_string(ptr, "version", o,
699                                            &(*p)->srw_version))
700                     ;
701                 else if (match_element(ptr, "record"))
702                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
703                                    client_data, ns);
704                 else if (match_element(ptr, "diagnostics"))
705                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
706                                         &res->num_diagnostics,
707                                         client_data, ns);
708                 ;
709             }
710         }
711         else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
712         {
713             Z_SRW_scanRequest *req;
714             xmlNodePtr ptr = method->children;
715
716             (*p)->which = Z_SRW_scan_request;
717             req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
718                 odr_malloc(o, sizeof(*req));
719             req->query_type = Z_SRW_query_type_cql;
720             req->scanClause.cql = 0;
721             req->responsePosition = 0;
722             req->maximumTerms = 0;
723             req->stylesheet = 0;
724             req->database = 0;
725             
726             for (; ptr; ptr = ptr->next)
727             {
728                 if (match_xsd_string(ptr, "version", o,
729                                      &(*p)->srw_version))
730                     ;
731                 else if (match_xsd_string(ptr, "scanClause", o,
732                                      &req->scanClause.cql))
733                     ;
734                 else if (match_xsd_string(ptr, "pScanClause", o,
735                                           &req->scanClause.pqf))
736                 {
737                     req->query_type = Z_SRW_query_type_pqf;
738                 }
739                 else if (match_xsd_integer(ptr, "responsePosition", o,
740                                            &req->responsePosition))
741                     ;
742                 else if (match_xsd_integer(ptr, "maximumTerms", o,
743                                            &req->maximumTerms))
744                     ;
745                 else if (match_xsd_string(ptr, "stylesheet", o,
746                                           &req->stylesheet))
747                     ;
748                 else if (match_xsd_string(ptr, "database", o,
749                                           &req->database))
750                     ;
751             }
752         }
753         else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
754         {
755             Z_SRW_scanResponse *res;
756             xmlNodePtr ptr = method->children;
757
758             (*p)->which = Z_SRW_scan_response;
759             res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
760                 odr_malloc(o, sizeof(*res));
761             res->terms = 0;
762             res->num_terms = 0;
763             res->diagnostics = 0;
764             res->num_diagnostics = 0;
765             
766             for (; ptr; ptr = ptr->next)
767             {
768                 if (match_xsd_string(ptr, "version", o,
769                                      &(*p)->srw_version))
770                     ;
771                 else if (match_element(ptr, "terms"))
772                     yaz_srw_terms(o, ptr, &res->terms,
773                                   &res->num_terms, client_data,
774                                   ns);
775                 else if (match_element(ptr, "diagnostics"))
776                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
777                                         &res->num_diagnostics,
778                                         client_data, ns);
779             }
780         }
781         else
782         {
783             *p = 0;
784             return -1;
785         }
786     }
787     else if (o->direction == ODR_ENCODE)
788     {
789         Z_SRW_PDU **p = handler_data;
790         xmlNsPtr ns_srw;
791         
792         if ((*p)->which == Z_SRW_searchRetrieve_request)
793         {
794             Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
795             xmlNodePtr ptr = xmlNewChild(pptr, 0,
796                                          BAD_CAST "searchRetrieveRequest", 0);
797             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
798             xmlSetNs(ptr, ns_srw);
799
800             if ((*p)->srw_version)
801                 add_xsd_string(ptr, "version", (*p)->srw_version);
802             switch(req->query_type)
803             {
804             case Z_SRW_query_type_cql:
805                 add_xsd_string(ptr, "query", req->query.cql);
806                 break;
807             case Z_SRW_query_type_xcql:
808                 add_xsd_string(ptr, "xQuery", req->query.xcql);
809                 break;
810             case Z_SRW_query_type_pqf:
811                 add_xsd_string(ptr, "pQuery", req->query.pqf);
812                 break;
813             }
814             add_xsd_integer(ptr, "startRecord", req->startRecord);
815             add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
816             add_xsd_string(ptr, "recordPacking", req->recordPacking);
817             add_xsd_string(ptr, "recordSchema", req->recordSchema);
818             add_xsd_string(ptr, "recordXPath", req->recordXPath);
819             add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
820             switch(req->sort_type)
821             {
822             case Z_SRW_sort_type_none:
823                 break;
824             case Z_SRW_sort_type_sort:
825                 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
826                 break;
827             case Z_SRW_sort_type_xSort:
828                 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
829                 break;
830             }
831             add_xsd_string(ptr, "stylesheet", req->stylesheet);
832             add_xsd_string(ptr, "database", req->database);
833         }
834         else if ((*p)->which == Z_SRW_searchRetrieve_response)
835         {
836             Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
837             xmlNodePtr ptr = xmlNewChild(pptr, 0,
838                                          BAD_CAST "searchRetrieveResponse", 0);
839             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
840             xmlSetNs(ptr, ns_srw);
841
842             if ((*p)->srw_version)
843                 add_xsd_string(ptr, "version", (*p)->srw_version);
844             add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
845             add_xsd_string(ptr, "resultSetId", res->resultSetId);
846             add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
847             if (res->num_records)
848             {
849                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
850                 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
851                                 &res->num_records,
852                                 client_data, ns);
853             }
854             add_xsd_integer(ptr, "nextRecordPosition",
855                             res->nextRecordPosition);
856             if (res->num_diagnostics)
857             {
858                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
859                                               0);
860                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
861                                     &res->num_diagnostics, client_data, ns);
862             }
863         }
864         else if ((*p)->which == Z_SRW_explain_request)
865         {
866             Z_SRW_explainRequest *req = (*p)->u.explain_request;
867             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest",
868                                          0);
869             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
870             xmlSetNs(ptr, ns_srw);
871
872             add_xsd_string(ptr, "version", (*p)->srw_version);
873             add_xsd_string(ptr, "recordPacking", req->recordPacking);
874             add_xsd_string(ptr, "stylesheet", req->stylesheet);
875             add_xsd_string(ptr, "database", req->database);
876         }
877         else if ((*p)->which == Z_SRW_explain_response)
878         {
879             Z_SRW_explainResponse *res = (*p)->u.explain_response;
880             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse",
881                                          0);
882             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
883             xmlSetNs(ptr, ns_srw);
884
885             add_xsd_string(ptr, "version", (*p)->srw_version);
886             if (1)
887             {
888                 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
889                 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
890                                client_data, ns);
891             }
892             if (res->num_diagnostics)
893             {
894                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
895                                               0);
896                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
897                                     &res->num_diagnostics, client_data, ns);
898             }
899         }
900         else if ((*p)->which == Z_SRW_scan_request)
901         {
902             Z_SRW_scanRequest *req = (*p)->u.scan_request;
903             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
904             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
905             xmlSetNs(ptr, ns_srw);
906
907             add_xsd_string(ptr, "version", (*p)->srw_version);
908             switch(req->query_type)
909             {
910             case Z_SRW_query_type_cql:
911                 add_xsd_string(ptr, "scanClause", req->scanClause.cql);
912                 break;
913             case Z_SRW_query_type_pqf:
914                 add_xsd_string(ptr, "pScanClause", req->scanClause.pqf);
915                 break;
916             }
917             add_xsd_integer(ptr, "responsePosition", req->responsePosition);
918             add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
919             add_xsd_string(ptr, "stylesheet", req->stylesheet);
920             add_xsd_string(ptr, "database", req->database);
921         }
922         else if ((*p)->which == Z_SRW_scan_response)
923         {
924             Z_SRW_scanResponse *res = (*p)->u.scan_response;
925             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
926             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
927             xmlSetNs(ptr, ns_srw);
928
929             add_xsd_string(ptr, "version", (*p)->srw_version);
930
931             if (res->num_terms)
932             {
933                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
934                 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
935                               client_data, ns);
936             }
937             if (res->num_diagnostics)
938             {
939                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
940                                               0);
941                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
942                                     &res->num_diagnostics, client_data, ns);
943             }
944         }
945         else
946             return -1;
947
948     }
949     return 0;
950 }
951
952 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
953                   void *client_data, const char *ns)
954 {
955     xmlNodePtr pptr = (xmlNodePtr) vptr;
956     if (o->direction == ODR_DECODE)
957     {
958         Z_SRW_PDU **p = handler_data;
959         xmlNodePtr method = pptr->children;
960
961         while (method && method->type == XML_TEXT_NODE)
962             method = method->next;
963         
964         if (!method)
965             return -1;
966         if (method->type != XML_ELEMENT_NODE)
967             return -1;
968
969         *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(**p));
970         (*p)->srw_version = odr_strdup(o, "1.1");
971         
972         if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
973         {
974             xmlNodePtr ptr = method->children;
975             Z_SRW_updateRequest *req;
976             char *oper = 0;
977
978             (*p)->which = Z_SRW_update_request;
979             req = (*p)->u.update_request = (Z_SRW_updateRequest *)
980                 odr_malloc(o, sizeof(*req));
981             req->database = 0;
982             req->operation = 0;
983             req->recordId = 0;
984             req->recordVersion = 0;
985             req->recordOldVersion = 0;
986             req->record.recordData_buf = 0;
987             req->record.recordData_len = 0;
988             req->record.recordSchema = 0;
989             req->record.recordPacking = 0;
990             req->extra_record = 0;
991             req->extraRequestData = 0;
992             req->stylesheet = 0;
993
994             for (; ptr; ptr = ptr->next)
995             {
996                 if (match_xsd_string(ptr, "version", o,
997                                      &(*p)->srw_version))
998                     ;
999                 else if (match_xsd_string(ptr, "operation", o, 
1000                                           &oper)){
1001                     if ( oper ){
1002                         if ( !strcmp(oper, "delete"))
1003                             req->operation = "delete";
1004                         else if (!strcmp(oper,"replace" ))
1005                             req->operation = "replace";
1006                         else if ( !strcmp( oper, "insert"))
1007                             req->operation = "insert";
1008                     }
1009                 }
1010                 else if (match_xsd_string(ptr, "recordId", o,
1011                                           &req->recordId))
1012                     ;
1013                 else if (match_xsd_string(ptr, "recordVersion", o,
1014                                           &req->recordVersion))
1015                     ;
1016                 else if (match_element(ptr, "record"))
1017                     yaz_srw_record(o, ptr, &req->record, &req->extra_record,
1018                                    client_data, ns);
1019                 else if (match_xsd_string(ptr, "stylesheet", o,
1020                                            &req->stylesheet))
1021                     ;
1022                 else if (match_xsd_string(ptr, "database", o,
1023                                            &req->database))
1024                     ;
1025             }
1026         }
1027         else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1028         {
1029             xmlNodePtr ptr = method->children;
1030             Z_SRW_updateResponse *res;
1031
1032             (*p)->which = Z_SRW_update_response;
1033             res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1034                 odr_malloc(o, sizeof(*res));
1035
1036             res->operationStatus = 0;
1037             res->recordId = 0;
1038             res->recordVersion = 0;
1039             res->recordChecksum = 0;
1040             res->diagnostics = 0;
1041             res->num_diagnostics = 0;
1042             res->record.recordData_buf = 0;
1043             res->record.recordData_len = 0;
1044             res->record.recordSchema = 0;
1045             res->record.recordPacking = 0;
1046             res->extra_record = 0;
1047             res->extraResponseData = 0;
1048
1049             for (; ptr; ptr = ptr->next)
1050             {
1051                 if (match_xsd_string(ptr, "version", o,
1052                                      &(*p)->srw_version))
1053                     ;
1054                 else if (match_xsd_string(ptr, "operationStatus", o, 
1055                                       &res->operationStatus ))
1056                     ;
1057                 else if (match_xsd_string(ptr, "recordId", o, 
1058                                           &res->recordId))
1059                     ;
1060                 else if (match_xsd_string(ptr, "recordVersion", o, 
1061                                            &res->recordVersion ))
1062                     ;
1063                 else if (match_element(ptr, "record"))
1064                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
1065                                    client_data, ns);
1066                 else if (match_element(ptr, "diagnostics"))
1067                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1068                                         &res->num_diagnostics,
1069                                         client_data, ns);
1070             }
1071         }
1072         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1073         {
1074         }
1075         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1076         {
1077         }
1078         else
1079         {
1080             *p = 0;
1081             return -1;
1082         }
1083     }
1084     else if (o->direction == ODR_ENCODE)
1085     {
1086         Z_SRW_PDU **p = handler_data;
1087         xmlNsPtr ns_srw;
1088
1089         if ((*p)->which == Z_SRW_update_request)
1090         {
1091             Z_SRW_updateRequest *req = (*p)->u.update_request;
1092             xmlNodePtr ptr = xmlNewChild(pptr, 0, "updateRequest", 0);
1093             ns_srw = xmlNewNs(ptr, ns, "zu");
1094             xmlSetNs(ptr, ns_srw);
1095
1096             add_xsd_string(ptr, "version", (*p)->srw_version);
1097             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1098             add_xsd_string(ptr, "database", req->database);
1099         }
1100         else if ((*p)->which == Z_SRW_update_response)
1101         {
1102             Z_SRW_updateResponse *res = (*p)->u.update_response;
1103             xmlNodePtr ptr = xmlNewChild(pptr, 0, "updateResponse", 0);
1104             ns_srw = xmlNewNs(ptr, ns, "zu");
1105             xmlSetNs(ptr, ns_srw);
1106             
1107             add_xsd_string(ptr, "version", (*p)->srw_version);
1108             add_xsd_string(ptr, "operationStatus", res->operationStatus );
1109             add_xsd_string(ptr, "recordId", res->recordId );
1110             if (res->recordVersion)
1111                 add_xsd_string(ptr, "recordVersion", res->recordVersion );
1112             if (res->recordChecksum)
1113                 add_xsd_string(ptr, "recordChecksum", res->recordChecksum );
1114             if (res->record.recordData_len)
1115             {
1116                 xmlNodePtr rptr = xmlNewChild(ptr, 0, "record", 0);
1117                 yaz_srw_record(o, rptr, &res->record, &res->extra_record,
1118                                client_data, ns);
1119             }
1120             if (res->num_diagnostics)
1121             {
1122                 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
1123                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1124                                     &res->num_diagnostics, client_data, ns);
1125             }
1126             if ( res->extraResponseData )
1127                 add_xsd_string(ptr, "extraResponseData", res->extraResponseData);
1128         }
1129         else
1130             return -1;
1131
1132     }
1133     return 0;
1134 }
1135
1136 #endif
1137
1138
1139 /*
1140  * Local variables:
1141  * c-basic-offset: 4
1142  * indent-tabs-mode: nil
1143  * End:
1144  * vim: shiftwidth=4 tabstop=8 expandtab
1145  */
1146