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;
59 /* linked list of chain steps */
60 struct icu_chain_step * csteps;
63 int icu_check_status(UErrorCode status)
65 if (U_FAILURE(status))
67 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
73 static struct icu_chain_step *icu_chain_step_create(
74 struct icu_chain * chain, enum icu_chain_step_type type,
78 struct icu_chain_step * step = 0;
80 if (!chain || !type || !rule)
83 step = (struct icu_chain_step *) xmalloc(sizeof(*step));
86 /* create auxilary objects */
89 case ICU_chain_step_type_display:
91 case ICU_chain_step_type_casemap:
92 step->u.casemap = icu_casemap_create(rule[0], status);
94 case ICU_chain_step_type_transform:
95 /* rule omitted. Only ID used */
96 step->u.transform = icu_transform_create((const char *) rule, 'f',
99 case ICU_chain_step_type_tokenize:
100 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
101 (char) rule[0], status);
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);
115 static void icu_chain_step_destroy(struct icu_chain_step * step)
120 icu_chain_step_destroy(step->previous);
124 case ICU_chain_step_type_display:
126 case ICU_chain_step_type_casemap:
127 icu_casemap_destroy(step->u.casemap);
129 case ICU_chain_step_type_transform:
130 case ICU_chain_step_type_transliterate:
131 icu_transform_destroy(step->u.transform);
133 case ICU_chain_step_type_tokenize:
134 icu_tokenizer_destroy(step->u.tokenizer);
142 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
144 struct icu_chain_step *step = 0;
145 struct icu_chain_step **sp = &step;
148 *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
149 (*sp)->type = old->type;
153 case ICU_chain_step_type_display:
155 case ICU_chain_step_type_casemap:
156 (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
158 case ICU_chain_step_type_transform:
159 case ICU_chain_step_type_transliterate:
160 (*sp)->u.transform = icu_transform_clone(old->u.transform);
162 case ICU_chain_step_type_tokenize:
163 (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
165 case ICU_chain_step_type_none:
169 sp = &(*sp)->previous;
175 struct icu_chain *icu_chain_create(const char *locale, int sort,
178 struct icu_chain * chain
179 = (struct icu_chain *) xmalloc(sizeof(*chain));
181 *status = U_ZERO_ERROR;
184 chain->locale = xstrdup(locale);
188 chain->coll = ucol_open((const char *) chain->locale, status);
190 if (U_FAILURE(*status))
198 void icu_chain_destroy(struct icu_chain * chain)
203 ucol_close(chain->coll);
206 icu_iter_destroy(chain->iter);
207 icu_chain_step_destroy(chain->csteps);
208 xfree(chain->locale);
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);
217 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node,
222 struct icu_chain * chain = 0;
224 *status = U_ZERO_ERROR;
226 if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
230 xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node,
231 (xmlChar *) "locale");
235 chain = icu_chain_create((const char *) xml_locale, sort, status);
243 for (node = xml_node->children; node; node = node->next)
246 struct icu_chain_step * step = 0;
248 if (node->type != XML_ELEMENT_NODE)
251 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
253 if (!strcmp((const char *) node->name, "casemap"))
254 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
255 (const uint8_t *) xml_rule, status);
256 else if (!strcmp((const char *) node->name, "transform"))
257 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
258 (const uint8_t *) xml_rule, status);
259 else if (!strcmp((const char *) node->name, "transliterate"))
260 step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
261 (const uint8_t *) xml_rule, status);
262 else if (!strcmp((const char *) node->name, "tokenize"))
263 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
264 (const uint8_t *) xml_rule, status);
265 else if (!strcmp((const char *) node->name, "display"))
266 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
267 (const uint8_t *) "", status);
268 else if (!strcmp((const char *) node->name, "normalize"))
270 yaz_log(YLOG_WARN, "Element %s is deprecated. "
271 "Use transform instead", node->name);
272 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
273 (const uint8_t *) xml_rule, status);
275 else if (!strcmp((const char *) node->name, "index")
276 || !strcmp((const char *) node->name, "sortkey"))
278 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
279 "Remove it from the configuration", node->name);
283 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
284 icu_chain_destroy(chain);
288 if (step && U_FAILURE(*status))
290 icu_chain_destroy(chain);
298 static struct icu_chain_step *icu_chain_insert_step(
299 struct icu_chain * chain, enum icu_chain_step_type type,
300 const uint8_t * rule, UErrorCode *status)
302 struct icu_chain_step * step = 0;
303 if (!chain || !type || !rule)
306 /* create actual chain step with this buffer */
307 step = icu_chain_step_create(chain, type, rule,
310 step->previous = chain->csteps;
311 chain->csteps = step;
317 struct icu_chain *chain;
318 struct icu_buf_utf16 *last;
320 struct icu_buf_utf8 *display;
321 struct icu_buf_utf8 *sort8;
322 struct icu_buf_utf8 *result;
323 struct icu_buf_utf16 *input;
325 struct icu_chain_step *steps;
328 void icu_utf16_print(struct icu_buf_utf16 *src16)
330 UErrorCode status = U_ZERO_ERROR;
332 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
333 icu_utf16_to_utf8(dst8, src16, &status);
335 assert(status != 1234);
336 if (U_FAILURE(status))
342 p = icu_buf_utf8_to_cstr(dst8);
345 icu_buf_utf8_destroy(dst8);
348 struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
349 struct icu_chain_step *step,
350 struct icu_buf_utf16 *src)
356 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
360 case ICU_chain_step_type_casemap:
363 struct icu_buf_utf16 *src = dst;
365 dst = icu_buf_utf16_create(0);
366 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
367 iter->chain->locale);
368 icu_buf_utf16_destroy(src);
371 case ICU_chain_step_type_tokenize:
374 struct icu_buf_utf16 *src = dst;
376 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
377 icu_buf_utf16_destroy(src);
379 dst = icu_buf_utf16_create(0);
380 iter->status = U_ZERO_ERROR;
381 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
383 icu_buf_utf16_destroy(dst);
387 case ICU_chain_step_type_transform:
388 case ICU_chain_step_type_transliterate:
391 struct icu_buf_utf16 *src = dst;
392 dst = icu_buf_utf16_create(0);
393 icu_transform_trans(step->u.transform, dst, src, &iter->status);
394 icu_buf_utf16_destroy(src);
397 case ICU_chain_step_type_display:
399 icu_utf16_to_utf8(iter->display, dst, &iter->status);
408 yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
410 yaz_icu_iter_t iter = xmalloc(sizeof(*iter));
412 iter->status = U_ZERO_ERROR;
413 iter->display = icu_buf_utf8_create(0);
414 iter->sort8 = icu_buf_utf8_create(0);
415 iter->result = icu_buf_utf8_create(0);
416 iter->last = 0; /* no last returned string (yet) */
417 iter->steps = icu_chain_step_clone(chain->csteps);
423 void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
426 icu_buf_utf16_destroy(iter->input);
427 iter->input = icu_buf_utf16_create(0);
428 iter->token_count = 0;
429 /* fill and assign input string.. It will be 0 after
431 icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
434 void icu_iter_destroy(yaz_icu_iter_t iter)
438 icu_buf_utf8_destroy(iter->display);
439 icu_buf_utf8_destroy(iter->sort8);
440 icu_buf_utf8_destroy(iter->result);
442 icu_buf_utf16_destroy(iter->input);
443 icu_chain_step_destroy(iter->steps);
448 int icu_iter_next(yaz_icu_iter_t iter)
450 if (!iter->input && iter->last == 0)
454 /* on first call, iter->input is the input string. Thereafter: 0. */
455 iter->last = icu_iter_invoke(iter, iter->steps ?
456 iter->steps : iter->chain->csteps,
465 if (iter->chain->sort)
467 icu_sortkey8_from_utf16(iter->chain->coll,
468 iter->sort8, iter->last,
471 icu_utf16_to_utf8(iter->result, iter->last, &iter->status);
472 icu_buf_utf16_destroy(iter->last);
478 const char *icu_iter_get_norm(yaz_icu_iter_t iter)
480 return icu_buf_utf8_to_cstr(iter->result);
483 const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
485 return icu_buf_utf8_to_cstr(iter->sort8);
488 const char *icu_iter_get_display(yaz_icu_iter_t iter)
490 return icu_buf_utf8_to_cstr(iter->display);
493 int icu_iter_get_token_number(yaz_icu_iter_t iter)
495 return iter->token_count;
498 int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr,
502 icu_iter_destroy(chain->iter);
503 chain->iter = icu_iter_create(chain);
504 icu_iter_first(chain->iter, src8cstr);
508 int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
510 *status = U_ZERO_ERROR;
511 return icu_iter_next(chain->iter);
514 int icu_chain_token_number(struct icu_chain * chain)
516 if (chain && chain->iter)
517 return chain->iter->token_count;
521 const char * icu_chain_token_display(struct icu_chain * chain)
524 return icu_iter_get_display(chain->iter);
528 const char * icu_chain_token_norm(struct icu_chain * chain)
531 return icu_iter_get_norm(chain->iter);
535 const char * icu_chain_token_sortkey(struct icu_chain * chain)
538 return icu_iter_get_sortkey(chain->iter);
542 #endif /* YAZ_HAVE_ICU */
547 * c-file-style: "Stroustrup"
548 * indent-tabs-mode: nil
550 * vim: shiftwidth=4 tabstop=8 expandtab