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>
20 #include <yaz/stemmer.h>
24 #include <yaz/nmem_xml.h>
30 #include <unicode/ustring.h> /* some more string fcns*/
31 #include <unicode/uchar.h> /* char names */
33 enum icu_chain_step_type {
34 ICU_chain_step_type_none,
35 ICU_chain_step_type_display, /* convert to utf8 display format */
36 ICU_chain_step_type_casemap, /* apply utf16 charmap */
37 ICU_chain_step_type_transform, /* apply utf16 transform */
38 ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
39 ICU_chain_step_type_transliterate, /* apply utf16 tokenization */
40 YAZ_chain_step_type_stemming /* apply utf16 stemming (YAZ) */
45 /* type and action object */
46 enum icu_chain_step_type type;
48 struct icu_casemap *casemap;
49 struct icu_transform *transform;
50 struct icu_tokenizer *tokenizer;
51 yaz_stemmer_p stemmer;
53 struct icu_chain_step *previous;
64 /* linked list of chain steps */
65 struct icu_chain_step *csteps;
68 int icu_check_status(UErrorCode status)
70 if (U_FAILURE(status))
72 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
78 static struct icu_chain_step *icu_chain_insert_step(
79 struct icu_chain *chain, enum icu_chain_step_type type,
80 const uint8_t *rule, UErrorCode *status)
82 struct icu_chain_step *step = 0;
84 if (!chain || !type || !rule)
87 step = (struct icu_chain_step *) xmalloc(sizeof(*step));
90 /* create auxilary objects */
93 case ICU_chain_step_type_display:
95 case ICU_chain_step_type_casemap:
96 step->u.casemap = icu_casemap_create(rule[0], status);
98 case ICU_chain_step_type_transform:
99 /* rule omitted. Only ID used */
100 step->u.transform = icu_transform_create((const char *) rule, 'f',
103 case ICU_chain_step_type_tokenize:
104 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
105 (char) rule[0], status);
107 case ICU_chain_step_type_transliterate:
108 /* we pass a dummy ID to utrans_openU.. */
109 step->u.transform = icu_transform_create("custom", 'f',
110 (const char *) rule, status);
112 case YAZ_chain_step_type_stemming:
113 step->u.stemmer = yaz_stemmer_create((char *) chain->locale, (const char *) rule, status);
118 step->previous = chain->csteps;
119 chain->csteps = step;
125 static void icu_chain_step_destroy(struct icu_chain_step *step)
130 icu_chain_step_destroy(step->previous);
134 case ICU_chain_step_type_display:
136 case ICU_chain_step_type_casemap:
137 icu_casemap_destroy(step->u.casemap);
139 case ICU_chain_step_type_transform:
140 case ICU_chain_step_type_transliterate:
141 icu_transform_destroy(step->u.transform);
143 case ICU_chain_step_type_tokenize:
144 icu_tokenizer_destroy(step->u.tokenizer);
146 case YAZ_chain_step_type_stemming:
147 yaz_stemmer_destroy(step->u.stemmer);
155 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
157 struct icu_chain_step *step = 0;
158 struct icu_chain_step **sp = &step;
161 *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
162 (*sp)->type = old->type;
166 case ICU_chain_step_type_display:
168 case ICU_chain_step_type_casemap:
169 (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
171 case ICU_chain_step_type_transform:
172 case ICU_chain_step_type_transliterate:
173 (*sp)->u.transform = icu_transform_clone(old->u.transform);
175 case ICU_chain_step_type_tokenize:
176 (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
178 case YAZ_chain_step_type_stemming:
179 yaz_stemmer_clone(step->u.stemmer);
181 case ICU_chain_step_type_none:
185 sp = &(*sp)->previous;
191 struct icu_chain *icu_chain_create(const char *locale, int sort,
194 struct icu_chain *chain
195 = (struct icu_chain *) xmalloc(sizeof(*chain));
197 *status = U_ZERO_ERROR;
200 chain->locale = xstrdup(locale);
204 chain->coll = ucol_open((const char *) chain->locale, status);
206 if (U_FAILURE(*status))
214 void icu_chain_destroy(struct icu_chain *chain)
219 ucol_close(chain->coll);
222 icu_iter_destroy(chain->iter);
223 icu_chain_step_destroy(chain->csteps);
224 xfree(chain->locale);
229 static struct icu_chain_step *icu_chain_insert_step(
230 struct icu_chain *chain, enum icu_chain_step_type type,
231 const uint8_t *rule, UErrorCode *status);
233 struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
239 struct icu_chain *chain = 0;
242 *status = U_ZERO_ERROR;
244 if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
248 xmlChar *xml_locale = xmlGetProp((xmlNode *) xml_node,
249 (xmlChar *) "locale");
253 chain = icu_chain_create((const char *) xml_locale, sort, status);
261 nmem = nmem_create();
262 for (node = xml_node->children; node; node = node->next)
265 struct icu_chain_step *step = 0;
266 struct _xmlAttr *attr;
269 if (node->type != XML_ELEMENT_NODE)
272 for (attr = node->properties; attr; attr = attr->next)
274 if (!strcmp((const char *) attr->name, "rule"))
276 rule = nmem_text_node_cdata(attr->children, nmem);
280 yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
281 "element '%s'", attr->name, node->name);
286 if (!rule && node->children)
287 rule = nmem_text_node_cdata(node->children, nmem);
289 if (!strcmp((const char *) node->name, "casemap"))
290 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
291 (const uint8_t *) rule, status);
292 else if (!strcmp((const char *) node->name, "transform"))
293 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
294 (const uint8_t *) rule, status);
295 else if (!strcmp((const char *) node->name, "transliterate"))
296 step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
297 (const uint8_t *) rule, status);
298 else if (!strcmp((const char *) node->name, "tokenize"))
299 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
300 (const uint8_t *) rule, status);
301 else if (!strcmp((const char *) node->name, "display"))
302 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
303 (const uint8_t *) "", status);
304 else if (!strcmp((const char *) node->name, "stemming"))
305 step = icu_chain_insert_step(chain, YAZ_chain_step_type_stemming,
306 (const uint8_t *) rule, status);
307 else if (!strcmp((const char *) node->name, "normalize"))
309 yaz_log(YLOG_WARN, "Element %s is deprecated. "
310 "Use transform instead", node->name);
311 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
312 (const uint8_t *) rule, status);
314 else if (!strcmp((const char *) node->name, "index")
315 || !strcmp((const char *) node->name, "sortkey"))
317 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
318 "Remove it from the configuration", node->name);
322 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
326 if (step && U_FAILURE(*status))
335 icu_chain_destroy(chain);
342 struct icu_chain *chain;
343 struct icu_buf_utf16 *last;
345 struct icu_buf_utf8 *display;
346 struct icu_buf_utf8 *sort8;
347 struct icu_buf_utf8 *result;
348 struct icu_buf_utf16 *input;
350 struct icu_chain_step *steps;
353 void icu_utf16_print(struct icu_buf_utf16 *src16)
355 UErrorCode status = U_ZERO_ERROR;
357 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
358 icu_utf16_to_utf8(dst8, src16, &status);
360 assert(status != 1234);
361 if (U_FAILURE(status))
367 p = icu_buf_utf8_to_cstr(dst8);
370 icu_buf_utf8_destroy(dst8);
373 struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
374 struct icu_chain_step *step,
375 struct icu_buf_utf16 *src)
381 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
385 case ICU_chain_step_type_casemap:
388 struct icu_buf_utf16 *src = dst;
390 dst = icu_buf_utf16_create(0);
391 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
392 iter->chain->locale);
393 icu_buf_utf16_destroy(src);
396 case ICU_chain_step_type_tokenize:
399 struct icu_buf_utf16 *src = dst;
401 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
402 icu_buf_utf16_destroy(src);
404 dst = icu_buf_utf16_create(0);
405 iter->status = U_ZERO_ERROR;
406 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
408 icu_buf_utf16_destroy(dst);
412 case ICU_chain_step_type_transform:
413 case ICU_chain_step_type_transliterate:
416 struct icu_buf_utf16 *src = dst;
417 dst = icu_buf_utf16_create(0);
418 icu_transform_trans(step->u.transform, dst, src, &iter->status);
419 icu_buf_utf16_destroy(src);
422 case ICU_chain_step_type_display:
424 icu_utf16_to_utf8(iter->display, dst, &iter->status);
426 case YAZ_chain_step_type_stemming:
429 struct icu_buf_utf16 *src = dst;
430 dst = icu_buf_utf16_create(0);
431 yaz_stemmer_stem(step->u.stemmer, dst, src, &iter->status);
432 icu_buf_utf16_destroy(src);
442 yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
444 yaz_icu_iter_t iter = xmalloc(sizeof(*iter));
446 iter->status = U_ZERO_ERROR;
447 iter->display = icu_buf_utf8_create(0);
448 iter->sort8 = icu_buf_utf8_create(0);
449 iter->result = icu_buf_utf8_create(0);
450 iter->last = 0; /* no last returned string (yet) */
451 iter->steps = icu_chain_step_clone(chain->csteps);
457 void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
460 icu_buf_utf16_destroy(iter->input);
461 iter->input = icu_buf_utf16_create(0);
462 iter->token_count = 0;
463 /* fill and assign input string.. It will be 0 after
465 icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
468 void icu_iter_destroy(yaz_icu_iter_t iter)
472 icu_buf_utf8_destroy(iter->display);
473 icu_buf_utf8_destroy(iter->sort8);
474 icu_buf_utf8_destroy(iter->result);
476 icu_buf_utf16_destroy(iter->input);
477 icu_chain_step_destroy(iter->steps);
482 int icu_iter_next(yaz_icu_iter_t iter)
484 if (!iter->input && iter->last == 0)
488 /* on first call, iter->input is the input string. Thereafter: 0. */
489 iter->last = icu_iter_invoke(iter, iter->steps ?
490 iter->steps : iter->chain->csteps,
499 if (iter->chain->sort)
501 icu_sortkey8_from_utf16(iter->chain->coll,
502 iter->sort8, iter->last,
505 icu_utf16_to_utf8(iter->result, iter->last, &iter->status);
506 icu_buf_utf16_destroy(iter->last);
512 const char *icu_iter_get_norm(yaz_icu_iter_t iter)
514 return icu_buf_utf8_to_cstr(iter->result);
517 const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
519 return icu_buf_utf8_to_cstr(iter->sort8);
522 const char *icu_iter_get_display(yaz_icu_iter_t iter)
524 return icu_buf_utf8_to_cstr(iter->display);
527 int icu_iter_get_token_number(yaz_icu_iter_t iter)
529 return iter->token_count;
532 int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr,
536 icu_iter_destroy(chain->iter);
537 chain->iter = icu_iter_create(chain);
538 icu_iter_first(chain->iter, src8cstr);
542 int icu_chain_next_token(struct icu_chain *chain, UErrorCode *status)
544 *status = U_ZERO_ERROR;
545 return icu_iter_next(chain->iter);
548 int icu_chain_token_number(struct icu_chain *chain)
550 if (chain && chain->iter)
551 return chain->iter->token_count;
555 const char *icu_chain_token_display(struct icu_chain *chain)
558 return icu_iter_get_display(chain->iter);
562 const char *icu_chain_token_norm(struct icu_chain *chain)
565 return icu_iter_get_norm(chain->iter);
569 const char *icu_chain_token_sortkey(struct icu_chain *chain)
572 return icu_iter_get_sortkey(chain->iter);
576 #endif /* YAZ_HAVE_ICU */
581 * c-file-style: "Stroustrup"
582 * indent-tabs-mode: nil
584 * vim: shiftwidth=4 tabstop=8 expandtab