1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
207 xml_node = xml_node->next;
208 chain = icu_chain_xml_config(xml_node, 1, &status);
209 if (!chain || U_FAILURE(status)){
210 //xmlDocPtr icu_doc = 0;
211 //xmlChar *xmlstr = 0;
213 //xmlDocDumpMemory(icu_doc, size);
215 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
216 "<%s>\n ... \n</%s>",
217 xml_node->name, xml_node->name);
220 return pp2_charset_create_icu(chain);
221 #else // YAZ_HAVE_ICU
222 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
223 "<%s>\n ... \n</%s>",
224 xml_node->name, xml_node->name);
226 "But no ICU support is compiled into the YAZ library.");
228 #endif // YAZ_HAVE_ICU
231 pp2_charset_t pp2_charset_create(void)
233 pp2_charset_t pct = xmalloc(sizeof(*pct));
235 pct->token_next_handler = pp2_charset_token_null;
236 pct->get_sort_handler = pp2_get_sort_ascii;
237 pct->get_display_handler = pp2_get_display_ascii;
238 pct->get_org_handler = pp2_get_org_ascii;
241 #endif // YAZ_HAVE_ICU
245 pp2_charset_t pp2_charset_create_a_to_z(void)
247 pp2_charset_t pct = pp2_charset_create();
248 pct->token_next_handler = pp2_charset_token_a_to_z;
253 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
255 pp2_charset_t pct = pp2_charset_create();
258 pct->icu_chn = icu_chn;
259 pct->icu_sts = U_ZERO_ERROR;
260 pct->token_next_handler = pp2_charset_token_icu;
261 pct->get_sort_handler = pp2_get_sort_icu;
262 pct->get_display_handler = pp2_get_display_icu;
263 pct->get_org_handler = pp2_get_org_icu;
267 #endif // YAZ_HAVE_ICU
269 void pp2_charset_destroy(pp2_charset_t pct)
272 icu_chain_destroy(pct->icu_chn);
277 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
280 struct pp2_charset_entry *pce;
281 for (pce = pft->list; pce; pce = pce->next)
282 if (!strcmp(id, pce->name))
283 return pp2_charset_tokenize(pce->pct);
287 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
289 pp2_charset_token_t prt = xmalloc(sizeof(*prt));
293 prt->norm_str = wrbuf_alloc();
294 prt->sort_str = wrbuf_alloc();
302 prt->iter = icu_iter_create(pct->icu_chn);
309 void pp2_charset_token_first(pp2_charset_token_t prt,
310 const char *buf, int skip_article)
316 char *pout = firstword;
317 char articles[] = "the den der die des an a "; // must end in space
319 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
320 *pout++ = tolower(*(unsigned char *)p);
323 if (strstr(articles, firstword))
327 wrbuf_rewind(prt->norm_str);
328 wrbuf_rewind(prt->sort_str);
336 icu_iter_first(prt->iter, buf);
338 #endif // YAZ_HAVE_ICU
341 void pp2_charset_token_destroy(pp2_charset_token_t prt)
346 icu_iter_destroy(prt->iter);
349 wrbuf_destroy(prt->norm_str);
351 wrbuf_destroy(prt->sort_str);
355 const char *pp2_charset_token_next(pp2_charset_token_t prt)
358 return (prt->pct->token_next_handler)(prt);
361 const char *pp2_get_sort(pp2_charset_token_t prt)
363 return prt->pct->get_sort_handler(prt);
366 const char *pp2_get_display(pp2_charset_token_t prt)
368 return prt->pct->get_display_handler(prt);
371 void pp2_get_org(pp2_charset_token_t prt, size_t *start, size_t *len)
373 prt->pct->get_org_handler(prt, start, len);
377 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
378 /* original tokenizer with our tokenize interface, but we
379 add +1 to ensure no '\0' are in our string (except for EOF)
381 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
383 const char *cp = prt->cp;
386 prt->start = cp - prt->cp0;
387 /* skip white space */
388 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
396 /* now read the term itself */
399 wrbuf_rewind(prt->norm_str);
400 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
402 wrbuf_putc(prt->norm_str, c);
405 prt->len = (cp - prt->cp0) - prt->start;
407 return wrbuf_cstr(prt->norm_str);
410 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
412 if (prt->last_cp == 0)
416 char *tmp = xstrdup(prt->last_cp);
418 result = normalize7bit_mergekey(tmp);
420 wrbuf_rewind(prt->sort_str);
421 wrbuf_puts(prt->sort_str, result);
423 return wrbuf_cstr(prt->sort_str);
427 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
429 if (prt->last_cp == 0)
433 return wrbuf_cstr(prt->norm_str);
437 static void pp2_get_org_ascii(pp2_charset_token_t prt,
438 size_t *start, size_t *len)
444 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
446 const char *cp = prt->cp;
448 prt->last_cp = *cp ? cp : 0;
452 prt->len = cp - prt->cp0;
457 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
459 if (icu_iter_next(prt->iter))
461 return icu_iter_get_norm(prt->iter);
466 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
468 return icu_iter_get_sortkey(prt->iter);
471 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
473 return icu_iter_get_display(prt->iter);
476 static void pp2_get_org_icu(pp2_charset_token_t prt, size_t *start, size_t *len)
478 icu_iter_get_org_info(prt->iter, start, len);
481 #endif // YAZ_HAVE_ICU
487 * c-file-style: "Stroustrup"
488 * indent-tabs-mode: nil
490 * vim: shiftwidth=4 tabstop=8 expandtab