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);
57 struct icu_chain * icu_chn;
62 static const char *pp2_charset_token_null(pp2_charset_token_t prt);
63 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt);
64 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt);
65 static const char *pp2_get_display_ascii(pp2_charset_token_t prt);
68 static const char *pp2_charset_token_icu(pp2_charset_token_t prt);
69 static const char *pp2_get_sort_icu(pp2_charset_token_t prt);
70 static const char *pp2_get_display_icu(pp2_charset_token_t prt);
73 /* tokenzier handle */
74 struct pp2_charset_token_s {
75 const char *cp; /* unnormalized buffer we're tokenizing */
76 const char *last_cp; /* pointer to last token we're dealing with */
77 pp2_charset_t pct; /* our main charset handle (type+config) */
78 WRBUF norm_str; /* normized string we return (temporarily) */
79 WRBUF sort_str; /* sort string we return (temporarily) */
85 struct pp2_charset_fact_s {
86 struct pp2_charset_entry *list;
90 struct pp2_charset_entry {
91 struct pp2_charset_entry *next;
97 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
98 pp2_charset_t pct, const char *default_id);
100 pp2_charset_fact_t pp2_charset_fact_create(void)
102 pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
106 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "relevance");
107 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "sort");
108 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "mergekey");
109 pp2_charset_fact_add(pft, pp2_charset_create(), "facet");
113 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
117 assert(pft->ref_count >= 1);
119 if (pft->ref_count == 0)
121 struct pp2_charset_entry *pce = pft->list;
124 struct pp2_charset_entry *next = pce->next;
125 pp2_charset_destroy(pce->pct);
135 int pp2_charset_fact_add(pp2_charset_fact_t pft,
136 pp2_charset_t pct, const char *default_id)
138 struct pp2_charset_entry *pce;
140 for (pce = pft->list; pce; pce = pce->next)
141 if (!strcmp(default_id, pce->name))
146 pce = xmalloc(sizeof(*pce));
147 pce->name = xstrdup(default_id);
148 pce->next = pft->list;
153 pp2_charset_destroy(pce->pct);
159 int pp2_charset_fact_define(pp2_charset_fact_t pft,
160 xmlNode *xml_node, const char *default_id)
167 pct = pp2_charset_create_xml(xml_node);
172 id = xmlGetProp(xml_node, (xmlChar*) "id");
175 yaz_log(YLOG_WARN, "Missing id for icu_chain");
176 pp2_charset_destroy(pct);
179 default_id = (const char *) id;
181 r = pp2_charset_fact_add(pft, pct, default_id);
187 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
192 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
195 UErrorCode status = U_ZERO_ERROR;
196 struct icu_chain *chain = 0;
197 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
198 xml_node = xml_node->next;
199 chain = icu_chain_xml_config(xml_node, 1, &status);
200 if (!chain || U_FAILURE(status)){
201 //xmlDocPtr icu_doc = 0;
202 //xmlChar *xmlstr = 0;
204 //xmlDocDumpMemory(icu_doc, size);
206 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
207 "<%s>\n ... \n</%s>",
208 xml_node->name, xml_node->name);
211 return pp2_charset_create_icu(chain);
212 #else // YAZ_HAVE_ICU
213 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
214 "<%s>\n ... \n</%s>",
215 xml_node->name, xml_node->name);
217 "But no ICU support is compiled into the YAZ library.");
219 #endif // YAZ_HAVE_ICU
222 pp2_charset_t pp2_charset_create(void)
224 pp2_charset_t pct = xmalloc(sizeof(*pct));
226 pct->token_next_handler = pp2_charset_token_null;
227 pct->get_sort_handler = pp2_get_sort_ascii;
228 pct->get_display_handler = pp2_get_display_ascii;
231 #endif // YAZ_HAVE_ICU
235 pp2_charset_t pp2_charset_create_a_to_z(void)
237 pp2_charset_t pct = pp2_charset_create();
238 pct->token_next_handler = pp2_charset_token_a_to_z;
243 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
245 pp2_charset_t pct = pp2_charset_create();
248 pct->icu_chn = icu_chn;
249 pct->icu_sts = U_ZERO_ERROR;
250 pct->token_next_handler = pp2_charset_token_icu;
251 pct->get_sort_handler = pp2_get_sort_icu;
252 pct->get_display_handler = pp2_get_display_icu;
256 #endif // YAZ_HAVE_ICU
258 void pp2_charset_destroy(pp2_charset_t pct)
261 icu_chain_destroy(pct->icu_chn);
266 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
269 struct pp2_charset_entry *pce;
270 for (pce = pft->list; pce; pce = pce->next)
271 if (!strcmp(id, pce->name))
272 return pp2_charset_tokenize(pce->pct);
276 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
278 pp2_charset_token_t prt = xmalloc(sizeof(*prt));
282 prt->norm_str = wrbuf_alloc();
283 prt->sort_str = wrbuf_alloc();
291 prt->iter = icu_iter_create(pct->icu_chn);
296 void pp2_charset_token_first(pp2_charset_token_t prt,
297 const char *buf, int skip_article)
303 char *pout = firstword;
304 char articles[] = "the den der die des an a "; // must end in space
306 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
307 *pout++ = tolower(*(unsigned char *)p);
310 if (strstr(articles, firstword))
314 wrbuf_rewind(prt->norm_str);
315 wrbuf_rewind(prt->sort_str);
322 icu_iter_first(prt->iter, buf);
324 #endif // YAZ_HAVE_ICU
327 void pp2_charset_token_destroy(pp2_charset_token_t prt)
332 icu_iter_destroy(prt->iter);
335 wrbuf_destroy(prt->norm_str);
337 wrbuf_destroy(prt->sort_str);
341 const char *pp2_charset_token_next(pp2_charset_token_t prt)
344 return (prt->pct->token_next_handler)(prt);
347 const char *pp2_get_sort(pp2_charset_token_t prt)
349 return prt->pct->get_sort_handler(prt);
352 const char *pp2_get_display(pp2_charset_token_t prt)
354 return prt->pct->get_display_handler(prt);
357 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
358 /* original tokenizer with our tokenize interface, but we
359 add +1 to ensure no '\0' are in our string (except for EOF)
361 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
363 const char *cp = prt->cp;
366 /* skip white space */
367 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
375 /* now read the term itself */
378 wrbuf_rewind(prt->norm_str);
379 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
381 wrbuf_putc(prt->norm_str, c);
385 return wrbuf_cstr(prt->norm_str);
388 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
390 if (prt->last_cp == 0)
394 char *tmp = xstrdup(prt->last_cp);
396 result = normalize7bit_mergekey(tmp);
398 wrbuf_rewind(prt->sort_str);
399 wrbuf_puts(prt->sort_str, result);
401 return wrbuf_cstr(prt->sort_str);
405 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
407 if (prt->last_cp == 0)
411 return wrbuf_cstr(prt->norm_str);
415 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
417 const char *cp = prt->cp;
419 prt->last_cp = *cp ? cp : 0;
427 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
429 if (icu_iter_next(prt->iter))
431 return icu_iter_get_norm(prt->iter);
436 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
438 return icu_iter_get_sortkey(prt->iter);
441 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
443 return icu_iter_get_display(prt->iter);
446 #endif // YAZ_HAVE_ICU
452 * c-file-style: "Stroustrup"
453 * indent-tabs-mode: nil
455 * vim: shiftwidth=4 tabstop=8 expandtab