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>
25 #include <yaz/xml_get.h>
31 #include <unicode/ustring.h> /* some more string fcns*/
32 #include <unicode/uchar.h> /* char names */
34 enum icu_chain_step_type {
35 ICU_chain_step_type_none,
36 ICU_chain_step_type_display, /* convert to utf8 display format */
37 ICU_chain_step_type_casemap, /* apply utf16 charmap */
38 ICU_chain_step_type_transform, /* apply utf16 transform */
39 ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
40 ICU_chain_step_type_transliterate, /* apply utf16 tokenization */
41 YAZ_chain_step_type_stemming, /* apply utf16 stemming (YAZ) */
42 ICU_chain_step_type_join
47 /* type and action object */
48 enum icu_chain_step_type type;
50 struct icu_casemap *casemap;
51 struct icu_transform *transform;
52 struct icu_tokenizer *tokenizer;
53 yaz_stemmer_p stemmer;
54 struct icu_buf_utf16 *join;
56 struct icu_chain_step *previous;
67 /* linked list of chain steps */
68 struct icu_chain_step *csteps;
71 int icu_check_status(UErrorCode status)
73 if (U_FAILURE(status))
75 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
81 static struct icu_chain_step *icu_chain_insert_step(
82 struct icu_chain *chain, enum icu_chain_step_type type,
83 const char *rule, UErrorCode *status)
85 struct icu_chain_step *step = 0;
90 step = (struct icu_chain_step *) xmalloc(sizeof(*step));
95 case ICU_chain_step_type_display:
97 case ICU_chain_step_type_casemap:
99 step->u.casemap = icu_casemap_create(rule[0], status);
101 case ICU_chain_step_type_transform:
103 /* rule omitted. Only ID used */
104 step->u.transform = icu_transform_create(rule, 'f', 0, status);
106 case ICU_chain_step_type_tokenize:
108 step->u.tokenizer = icu_tokenizer_create(chain->locale, rule[0], status);
110 case ICU_chain_step_type_transliterate:
112 /* we pass a dummy ID to utrans_openU.. */
113 step->u.transform = icu_transform_create("custom", 'f', rule, status);
115 case YAZ_chain_step_type_stemming:
117 step->u.stemmer = yaz_stemmer_create(chain->locale, rule, status);
119 case ICU_chain_step_type_join:
121 step->u.join = icu_buf_utf16_create(0);
122 icu_utf16_from_utf8_cstr(step->u.join, rule, status);
127 step->previous = chain->csteps;
128 chain->csteps = step;
134 static void icu_chain_step_destroy(struct icu_chain_step *step)
139 icu_chain_step_destroy(step->previous);
143 case ICU_chain_step_type_display:
145 case ICU_chain_step_type_casemap:
146 icu_casemap_destroy(step->u.casemap);
148 case ICU_chain_step_type_transform:
149 case ICU_chain_step_type_transliterate:
150 icu_transform_destroy(step->u.transform);
152 case ICU_chain_step_type_tokenize:
153 icu_tokenizer_destroy(step->u.tokenizer);
155 case YAZ_chain_step_type_stemming:
156 yaz_stemmer_destroy(step->u.stemmer);
158 case ICU_chain_step_type_join:
159 icu_buf_utf16_destroy(step->u.join);
167 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
169 struct icu_chain_step *step = 0;
170 struct icu_chain_step **sp = &step;
173 *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
174 (*sp)->type = old->type;
178 case ICU_chain_step_type_display:
180 case ICU_chain_step_type_casemap:
181 (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
183 case ICU_chain_step_type_transform:
184 case ICU_chain_step_type_transliterate:
185 (*sp)->u.transform = icu_transform_clone(old->u.transform);
187 case ICU_chain_step_type_tokenize:
188 (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
190 case YAZ_chain_step_type_stemming:
191 (*sp)->u.stemmer = yaz_stemmer_clone(old->u.stemmer);
193 case ICU_chain_step_type_none:
195 case ICU_chain_step_type_join:
196 (*sp)->u.join = icu_buf_utf16_create(0);
197 (*sp)->u.join = icu_buf_utf16_copy((*sp)->u.join, old->u.join);
201 sp = &(*sp)->previous;
207 struct icu_chain *icu_chain_create(const char *locale, int sort,
210 struct icu_chain *chain;
211 UCollator *coll = ucol_open(locale, status);
213 if (U_FAILURE(*status))
216 chain = (struct icu_chain *) xmalloc(sizeof(*chain));
218 chain->locale = xstrdup(locale);
226 void icu_chain_destroy(struct icu_chain *chain)
231 ucol_close(chain->coll);
234 icu_iter_destroy(chain->iter);
235 icu_chain_step_destroy(chain->csteps);
236 xfree(chain->locale);
241 struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
247 struct icu_chain *chain = 0;
250 *status = U_ZERO_ERROR;
252 if (xml_node && xml_node->type == XML_ELEMENT_NODE)
254 const char *xml_locale = yaz_xml_get_prop((xmlNode *) xml_node,
257 chain = icu_chain_create((const char *) xml_locale, sort, status);
263 nmem = nmem_create();
264 for (node = xml_node->children; node; node = node->next)
267 struct icu_chain_step *step = 0;
268 const char *attr_str;
271 if (node->type != XML_ELEMENT_NODE)
273 attr_str = yaz_xml_get_prop(node, "rule%s", &rule);
276 yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
277 "element '%s'", attr_str, node->name);
280 if (!rule && node->children)
281 rule = nmem_text_node_cdata(node->children, nmem);
283 if (!rule && strcmp((const char *) node->name, "display"))
285 yaz_log(YLOG_WARN, "Missing attribute 'rule' for element %s",
286 (const char *) node->name);
290 if (!strcmp((const char *) node->name, "casemap"))
291 step = icu_chain_insert_step(chain,
292 ICU_chain_step_type_casemap,
294 else if (!strcmp((const char *) node->name, "transform"))
295 step = icu_chain_insert_step(chain,
296 ICU_chain_step_type_transform,
298 else if (!strcmp((const char *) node->name, "transliterate"))
299 step = icu_chain_insert_step(chain,
300 ICU_chain_step_type_transliterate,
302 else if (!strcmp((const char *) node->name, "tokenize"))
303 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
305 else if (!strcmp((const char *) node->name, "display"))
306 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
308 else if (!strcmp((const char *) node->name, "stemming"))
309 step = icu_chain_insert_step(chain, YAZ_chain_step_type_stemming,
311 else if (!strcmp((const char *) node->name, "join"))
312 step = icu_chain_insert_step(chain, ICU_chain_step_type_join,
314 else if (!strcmp((const char *) node->name, "normalize"))
316 yaz_log(YLOG_WARN, "Element %s is deprecated. "
317 "Use transform instead", node->name);
318 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
321 else if (!strcmp((const char *) node->name, "index")
322 || !strcmp((const char *) node->name, "sortkey"))
324 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
325 "Remove it from the configuration", node->name);
329 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
335 yaz_log(YLOG_WARN, "Step not created for %s", node->name);
338 if (step && U_FAILURE(*status))
340 yaz_log(YLOG_WARN, "ICU Error %d %s for element %s, rule %s",
341 *status, u_errorName(*status), node->name, rule ?
350 icu_chain_destroy(chain);
357 struct icu_chain *chain;
358 struct icu_buf_utf16 *last;
359 struct icu_buf_utf16 *org;
360 struct icu_buf_utf8 *org8;
362 struct icu_buf_utf8 *display;
363 struct icu_buf_utf8 *sort8;
364 struct icu_buf_utf8 *result;
370 struct icu_chain_step *steps;
373 void icu_utf16_print(struct icu_buf_utf16 *src16)
375 UErrorCode status = U_ZERO_ERROR;
377 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
378 icu_utf16_to_utf8(dst8, src16, &status);
380 if (U_FAILURE(status))
386 p = icu_buf_utf8_to_cstr(dst8);
389 icu_buf_utf8_destroy(dst8);
392 struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
393 struct icu_chain_step *step,
394 struct icu_buf_utf16 *src)
400 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
404 case ICU_chain_step_type_casemap:
407 struct icu_buf_utf16 *src = dst;
409 dst = icu_buf_utf16_create(0);
410 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
411 iter->chain->locale);
412 icu_buf_utf16_destroy(src);
415 case ICU_chain_step_type_tokenize:
418 struct icu_buf_utf16 *src = dst;
420 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
422 { /* no need to copy if it's already the same */
423 iter->utf8_base = iter->utf16_base = 0;
424 icu_buf_utf16_copy(iter->org, src);
426 icu_buf_utf16_destroy(src);
428 dst = icu_buf_utf16_create(0);
429 iter->status = U_ZERO_ERROR;
430 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status,
431 &iter->org_start, &iter->org_len))
433 icu_buf_utf16_destroy(dst);
437 case ICU_chain_step_type_transform:
438 case ICU_chain_step_type_transliterate:
441 struct icu_buf_utf16 *src = dst;
442 dst = icu_buf_utf16_create(0);
443 icu_transform_trans(step->u.transform, dst, src, &iter->status);
444 icu_buf_utf16_destroy(src);
447 case ICU_chain_step_type_display:
449 icu_utf16_to_utf8(iter->display, dst, &iter->status);
451 case YAZ_chain_step_type_stemming:
454 struct icu_buf_utf16 *src = dst;
455 dst = icu_buf_utf16_create(0);
456 yaz_stemmer_stem(step->u.stemmer, dst, src, &iter->status);
457 icu_buf_utf16_destroy(src);
460 case ICU_chain_step_type_join:
465 struct icu_buf_utf16 *dst1 =
466 icu_iter_invoke(iter, step->previous, 0);
470 dst = icu_buf_utf16_append(dst, step->u.join);
471 dst = icu_buf_utf16_append(dst, dst1);
472 icu_buf_utf16_destroy(dst1);
483 yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
485 yaz_icu_iter_t iter = xmalloc(sizeof(*iter));
487 iter->status = U_ZERO_ERROR;
488 iter->display = icu_buf_utf8_create(0);
489 iter->sort8 = icu_buf_utf8_create(0);
490 iter->result = icu_buf_utf8_create(0);
491 iter->org = icu_buf_utf16_create(0);
493 iter->last = 0; /* no last returned string (yet) */
494 iter->steps = icu_chain_step_clone(chain->csteps);
495 iter->token_count = 0;
500 void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
502 struct icu_buf_utf16 *src = icu_buf_utf16_create(0);
503 icu_utf16_from_utf8_cstr(src, src8cstr, &iter->status);
504 icu_buf_utf16_copy(iter->org, src);
505 iter->token_count = 0;
507 iter->utf8_base = iter->utf16_base = 0;
508 iter->org_len = src->utf16_len;
509 iter->last = icu_iter_invoke(iter, iter->steps, src);
512 void icu_iter_destroy(yaz_icu_iter_t iter)
516 icu_buf_utf8_destroy(iter->display);
517 icu_buf_utf8_destroy(iter->sort8);
518 icu_buf_utf8_destroy(iter->result);
519 icu_buf_utf16_destroy(iter->org);
520 icu_buf_utf8_destroy(iter->org8);
521 icu_chain_step_destroy(iter->steps);
526 int icu_iter_next(yaz_icu_iter_t iter)
528 if (iter->token_count && iter->last)
529 iter->last = icu_iter_invoke(iter, iter->steps, 0);
535 if (iter->chain->sort)
537 icu_sortkey8_from_utf16(iter->chain->coll,
538 iter->sort8, iter->last,
541 icu_utf16_to_utf8(iter->result, iter->last, &iter->status);
542 icu_buf_utf16_destroy(iter->last);
548 const char *icu_iter_get_norm(yaz_icu_iter_t iter)
550 return icu_buf_utf8_to_cstr(iter->result);
553 const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
555 return icu_buf_utf8_to_cstr(iter->sort8);
558 const char *icu_iter_get_display(yaz_icu_iter_t iter)
560 return icu_buf_utf8_to_cstr(iter->display);
563 int icu_iter_get_token_number(yaz_icu_iter_t iter)
565 return iter->token_count;
569 void icu_iter_get_org_info2(yaz_icu_iter_t iter, size_t *start, size_t *len,
572 int32_t len1 = 0, len2 = 0;
573 UErrorCode status = U_ZERO_ERROR;
575 if (iter->org_start < iter->utf16_base)
578 iter->utf16_base = 0;
580 u_strToUTF8(0, 0, &len1,
581 iter->org->utf16 + iter->utf16_base,
582 iter->org_start - iter->utf16_base,
585 status = U_ZERO_ERROR;
587 *start = len1 + iter->utf8_base;
589 u_strToUTF8(0, 0, &len2,
590 iter->org->utf16 + iter->utf16_base,
591 iter->org_start - iter->utf16_base + iter->org_len,
599 iter->org8 = icu_buf_utf8_create(0);
600 status = U_ZERO_ERROR;
601 icu_utf16_to_utf8(iter->org8, iter->org, &status);
602 *cstr = icu_buf_utf8_to_cstr(iter->org8);
604 iter->utf8_base = *start;
605 iter->utf16_base = iter->org_start;
608 void icu_iter_get_org_info(yaz_icu_iter_t iter, size_t *start, size_t *len)
610 icu_iter_get_org_info2(iter, start, len, 0);
613 int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr,
617 icu_iter_destroy(chain->iter);
618 chain->iter = icu_iter_create(chain);
619 icu_iter_first(chain->iter, src8cstr);
623 int icu_chain_next_token(struct icu_chain *chain, UErrorCode *status)
625 *status = U_ZERO_ERROR;
626 return icu_iter_next(chain->iter);
629 int icu_chain_token_number(struct icu_chain *chain)
631 if (chain && chain->iter)
632 return chain->iter->token_count;
636 const char *icu_chain_token_display(struct icu_chain *chain)
639 return icu_iter_get_display(chain->iter);
643 const char *icu_chain_token_norm(struct icu_chain *chain)
646 return icu_iter_get_norm(chain->iter);
650 const char *icu_chain_token_sortkey(struct icu_chain *chain)
653 return icu_iter_get_sortkey(chain->iter);
657 void icu_chain_get_org_info(struct icu_chain *chain, size_t *start, size_t *len)
660 icu_iter_get_org_info(chain->iter, start, len);
663 void icu_chain_get_org_info2(struct icu_chain *chain, size_t *start,
664 size_t *len, const char **cstr)
667 icu_iter_get_org_info2(chain->iter, start, len, cstr);
671 #endif /* YAZ_HAVE_ICU */
676 * c-file-style: "Stroustrup"
677 * indent-tabs-mode: nil
679 * vim: shiftwidth=4 tabstop=8 expandtab