1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 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"
43 typedef struct pp2_charset_s *pp2_charset_t;
44 static pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node);
45 static pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn);
46 static pp2_charset_t pp2_charset_create_a_to_z(void);
47 static void pp2_charset_destroy(pp2_charset_t pct);
48 static pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct);
51 struct pp2_charset_s {
52 const char *(*token_next_handler)(pp2_charset_token_t prt);
53 const char *(*get_sort_handler)(pp2_charset_token_t prt);
54 const char *(*get_display_handler)(pp2_charset_token_t prt);
56 struct icu_chain * icu_chn;
61 static const char *pp2_charset_token_null(pp2_charset_token_t prt);
62 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt);
63 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt);
64 static const char *pp2_get_display_ascii(pp2_charset_token_t prt);
67 static const char *pp2_charset_token_icu(pp2_charset_token_t prt);
68 static const char *pp2_get_sort_icu(pp2_charset_token_t prt);
69 static const char *pp2_get_display_icu(pp2_charset_token_t prt);
72 /* tokenzier handle */
73 struct pp2_charset_token_s {
74 const char *cp; /* unnormalized buffer we're tokenizing */
75 const char *last_cp; /* pointer to last token we're dealing with */
76 pp2_charset_t pct; /* our main charset handle (type+config) */
77 WRBUF norm_str; /* normized string we return (temporarily) */
78 WRBUF sort_str; /* sort string we return (temporarily) */
84 struct pp2_charset_fact_s {
85 struct pp2_charset_entry *list;
89 struct pp2_charset_entry {
90 struct pp2_charset_entry *next;
96 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
97 pp2_charset_t pct, const char *default_id);
99 pp2_charset_fact_t pp2_charset_fact_create(void)
101 pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
105 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "relevance");
106 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "sort");
107 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "mergekey");
108 pp2_charset_fact_add(pft, pp2_charset_create(0), "facet");
112 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
116 assert(pft->ref_count >= 1);
118 if (pft->ref_count == 0)
120 struct pp2_charset_entry *pce = pft->list;
123 struct pp2_charset_entry *next = pce->next;
124 pp2_charset_destroy(pce->pct);
134 int pp2_charset_fact_add(pp2_charset_fact_t pft,
135 pp2_charset_t pct, const char *default_id)
137 struct pp2_charset_entry *pce;
139 for (pce = pft->list; pce; pce = pce->next)
140 if (!strcmp(default_id, pce->name))
145 pce = xmalloc(sizeof(*pce));
146 pce->name = xstrdup(default_id);
147 pce->next = pft->list;
152 pp2_charset_destroy(pce->pct);
158 int pp2_charset_fact_define(pp2_charset_fact_t pft,
159 xmlNode *xml_node, const char *default_id)
166 pct = pp2_charset_create_xml(xml_node);
171 id = xmlGetProp(xml_node, (xmlChar*) "id");
174 yaz_log(YLOG_WARN, "Missing id for icu_chain");
175 pp2_charset_destroy(pct);
178 default_id = (const char *) id;
180 r = pp2_charset_fact_add(pft, pct, default_id);
186 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
191 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
194 UErrorCode status = U_ZERO_ERROR;
195 struct icu_chain *chain = 0;
196 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
197 xml_node = xml_node->next;
198 chain = icu_chain_xml_config(xml_node, 1, &status);
199 if (!chain || U_FAILURE(status)){
200 //xmlDocPtr icu_doc = 0;
201 //xmlChar *xmlstr = 0;
203 //xmlDocDumpMemory(icu_doc, size);
205 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
206 "<%s>\n ... \n</%s>",
207 xml_node->name, xml_node->name);
210 return pp2_charset_create(chain);
211 #else // YAZ_HAVE_ICU
212 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
213 "<%s>\n ... \n</%s>",
214 xml_node->name, xml_node->name);
216 "But no ICU support is compiled into the YAZ library.");
218 #endif // YAZ_HAVE_ICU
221 pp2_charset_t pp2_charset_create_a_to_z(void)
223 pp2_charset_t pct = pp2_charset_create(0);
224 pct->token_next_handler = pp2_charset_token_a_to_z;
228 pp2_charset_t pp2_charset_create(struct icu_chain *icu_chn)
230 pp2_charset_t pct = xmalloc(sizeof(*pct));
232 pct->token_next_handler = pp2_charset_token_null;
233 pct->get_sort_handler = pp2_get_sort_ascii;
234 pct->get_display_handler = pp2_get_display_ascii;
239 pct->icu_chn = icu_chn;
240 pct->icu_sts = U_ZERO_ERROR;
241 pct->token_next_handler = pp2_charset_token_icu;
242 pct->get_sort_handler = pp2_get_sort_icu;
243 pct->get_display_handler = pp2_get_display_icu;
245 #endif // YAZ_HAVE_ICU
249 void pp2_charset_destroy(pp2_charset_t pct)
252 icu_chain_destroy(pct->icu_chn);
257 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
260 struct pp2_charset_entry *pce;
261 for (pce = pft->list; pce; pce = pce->next)
262 if (!strcmp(id, pce->name))
263 return pp2_charset_tokenize(pce->pct);
267 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
269 pp2_charset_token_t prt = xmalloc(sizeof(*prt));
273 prt->norm_str = wrbuf_alloc();
274 prt->sort_str = wrbuf_alloc();
282 prt->iter = icu_iter_create(pct->icu_chn);
287 void pp2_charset_token_first(pp2_charset_token_t prt,
288 const char *buf, int skip_article)
294 char *pout = firstword;
295 char articles[] = "the den der die des an a "; // must end in space
297 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
298 *pout++ = tolower(*(unsigned char *)p);
301 if (strstr(articles, firstword))
305 wrbuf_rewind(prt->norm_str);
306 wrbuf_rewind(prt->sort_str);
313 icu_iter_first(prt->iter, buf);
315 #endif // YAZ_HAVE_ICU
318 void pp2_charset_token_destroy(pp2_charset_token_t prt)
323 icu_iter_destroy(prt->iter);
326 wrbuf_destroy(prt->norm_str);
328 wrbuf_destroy(prt->sort_str);
332 const char *pp2_charset_token_next(pp2_charset_token_t prt)
335 return (prt->pct->token_next_handler)(prt);
338 const char *pp2_get_sort(pp2_charset_token_t prt)
340 return prt->pct->get_sort_handler(prt);
343 const char *pp2_get_display(pp2_charset_token_t prt)
345 return prt->pct->get_display_handler(prt);
348 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
349 /* original tokenizer with our tokenize interface, but we
350 add +1 to ensure no '\0' are in our string (except for EOF)
352 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
354 const char *cp = prt->cp;
357 /* skip white space */
358 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
366 /* now read the term itself */
369 wrbuf_rewind(prt->norm_str);
370 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
372 wrbuf_putc(prt->norm_str, c);
376 return wrbuf_cstr(prt->norm_str);
379 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
381 if (prt->last_cp == 0)
385 char *tmp = xstrdup(prt->last_cp);
387 result = normalize7bit_mergekey(tmp);
389 wrbuf_rewind(prt->sort_str);
390 wrbuf_puts(prt->sort_str, result);
392 return wrbuf_cstr(prt->sort_str);
396 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
398 if (prt->last_cp == 0)
402 return wrbuf_cstr(prt->norm_str);
406 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
408 const char *cp = prt->cp;
410 prt->last_cp = *cp ? cp : 0;
418 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
420 if (icu_iter_next(prt->iter))
422 return icu_iter_get_norm(prt->iter);
427 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
429 return icu_iter_get_sortkey(prt->iter);
432 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
434 return icu_iter_get_display(prt->iter);
437 #endif // YAZ_HAVE_ICU
443 * c-file-style: "Stroustrup"
444 * indent-tabs-mode: nil
446 * vim: shiftwidth=4 tabstop=8 expandtab