1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements RPN to CQL conversion
16 #include <yaz/rpn2cql.h>
17 #include <yaz/xmalloc.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/z-core.h>
20 #include <yaz/wrbuf.h>
21 #include <yaz/logrpn.h> /* For yaz_prox_unit_name() */
23 static void wrbuf_vputs(const char *buf, void *client_data)
25 wrbuf_write((WRBUF) client_data, buf, strlen(buf));
28 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
31 int server_choice = 1;
32 for (j = 0; j < attributes->num_attributes; j++)
34 Z_AttributeElement *ae = attributes->attributes[j];
35 if (*ae->attributeType == 1) /* use attribute */
37 if (ae->which == Z_AttributeValue_complex)
39 Z_ComplexAttribute *ca = ae->value.complex;
41 for (i = 0; i < ca->num_list; i++)
43 Z_StringOrNumeric *son = ca->list[i];
44 if (son->which == Z_StringOrNumeric_string)
48 server_choice = 0; /* not serverChoice because we have use attr */
52 return "cql.serverChoice";
56 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
59 for (j = 0; j < attributes->num_attributes; j++)
61 Z_AttributeElement *ae = attributes->attributes[j];
62 if (*ae->attributeType == 2) /* relation attribute */
64 if (ae->which == Z_AttributeValue_numeric)
66 /* Only support for numeric relation */
67 Odr_int *relation = ae->value.numeric;
68 /* map this numeric to representation in CQL */
71 /* Unsure on whether this is the relation attribute constants? */
72 case Z_ProximityOperator_Prox_lessThan:
74 case Z_ProximityOperator_Prox_lessThanOrEqual:
76 case Z_ProximityOperator_Prox_equal:
78 case Z_ProximityOperator_Prox_greaterThanOrEqual:
80 case Z_ProximityOperator_Prox_greaterThan:
82 case Z_ProximityOperator_Prox_notEqual:
85 /* phonetic is not supported in CQL */
88 /* stem is not supported in CQL */
91 /* relevance is supported in CQL, but not implemented yet */
94 /* Invalid relation */
99 /* Can we have a complex relation value?
100 Should we implement something?
108 static int rpn2cql_attr(cql_transform_t ct,
109 Z_AttributeList *attributes, WRBUF w)
111 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
112 const char *index = cql_lookup_reverse(ct, "index.", attributes);
113 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
115 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
117 index = lookup_index_from_string_attr(attributes);
119 /* Attempt to fix bug #2978: Look for a relation attribute */
121 relation = lookup_relation_index_from_attr(attributes);
125 cql_transform_set_error(ct,
126 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
129 /* for serverChoice we omit index+relation+structure */
130 if (strcmp(index, "cql.serverChoice"))
132 wrbuf_puts(w, index);
135 if (!strcmp(relation, "exact"))
137 else if (!strcmp(relation, "eq"))
139 else if (!strcmp(relation, "le"))
141 else if (!strcmp(relation, "ge"))
143 /* Missing mapping of not equal, phonetic, stem and relevance */
144 wrbuf_puts(w, relation);
151 if (strcmp(structure, "*"))
154 wrbuf_puts(w, structure);
162 static Odr_int lookup_truncation(Z_AttributeList *attributes)
165 for (j = 0; j < attributes->num_attributes; j++)
167 Z_AttributeElement *ae = attributes->attributes[j];
168 if (*ae->attributeType == 5) /* truncation attribute */
170 if (ae->which == Z_AttributeValue_numeric)
171 return *(ae->value.numeric);
174 /* No truncation specified */
178 static int rpn2cql_simple(cql_transform_t ct,
179 void (*pr)(const char *buf, void *client_data),
181 Z_Operand *q, WRBUF w)
184 if (q->which != Z_Operand_APT)
187 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
191 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
192 Z_Term *term = apt->term;
193 const char *sterm = 0;
195 Odr_int trunc = lookup_truncation(apt->attributes);
199 ret = rpn2cql_attr(ct, apt->attributes, w);
204 lterm = term->u.general->len;
205 sterm = (const char *) term->u.general->buf;
208 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
210 case Z_Term_characterString:
211 sterm = term->u.characterString;
212 lterm = strlen(sterm);
215 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
219 if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
222 for (i = 0 ; i < lterm; i++)
223 if (strchr(" ()=></", sterm[i]))
232 if (trunc == 2 || trunc == 3)
234 for (i = 0; i < lterm; i++)
236 if (sterm[i] == '\\' && i < lterm - 1)
239 if (strchr("*?\"\\", sterm[i]))
241 wrbuf_putc(w, sterm[i]);
243 else if (trunc == 102 && sterm[i] == '.' && sterm[i+1] == '*')
248 else if (trunc == 102 && sterm[i] == '.')
250 else if (trunc == 104 && sterm[i] == '?')
252 else if (trunc == 104 && sterm[i] == '#')
254 else if (strchr("*?\"", sterm[i]))
257 wrbuf_putc(w, sterm[i]);
260 wrbuf_putc(w, sterm[i]);
262 if (trunc == 1 || trunc == 3)
269 cql_transform_set_error(
270 ct, YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE, 0);
274 pr(wrbuf_cstr(w), client_data);
280 static int rpn2cql_structure(cql_transform_t ct,
281 void (*pr)(const char *buf, void *client_data),
283 Z_RPNStructure *q, int nested,
286 if (q->which == Z_RPNStructure_simple)
287 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
290 Z_Operator *op = q->u.complex->roperator;
291 Z_ProximityOperator *prox;
295 pr("(", client_data);
297 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
303 pr(" and ", client_data);
306 pr(" or ", client_data);
308 case Z_Operator_and_not:
309 pr(" not ", client_data);
311 case Z_Operator_prox: {
312 pr(" prox", client_data);
314 /* No way to express Odr_bool *exclusion -- ignore it */
315 if (prox->distance) {
316 char buf[21]; /* Enough for any 64-bit int */
317 char *op2name[6] = { "<", "<=", "=", ">=", ">","<>" };
318 pr("/distance", client_data);
319 if (!prox->relationType ||
320 *prox->relationType < Z_ProximityOperator_Prox_lessThan ||
321 *prox->relationType > Z_ProximityOperator_Prox_notEqual) {
322 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH,
323 "unrecognised proximity relationType");
326 pr(op2name[*prox->relationType-1], client_data);
327 sprintf(buf, "%ld", (long) *prox->distance);
328 pr(buf, client_data);
331 if (*prox->ordered) {
332 pr("/ordered", client_data);
334 pr("/unordered", client_data);
337 if (prox->which != Z_ProximityOperator_known ||
338 *prox->u.known != Z_ProxUnit_word) {
339 pr("/unit=", client_data);
340 pr(yaz_prox_unit_name(prox), client_data);
342 pr(" ", client_data);
346 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
348 pr(")", client_data);
353 int cql_transform_rpn2cql_stream(cql_transform_t ct,
354 void (*pr)(const char *buf, void *client_data),
359 WRBUF w = wrbuf_alloc();
360 cql_transform_set_error(ct, 0, 0);
361 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
367 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
371 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
377 * c-file-style: "Stroustrup"
378 * indent-tabs-mode: nil
380 * vim: shiftwidth=4 tabstop=8 expandtab