1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
20 #include <yaz/stemmer.h>
24 #include <yaz/nmem_xml.h>
30 #include <unicode/ustring.h> /* some more string fcns*/
31 #include <unicode/uchar.h> /* char names */
33 enum icu_chain_step_type {
34 ICU_chain_step_type_none,
35 ICU_chain_step_type_display, /* convert to utf8 display format */
36 ICU_chain_step_type_casemap, /* apply utf16 charmap */
37 ICU_chain_step_type_transform, /* apply utf16 transform */
38 ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
39 ICU_chain_step_type_transliterate, /* apply utf16 tokenization */
40 YAZ_chain_step_type_stemming, /* apply utf16 stemming (YAZ) */
41 ICU_chain_step_type_join
46 /* type and action object */
47 enum icu_chain_step_type type;
49 struct icu_casemap *casemap;
50 struct icu_transform *transform;
51 struct icu_tokenizer *tokenizer;
52 yaz_stemmer_p stemmer;
53 struct icu_buf_utf16 *join;
55 struct icu_chain_step *previous;
66 /* linked list of chain steps */
67 struct icu_chain_step *csteps;
70 int icu_check_status(UErrorCode status)
72 if (U_FAILURE(status))
74 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
80 static struct icu_chain_step *icu_chain_insert_step(
81 struct icu_chain *chain, enum icu_chain_step_type type,
82 const char *rule, UErrorCode *status)
84 struct icu_chain_step *step = 0;
89 step = (struct icu_chain_step *) xmalloc(sizeof(*step));
94 case ICU_chain_step_type_display:
96 case ICU_chain_step_type_casemap:
98 step->u.casemap = icu_casemap_create(rule[0], status);
100 case ICU_chain_step_type_transform:
102 /* rule omitted. Only ID used */
103 step->u.transform = icu_transform_create(rule, 'f', 0, status);
105 case ICU_chain_step_type_tokenize:
107 step->u.tokenizer = icu_tokenizer_create(chain->locale, rule[0], status);
109 case ICU_chain_step_type_transliterate:
111 /* we pass a dummy ID to utrans_openU.. */
112 step->u.transform = icu_transform_create("custom", 'f', rule, status);
114 case YAZ_chain_step_type_stemming:
116 step->u.stemmer = yaz_stemmer_create(chain->locale, rule, status);
118 case ICU_chain_step_type_join:
120 step->u.join = icu_buf_utf16_create(0);
121 icu_utf16_from_utf8_cstr(step->u.join, rule, status);
126 step->previous = chain->csteps;
127 chain->csteps = step;
133 static void icu_chain_step_destroy(struct icu_chain_step *step)
138 icu_chain_step_destroy(step->previous);
142 case ICU_chain_step_type_display:
144 case ICU_chain_step_type_casemap:
145 icu_casemap_destroy(step->u.casemap);
147 case ICU_chain_step_type_transform:
148 case ICU_chain_step_type_transliterate:
149 icu_transform_destroy(step->u.transform);
151 case ICU_chain_step_type_tokenize:
152 icu_tokenizer_destroy(step->u.tokenizer);
154 case YAZ_chain_step_type_stemming:
155 yaz_stemmer_destroy(step->u.stemmer);
157 case ICU_chain_step_type_join:
158 icu_buf_utf16_destroy(step->u.join);
166 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
168 struct icu_chain_step *step = 0;
169 struct icu_chain_step **sp = &step;
172 *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
173 (*sp)->type = old->type;
177 case ICU_chain_step_type_display:
179 case ICU_chain_step_type_casemap:
180 (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
182 case ICU_chain_step_type_transform:
183 case ICU_chain_step_type_transliterate:
184 (*sp)->u.transform = icu_transform_clone(old->u.transform);
186 case ICU_chain_step_type_tokenize:
187 (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
189 case YAZ_chain_step_type_stemming:
190 (*sp)->u.stemmer = yaz_stemmer_clone(old->u.stemmer);
192 case ICU_chain_step_type_none:
194 case ICU_chain_step_type_join:
195 (*sp)->u.join = icu_buf_utf16_create(0);
196 (*sp)->u.join = icu_buf_utf16_copy((*sp)->u.join, old->u.join);
200 sp = &(*sp)->previous;
206 struct icu_chain *icu_chain_create(const char *locale, int sort,
209 struct icu_chain *chain;
210 UCollator *coll = ucol_open(locale, status);
212 if (U_FAILURE(*status))
215 chain = (struct icu_chain *) xmalloc(sizeof(*chain));
217 chain->locale = xstrdup(locale);
225 void icu_chain_destroy(struct icu_chain *chain)
230 ucol_close(chain->coll);
233 icu_iter_destroy(chain->iter);
234 icu_chain_step_destroy(chain->csteps);
235 xfree(chain->locale);
240 struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
246 struct icu_chain *chain = 0;
249 *status = U_ZERO_ERROR;
251 if (xml_node && xml_node->type == XML_ELEMENT_NODE)
253 xmlChar *xml_locale = xmlGetProp((xmlNode *) xml_node,
254 (xmlChar *) "locale");
257 chain = icu_chain_create((const char *) xml_locale, sort, status);
265 nmem = nmem_create();
266 for (node = xml_node->children; node; node = node->next)
269 struct icu_chain_step *step = 0;
270 struct _xmlAttr *attr;
273 if (node->type != XML_ELEMENT_NODE)
276 for (attr = node->properties; attr; attr = attr->next)
278 if (!strcmp((const char *) attr->name, "rule"))
280 rule = nmem_text_node_cdata(attr->children, nmem);
284 yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
285 "element '%s'", attr->name, node->name);
289 if (!rule && node->children)
290 rule = nmem_text_node_cdata(node->children, nmem);
292 if (!rule && strcmp((const char *) node->name, "display"))
294 yaz_log(YLOG_WARN, "Missing attribute 'rule' for element %s",
295 (const char *) node->name);
299 if (!strcmp((const char *) node->name, "casemap"))
300 step = icu_chain_insert_step(chain,
301 ICU_chain_step_type_casemap,
303 else if (!strcmp((const char *) node->name, "transform"))
304 step = icu_chain_insert_step(chain,
305 ICU_chain_step_type_transform,
307 else if (!strcmp((const char *) node->name, "transliterate"))
308 step = icu_chain_insert_step(chain,
309 ICU_chain_step_type_transliterate,
311 else if (!strcmp((const char *) node->name, "tokenize"))
312 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
314 else if (!strcmp((const char *) node->name, "display"))
315 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
317 else if (!strcmp((const char *) node->name, "stemming"))
318 step = icu_chain_insert_step(chain, YAZ_chain_step_type_stemming,
320 else if (!strcmp((const char *) node->name, "join"))
321 step = icu_chain_insert_step(chain, ICU_chain_step_type_join,
323 else if (!strcmp((const char *) node->name, "normalize"))
325 yaz_log(YLOG_WARN, "Element %s is deprecated. "
326 "Use transform instead", node->name);
327 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
330 else if (!strcmp((const char *) node->name, "index")
331 || !strcmp((const char *) node->name, "sortkey"))
333 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
334 "Remove it from the configuration", node->name);
338 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
344 yaz_log(YLOG_WARN, "Step not created for %s", node->name);
347 if (step && U_FAILURE(*status))
349 yaz_log(YLOG_WARN, "ICU Error %d %s for element %s, rule %s",
350 *status, u_errorName(*status), node->name, rule ?
359 icu_chain_destroy(chain);
366 struct icu_chain *chain;
367 struct icu_buf_utf16 *last;
368 struct icu_buf_utf16 *org;
369 struct icu_buf_utf8 *org8;
371 struct icu_buf_utf8 *display;
372 struct icu_buf_utf8 *sort8;
373 struct icu_buf_utf8 *result;
379 struct icu_chain_step *steps;
382 void icu_utf16_print(struct icu_buf_utf16 *src16)
384 UErrorCode status = U_ZERO_ERROR;
386 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
387 icu_utf16_to_utf8(dst8, src16, &status);
389 if (U_FAILURE(status))
395 p = icu_buf_utf8_to_cstr(dst8);
398 icu_buf_utf8_destroy(dst8);
401 struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
402 struct icu_chain_step *step,
403 struct icu_buf_utf16 *src)
409 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
413 case ICU_chain_step_type_casemap:
416 struct icu_buf_utf16 *src = dst;
418 dst = icu_buf_utf16_create(0);
419 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
420 iter->chain->locale);
421 icu_buf_utf16_destroy(src);
424 case ICU_chain_step_type_tokenize:
427 struct icu_buf_utf16 *src = dst;
429 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
431 { /* no need to copy if it's already the same */
432 iter->utf8_base = iter->utf16_base = 0;
433 icu_buf_utf16_copy(iter->org, src);
435 icu_buf_utf16_destroy(src);
437 dst = icu_buf_utf16_create(0);
438 iter->status = U_ZERO_ERROR;
439 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status,
440 &iter->org_start, &iter->org_len))
442 icu_buf_utf16_destroy(dst);
446 case ICU_chain_step_type_transform:
447 case ICU_chain_step_type_transliterate:
450 struct icu_buf_utf16 *src = dst;
451 dst = icu_buf_utf16_create(0);
452 icu_transform_trans(step->u.transform, dst, src, &iter->status);
453 icu_buf_utf16_destroy(src);
456 case ICU_chain_step_type_display:
458 icu_utf16_to_utf8(iter->display, dst, &iter->status);
460 case YAZ_chain_step_type_stemming:
463 struct icu_buf_utf16 *src = dst;
464 dst = icu_buf_utf16_create(0);
465 yaz_stemmer_stem(step->u.stemmer, dst, src, &iter->status);
466 icu_buf_utf16_destroy(src);
469 case ICU_chain_step_type_join:
474 struct icu_buf_utf16 *dst1 =
475 icu_iter_invoke(iter, step->previous, 0);
479 dst = icu_buf_utf16_append(dst, step->u.join);
480 dst = icu_buf_utf16_append(dst, dst1);
481 icu_buf_utf16_destroy(dst1);
492 yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
494 yaz_icu_iter_t iter = xmalloc(sizeof(*iter));
496 iter->status = U_ZERO_ERROR;
497 iter->display = icu_buf_utf8_create(0);
498 iter->sort8 = icu_buf_utf8_create(0);
499 iter->result = icu_buf_utf8_create(0);
500 iter->org = icu_buf_utf16_create(0);
502 iter->last = 0; /* no last returned string (yet) */
503 iter->steps = icu_chain_step_clone(chain->csteps);
504 iter->token_count = 0;
509 void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
511 struct icu_buf_utf16 *src = icu_buf_utf16_create(0);
512 icu_utf16_from_utf8_cstr(src, src8cstr, &iter->status);
513 icu_buf_utf16_copy(iter->org, src);
514 iter->token_count = 0;
516 iter->utf8_base = iter->utf16_base = 0;
517 iter->org_len = src->utf16_len;
518 iter->last = icu_iter_invoke(iter, iter->steps, src);
521 void icu_iter_destroy(yaz_icu_iter_t iter)
525 icu_buf_utf8_destroy(iter->display);
526 icu_buf_utf8_destroy(iter->sort8);
527 icu_buf_utf8_destroy(iter->result);
528 icu_buf_utf16_destroy(iter->org);
529 icu_buf_utf8_destroy(iter->org8);
530 icu_chain_step_destroy(iter->steps);
535 int icu_iter_next(yaz_icu_iter_t iter)
537 if (iter->token_count && iter->last)
538 iter->last = icu_iter_invoke(iter, iter->steps, 0);
544 if (iter->chain->sort)
546 icu_sortkey8_from_utf16(iter->chain->coll,
547 iter->sort8, iter->last,
550 icu_utf16_to_utf8(iter->result, iter->last, &iter->status);
551 icu_buf_utf16_destroy(iter->last);
557 const char *icu_iter_get_norm(yaz_icu_iter_t iter)
559 return icu_buf_utf8_to_cstr(iter->result);
562 const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
564 return icu_buf_utf8_to_cstr(iter->sort8);
567 const char *icu_iter_get_display(yaz_icu_iter_t iter)
569 return icu_buf_utf8_to_cstr(iter->display);
572 int icu_iter_get_token_number(yaz_icu_iter_t iter)
574 return iter->token_count;
578 void icu_iter_get_org_info2(yaz_icu_iter_t iter, size_t *start, size_t *len,
581 int32_t len1 = 0, len2 = 0;
582 UErrorCode status = U_ZERO_ERROR;
584 if (iter->org_start < iter->utf16_base)
587 iter->utf16_base = 0;
589 u_strToUTF8(0, 0, &len1,
590 iter->org->utf16 + iter->utf16_base,
591 iter->org_start - iter->utf16_base,
594 status = U_ZERO_ERROR;
596 *start = len1 + iter->utf8_base;
598 u_strToUTF8(0, 0, &len2,
599 iter->org->utf16 + iter->utf16_base,
600 iter->org_start - iter->utf16_base + iter->org_len,
608 iter->org8 = icu_buf_utf8_create(0);
609 status = U_ZERO_ERROR;
610 icu_utf16_to_utf8(iter->org8, iter->org, &status);
611 *cstr = icu_buf_utf8_to_cstr(iter->org8);
613 iter->utf8_base = *start;
614 iter->utf16_base = iter->org_start;
617 void icu_iter_get_org_info(yaz_icu_iter_t iter, size_t *start, size_t *len)
619 icu_iter_get_org_info2(iter, start, len, 0);
622 int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr,
626 icu_iter_destroy(chain->iter);
627 chain->iter = icu_iter_create(chain);
628 icu_iter_first(chain->iter, src8cstr);
632 int icu_chain_next_token(struct icu_chain *chain, UErrorCode *status)
634 *status = U_ZERO_ERROR;
635 return icu_iter_next(chain->iter);
638 int icu_chain_token_number(struct icu_chain *chain)
640 if (chain && chain->iter)
641 return chain->iter->token_count;
645 const char *icu_chain_token_display(struct icu_chain *chain)
648 return icu_iter_get_display(chain->iter);
652 const char *icu_chain_token_norm(struct icu_chain *chain)
655 return icu_iter_get_norm(chain->iter);
659 const char *icu_chain_token_sortkey(struct icu_chain *chain)
662 return icu_iter_get_sortkey(chain->iter);
666 void icu_chain_get_org_info(struct icu_chain *chain, size_t *start, size_t *len)
669 icu_iter_get_org_info(chain->iter, start, len);
672 void icu_chain_get_org_info2(struct icu_chain *chain, size_t *start,
673 size_t *len, const char **cstr)
676 icu_iter_get_org_info2(chain->iter, start, len, cstr);
680 #endif /* YAZ_HAVE_ICU */
685 * c-file-style: "Stroustrup"
686 * indent-tabs-mode: nil
688 * vim: shiftwidth=4 tabstop=8 expandtab