ICU: remove check for attr rule (not required)
[yaz-moved-to-github.git] / src / icu_chain.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file
8  * \brief ICU chain
9  */
10
11 #if HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #if YAZ_HAVE_ICU
16 #include <yaz/xmalloc.h>
17
18 #include <yaz/icu_I18N.h>
19
20 #include <yaz/log.h>
21 #include <yaz/nmem.h>
22 #include <yaz/nmem_xml.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include <unicode/ustring.h>  /* some more string fcns*/
29 #include <unicode/uchar.h>    /* char names           */
30
31 enum icu_chain_step_type {
32     ICU_chain_step_type_none,
33     ICU_chain_step_type_display,   /* convert to utf8 display format */
34     ICU_chain_step_type_casemap,   /* apply utf16 charmap */
35     ICU_chain_step_type_transform, /* apply utf16 transform */
36     ICU_chain_step_type_tokenize,  /* apply utf16 tokenization */
37     ICU_chain_step_type_transliterate  /* apply utf16 tokenization */
38 };
39
40 struct icu_chain_step
41 {
42     /* type and action object */
43     enum icu_chain_step_type type;
44     union {
45         struct icu_casemap *casemap;
46         struct icu_transform *transform;
47         struct icu_tokenizer *tokenizer;  
48     } u;
49     struct icu_chain_step *previous;
50 };
51
52 struct icu_chain
53 {
54     yaz_icu_iter_t iter;
55     char *locale;
56     int sort;
57
58     UCollator *coll;
59     
60     /* linked list of chain steps */
61     struct icu_chain_step *csteps;
62 };
63
64 int icu_check_status(UErrorCode status)
65 {
66     if (U_FAILURE(status))
67     {
68         yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
69         return 0;   
70     }
71     return 1;
72 }
73
74 static struct icu_chain_step *icu_chain_step_create(
75     struct icu_chain *chain,  enum icu_chain_step_type type,
76     const uint8_t *rule, UErrorCode *status)
77 {
78     struct icu_chain_step *step = 0;
79     
80     if (!chain || !type || !rule)
81         return 0;
82
83     step = (struct icu_chain_step *) xmalloc(sizeof(*step));
84
85     step->type = type;
86     /* create auxilary objects */
87     switch (step->type)
88     {
89     case ICU_chain_step_type_display:
90         break;
91     case ICU_chain_step_type_casemap:
92         step->u.casemap = icu_casemap_create(rule[0], status);
93         break;
94     case ICU_chain_step_type_transform:
95         /* rule omitted. Only ID used */
96         step->u.transform = icu_transform_create((const char *) rule, 'f',
97                                                  0, status);
98         break;
99     case ICU_chain_step_type_tokenize:
100         step->u.tokenizer = icu_tokenizer_create((char *) chain->locale, 
101                                                  (char) rule[0], status);
102         break;
103     case ICU_chain_step_type_transliterate:
104         /* we pass a dummy ID to utrans_openU.. */
105         step->u.transform = icu_transform_create("custom", 'f',
106                                                  (const char *) rule, status);
107         break;
108     default:
109         break;
110     }
111     return step;
112 }
113
114
115 static void icu_chain_step_destroy(struct icu_chain_step *step)
116 {
117     if (!step)
118         return;
119
120     icu_chain_step_destroy(step->previous);
121
122     switch (step->type)
123     {
124     case ICU_chain_step_type_display:
125         break;
126     case ICU_chain_step_type_casemap:
127         icu_casemap_destroy(step->u.casemap);
128         break;
129     case ICU_chain_step_type_transform:
130     case ICU_chain_step_type_transliterate:
131         icu_transform_destroy(step->u.transform);
132         break;
133     case ICU_chain_step_type_tokenize:
134         icu_tokenizer_destroy(step->u.tokenizer);
135         break;
136     default:
137         break;
138     }
139     xfree(step);
140 }
141
142 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
143 {
144     struct icu_chain_step *step = 0;
145     struct icu_chain_step **sp = &step;
146     while (old)
147     {
148         *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
149         (*sp)->type = old->type;
150         
151         switch ((*sp)->type)
152         {
153         case ICU_chain_step_type_display:
154             break;
155         case ICU_chain_step_type_casemap:
156             (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
157             break;
158         case ICU_chain_step_type_transform:
159         case ICU_chain_step_type_transliterate:
160             (*sp)->u.transform = icu_transform_clone(old->u.transform);
161             break;
162         case ICU_chain_step_type_tokenize:
163             (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
164             break;
165         case ICU_chain_step_type_none:
166             break;
167         }
168         old = old->previous;
169         sp = &(*sp)->previous;
170     }
171     *sp = 0;
172     return step;
173 }
174
175 struct icu_chain *icu_chain_create(const char *locale, int sort,
176                                    UErrorCode *status)
177 {
178     struct icu_chain *chain 
179         = (struct icu_chain *) xmalloc(sizeof(*chain));
180
181     *status = U_ZERO_ERROR;
182
183     chain->iter = 0;
184     chain->locale = xstrdup(locale);
185
186     chain->sort = sort;
187
188     chain->coll = ucol_open((const char *) chain->locale, status);
189
190     if (U_FAILURE(*status))
191         return 0;
192
193     chain->csteps = 0;
194
195     return chain;
196 }
197
198 void icu_chain_destroy(struct icu_chain *chain)
199 {
200     if (chain)
201     {
202         if (chain->coll)
203             ucol_close(chain->coll);
204
205         if (chain->iter)
206             icu_iter_destroy(chain->iter);
207         icu_chain_step_destroy(chain->csteps);
208         xfree(chain->locale);
209         xfree(chain);
210     }
211 }
212
213 static struct icu_chain_step *icu_chain_insert_step(
214     struct icu_chain *chain, enum icu_chain_step_type type,
215     const uint8_t *rule, UErrorCode *status);
216
217 struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node, 
218                                        int sort,
219                                        UErrorCode *status)
220 {
221     xmlNode *node = 0;
222     int no_errors = 0;
223     struct icu_chain *chain = 0;
224     NMEM nmem = 0;
225    
226     *status = U_ZERO_ERROR;
227
228     if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
229         return 0;
230     
231     {
232         xmlChar *xml_locale = xmlGetProp((xmlNode *) xml_node, 
233                                          (xmlChar *) "locale");
234         
235         if (xml_locale)
236         {
237             chain = icu_chain_create((const char *) xml_locale, sort, status);
238             xmlFree(xml_locale);
239         }
240         
241     }
242     if (!chain)
243         return 0;
244
245     nmem = nmem_create();
246     for (node = xml_node->children; node; node = node->next)
247     {
248         char *rule = 0;
249         struct icu_chain_step *step = 0;
250         struct _xmlAttr *attr;
251
252         nmem_reset(nmem);
253         if (node->type != XML_ELEMENT_NODE)
254             continue;
255
256         for (attr = node->properties; attr; attr = attr->next)
257         {
258             if (!strcmp((const char *) attr->name, "rule"))
259             {
260                 rule = nmem_text_node_cdata(attr->children, nmem);
261             }
262             else
263             {
264                 yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
265                         "element '%s'", attr->name, node->name);
266                 no_errors++;
267                 continue;
268             }
269         }
270         if (!rule && node->children)
271             rule = nmem_text_node_cdata(node->children, nmem);
272         
273         if (!strcmp((const char *) node->name, "casemap"))
274             step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
275                                          (const uint8_t *) rule, status);
276         else if (!strcmp((const char *) node->name, "transform"))
277             step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
278                                          (const uint8_t *) rule, status);
279         else if (!strcmp((const char *) node->name, "transliterate"))
280             step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate, 
281                                          (const uint8_t *) rule, status);
282         else if (!strcmp((const char *) node->name, "tokenize"))
283             step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
284                                          (const uint8_t *) rule, status);
285         else if (!strcmp((const char *) node->name, "display"))
286             step = icu_chain_insert_step(chain, ICU_chain_step_type_display, 
287                                          (const uint8_t *) "", status);
288         else if (!strcmp((const char *) node->name, "normalize"))
289         {
290             yaz_log(YLOG_WARN, "Element %s is deprecated. "
291                     "Use transform instead", node->name);
292             step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
293                                          (const uint8_t *) rule, status);
294         }
295         else if (!strcmp((const char *) node->name, "index")
296                  || !strcmp((const char *) node->name, "sortkey"))
297         {
298             yaz_log(YLOG_WARN, "Element %s is no longer needed. "
299                     "Remove it from the configuration", node->name);
300         }
301         else
302         {
303             yaz_log(YLOG_WARN, "Unknown element %s", node->name);
304             no_errors++;
305             continue;
306         }
307         if (step && U_FAILURE(*status))
308         {
309             no_errors++;
310             break;
311         }
312     }
313     nmem_destroy(nmem);
314     if (no_errors)
315     {
316         icu_chain_destroy(chain);
317         return 0;
318     }
319     return chain;
320 }
321
322
323 static struct icu_chain_step *icu_chain_insert_step(
324     struct icu_chain *chain, enum icu_chain_step_type type,
325     const uint8_t *rule, UErrorCode *status)
326 {    
327     struct icu_chain_step *step = 0;
328     if (!chain || !type || !rule)
329         return 0;
330
331     /* create actual chain step with this buffer */
332     step = icu_chain_step_create(chain, type, rule,
333                                  status);
334
335     step->previous = chain->csteps;
336     chain->csteps = step;
337
338     return step;
339 }
340
341 struct icu_iter {
342     struct icu_chain *chain;
343     struct icu_buf_utf16 *last;
344     UErrorCode status;
345     struct icu_buf_utf8 *display;
346     struct icu_buf_utf8 *sort8;
347     struct icu_buf_utf8 *result;
348     struct icu_buf_utf16 *input;
349     int token_count;
350     struct icu_chain_step *steps;
351 };
352
353 void icu_utf16_print(struct icu_buf_utf16 *src16)
354 {
355     UErrorCode status = U_ZERO_ERROR;
356     const char *p;
357     struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
358     icu_utf16_to_utf8(dst8, src16, &status);
359
360     assert(status != 1234);
361     if (U_FAILURE(status))
362     {
363         printf("failure");
364     }
365     else
366     {
367         p = icu_buf_utf8_to_cstr(dst8);
368         printf("%s", p);
369     }
370     icu_buf_utf8_destroy(dst8);
371 }
372
373 struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
374                                       struct icu_chain_step *step,
375                                       struct icu_buf_utf16 *src)
376 {
377     if (!step)
378         return src;
379     else
380     {
381         struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
382         
383         switch (step->type)
384         {
385         case ICU_chain_step_type_casemap:
386             if (dst)
387             {
388                 struct icu_buf_utf16 *src = dst;
389
390                 dst = icu_buf_utf16_create(0);
391                 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
392                                     iter->chain->locale);
393                 icu_buf_utf16_destroy(src);
394             }
395             break;
396         case ICU_chain_step_type_tokenize:
397             if (dst)
398             {
399                 struct icu_buf_utf16 *src = dst;
400
401                 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
402                 icu_buf_utf16_destroy(src);
403             }
404             dst = icu_buf_utf16_create(0);
405             iter->status = U_ZERO_ERROR;
406             if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
407             {
408                 icu_buf_utf16_destroy(dst);
409                 dst = 0;
410             }
411             break;
412         case ICU_chain_step_type_transform:
413         case ICU_chain_step_type_transliterate:
414             if (dst)
415             {
416                 struct icu_buf_utf16 *src = dst;
417                 dst = icu_buf_utf16_create(0);
418                 icu_transform_trans(step->u.transform, dst, src, &iter->status);
419                 icu_buf_utf16_destroy(src);
420             }
421             break;
422         case ICU_chain_step_type_display:
423             if (dst)
424                 icu_utf16_to_utf8(iter->display, dst, &iter->status);
425             break;
426         default:
427             assert(0);
428         }
429         return dst;
430     }
431 }
432
433 yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
434 {
435     yaz_icu_iter_t iter = xmalloc(sizeof(*iter));
436     iter->chain = chain;
437     iter->status = U_ZERO_ERROR;
438     iter->display = icu_buf_utf8_create(0);
439     iter->sort8 = icu_buf_utf8_create(0);
440     iter->result = icu_buf_utf8_create(0);
441     iter->last = 0; /* no last returned string (yet) */
442     iter->steps = icu_chain_step_clone(chain->csteps);
443     iter->input = 0;
444
445     return iter;
446 }
447
448 void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
449 {
450     if (iter->input)
451         icu_buf_utf16_destroy(iter->input);
452     iter->input = icu_buf_utf16_create(0);
453     iter->token_count = 0;
454     /* fill and assign input string.. It will be 0 after
455        first iteration */
456     icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
457 }
458
459 void icu_iter_destroy(yaz_icu_iter_t iter)
460 {
461     if (iter)
462     {
463         icu_buf_utf8_destroy(iter->display);
464         icu_buf_utf8_destroy(iter->sort8);
465         icu_buf_utf8_destroy(iter->result);
466         if (iter->input)
467             icu_buf_utf16_destroy(iter->input);
468         icu_chain_step_destroy(iter->steps);
469         xfree(iter);
470     }
471 }
472
473 int icu_iter_next(yaz_icu_iter_t iter)
474 {
475     if (!iter->input && iter->last == 0)
476         return 0;
477     else
478     {
479         /* on first call, iter->input is the input string. Thereafter: 0. */
480         iter->last = icu_iter_invoke(iter, iter->steps ?
481                                      iter->steps : iter->chain->csteps,
482                                      iter->input);
483         iter->input = 0;
484         
485         if (!iter->last)
486             return 0;
487
488         iter->token_count++;
489
490         if (iter->chain->sort)
491         {        
492             icu_sortkey8_from_utf16(iter->chain->coll,
493                                     iter->sort8, iter->last,
494                                     &iter->status);
495         }
496         icu_utf16_to_utf8(iter->result, iter->last, &iter->status);
497         icu_buf_utf16_destroy(iter->last);
498
499         return 1;
500     }
501 }
502
503 const char *icu_iter_get_norm(yaz_icu_iter_t iter)
504 {
505     return icu_buf_utf8_to_cstr(iter->result);
506 }
507
508 const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
509 {
510     return icu_buf_utf8_to_cstr(iter->sort8);
511 }
512
513 const char *icu_iter_get_display(yaz_icu_iter_t iter)
514
515     return icu_buf_utf8_to_cstr(iter->display);   
516 }
517
518 int icu_iter_get_token_number(yaz_icu_iter_t iter)
519
520     return iter->token_count;
521 }
522
523 int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr, 
524                           UErrorCode *status)
525 {
526     if (chain->iter)
527         icu_iter_destroy(chain->iter);
528     chain->iter = icu_iter_create(chain);
529     icu_iter_first(chain->iter, src8cstr);
530     return 1;
531 }
532
533 int icu_chain_next_token(struct icu_chain *chain, UErrorCode *status)
534 {
535     *status = U_ZERO_ERROR;
536     return icu_iter_next(chain->iter);
537 }
538
539 int icu_chain_token_number(struct icu_chain *chain)
540 {
541     if (chain && chain->iter)
542         return chain->iter->token_count;
543     return 0;
544 }
545
546 const char *icu_chain_token_display(struct icu_chain *chain)
547 {
548     if (chain->iter)
549         return icu_iter_get_display(chain->iter);
550     return 0;
551 }
552
553 const char *icu_chain_token_norm(struct icu_chain *chain)
554 {
555     if (chain->iter)
556         return icu_iter_get_norm(chain->iter);
557     return 0;
558 }
559
560 const char *icu_chain_token_sortkey(struct icu_chain *chain)
561 {
562     if (chain->iter)
563         return icu_iter_get_sortkey(chain->iter);
564     return 0;
565 }
566
567 #endif /* YAZ_HAVE_ICU */
568
569 /*
570  * Local variables:
571  * c-basic-offset: 4
572  * c-file-style: "Stroustrup"
573  * indent-tabs-mode: nil
574  * End:
575  * vim: shiftwidth=4 tabstop=8 expandtab
576  */
577