Separate the logging utilities for queries in two . The multi-line
[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.42 2006-01-20 10:34:52 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 && *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 ? *extra + i : 0),
371                            client_data, ns);
372         }
373     }
374     return 0;
375 }
376
377 static int yaz_srw_diagnostics(ODR o, xmlNodePtr pptr, Z_SRW_diagnostic **recs,
378                                int *num, void *client_data, const char *ns)
379 {
380     if (o->direction == ODR_DECODE)
381     {
382         int i;
383         xmlNodePtr ptr;
384         *num = 0;
385         for (ptr = pptr->children; ptr; ptr = ptr->next)
386         {
387             if (ptr->type == XML_ELEMENT_NODE &&
388                 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
389                 (*num)++;
390         }
391         if (!*num)
392             return 1;
393         *recs = (Z_SRW_diagnostic *) odr_malloc(o, *num * sizeof(**recs));
394         for (i = 0; i < *num; i++)
395         {
396             (*recs)[i].uri = 0;
397             (*recs)[i].details = 0;
398             (*recs)[i].message = 0;
399         } 
400         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next)
401         {
402             if (ptr->type == XML_ELEMENT_NODE &&
403                 !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
404             {
405                 xmlNodePtr rptr;
406                 (*recs)[i].uri = 0;
407                 (*recs)[i].details = 0;
408                 (*recs)[i].message = 0;
409                 for (rptr = ptr->children; rptr; rptr = rptr->next)
410                 {
411                     if (match_xsd_string(rptr, "uri", o, 
412                                          &(*recs)[i].uri))
413                         ;
414                     else if (match_xsd_string(rptr, "details", o, 
415                                               &(*recs)[i].details))
416                         ;
417                     else if (match_xsd_string(rptr, "message", o, 
418                                               &(*recs)[i].message))
419                         ;
420                 }
421                 i++;
422             }
423         }
424     }
425     else if (o->direction == ODR_ENCODE)
426     {
427         int i;
428         xmlNsPtr ns_diag =
429             xmlNewNs(pptr, BAD_CAST
430                      "http://www.loc.gov/zing/srw/diagnostic/", 0);
431         for (i = 0; i < *num; i++)
432         {
433             const char *std_diag = "info:srw/diagnostic/1/";
434             xmlNodePtr rptr = xmlNewChild(pptr, ns_diag,
435                                           BAD_CAST "diagnostic", 0);
436             add_xsd_string(rptr, "uri", (*recs)[i].uri);
437             if ((*recs)[i].message)
438                 add_xsd_string(rptr, "message", (*recs)[i].message);
439             else if ((*recs)[i].uri && 
440                      !strncmp((*recs)[i].uri, std_diag, strlen(std_diag)))
441             {
442                 int no = atoi((*recs)[i].uri + strlen(std_diag));
443                 const char *message = yaz_diag_srw_str(no);
444                 if (message)
445                     add_xsd_string(rptr, "message", message);
446             }
447             add_xsd_string(rptr, "details", (*recs)[i].details);
448         }
449     }
450     return 0;
451 }
452
453 static int yaz_srw_term(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm *term,
454                         void *client_data, const char *ns)
455 {
456     if (o->direction == ODR_DECODE)
457     {
458         xmlNodePtr ptr;
459         term->value = 0;
460         term->numberOfRecords = 0;
461         term->displayTerm = 0;
462         term->whereInList = 0;
463         for (ptr = pptr->children; ptr; ptr = ptr->next)
464         {
465             if (match_xsd_string(ptr, "value", o,  &term->value))
466                 ;
467             else if (match_xsd_integer(ptr, "numberOfRecords", o, 
468                                    &term->numberOfRecords))
469                 ;
470             else if (match_xsd_string(ptr, "displayTerm", o, 
471                                       &term->displayTerm))
472                 ;
473             else if (match_xsd_string(ptr, "whereInList", o, 
474                                       &term->whereInList))
475                 ;
476         }
477     }
478     else if (o->direction == ODR_ENCODE)
479     {
480         xmlNodePtr ptr = pptr;
481         add_xsd_string(ptr, "value", term->value);
482         add_xsd_integer(ptr, "numberOfRecords", term->numberOfRecords);
483         add_xsd_string(ptr, "displayTerm", term->displayTerm);
484         add_xsd_string(ptr, "whereInList", term->whereInList);
485     }
486     return 0;
487 }
488
489 static int yaz_srw_terms(ODR o, xmlNodePtr pptr, Z_SRW_scanTerm **terms,
490                          int *num, void *client_data, const char *ns)
491 {
492     if (o->direction == ODR_DECODE)
493     {
494         int i;
495         xmlNodePtr ptr;
496         *num = 0;
497         for (ptr = pptr->children; ptr; ptr = ptr->next)
498         {
499             if (ptr->type == XML_ELEMENT_NODE &&
500                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
501                 (*num)++;
502         }
503         if (!*num)
504             return 1;
505         *terms = (Z_SRW_scanTerm *) odr_malloc(o, *num * sizeof(**terms));
506         for (i = 0, ptr = pptr->children; ptr; ptr = ptr->next, i++)
507         {
508             if (ptr->type == XML_ELEMENT_NODE &&
509                 !xmlStrcmp(ptr->name, BAD_CAST "term"))
510                 yaz_srw_term(o, ptr, (*terms)+i, client_data, ns);
511         }
512     }
513     else if (o->direction == ODR_ENCODE)
514     {
515         int i;
516         for (i = 0; i < *num; i++)
517         {
518             xmlNodePtr rptr = xmlNewChild(pptr, 0, BAD_CAST "term", 0);
519             yaz_srw_term(o, rptr, (*terms)+i, client_data, ns);
520         }
521     }
522     return 0;
523 }
524
525 int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
526                   void *client_data, const char *ns)
527 {
528     xmlNodePtr pptr = (xmlNodePtr) vptr;
529     if (o->direction == ODR_DECODE)
530     {
531         Z_SRW_PDU **p = handler_data;
532         xmlNodePtr method = pptr->children;
533
534         while (method && method->type == XML_TEXT_NODE)
535             method = method->next;
536         
537         if (!method)
538             return -1;
539         if (method->type != XML_ELEMENT_NODE)
540             return -1;
541
542         *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(**p));
543         (*p)->srw_version = odr_strdup(o, "1.1");
544         
545         if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveRequest"))
546         {
547             xmlNodePtr ptr = method->children;
548             Z_SRW_searchRetrieveRequest *req;
549
550             (*p)->which = Z_SRW_searchRetrieve_request;
551             req = (*p)->u.request = (Z_SRW_searchRetrieveRequest *)
552                 odr_malloc(o, sizeof(*req));
553             req->query_type = Z_SRW_query_type_cql;
554             req->query.cql = 0;
555             req->sort_type = Z_SRW_sort_type_none;
556             req->sort.none = 0;
557             req->startRecord = 0;
558             req->maximumRecords = 0;
559             req->recordSchema = 0;
560             req->recordPacking = 0;
561             req->recordXPath = 0;
562             req->resultSetTTL = 0;
563             req->stylesheet = 0;
564             req->database = 0;
565
566             for (; ptr; ptr = ptr->next)
567             {
568                 if (match_xsd_string(ptr, "version", o,
569                                      &(*p)->srw_version))
570                     ;
571                 else if (match_xsd_string(ptr, "query", o, 
572                                      &req->query.cql))
573                     req->query_type = Z_SRW_query_type_cql;
574                 else if (match_xsd_string(ptr, "pQuery", o, 
575                                      &req->query.pqf))
576                     req->query_type = Z_SRW_query_type_pqf;
577                 else if (match_xsd_string(ptr, "xQuery", o, 
578                                      &req->query.xcql))
579                     req->query_type = Z_SRW_query_type_xcql;
580                 else if (match_xsd_integer(ptr, "startRecord", o,
581                                            &req->startRecord))
582                     ;
583                 else if (match_xsd_integer(ptr, "maximumRecords", o,
584                                            &req->maximumRecords))
585                     ;
586                 else if (match_xsd_string(ptr, "recordPacking", o,
587                                           &req->recordPacking))
588                     ;
589                 else if (match_xsd_string(ptr, "recordSchema", o, 
590                                           &req->recordSchema))
591                     ;
592                 else if (match_xsd_string(ptr, "recordXPath", o,
593                                           &req->recordXPath))
594                     ;
595                 else if (match_xsd_string(ptr, "resultSetTTL", o,
596                                            &req->database))
597                     ;
598                 else if (match_xsd_string(ptr, "sortKeys", o, 
599                                           &req->sort.sortKeys))
600                     req->sort_type = Z_SRW_sort_type_sort;
601                 else if (match_xsd_string(ptr, "stylesheet", o,
602                                            &req->stylesheet))
603                     ;
604                 else if (match_xsd_string(ptr, "database", o,
605                                            &req->database))
606                     ;
607                 /* missing is xQuery, xSortKeys .. */
608             }
609         }
610         else if (!xmlStrcmp(method->name, BAD_CAST "searchRetrieveResponse"))
611         {
612             xmlNodePtr ptr = method->children;
613             Z_SRW_searchRetrieveResponse *res;
614
615             (*p)->which = Z_SRW_searchRetrieve_response;
616             res = (*p)->u.response = (Z_SRW_searchRetrieveResponse *)
617                 odr_malloc(o, sizeof(*res));
618
619             res->numberOfRecords = 0;
620             res->resultSetId = 0;
621             res->resultSetIdleTime = 0;
622             res->records = 0;
623             res->num_records = 0;
624             res->diagnostics = 0;
625             res->num_diagnostics = 0;
626             res->nextRecordPosition = 0;
627
628             for (; ptr; ptr = ptr->next)
629             {
630                 if (match_xsd_string(ptr, "version", o,
631                                      &(*p)->srw_version))
632                     ;
633                 else if (match_xsd_integer(ptr, "numberOfRecords", o, 
634                                       &res->numberOfRecords))
635                     ;
636                 else if (match_xsd_string(ptr, "resultSetId", o, 
637                                           &res->resultSetId))
638                     ;
639                 else if (match_xsd_integer(ptr, "resultSetIdleTime", o, 
640                                            &res->resultSetIdleTime))
641                     ;
642                 else if (match_element(ptr, "records"))
643                     yaz_srw_records(o, ptr, &res->records,
644                                     &res->extra_records, 
645                                     &res->num_records, client_data, ns);
646                 else if (match_xsd_integer(ptr, "nextRecordPosition", o,
647                                            &res->nextRecordPosition))
648                     ;
649                 else if (match_element(ptr, "diagnostics"))
650                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
651                                         &res->num_diagnostics,
652                                         client_data, ns);
653             }
654         }
655         else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
656         {
657             Z_SRW_explainRequest *req;
658             xmlNodePtr ptr = method->children;
659             
660             (*p)->which = Z_SRW_explain_request;
661             req = (*p)->u.explain_request = (Z_SRW_explainRequest *)
662                 odr_malloc(o, sizeof(*req));
663             req->recordPacking = 0;
664             req->database = 0;
665             req->stylesheet = 0;
666             for (; ptr; ptr = ptr->next)
667             {
668                 if (match_xsd_string(ptr, "version", o,
669                                            &(*p)->srw_version))
670                     ;
671                 else if (match_xsd_string(ptr, "stylesheet", o,
672                                           &req->stylesheet))
673                     ;
674                 else if (match_xsd_string(ptr, "recordPacking", o,
675                                      &req->recordPacking))
676                     ;
677                 else if (match_xsd_string(ptr, "database", o,
678                                      &req->database))
679                     ;
680             }
681         }
682         else if (!xmlStrcmp(method->name, BAD_CAST "explainResponse"))
683         {
684             Z_SRW_explainResponse *res;
685             xmlNodePtr ptr = method->children;
686
687             (*p)->which = Z_SRW_explain_response;
688             res = (*p)->u.explain_response = (Z_SRW_explainResponse*)
689                 odr_malloc(o, sizeof(*res));
690             res->diagnostics = 0;
691             res->num_diagnostics = 0;
692             res->record.recordSchema = 0;
693             res->record.recordData_buf = 0;
694             res->record.recordData_len = 0;
695             res->record.recordPosition = 0;
696
697             for (; ptr; ptr = ptr->next)
698             {
699                 if (match_xsd_string(ptr, "version", o,
700                                            &(*p)->srw_version))
701                     ;
702                 else if (match_element(ptr, "record"))
703                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
704                                    client_data, ns);
705                 else if (match_element(ptr, "diagnostics"))
706                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
707                                         &res->num_diagnostics,
708                                         client_data, ns);
709                 ;
710             }
711         }
712         else if (!xmlStrcmp(method->name, BAD_CAST "scanRequest"))
713         {
714             Z_SRW_scanRequest *req;
715             xmlNodePtr ptr = method->children;
716
717             (*p)->which = Z_SRW_scan_request;
718             req = (*p)->u.scan_request = (Z_SRW_scanRequest *)
719                 odr_malloc(o, sizeof(*req));
720             req->query_type = Z_SRW_query_type_cql;
721             req->scanClause.cql = 0;
722             req->responsePosition = 0;
723             req->maximumTerms = 0;
724             req->stylesheet = 0;
725             req->database = 0;
726             
727             for (; ptr; ptr = ptr->next)
728             {
729                 if (match_xsd_string(ptr, "version", o,
730                                      &(*p)->srw_version))
731                     ;
732                 else if (match_xsd_string(ptr, "scanClause", o,
733                                      &req->scanClause.cql))
734                     ;
735                 else if (match_xsd_string(ptr, "pScanClause", o,
736                                           &req->scanClause.pqf))
737                 {
738                     req->query_type = Z_SRW_query_type_pqf;
739                 }
740                 else if (match_xsd_integer(ptr, "responsePosition", o,
741                                            &req->responsePosition))
742                     ;
743                 else if (match_xsd_integer(ptr, "maximumTerms", o,
744                                            &req->maximumTerms))
745                     ;
746                 else if (match_xsd_string(ptr, "stylesheet", o,
747                                           &req->stylesheet))
748                     ;
749                 else if (match_xsd_string(ptr, "database", o,
750                                           &req->database))
751                     ;
752             }
753         }
754         else if (!xmlStrcmp(method->name, BAD_CAST "scanResponse"))
755         {
756             Z_SRW_scanResponse *res;
757             xmlNodePtr ptr = method->children;
758
759             (*p)->which = Z_SRW_scan_response;
760             res = (*p)->u.scan_response = (Z_SRW_scanResponse *)
761                 odr_malloc(o, sizeof(*res));
762             res->terms = 0;
763             res->num_terms = 0;
764             res->diagnostics = 0;
765             res->num_diagnostics = 0;
766             
767             for (; ptr; ptr = ptr->next)
768             {
769                 if (match_xsd_string(ptr, "version", o,
770                                      &(*p)->srw_version))
771                     ;
772                 else if (match_element(ptr, "terms"))
773                     yaz_srw_terms(o, ptr, &res->terms,
774                                   &res->num_terms, client_data,
775                                   ns);
776                 else if (match_element(ptr, "diagnostics"))
777                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
778                                         &res->num_diagnostics,
779                                         client_data, ns);
780             }
781         }
782         else
783         {
784             *p = 0;
785             return -1;
786         }
787     }
788     else if (o->direction == ODR_ENCODE)
789     {
790         Z_SRW_PDU **p = handler_data;
791         xmlNsPtr ns_srw;
792         
793         if ((*p)->which == Z_SRW_searchRetrieve_request)
794         {
795             Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
796             xmlNodePtr ptr = xmlNewChild(pptr, 0,
797                                          BAD_CAST "searchRetrieveRequest", 0);
798             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
799             xmlSetNs(ptr, ns_srw);
800
801             if ((*p)->srw_version)
802                 add_xsd_string(ptr, "version", (*p)->srw_version);
803             switch(req->query_type)
804             {
805             case Z_SRW_query_type_cql:
806                 add_xsd_string(ptr, "query", req->query.cql);
807                 break;
808             case Z_SRW_query_type_xcql:
809                 add_xsd_string(ptr, "xQuery", req->query.xcql);
810                 break;
811             case Z_SRW_query_type_pqf:
812                 add_xsd_string(ptr, "pQuery", req->query.pqf);
813                 break;
814             }
815             add_xsd_integer(ptr, "startRecord", req->startRecord);
816             add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
817             add_xsd_string(ptr, "recordPacking", req->recordPacking);
818             add_xsd_string(ptr, "recordSchema", req->recordSchema);
819             add_xsd_string(ptr, "recordXPath", req->recordXPath);
820             add_xsd_integer(ptr, "resultSetTTL", req->resultSetTTL);
821             switch(req->sort_type)
822             {
823             case Z_SRW_sort_type_none:
824                 break;
825             case Z_SRW_sort_type_sort:
826                 add_xsd_string(ptr, "sortKeys", req->sort.sortKeys);
827                 break;
828             case Z_SRW_sort_type_xSort:
829                 add_xsd_string(ptr, "xSortKeys", req->sort.xSortKeys);
830                 break;
831             }
832             add_xsd_string(ptr, "stylesheet", req->stylesheet);
833             add_xsd_string(ptr, "database", req->database);
834         }
835         else if ((*p)->which == Z_SRW_searchRetrieve_response)
836         {
837             Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
838             xmlNodePtr ptr = xmlNewChild(pptr, 0,
839                                          BAD_CAST "searchRetrieveResponse", 0);
840             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
841             xmlSetNs(ptr, ns_srw);
842
843             if ((*p)->srw_version)
844                 add_xsd_string(ptr, "version", (*p)->srw_version);
845             add_xsd_integer(ptr, "numberOfRecords", res->numberOfRecords);
846             add_xsd_string(ptr, "resultSetId", res->resultSetId);
847             add_xsd_integer(ptr, "resultSetIdleTime", res->resultSetIdleTime);
848             if (res->num_records)
849             {
850                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "records", 0);
851                 yaz_srw_records(o, rptr, &res->records, &res->extra_records,
852                                 &res->num_records,
853                                 client_data, ns);
854             }
855             add_xsd_integer(ptr, "nextRecordPosition",
856                             res->nextRecordPosition);
857             if (res->num_diagnostics)
858             {
859                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
860                                               0);
861                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
862                                     &res->num_diagnostics, client_data, ns);
863             }
864         }
865         else if ((*p)->which == Z_SRW_explain_request)
866         {
867             Z_SRW_explainRequest *req = (*p)->u.explain_request;
868             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest",
869                                          0);
870             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
871             xmlSetNs(ptr, ns_srw);
872
873             add_xsd_string(ptr, "version", (*p)->srw_version);
874             add_xsd_string(ptr, "recordPacking", req->recordPacking);
875             add_xsd_string(ptr, "stylesheet", req->stylesheet);
876             add_xsd_string(ptr, "database", req->database);
877         }
878         else if ((*p)->which == Z_SRW_explain_response)
879         {
880             Z_SRW_explainResponse *res = (*p)->u.explain_response;
881             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse",
882                                          0);
883             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
884             xmlSetNs(ptr, ns_srw);
885
886             add_xsd_string(ptr, "version", (*p)->srw_version);
887             if (1)
888             {
889                 xmlNodePtr ptr1 = xmlNewChild(ptr, 0, BAD_CAST "record", 0);
890                 yaz_srw_record(o, ptr1, &res->record, &res->extra_record,
891                                client_data, ns);
892             }
893             if (res->num_diagnostics)
894             {
895                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
896                                               0);
897                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
898                                     &res->num_diagnostics, client_data, ns);
899             }
900         }
901         else if ((*p)->which == Z_SRW_scan_request)
902         {
903             Z_SRW_scanRequest *req = (*p)->u.scan_request;
904             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
905             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
906             xmlSetNs(ptr, ns_srw);
907
908             add_xsd_string(ptr, "version", (*p)->srw_version);
909             switch(req->query_type)
910             {
911             case Z_SRW_query_type_cql:
912                 add_xsd_string(ptr, "scanClause", req->scanClause.cql);
913                 break;
914             case Z_SRW_query_type_pqf:
915                 add_xsd_string(ptr, "pScanClause", req->scanClause.pqf);
916                 break;
917             }
918             add_xsd_integer(ptr, "responsePosition", req->responsePosition);
919             add_xsd_integer(ptr, "maximumTerms", req->maximumTerms);
920             add_xsd_string(ptr, "stylesheet", req->stylesheet);
921             add_xsd_string(ptr, "database", req->database);
922         }
923         else if ((*p)->which == Z_SRW_scan_response)
924         {
925             Z_SRW_scanResponse *res = (*p)->u.scan_response;
926             xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
927             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
928             xmlSetNs(ptr, ns_srw);
929
930             add_xsd_string(ptr, "version", (*p)->srw_version);
931
932             if (res->num_terms)
933             {
934                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "terms", 0);
935                 yaz_srw_terms(o, rptr, &res->terms, &res->num_terms,
936                               client_data, ns);
937             }
938             if (res->num_diagnostics)
939             {
940                 xmlNodePtr rptr = xmlNewChild(ptr, 0, BAD_CAST "diagnostics",
941                                               0);
942                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
943                                     &res->num_diagnostics, client_data, ns);
944             }
945         }
946         else
947             return -1;
948
949     }
950     return 0;
951 }
952
953 int yaz_ucp_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
954                   void *client_data, const char *ns)
955 {
956     xmlNodePtr pptr = (xmlNodePtr) vptr;
957     if (o->direction == ODR_DECODE)
958     {
959         Z_SRW_PDU **p = handler_data;
960         xmlNodePtr method = pptr->children;
961
962         while (method && method->type == XML_TEXT_NODE)
963             method = method->next;
964         
965         if (!method)
966             return -1;
967         if (method->type != XML_ELEMENT_NODE)
968             return -1;
969
970         *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(**p));
971         (*p)->srw_version = odr_strdup(o, "1.1");
972         
973         if (!xmlStrcmp(method->name, BAD_CAST "updateRequest"))
974         {
975             xmlNodePtr ptr = method->children;
976             Z_SRW_updateRequest *req;
977             char *oper = 0;
978
979             (*p)->which = Z_SRW_update_request;
980             req = (*p)->u.update_request = (Z_SRW_updateRequest *)
981                 odr_malloc(o, sizeof(*req));
982             req->database = 0;
983             req->operation = 0;
984             req->recordId = 0;
985             req->recordVersion = 0;
986             req->recordOldVersion = 0;
987             req->record.recordData_buf = 0;
988             req->record.recordData_len = 0;
989             req->record.recordSchema = 0;
990             req->record.recordPacking = 0;
991             req->extra_record = 0;
992             req->extraRequestData = 0;
993             req->stylesheet = 0;
994
995             for (; ptr; ptr = ptr->next)
996             {
997                 if (match_xsd_string(ptr, "version", o,
998                                      &(*p)->srw_version))
999                     ;
1000                 else if (match_xsd_string(ptr, "operation", o, 
1001                                           &oper)){
1002                     if ( oper ){
1003                         if ( !strcmp(oper, "delete"))
1004                             req->operation = "delete";
1005                         else if (!strcmp(oper,"replace" ))
1006                             req->operation = "replace";
1007                         else if ( !strcmp( oper, "insert"))
1008                             req->operation = "insert";
1009                     }
1010                 }
1011                 else if (match_xsd_string(ptr, "recordId", o,
1012                                           &req->recordId))
1013                     ;
1014                 else if (match_xsd_string(ptr, "recordVersion", o,
1015                                           &req->recordVersion))
1016                     ;
1017                 else if (match_element(ptr, "record"))
1018                     yaz_srw_record(o, ptr, &req->record, &req->extra_record,
1019                                    client_data, ns);
1020                 else if (match_xsd_string(ptr, "stylesheet", o,
1021                                            &req->stylesheet))
1022                     ;
1023                 else if (match_xsd_string(ptr, "database", o,
1024                                            &req->database))
1025                     ;
1026             }
1027         }
1028         else if (!xmlStrcmp(method->name, BAD_CAST "updateResponse"))
1029         {
1030             xmlNodePtr ptr = method->children;
1031             Z_SRW_updateResponse *res;
1032
1033             (*p)->which = Z_SRW_update_response;
1034             res = (*p)->u.update_response = (Z_SRW_updateResponse *)
1035                 odr_malloc(o, sizeof(*res));
1036
1037             res->operationStatus = 0;
1038             res->recordId = 0;
1039             res->recordVersion = 0;
1040             res->recordChecksum = 0;
1041             res->diagnostics = 0;
1042             res->num_diagnostics = 0;
1043             res->record.recordData_buf = 0;
1044             res->record.recordData_len = 0;
1045             res->record.recordSchema = 0;
1046             res->record.recordPacking = 0;
1047             res->extra_record = 0;
1048             res->extraResponseData = 0;
1049
1050             for (; ptr; ptr = ptr->next)
1051             {
1052                 if (match_xsd_string(ptr, "version", o,
1053                                      &(*p)->srw_version))
1054                     ;
1055                 else if (match_xsd_string(ptr, "operationStatus", o, 
1056                                       &res->operationStatus ))
1057                     ;
1058                 else if (match_xsd_string(ptr, "recordId", o, 
1059                                           &res->recordId))
1060                     ;
1061                 else if (match_xsd_string(ptr, "recordVersion", o, 
1062                                            &res->recordVersion ))
1063                     ;
1064                 else if (match_element(ptr, "record"))
1065                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
1066                                    client_data, ns);
1067                 else if (match_element(ptr, "diagnostics"))
1068                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
1069                                         &res->num_diagnostics,
1070                                         client_data, ns);
1071             }
1072         }
1073         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateRequest"))
1074         {
1075         }
1076         else if (!xmlStrcmp(method->name, BAD_CAST "explainUpdateResponse"))
1077         {
1078         }
1079         else
1080         {
1081             *p = 0;
1082             return -1;
1083         }
1084     }
1085     else if (o->direction == ODR_ENCODE)
1086     {
1087         Z_SRW_PDU **p = handler_data;
1088         xmlNsPtr ns_srw;
1089
1090         if ((*p)->which == Z_SRW_update_request)
1091         {
1092             Z_SRW_updateRequest *req = (*p)->u.update_request;
1093             xmlNodePtr ptr = xmlNewChild(pptr, 0, "updateRequest", 0);
1094             ns_srw = xmlNewNs(ptr, ns, "zu");
1095             xmlSetNs(ptr, ns_srw);
1096
1097             add_xsd_string(ptr, "version", (*p)->srw_version);
1098             add_xsd_string(ptr, "stylesheet", req->stylesheet);
1099             add_xsd_string(ptr, "database", req->database);
1100         }
1101         else if ((*p)->which == Z_SRW_update_response)
1102         {
1103             Z_SRW_updateResponse *res = (*p)->u.update_response;
1104             xmlNodePtr ptr = xmlNewChild(pptr, 0, (xmlChar *) 
1105                                          "updateResponse", 0);
1106             ns_srw = xmlNewNs(ptr, ns, "zu");
1107             xmlSetNs(ptr, ns_srw);
1108             
1109             add_xsd_string(ptr, "version", (*p)->srw_version);
1110             add_xsd_string(ptr, "operationStatus", res->operationStatus );
1111             add_xsd_string(ptr, "recordId", res->recordId );
1112             if (res->recordVersion)
1113                 add_xsd_string(ptr, "recordVersion", res->recordVersion );
1114             if (res->recordChecksum)
1115                 add_xsd_string(ptr, "recordChecksum", res->recordChecksum );
1116             if (res->record.recordData_len)
1117             {
1118                 xmlNodePtr rptr = xmlNewChild(ptr, 0, "record", 0);
1119                 yaz_srw_record(o, rptr, &res->record, &res->extra_record,
1120                                client_data, ns);
1121             }
1122             if (res->num_diagnostics)
1123             {
1124                 xmlNodePtr rptr = xmlNewChild(ptr, 0, "diagnostics", 0);
1125                 yaz_srw_diagnostics(o, rptr, &res->diagnostics,
1126                                     &res->num_diagnostics, client_data, ns);
1127             }
1128             if ( res->extraResponseData )
1129                 add_xsd_string(ptr, "extraResponseData", res->extraResponseData);
1130         }
1131         else
1132             return -1;
1133
1134     }
1135     return 0;
1136 }
1137
1138 #endif
1139
1140
1141 /*
1142  * Local variables:
1143  * c-basic-offset: 4
1144  * indent-tabs-mode: nil
1145  * End:
1146  * vim: shiftwidth=4 tabstop=8 expandtab
1147  */
1148