1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
27 #include <unicode/ustring.h> /* some more string fcns*/
28 #include <unicode/uchar.h> /* char names */
30 enum icu_chain_step_type {
31 ICU_chain_step_type_none,
32 ICU_chain_step_type_display, /* convert to utf8 display format */
33 ICU_chain_step_type_casemap, /* apply utf16 charmap */
34 ICU_chain_step_type_transform, /* apply utf16 transform */
35 ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
36 ICU_chain_step_type_transliterate /* apply utf16 tokenization */
43 /* type and action object */
44 enum icu_chain_step_type type;
46 struct icu_casemap * casemap;
47 struct icu_transform * transform;
48 struct icu_tokenizer * tokenizer;
50 struct icu_chain_step * previous;
53 /* temprary post-action utf16 buffer */
54 struct icu_buf_utf16 * buf16;
64 struct icu_iter *iter;
74 const char * src8cstr;
76 /* number of tokens returned so far */
80 /* utf8 output buffers */
81 struct icu_buf_utf8 * norm8;
84 struct icu_buf_utf8 * display8;
85 struct icu_buf_utf8 * sort8;
87 /* utf16 source buffer */
88 struct icu_buf_utf16 * src16;
91 /* linked list of chain steps */
92 struct icu_chain_step * steps;
95 int icu_check_status(UErrorCode status)
97 if (U_FAILURE(status))
99 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
105 static struct icu_chain_step *icu_chain_step_create(
106 struct icu_chain * chain, enum icu_chain_step_type type,
107 const uint8_t * rule,
110 struct icu_buf_utf16 * buf16,
114 struct icu_chain_step * step = 0;
116 if(!chain || !type || !rule)
119 step = (struct icu_chain_step *) xmalloc(sizeof(struct icu_chain_step));
126 /* create auxilary objects */
129 case ICU_chain_step_type_display:
131 case ICU_chain_step_type_casemap:
132 step->u.casemap = icu_casemap_create(rule[0], status);
134 case ICU_chain_step_type_transform:
135 /* rule omitted. Only ID used */
136 step->u.transform = icu_transform_create((const char *) rule, 'f',
139 case ICU_chain_step_type_tokenize:
140 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
141 (char) rule[0], status);
143 case ICU_chain_step_type_transliterate:
144 /* we pass a dummy ID to utrans_openU.. */
145 step->u.transform = icu_transform_create("custom", 'f',
146 (const char *) rule, status);
155 static void icu_chain_step_destroy(struct icu_chain_step * step)
160 icu_chain_step_destroy(step->previous);
164 case ICU_chain_step_type_display:
166 case ICU_chain_step_type_casemap:
167 icu_casemap_destroy(step->u.casemap);
170 icu_buf_utf16_destroy(step->buf16);
173 case ICU_chain_step_type_transform:
174 case ICU_chain_step_type_transliterate:
175 icu_transform_destroy(step->u.transform);
178 icu_buf_utf16_destroy(step->buf16);
181 case ICU_chain_step_type_tokenize:
182 icu_tokenizer_destroy(step->u.tokenizer);
185 icu_buf_utf16_destroy(step->buf16);
194 struct icu_chain *icu_chain_create(const char *locale, int sort,
197 struct icu_chain * chain
198 = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
200 *status = U_ZERO_ERROR;
205 chain->locale = xstrdup(locale);
209 chain->coll = ucol_open((const char *) chain->locale, status);
211 if (U_FAILURE(*status))
215 chain->token_count = 0;
219 chain->norm8 = icu_buf_utf8_create(0);
222 chain->display8 = icu_buf_utf8_create(0);
223 chain->sort8 = icu_buf_utf8_create(0);
224 chain->src16 = icu_buf_utf16_create(0);
232 void icu_chain_destroy(struct icu_chain * chain)
237 ucol_close(chain->coll);
239 icu_buf_utf8_destroy(chain->norm8);
242 icu_iter_destroy(chain->iter);
244 icu_buf_utf8_destroy(chain->display8);
245 icu_buf_utf8_destroy(chain->sort8);
246 icu_buf_utf16_destroy(chain->src16);
249 icu_chain_step_destroy(chain->steps);
250 xfree(chain->locale);
255 static struct icu_chain_step *icu_chain_insert_step(
256 struct icu_chain * chain, enum icu_chain_step_type type,
257 const uint8_t * rule, UErrorCode *status);
259 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node,
264 struct icu_chain * chain = 0;
266 *status = U_ZERO_ERROR;
268 if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
272 xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node,
273 (xmlChar *) "locale");
277 chain = icu_chain_create((const char *) xml_locale, sort, status);
285 for (node = xml_node->children; node; node = node->next)
288 struct icu_chain_step * step = 0;
290 if (node->type != XML_ELEMENT_NODE)
293 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
295 if (!strcmp((const char *) node->name, "casemap"))
296 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
297 (const uint8_t *) xml_rule, status);
298 else if (!strcmp((const char *) node->name, "transform"))
299 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
300 (const uint8_t *) xml_rule, status);
301 else if (!strcmp((const char *) node->name, "transliterate"))
302 step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
303 (const uint8_t *) xml_rule, status);
304 else if (!strcmp((const char *) node->name, "tokenize"))
305 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
306 (const uint8_t *) xml_rule, status);
307 else if (!strcmp((const char *) node->name, "display"))
308 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
309 (const uint8_t *) "", status);
310 else if (!strcmp((const char *) node->name, "normalize"))
312 yaz_log(YLOG_WARN, "Element %s is deprecated. "
313 "Use transform instead", node->name);
314 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
315 (const uint8_t *) xml_rule, status);
317 else if (!strcmp((const char *) node->name, "index")
318 || !strcmp((const char *) node->name, "sortkey"))
320 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
321 "Remove it from the configuration", node->name);
325 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
326 icu_chain_destroy(chain);
330 if (step && U_FAILURE(*status))
332 icu_chain_destroy(chain);
339 static struct icu_chain_step *icu_chain_insert_step(
340 struct icu_chain * chain, enum icu_chain_step_type type,
341 const uint8_t * rule, UErrorCode *status)
343 struct icu_chain_step * step = 0;
346 struct icu_buf_utf16 * src16 = 0;
347 struct icu_buf_utf16 * buf16 = 0;
349 if (!chain || !type || !rule)
354 /* assign utf16 src buffers as needed */
355 if (chain->steps && chain->steps->buf16)
356 src16 = chain->steps->buf16;
357 else if (chain->src16)
358 src16 = chain->src16;
362 /* create utf16 destination buffers as needed, or */
365 case ICU_chain_step_type_display:
368 case ICU_chain_step_type_casemap:
369 buf16 = icu_buf_utf16_create(0);
371 case ICU_chain_step_type_transform:
372 case ICU_chain_step_type_transliterate:
373 buf16 = icu_buf_utf16_create(0);
375 case ICU_chain_step_type_tokenize:
376 buf16 = icu_buf_utf16_create(0);
383 /* create actual chain step with this buffer */
384 step = icu_chain_step_create(chain, type, rule,
391 step->previous = chain->steps;
399 static int icu_chain_step_next_token(struct icu_chain * chain,
400 struct icu_chain_step * step,
403 struct icu_buf_utf16 * src16 = 0;
404 int got_new_token = 0;
406 if (!chain || !chain->src16 || !step || !step->more_tokens)
409 /* assign utf16 src buffers as needed, advance in previous steps
410 tokens until non-zero token met, and setting stop condition */
414 src16 = step->previous->buf16;
415 /* tokens might be killed in previous steps, therefore looping */
417 while (step->need_new_token
418 && step->previous->more_tokens
421 = icu_chain_step_next_token(chain, step->previous, status);
424 { /* first step can only work once on chain->src16 input buffer */
425 src16 = chain->src16;
426 step->more_tokens = 0;
433 /* stop if nothing to process */
434 if (step->need_new_token && !got_new_token)
436 step->more_tokens = 0;
440 /* either an old token not finished yet, or a new token, thus
441 perform the work, eventually put this steps output in
442 step->buf16 or the chains UTF8 output buffers */
446 case ICU_chain_step_type_display:
447 icu_utf16_to_utf8(chain->display8, src16, status);
449 case ICU_chain_step_type_casemap:
450 icu_casemap_casemap(step->u.casemap,
451 step->buf16, src16, status,
454 case ICU_chain_step_type_transform:
455 case ICU_chain_step_type_transliterate:
456 icu_transform_trans(step->u.transform,
457 step->buf16, src16, status);
459 case ICU_chain_step_type_tokenize:
460 /* attach to new src16 token only first time during splitting */
461 if (step->need_new_token)
463 icu_tokenizer_attach(step->u.tokenizer, src16, status);
464 step->need_new_token = 0;
467 /* splitting one src16 token into multiple buf16 tokens */
469 = icu_tokenizer_next_token(step->u.tokenizer,
470 step->buf16, status);
472 /* make sure to get new previous token if this one had been used up
473 by recursive call to _same_ step */
475 if (!step->more_tokens)
477 step->more_tokens = icu_chain_step_next_token(chain, step, status);
478 return step->more_tokens; /* avoid one token count too much! */
486 if (U_FAILURE(*status))
489 /* if token disappered into thin air, tell caller */
490 /* if (!step->buf16->utf16_len && !step->more_tokens) */
498 struct icu_chain *chain;
499 struct icu_buf_utf16 *last;
501 struct icu_buf_utf8 *display;
502 struct icu_buf_utf8 *sort8;
503 struct icu_buf_utf16 *input;
507 void icu_utf16_print(struct icu_buf_utf16 *src16)
509 UErrorCode status = U_ZERO_ERROR;
511 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
512 icu_utf16_to_utf8(dst8, src16, &status);
514 assert(status != 1234);
515 if (U_FAILURE(status))
521 p = icu_buf_utf8_to_cstr(dst8);
524 icu_buf_utf8_destroy(dst8);
527 struct icu_buf_utf16 *icu_iter_invoke(struct icu_iter *iter,
528 struct icu_chain_step *step,
529 struct icu_buf_utf16 *src)
535 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
539 case ICU_chain_step_type_casemap:
542 struct icu_buf_utf16 *src = dst;
544 dst = icu_buf_utf16_create(0);
545 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
546 iter->chain->locale);
547 icu_buf_utf16_destroy(src);
550 case ICU_chain_step_type_tokenize:
553 struct icu_buf_utf16 *src = dst;
555 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
556 icu_buf_utf16_destroy(src);
558 dst = icu_buf_utf16_create(0);
559 iter->status = U_ZERO_ERROR;
560 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
562 icu_buf_utf16_destroy(dst);
566 case ICU_chain_step_type_transform:
567 case ICU_chain_step_type_transliterate:
570 struct icu_buf_utf16 *src = dst;
571 dst = icu_buf_utf16_create(0);
572 icu_transform_trans(step->u.transform, dst, src, &iter->status);
573 icu_buf_utf16_destroy(src);
576 case ICU_chain_step_type_display:
578 icu_utf16_to_utf8(iter->display, dst, &iter->status);
587 struct icu_iter *icu_iter_create(struct icu_chain *chain,
588 const char *src8cstr)
594 struct icu_iter *iter = xmalloc(sizeof(*iter));
596 iter->status = U_ZERO_ERROR;
597 iter->display = icu_buf_utf8_create(0);
598 iter->sort8 = icu_buf_utf8_create(0);
599 iter->token_count = 0;
600 iter->last = 0; /* no last returned string (yet) */
602 /* fill and assign input string.. It will be 0 after
604 iter->input = icu_buf_utf16_create(0);
605 icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
611 void icu_iter_destroy(struct icu_iter *iter)
615 icu_buf_utf8_destroy(iter->display);
616 icu_buf_utf8_destroy(iter->sort8);
618 icu_buf_utf16_destroy(iter->input);
623 int icu_iter_next(struct icu_iter *iter, struct icu_buf_utf8 *result)
625 if (!iter->input && iter->last == 0)
629 /* on first call, iter->input is the input string. Thereafter: 0. */
630 iter->last = icu_iter_invoke(iter, iter->chain->steps, iter->input);
638 if (iter->chain->sort)
640 icu_sortkey8_from_utf16(iter->chain->coll,
641 iter->sort8, iter->last,
644 icu_utf16_to_utf8(result, iter->last, &iter->status);
645 icu_buf_utf16_destroy(iter->last);
651 const char *icu_iter_get_sortkey(struct icu_iter *iter)
653 return icu_buf_utf8_to_cstr(iter->sort8);
656 const char *icu_iter_get_display(struct icu_iter *iter)
658 return icu_buf_utf8_to_cstr(iter->display);
661 int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr,
666 icu_iter_destroy(chain->iter);
667 chain->iter = icu_iter_create(chain, src8cstr);
670 struct icu_chain_step * stp = 0;
672 if (!chain || !src8cstr)
675 chain->src8cstr = src8cstr;
679 /* clear token count */
680 chain->token_count = 0;
682 /* clear all steps stop states */
685 stp->more_tokens = 1;
686 stp->need_new_token = 1;
690 /* finally convert UTF8 to UTF16 string if needed */
691 if (chain->steps || chain->sort)
692 icu_utf16_from_utf8_cstr(chain->src16, chain->src8cstr, status);
694 if (U_FAILURE(*status))
701 int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
704 *status = U_ZERO_ERROR;
705 return icu_iter_next(chain->iter, chain->norm8);
709 *status = U_ZERO_ERROR;
714 /* special case with no steps - same as index type binary */
717 if (chain->token_count)
721 chain->token_count++;
724 icu_sortkey8_from_utf16(chain->coll,
725 chain->sort8, chain->steps->buf16,
727 return chain->token_count;
730 /* usual case, one or more icu chain steps existing */
733 while (!got_token && chain->steps && chain->steps->more_tokens)
734 got_token = icu_chain_step_next_token(chain, chain->steps, status);
738 chain->token_count++;
740 icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
743 icu_sortkey8_from_utf16(chain->coll,
744 chain->sort8, chain->steps->buf16,
746 return chain->token_count;
754 int icu_chain_token_number(struct icu_chain * chain)
757 if (chain && chain->iter)
758 return chain->iter->token_count;
764 return chain->token_count;
768 const char * icu_chain_token_display(struct icu_chain * chain)
772 return icu_iter_get_display(chain->iter);
775 return icu_buf_utf8_to_cstr(chain->display8);
780 const char * icu_chain_token_norm(struct icu_chain * chain)
784 return icu_buf_utf8_to_cstr(chain->norm8);
787 return chain->src8cstr;
790 return icu_buf_utf8_to_cstr(chain->norm8);
795 const char * icu_chain_token_sortkey(struct icu_chain * chain)
799 return icu_iter_get_sortkey(chain->iter);
802 return icu_buf_utf8_to_cstr(chain->sort8);
807 #endif /* YAZ_HAVE_ICU */
812 * c-file-style: "Stroustrup"
813 * indent-tabs-mode: nil
815 * vim: shiftwidth=4 tabstop=8 expandtab