1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 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 */
41 /* type and action object */
42 enum icu_chain_step_type type;
44 struct icu_casemap * casemap;
45 struct icu_transform * transform;
46 struct icu_tokenizer * tokenizer;
48 struct icu_chain_step * previous;
53 struct icu_iter *iter;
59 /* utf8 output buffers */
60 struct icu_buf_utf8 * norm8;
62 /* linked list of chain steps */
63 struct icu_chain_step * csteps;
66 int icu_check_status(UErrorCode status)
68 if (U_FAILURE(status))
70 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
76 static struct icu_chain_step *icu_chain_step_create(
77 struct icu_chain * chain, enum icu_chain_step_type type,
81 struct icu_chain_step * step = 0;
83 if (!chain || !type || !rule)
86 step = (struct icu_chain_step *) xmalloc(sizeof(struct icu_chain_step));
89 /* create auxilary objects */
92 case ICU_chain_step_type_display:
94 case ICU_chain_step_type_casemap:
95 step->u.casemap = icu_casemap_create(rule[0], status);
97 case ICU_chain_step_type_transform:
98 /* rule omitted. Only ID used */
99 step->u.transform = icu_transform_create((const char *) rule, 'f',
102 case ICU_chain_step_type_tokenize:
103 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
104 (char) rule[0], status);
106 case ICU_chain_step_type_transliterate:
107 /* we pass a dummy ID to utrans_openU.. */
108 step->u.transform = icu_transform_create("custom", 'f',
109 (const char *) rule, status);
118 static void icu_chain_step_destroy(struct icu_chain_step * step)
123 icu_chain_step_destroy(step->previous);
127 case ICU_chain_step_type_display:
129 case ICU_chain_step_type_casemap:
130 icu_casemap_destroy(step->u.casemap);
132 case ICU_chain_step_type_transform:
133 case ICU_chain_step_type_transliterate:
134 icu_transform_destroy(step->u.transform);
136 case ICU_chain_step_type_tokenize:
137 icu_tokenizer_destroy(step->u.tokenizer);
145 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
147 struct icu_chain_step *step = 0;
148 struct icu_chain_step **sp = &step;
151 *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
152 (*sp)->type = old->type;
156 case ICU_chain_step_type_display:
158 case ICU_chain_step_type_casemap:
159 (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
161 case ICU_chain_step_type_transform:
162 case ICU_chain_step_type_transliterate:
163 (*sp)->u.transform = icu_transform_clone(old->u.transform);
165 case ICU_chain_step_type_tokenize:
166 (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
168 case ICU_chain_step_type_none:
172 sp = &(*sp)->previous;
178 struct icu_chain *icu_chain_create(const char *locale, int sort,
181 struct icu_chain * chain
182 = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
184 *status = U_ZERO_ERROR;
187 chain->locale = xstrdup(locale);
191 chain->coll = ucol_open((const char *) chain->locale, status);
193 if (U_FAILURE(*status))
196 chain->norm8 = icu_buf_utf8_create(0);
202 void icu_chain_destroy(struct icu_chain * chain)
207 ucol_close(chain->coll);
209 icu_buf_utf8_destroy(chain->norm8);
211 icu_iter_destroy(chain->iter);
212 icu_chain_step_destroy(chain->csteps);
213 xfree(chain->locale);
218 static struct icu_chain_step *icu_chain_insert_step(
219 struct icu_chain * chain, enum icu_chain_step_type type,
220 const uint8_t * rule, UErrorCode *status);
222 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node,
227 struct icu_chain * chain = 0;
229 *status = U_ZERO_ERROR;
231 if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
235 xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node,
236 (xmlChar *) "locale");
240 chain = icu_chain_create((const char *) xml_locale, sort, status);
248 for (node = xml_node->children; node; node = node->next)
251 struct icu_chain_step * step = 0;
253 if (node->type != XML_ELEMENT_NODE)
256 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
258 if (!strcmp((const char *) node->name, "casemap"))
259 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
260 (const uint8_t *) xml_rule, status);
261 else if (!strcmp((const char *) node->name, "transform"))
262 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
263 (const uint8_t *) xml_rule, status);
264 else if (!strcmp((const char *) node->name, "transliterate"))
265 step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
266 (const uint8_t *) xml_rule, status);
267 else if (!strcmp((const char *) node->name, "tokenize"))
268 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
269 (const uint8_t *) xml_rule, status);
270 else if (!strcmp((const char *) node->name, "display"))
271 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
272 (const uint8_t *) "", status);
273 else if (!strcmp((const char *) node->name, "normalize"))
275 yaz_log(YLOG_WARN, "Element %s is deprecated. "
276 "Use transform instead", node->name);
277 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
278 (const uint8_t *) xml_rule, status);
280 else if (!strcmp((const char *) node->name, "index")
281 || !strcmp((const char *) node->name, "sortkey"))
283 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
284 "Remove it from the configuration", node->name);
288 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
289 icu_chain_destroy(chain);
293 if (step && U_FAILURE(*status))
295 icu_chain_destroy(chain);
303 static struct icu_chain_step *icu_chain_insert_step(
304 struct icu_chain * chain, enum icu_chain_step_type type,
305 const uint8_t * rule, UErrorCode *status)
307 struct icu_chain_step * step = 0;
308 if (!chain || !type || !rule)
311 /* create actual chain step with this buffer */
312 step = icu_chain_step_create(chain, type, rule,
315 step->previous = chain->csteps;
316 chain->csteps = step;
322 struct icu_chain *chain;
323 struct icu_buf_utf16 *last;
325 struct icu_buf_utf8 *display;
326 struct icu_buf_utf8 *sort8;
327 struct icu_buf_utf16 *input;
329 struct icu_chain_step *steps;
332 void icu_utf16_print(struct icu_buf_utf16 *src16)
334 UErrorCode status = U_ZERO_ERROR;
336 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
337 icu_utf16_to_utf8(dst8, src16, &status);
339 assert(status != 1234);
340 if (U_FAILURE(status))
346 p = icu_buf_utf8_to_cstr(dst8);
349 icu_buf_utf8_destroy(dst8);
352 struct icu_buf_utf16 *icu_iter_invoke(struct icu_iter *iter,
353 struct icu_chain_step *step,
354 struct icu_buf_utf16 *src)
360 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
364 case ICU_chain_step_type_casemap:
367 struct icu_buf_utf16 *src = dst;
369 dst = icu_buf_utf16_create(0);
370 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
371 iter->chain->locale);
372 icu_buf_utf16_destroy(src);
375 case ICU_chain_step_type_tokenize:
378 struct icu_buf_utf16 *src = dst;
380 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
381 icu_buf_utf16_destroy(src);
383 dst = icu_buf_utf16_create(0);
384 iter->status = U_ZERO_ERROR;
385 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
387 icu_buf_utf16_destroy(dst);
391 case ICU_chain_step_type_transform:
392 case ICU_chain_step_type_transliterate:
395 struct icu_buf_utf16 *src = dst;
396 dst = icu_buf_utf16_create(0);
397 icu_transform_trans(step->u.transform, dst, src, &iter->status);
398 icu_buf_utf16_destroy(src);
401 case ICU_chain_step_type_display:
403 icu_utf16_to_utf8(iter->display, dst, &iter->status);
412 struct icu_iter *icu_iter_create(struct icu_chain *chain,
413 const char *src8cstr)
419 struct icu_iter *iter = xmalloc(sizeof(*iter));
421 iter->status = U_ZERO_ERROR;
422 iter->display = icu_buf_utf8_create(0);
423 iter->sort8 = icu_buf_utf8_create(0);
424 iter->token_count = 0;
425 iter->last = 0; /* no last returned string (yet) */
427 iter->steps = icu_chain_step_clone(chain->csteps);
432 /* fill and assign input string.. It will be 0 after
434 iter->input = icu_buf_utf16_create(0);
435 icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
440 void icu_iter_destroy(struct icu_iter *iter)
444 icu_buf_utf8_destroy(iter->display);
445 icu_buf_utf8_destroy(iter->sort8);
447 icu_buf_utf16_destroy(iter->input);
448 icu_chain_step_destroy(iter->steps);
453 int icu_iter_next(struct icu_iter *iter, struct icu_buf_utf8 *result)
455 if (!iter->input && iter->last == 0)
459 /* on first call, iter->input is the input string. Thereafter: 0. */
460 iter->last = icu_iter_invoke(iter, iter->steps ?
461 iter->steps : iter->chain->csteps,
470 if (iter->chain->sort)
472 icu_sortkey8_from_utf16(iter->chain->coll,
473 iter->sort8, iter->last,
476 icu_utf16_to_utf8(result, iter->last, &iter->status);
477 icu_buf_utf16_destroy(iter->last);
483 const char *icu_iter_get_sortkey(struct icu_iter *iter)
485 return icu_buf_utf8_to_cstr(iter->sort8);
488 const char *icu_iter_get_display(struct icu_iter *iter)
490 return icu_buf_utf8_to_cstr(iter->display);
493 int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr,
497 icu_iter_destroy(chain->iter);
498 chain->iter = icu_iter_create(chain, src8cstr);
502 int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
504 *status = U_ZERO_ERROR;
505 return icu_iter_next(chain->iter, chain->norm8);
508 int icu_chain_token_number(struct icu_chain * chain)
510 if (chain && chain->iter)
511 return chain->iter->token_count;
515 const char * icu_chain_token_display(struct icu_chain * chain)
518 return icu_iter_get_display(chain->iter);
522 const char * icu_chain_token_norm(struct icu_chain * chain)
525 return icu_buf_utf8_to_cstr(chain->norm8);
529 const char * icu_chain_token_sortkey(struct icu_chain * chain)
532 return icu_iter_get_sortkey(chain->iter);
536 #endif /* YAZ_HAVE_ICU */
541 * c-file-style: "Stroustrup"
542 * indent-tabs-mode: nil
544 * vim: shiftwidth=4 tabstop=8 expandtab