1 /* This file is part of Pazpar2.
2 Copyright (C) Index Data
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
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
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
21 \brief Pazpar2 Character set facilities
28 #include <yaz/xmalloc.h>
29 #include <yaz/wrbuf.h>
31 #include <yaz/yaz-version.h>
37 #include "normalize7bit.h"
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);
48 static pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn);
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);
59 struct icu_chain * icu_chn;
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);
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);
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) */
94 struct pp2_charset_fact_s {
95 struct pp2_charset_entry *list;
99 struct pp2_charset_entry {
100 struct pp2_charset_entry *next;
106 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
107 pp2_charset_t pct, const char *default_id);
109 pp2_charset_fact_t pp2_charset_fact_create(void)
111 pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
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");
122 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
126 assert(pft->ref_count >= 1);
128 if (pft->ref_count == 0)
130 struct pp2_charset_entry *pce = pft->list;
133 struct pp2_charset_entry *next = pce->next;
134 pp2_charset_destroy(pce->pct);
144 int pp2_charset_fact_add(pp2_charset_fact_t pft,
145 pp2_charset_t pct, const char *default_id)
147 struct pp2_charset_entry *pce;
149 for (pce = pft->list; pce; pce = pce->next)
150 if (!strcmp(default_id, pce->name))
155 pce = xmalloc(sizeof(*pce));
156 pce->name = xstrdup(default_id);
157 pce->next = pft->list;
162 pp2_charset_destroy(pce->pct);
168 int pp2_charset_fact_define(pp2_charset_fact_t pft,
169 xmlNode *xml_node, const char *default_id)
176 pct = pp2_charset_create_xml(xml_node);
181 id = xmlGetProp(xml_node, (xmlChar*) "id");
184 yaz_log(YLOG_WARN, "Missing id for icu_chain");
185 pp2_charset_destroy(pct);
188 default_id = (const char *) id;
190 r = pp2_charset_fact_add(pft, pct, default_id);
196 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
201 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
204 UErrorCode status = U_ZERO_ERROR;
205 struct icu_chain *chain = 0;
206 chain = icu_chain_xml_config(xml_node, 1, &status);
207 if (!chain || U_FAILURE(status))
209 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
210 "<%s>\n ... \n</%s>",
211 xml_node->name, xml_node->name);
214 return pp2_charset_create_icu(chain);
215 #else // YAZ_HAVE_ICU
216 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
217 "<%s>\n ... \n</%s>",
218 xml_node->name, xml_node->name);
220 "But no ICU support is compiled into the YAZ library.");
222 #endif // YAZ_HAVE_ICU
225 pp2_charset_t pp2_charset_create(void)
227 pp2_charset_t pct = xmalloc(sizeof(*pct));
229 pct->token_next_handler = pp2_charset_token_null;
230 pct->get_sort_handler = pp2_get_sort_ascii;
231 pct->get_display_handler = pp2_get_display_ascii;
232 pct->get_org_handler = pp2_get_org_ascii;
235 #endif // YAZ_HAVE_ICU
239 pp2_charset_t pp2_charset_create_a_to_z(void)
241 pp2_charset_t pct = pp2_charset_create();
242 pct->token_next_handler = pp2_charset_token_a_to_z;
247 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
249 pp2_charset_t pct = pp2_charset_create();
252 pct->icu_chn = icu_chn;
253 pct->icu_sts = U_ZERO_ERROR;
254 pct->token_next_handler = pp2_charset_token_icu;
255 pct->get_sort_handler = pp2_get_sort_icu;
256 pct->get_display_handler = pp2_get_display_icu;
257 pct->get_org_handler = pp2_get_org_icu;
261 #endif // YAZ_HAVE_ICU
263 void pp2_charset_destroy(pp2_charset_t pct)
266 icu_chain_destroy(pct->icu_chn);
271 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
274 struct pp2_charset_entry *pce;
275 for (pce = pft->list; pce; pce = pce->next)
276 if (!strcmp(id, pce->name))
277 return pp2_charset_tokenize(pce->pct);
281 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
283 pp2_charset_token_t prt = xmalloc(sizeof(*prt));
287 prt->norm_str = wrbuf_alloc();
288 prt->sort_str = wrbuf_alloc();
296 prt->iter = icu_iter_create(pct->icu_chn);
303 void pp2_charset_token_first(pp2_charset_token_t prt,
304 const char *buf, int skip_article)
310 char *pout = firstword;
311 char articles[] = "the den der die des an a "; // must end in space
313 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
314 *pout++ = tolower(*(unsigned char *)p);
317 if (strstr(articles, firstword))
321 wrbuf_rewind(prt->norm_str);
322 wrbuf_rewind(prt->sort_str);
330 icu_iter_first(prt->iter, buf);
332 #endif // YAZ_HAVE_ICU
335 void pp2_charset_token_destroy(pp2_charset_token_t prt)
340 icu_iter_destroy(prt->iter);
343 wrbuf_destroy(prt->norm_str);
345 wrbuf_destroy(prt->sort_str);
349 const char *pp2_charset_token_next(pp2_charset_token_t prt)
352 return (prt->pct->token_next_handler)(prt);
355 const char *pp2_get_sort(pp2_charset_token_t prt)
357 return prt->pct->get_sort_handler(prt);
360 const char *pp2_get_display(pp2_charset_token_t prt)
362 return prt->pct->get_display_handler(prt);
365 void pp2_get_org(pp2_charset_token_t prt, size_t *start, size_t *len)
367 prt->pct->get_org_handler(prt, start, len);
371 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
372 /* original tokenizer with our tokenize interface, but we
373 add +1 to ensure no '\0' are in our string (except for EOF)
375 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
377 const char *cp = prt->cp;
380 prt->start = cp - prt->cp0;
381 /* skip white space */
382 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
390 /* now read the term itself */
393 wrbuf_rewind(prt->norm_str);
394 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
396 wrbuf_putc(prt->norm_str, c);
399 prt->len = (cp - prt->cp0) - prt->start;
401 return wrbuf_cstr(prt->norm_str);
404 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
406 if (prt->last_cp == 0)
410 char *tmp = xstrdup(prt->last_cp);
412 result = normalize7bit_mergekey(tmp);
414 wrbuf_rewind(prt->sort_str);
415 wrbuf_puts(prt->sort_str, result);
417 return wrbuf_cstr(prt->sort_str);
421 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
423 if (prt->last_cp == 0)
427 return wrbuf_cstr(prt->norm_str);
431 static void pp2_get_org_ascii(pp2_charset_token_t prt,
432 size_t *start, size_t *len)
438 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
440 const char *cp = prt->cp;
442 prt->last_cp = *cp ? cp : 0;
446 prt->len = cp - prt->cp0;
451 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
453 if (icu_iter_next(prt->iter))
455 return icu_iter_get_norm(prt->iter);
460 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
462 return icu_iter_get_sortkey(prt->iter);
465 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
467 return icu_iter_get_display(prt->iter);
470 static void pp2_get_org_icu(pp2_charset_token_t prt, size_t *start, size_t *len)
472 icu_iter_get_org_info(prt->iter, start, len);
475 #endif // YAZ_HAVE_ICU
481 * c-file-style: "Stroustrup"
482 * indent-tabs-mode: nil
484 * vim: shiftwidth=4 tabstop=8 expandtab