Allow deprecated ICU definitions again
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file charsets.c
21     \brief Pazpar2 Character set facilities
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <yaz/xmalloc.h>
29 #include <yaz/wrbuf.h>
30 #include <yaz/log.h>
31 #include <yaz/yaz-version.h>
32 #include <ctype.h>
33 #include <assert.h>
34 #include <string.h>
35
36 #include "charsets.h"
37 #include "normalize7bit.h"
38
39 typedef struct pp2_charset_s *pp2_charset_t;
40 static pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node);
41 static pp2_charset_t pp2_charset_create(void);
42 static pp2_charset_t pp2_charset_create_a_to_z(void);
43 static void pp2_charset_destroy(pp2_charset_t pct);
44 static pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct);
45
46 #if YAZ_HAVE_ICU
47 #include <yaz/icu.h>
48 static pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn);
49 #endif
50
51 /* charset handle */
52 struct pp2_charset_s {
53     const char *(*token_next_handler)(pp2_charset_token_t prt);
54     const char *(*get_sort_handler)(pp2_charset_token_t prt);
55     const char *(*get_display_handler)(pp2_charset_token_t prt);
56     void (*get_org_handler)(pp2_charset_token_t ptr,
57                             size_t *start, size_t *len);
58 #if YAZ_HAVE_ICU
59     struct icu_chain * icu_chn;
60     UErrorCode icu_sts;
61 #endif
62 };
63
64 static const char *pp2_charset_token_null(pp2_charset_token_t prt);
65 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt);
66 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt);
67 static const char *pp2_get_display_ascii(pp2_charset_token_t prt);
68 static void pp2_get_org_ascii(pp2_charset_token_t prt,
69                               size_t *start, size_t *len);
70
71 #if YAZ_HAVE_ICU
72 static const char *pp2_charset_token_icu(pp2_charset_token_t prt);
73 static const char *pp2_get_sort_icu(pp2_charset_token_t prt);
74 static const char *pp2_get_display_icu(pp2_charset_token_t prt);
75 static void pp2_get_org_icu(pp2_charset_token_t prt,
76                             size_t *start, size_t *len);
77 #endif
78
79 /* tokenzier handle */
80 struct pp2_charset_token_s {
81     const char *cp;     /* unnormalized buffer we're tokenizing */
82     const char *last_cp;  /* pointer to last token we're dealing with */
83     pp2_charset_t pct;  /* our main charset handle (type+config) */
84     WRBUF norm_str;     /* normized string we return (temporarily) */
85     WRBUF sort_str;     /* sort string we return (temporarily) */
86 #if YAZ_HAVE_ICU
87     yaz_icu_iter_t iter;
88 #endif
89     const char *cp0;
90     size_t start;
91     size_t len;
92 };
93
94 struct pp2_charset_fact_s {
95     struct pp2_charset_entry *list;
96     int ref_count;
97 };
98
99 struct pp2_charset_entry {
100     struct pp2_charset_entry *next;
101     pp2_charset_t pct;
102     char *name;
103 };
104
105
106 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
107                                 pp2_charset_t pct, const char *default_id);
108
109 pp2_charset_fact_t pp2_charset_fact_create(void)
110 {
111     pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
112     pft->list = 0;
113     pft->ref_count = 1;
114
115     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "relevance");
116     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "sort");
117     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "mergekey");
118     pp2_charset_fact_add(pft, pp2_charset_create(), "facet");
119     return pft;
120 }
121
122 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
123 {
124     if (pft)
125     {
126         assert(pft->ref_count >= 1);
127         --(pft->ref_count);
128         if (pft->ref_count == 0)
129         {
130             struct pp2_charset_entry *pce = pft->list;
131             while (pce)
132             {
133                 struct pp2_charset_entry *next = pce->next;
134                 pp2_charset_destroy(pce->pct);
135                 xfree(pce->name);
136                 xfree(pce);
137                 pce = next;
138             }
139             xfree(pft);
140         }
141     }
142 }
143
144 int pp2_charset_fact_add(pp2_charset_fact_t pft,
145                          pp2_charset_t pct, const char *default_id)
146 {
147     struct pp2_charset_entry *pce;
148
149     for (pce = pft->list; pce; pce = pce->next)
150         if (!strcmp(default_id, pce->name))
151             break;
152
153     if (!pce)
154     {
155         pce = xmalloc(sizeof(*pce));
156         pce->name = xstrdup(default_id);
157         pce->next = pft->list;
158         pft->list = pce;
159     }
160     else
161     {
162         pp2_charset_destroy(pce->pct);
163     }
164     pce->pct = pct;
165     return 0;
166 }
167
168 int pp2_charset_fact_define(pp2_charset_fact_t pft,
169                             xmlNode *xml_node, const char *default_id)
170 {
171     int r;
172     pp2_charset_t pct;
173     xmlChar *id = 0;
174
175     assert(xml_node);
176
177     if (strcmp((const char *) xml_node->name, "icu_chain"))
178     {
179         yaz_log(YLOG_WARN, "Wrapper element <%s> deprecated", xml_node->name);
180         yaz_log(YLOG_LOG, "Use <icu_chain id=\"%s\">.. only", xml_node->name);
181         xml_node = xml_node->children;
182         while (xml_node && xml_node->type != XML_ELEMENT_NODE)
183             xml_node = xml_node->next;
184     }
185     if (!xml_node)
186     {
187         yaz_log(YLOG_FATAL, "Missing icu_chain element");
188         return -1;
189     }
190     pct = pp2_charset_create_xml(xml_node);
191     if (!pct)
192         return -1;
193     if (!default_id)
194     {
195         id = xmlGetProp(xml_node, (xmlChar*) "id");
196         if (!id)
197         {
198             yaz_log(YLOG_WARN, "Missing id for icu_chain");
199             pp2_charset_destroy(pct);
200             return -1;
201         }
202         default_id = (const char *) id;
203     }
204     r = pp2_charset_fact_add(pft, pct, default_id);
205     if (id)
206         xmlFree(id);
207     return r;
208 }
209
210 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
211 {
212     (pft->ref_count)++;
213 }
214
215 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
216 {
217 #if YAZ_HAVE_ICU
218     UErrorCode status = U_ZERO_ERROR;
219     struct icu_chain *chain = 0;
220     chain = icu_chain_xml_config(xml_node, 1, &status);
221     if (!chain || U_FAILURE(status))
222     {
223         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
224                 "<%s>\n ... \n</%s>",
225                 xml_node->name, xml_node->name);
226         return 0;
227     }
228     return pp2_charset_create_icu(chain);
229 #else // YAZ_HAVE_ICU
230     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
231             "<%s>\n ... \n</%s>",
232             xml_node->name, xml_node->name);
233     yaz_log(YLOG_FATAL,
234             "But no ICU support is compiled into the YAZ library.");
235     return 0;
236 #endif // YAZ_HAVE_ICU
237 }
238
239 pp2_charset_t pp2_charset_create(void)
240 {
241     pp2_charset_t pct = xmalloc(sizeof(*pct));
242
243     pct->token_next_handler = pp2_charset_token_null;
244     pct->get_sort_handler  = pp2_get_sort_ascii;
245     pct->get_display_handler  = pp2_get_display_ascii;
246     pct->get_org_handler = pp2_get_org_ascii;
247 #if YAZ_HAVE_ICU
248     pct->icu_chn = 0;
249 #endif // YAZ_HAVE_ICU
250     return pct;
251 }
252
253 pp2_charset_t pp2_charset_create_a_to_z(void)
254 {
255     pp2_charset_t pct = pp2_charset_create();
256     pct->token_next_handler = pp2_charset_token_a_to_z;
257     return pct;
258 }
259
260 #if YAZ_HAVE_ICU
261 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
262 {
263     pp2_charset_t pct = pp2_charset_create();
264     if (icu_chn)
265     {
266         pct->icu_chn = icu_chn;
267         pct->icu_sts = U_ZERO_ERROR;
268         pct->token_next_handler = pp2_charset_token_icu;
269         pct->get_sort_handler = pp2_get_sort_icu;
270         pct->get_display_handler = pp2_get_display_icu;
271         pct->get_org_handler = pp2_get_org_icu;
272     }
273     return pct;
274 }
275 #endif // YAZ_HAVE_ICU
276
277 void pp2_charset_destroy(pp2_charset_t pct)
278 {
279 #if YAZ_HAVE_ICU
280     icu_chain_destroy(pct->icu_chn);
281 #endif
282     xfree(pct);
283 }
284
285 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
286                                                const char *id)
287 {
288     struct pp2_charset_entry *pce;
289     for (pce = pft->list; pce; pce = pce->next)
290         if (!strcmp(id, pce->name))
291             return pp2_charset_tokenize(pce->pct);
292     return 0;
293 }
294
295 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
296 {
297     pp2_charset_token_t prt = xmalloc(sizeof(*prt));
298
299     assert(pct);
300
301     prt->norm_str = wrbuf_alloc();
302     prt->sort_str = wrbuf_alloc();
303     prt->cp = 0;
304     prt->last_cp = 0;
305     prt->pct = pct;
306
307 #if YAZ_HAVE_ICU
308     prt->iter = 0;
309     if (pct->icu_chn)
310         prt->iter = icu_iter_create(pct->icu_chn);
311 #endif
312     prt->start = 0;
313     prt->len = 0;
314     return prt;
315 }
316
317 void pp2_charset_token_first(pp2_charset_token_t prt,
318                              const char *buf, int skip_article)
319 {
320     if (skip_article)
321     {
322         const char *p = buf;
323         char firstword[64];
324         char *pout = firstword;
325         char articles[] = "the den der die des an a "; // must end in space
326
327         for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
328             *pout++ = tolower(*(unsigned char *)p);
329         *pout++ = ' ';
330         *pout++ = '\0';
331         if (strstr(articles, firstword))
332             buf = p;
333     }
334
335     wrbuf_rewind(prt->norm_str);
336     wrbuf_rewind(prt->sort_str);
337     prt->cp0 = buf;
338     prt->cp = buf;
339     prt->last_cp = 0;
340
341 #if YAZ_HAVE_ICU
342     if (prt->iter)
343     {
344         icu_iter_first(prt->iter, buf);
345     }
346 #endif // YAZ_HAVE_ICU
347 }
348
349 void pp2_charset_token_destroy(pp2_charset_token_t prt)
350 {
351     assert(prt);
352 #if YAZ_HAVE_ICU
353     if (prt->iter)
354         icu_iter_destroy(prt->iter);
355 #endif
356     if(prt->norm_str)
357         wrbuf_destroy(prt->norm_str);
358     if(prt->sort_str)
359         wrbuf_destroy(prt->sort_str);
360     xfree(prt);
361 }
362
363 const char *pp2_charset_token_next(pp2_charset_token_t prt)
364 {
365     assert(prt);
366     return (prt->pct->token_next_handler)(prt);
367 }
368
369 const char *pp2_get_sort(pp2_charset_token_t prt)
370 {
371     return prt->pct->get_sort_handler(prt);
372 }
373
374 const char *pp2_get_display(pp2_charset_token_t prt)
375 {
376     return prt->pct->get_display_handler(prt);
377 }
378
379 void pp2_get_org(pp2_charset_token_t prt, size_t *start, size_t *len)
380 {
381     prt->pct->get_org_handler(prt, start, len);
382 }
383
384
385 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
386 /* original tokenizer with our tokenize interface, but we
387    add +1 to ensure no '\0' are in our string (except for EOF)
388 */
389 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
390 {
391     const char *cp = prt->cp;
392     int c;
393
394     prt->start = cp - prt->cp0;
395     /* skip white space */
396     while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
397         cp++;
398     if (*cp == '\0')
399     {
400         prt->cp = cp;
401         prt->last_cp = 0;
402         return 0;
403     }
404     /* now read the term itself */
405
406     prt->last_cp = cp;
407     wrbuf_rewind(prt->norm_str);
408     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
409     {
410         wrbuf_putc(prt->norm_str, c);
411         cp++;
412     }
413     prt->len = (cp - prt->cp0) - prt->start;
414     prt->cp = cp;
415     return wrbuf_cstr(prt->norm_str);
416 }
417
418 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
419 {
420     if (prt->last_cp == 0)
421         return 0;
422     else
423     {
424         char *tmp = xstrdup(prt->last_cp);
425         char *result = 0;
426         result = normalize7bit_mergekey(tmp);
427
428         wrbuf_rewind(prt->sort_str);
429         wrbuf_puts(prt->sort_str, result);
430         xfree(tmp);
431         return wrbuf_cstr(prt->sort_str);
432     }
433 }
434
435 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
436 {
437     if (prt->last_cp == 0)
438         return 0;
439     else
440     {
441         return wrbuf_cstr(prt->norm_str);
442     }
443 }
444
445 static void pp2_get_org_ascii(pp2_charset_token_t prt,
446                               size_t *start, size_t *len)
447 {
448     *start = prt->start;
449     *len = prt->len;
450 }
451
452 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
453 {
454     const char *cp = prt->cp;
455
456     prt->last_cp = *cp ? cp : 0;
457     while (*cp)
458         cp++;
459     prt->cp = cp;
460     prt->len = cp - prt->cp0;
461     return prt->last_cp;
462 }
463
464 #if YAZ_HAVE_ICU
465 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
466 {
467     if (icu_iter_next(prt->iter))
468     {
469         return icu_iter_get_norm(prt->iter);
470     }
471     return 0;
472 }
473
474 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
475 {
476     return icu_iter_get_sortkey(prt->iter);
477 }
478
479 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
480 {
481     return icu_iter_get_display(prt->iter);
482 }
483
484 static void pp2_get_org_icu(pp2_charset_token_t prt, size_t *start, size_t *len)
485 {
486     icu_iter_get_org_info(prt->iter, start, len);
487 }
488
489 #endif // YAZ_HAVE_ICU
490
491
492 /*
493  * Local variables:
494  * c-basic-offset: 4
495  * c-file-style: "Stroustrup"
496  * indent-tabs-mode: nil
497  * End:
498  * vim: shiftwidth=4 tabstop=8 expandtab
499  */
500