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 */
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 * steps;
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 *icu_chain_create(const char *locale, int sort,
148 struct icu_chain * chain
149 = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
151 *status = U_ZERO_ERROR;
154 chain->locale = xstrdup(locale);
158 chain->coll = ucol_open((const char *) chain->locale, status);
160 if (U_FAILURE(*status))
163 chain->norm8 = icu_buf_utf8_create(0);
169 void icu_chain_destroy(struct icu_chain * chain)
174 ucol_close(chain->coll);
176 icu_buf_utf8_destroy(chain->norm8);
178 icu_iter_destroy(chain->iter);
179 icu_chain_step_destroy(chain->steps);
180 xfree(chain->locale);
185 static struct icu_chain_step *icu_chain_insert_step(
186 struct icu_chain * chain, enum icu_chain_step_type type,
187 const uint8_t * rule, UErrorCode *status);
189 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node,
194 struct icu_chain * chain = 0;
196 *status = U_ZERO_ERROR;
198 if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
202 xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node,
203 (xmlChar *) "locale");
207 chain = icu_chain_create((const char *) xml_locale, sort, status);
215 for (node = xml_node->children; node; node = node->next)
218 struct icu_chain_step * step = 0;
220 if (node->type != XML_ELEMENT_NODE)
223 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
225 if (!strcmp((const char *) node->name, "casemap"))
226 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
227 (const uint8_t *) xml_rule, status);
228 else if (!strcmp((const char *) node->name, "transform"))
229 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
230 (const uint8_t *) xml_rule, status);
231 else if (!strcmp((const char *) node->name, "transliterate"))
232 step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
233 (const uint8_t *) xml_rule, status);
234 else if (!strcmp((const char *) node->name, "tokenize"))
235 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
236 (const uint8_t *) xml_rule, status);
237 else if (!strcmp((const char *) node->name, "display"))
238 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
239 (const uint8_t *) "", status);
240 else if (!strcmp((const char *) node->name, "normalize"))
242 yaz_log(YLOG_WARN, "Element %s is deprecated. "
243 "Use transform instead", node->name);
244 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
245 (const uint8_t *) xml_rule, status);
247 else if (!strcmp((const char *) node->name, "index")
248 || !strcmp((const char *) node->name, "sortkey"))
250 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
251 "Remove it from the configuration", node->name);
255 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
256 icu_chain_destroy(chain);
260 if (step && U_FAILURE(*status))
262 icu_chain_destroy(chain);
269 static struct icu_chain_step *icu_chain_insert_step(
270 struct icu_chain * chain, enum icu_chain_step_type type,
271 const uint8_t * rule, UErrorCode *status)
273 struct icu_chain_step * step = 0;
274 if (!chain || !type || !rule)
277 /* create actual chain step with this buffer */
278 step = icu_chain_step_create(chain, type, rule,
281 step->previous = chain->steps;
288 struct icu_chain *chain;
289 struct icu_buf_utf16 *last;
291 struct icu_buf_utf8 *display;
292 struct icu_buf_utf8 *sort8;
293 struct icu_buf_utf16 *input;
297 void icu_utf16_print(struct icu_buf_utf16 *src16)
299 UErrorCode status = U_ZERO_ERROR;
301 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
302 icu_utf16_to_utf8(dst8, src16, &status);
304 assert(status != 1234);
305 if (U_FAILURE(status))
311 p = icu_buf_utf8_to_cstr(dst8);
314 icu_buf_utf8_destroy(dst8);
317 struct icu_buf_utf16 *icu_iter_invoke(struct icu_iter *iter,
318 struct icu_chain_step *step,
319 struct icu_buf_utf16 *src)
325 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
329 case ICU_chain_step_type_casemap:
332 struct icu_buf_utf16 *src = dst;
334 dst = icu_buf_utf16_create(0);
335 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
336 iter->chain->locale);
337 icu_buf_utf16_destroy(src);
340 case ICU_chain_step_type_tokenize:
343 struct icu_buf_utf16 *src = dst;
345 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
346 icu_buf_utf16_destroy(src);
348 dst = icu_buf_utf16_create(0);
349 iter->status = U_ZERO_ERROR;
350 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
352 icu_buf_utf16_destroy(dst);
356 case ICU_chain_step_type_transform:
357 case ICU_chain_step_type_transliterate:
360 struct icu_buf_utf16 *src = dst;
361 dst = icu_buf_utf16_create(0);
362 icu_transform_trans(step->u.transform, dst, src, &iter->status);
363 icu_buf_utf16_destroy(src);
366 case ICU_chain_step_type_display:
368 icu_utf16_to_utf8(iter->display, dst, &iter->status);
377 struct icu_iter *icu_iter_create(struct icu_chain *chain,
378 const char *src8cstr)
384 struct icu_iter *iter = xmalloc(sizeof(*iter));
386 iter->status = U_ZERO_ERROR;
387 iter->display = icu_buf_utf8_create(0);
388 iter->sort8 = icu_buf_utf8_create(0);
389 iter->token_count = 0;
390 iter->last = 0; /* no last returned string (yet) */
392 /* fill and assign input string.. It will be 0 after
394 iter->input = icu_buf_utf16_create(0);
395 icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
401 void icu_iter_destroy(struct icu_iter *iter)
405 icu_buf_utf8_destroy(iter->display);
406 icu_buf_utf8_destroy(iter->sort8);
408 icu_buf_utf16_destroy(iter->input);
413 int icu_iter_next(struct icu_iter *iter, struct icu_buf_utf8 *result)
415 if (!iter->input && iter->last == 0)
419 /* on first call, iter->input is the input string. Thereafter: 0. */
420 iter->last = icu_iter_invoke(iter, iter->chain->steps, iter->input);
428 if (iter->chain->sort)
430 icu_sortkey8_from_utf16(iter->chain->coll,
431 iter->sort8, iter->last,
434 icu_utf16_to_utf8(result, iter->last, &iter->status);
435 icu_buf_utf16_destroy(iter->last);
441 const char *icu_iter_get_sortkey(struct icu_iter *iter)
443 return icu_buf_utf8_to_cstr(iter->sort8);
446 const char *icu_iter_get_display(struct icu_iter *iter)
448 return icu_buf_utf8_to_cstr(iter->display);
451 int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr,
455 icu_iter_destroy(chain->iter);
456 chain->iter = icu_iter_create(chain, src8cstr);
460 int icu_chain_next_token(struct icu_chain * chain, UErrorCode *status)
462 *status = U_ZERO_ERROR;
463 return icu_iter_next(chain->iter, chain->norm8);
466 int icu_chain_token_number(struct icu_chain * chain)
468 if (chain && chain->iter)
469 return chain->iter->token_count;
473 const char * icu_chain_token_display(struct icu_chain * chain)
476 return icu_iter_get_display(chain->iter);
480 const char * icu_chain_token_norm(struct icu_chain * chain)
483 return icu_buf_utf8_to_cstr(chain->norm8);
487 const char * icu_chain_token_sortkey(struct icu_chain * chain)
490 return icu_iter_get_sortkey(chain->iter);
494 #endif /* YAZ_HAVE_ICU */
499 * c-file-style: "Stroustrup"
500 * indent-tabs-mode: nil
502 * vim: shiftwidth=4 tabstop=8 expandtab