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)
177 if (strcmp((const char *) xml_node->name, "icu_chain"))
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;
187 yaz_log(YLOG_FATAL, "Missing icu_chain element");
190 pct = pp2_charset_create_xml(xml_node);
195 id = xmlGetProp(xml_node, (xmlChar*) "id");
198 yaz_log(YLOG_WARN, "Missing id for icu_chain");
199 pp2_charset_destroy(pct);
202 default_id = (const char *) id;
204 r = pp2_charset_fact_add(pft, pct, default_id);
210 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
215 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
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))
223 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
224 "<%s>\n ... \n</%s>",
225 xml_node->name, xml_node->name);
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);
234 "But no ICU support is compiled into the YAZ library.");
236 #endif // YAZ_HAVE_ICU
239 pp2_charset_t pp2_charset_create(void)
241 pp2_charset_t pct = xmalloc(sizeof(*pct));
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;
249 #endif // YAZ_HAVE_ICU
253 pp2_charset_t pp2_charset_create_a_to_z(void)
255 pp2_charset_t pct = pp2_charset_create();
256 pct->token_next_handler = pp2_charset_token_a_to_z;
261 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
263 pp2_charset_t pct = pp2_charset_create();
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;
275 #endif // YAZ_HAVE_ICU
277 void pp2_charset_destroy(pp2_charset_t pct)
280 icu_chain_destroy(pct->icu_chn);
285 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
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);
295 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
297 pp2_charset_token_t prt = xmalloc(sizeof(*prt));
301 prt->norm_str = wrbuf_alloc();
302 prt->sort_str = wrbuf_alloc();
310 prt->iter = icu_iter_create(pct->icu_chn);
317 void pp2_charset_token_first(pp2_charset_token_t prt,
318 const char *buf, int skip_article)
324 char *pout = firstword;
325 char articles[] = "the den der die des an a "; // must end in space
327 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
328 *pout++ = tolower(*(unsigned char *)p);
331 if (strstr(articles, firstword))
335 wrbuf_rewind(prt->norm_str);
336 wrbuf_rewind(prt->sort_str);
344 icu_iter_first(prt->iter, buf);
346 #endif // YAZ_HAVE_ICU
349 void pp2_charset_token_destroy(pp2_charset_token_t prt)
354 icu_iter_destroy(prt->iter);
357 wrbuf_destroy(prt->norm_str);
359 wrbuf_destroy(prt->sort_str);
363 const char *pp2_charset_token_next(pp2_charset_token_t prt)
366 return (prt->pct->token_next_handler)(prt);
369 const char *pp2_get_sort(pp2_charset_token_t prt)
371 return prt->pct->get_sort_handler(prt);
374 const char *pp2_get_display(pp2_charset_token_t prt)
376 return prt->pct->get_display_handler(prt);
379 void pp2_get_org(pp2_charset_token_t prt, size_t *start, size_t *len)
381 prt->pct->get_org_handler(prt, start, len);
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)
389 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
391 const char *cp = prt->cp;
394 prt->start = cp - prt->cp0;
395 /* skip white space */
396 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
404 /* now read the term itself */
407 wrbuf_rewind(prt->norm_str);
408 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
410 wrbuf_putc(prt->norm_str, c);
413 prt->len = (cp - prt->cp0) - prt->start;
415 return wrbuf_cstr(prt->norm_str);
418 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
420 if (prt->last_cp == 0)
424 char *tmp = xstrdup(prt->last_cp);
426 result = normalize7bit_mergekey(tmp);
428 wrbuf_rewind(prt->sort_str);
429 wrbuf_puts(prt->sort_str, result);
431 return wrbuf_cstr(prt->sort_str);
435 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
437 if (prt->last_cp == 0)
441 return wrbuf_cstr(prt->norm_str);
445 static void pp2_get_org_ascii(pp2_charset_token_t prt,
446 size_t *start, size_t *len)
452 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
454 const char *cp = prt->cp;
456 prt->last_cp = *cp ? cp : 0;
460 prt->len = cp - prt->cp0;
465 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
467 if (icu_iter_next(prt->iter))
469 return icu_iter_get_norm(prt->iter);
474 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
476 return icu_iter_get_sortkey(prt->iter);
479 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
481 return icu_iter_get_display(prt->iter);
484 static void pp2_get_org_icu(pp2_charset_token_t prt, size_t *start, size_t *len)
486 icu_iter_get_org_info(prt->iter, start, len);
489 #endif // YAZ_HAVE_ICU
495 * c-file-style: "Stroustrup"
496 * indent-tabs-mode: nil
498 * vim: shiftwidth=4 tabstop=8 expandtab