1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements CQL to XCQL conversion.
17 static void pr_n(const char *buf,
18 void (*pr)(const char *buf, void *client_data),
19 void *client_data, int n)
23 (*pr)(" ", client_data);
24 (*pr)(buf, client_data);
27 static void pr_cdata(const char *buf,
28 void (*pr)(const char *buf, void *client_data),
31 const char *src = buf;
38 (*pr)("&", client_data);
41 (*pr)("<", client_data);
44 (*pr)(">", client_data);
49 (*pr)(bf, client_data);
55 static void prefixes(struct cql_node *cn,
56 void (*pr)(const char *buf, void *client_data),
57 void *client_data, int level)
60 if (cn->u.st.index_uri)
62 pr_n("<prefixes>\n", pr, client_data, level);
65 pr_n("<prefix>\n", pr, client_data, level+2);
66 pr_n("<identifier>", pr, client_data, level+4);
67 pr_cdata(cn->u.st.index_uri, pr, client_data);
68 pr_n("</identifier>\n", pr, client_data, 0);
69 pr_n("</prefix>\n", pr, client_data, level+2);
71 if (cn->u.st.relation_uri && cn->u.st.relation)
74 pr_n("<prefixes>\n", pr, client_data, level);
75 pr_n("<prefix>\n", pr, client_data, level+2);
76 pr_n("<name>", pr, client_data, level+4);
77 pr_cdata("rel", pr, client_data);
78 pr_n("</name>\n", pr, client_data, 0);
79 pr_n("<identifier>", pr, client_data, level+4);
80 pr_cdata(cn->u.st.relation_uri, pr, client_data);
81 pr_n("</identifier>\n", pr, client_data, 0);
82 pr_n("</prefix>\n", pr, client_data, level+2);
85 pr_n("</prefixes>\n", pr, client_data, level);
88 static void cql_to_xml_mod(struct cql_node *m,
89 void (*pr)(const char *buf, void *client_data),
90 void *client_data, int level)
94 pr_n("<modifiers>\n", pr, client_data, level);
95 for (; m; m = m->u.st.modifiers)
97 pr_n("<modifier>\n", pr, client_data, level+2);
98 pr_n("<type>", pr, client_data, level+4);
99 pr_cdata(m->u.st.index, pr, client_data);
100 pr_n("</type>\n", pr, client_data, 0);
101 if (m->u.st.relation)
103 pr_n("<comparison>", pr, client_data, level+4);
104 pr_cdata(m->u.st.relation, pr, client_data);
105 pr_n("</comparison>\n", pr, client_data, 0);
109 pr_n("<value>", pr, client_data, level+4);
110 pr_cdata(m->u.st.term, pr, client_data);
111 pr_n("</value>\n", pr, client_data, 0);
113 pr_n("</modifier>\n", pr, client_data, level+2);
115 pr_n("</modifiers>\n", pr, client_data, level);
119 static void cql_to_xml_r(struct cql_node *cn,
120 void (*pr)(const char *buf, void *client_data),
121 void *client_data, int level)
128 pr_n("<searchClause>\n", pr, client_data, level);
129 prefixes(cn, pr, client_data, level+2);
132 pr_n("<index>", pr, client_data, level+2);
133 pr_cdata(cn->u.st.index, pr, client_data);
134 pr_n("</index>\n", pr, client_data, 0);
136 if (cn->u.st.relation)
138 pr_n("<relation>\n", pr, client_data, level+2);
139 pr_n("<value>", pr, client_data, level+4);
140 if (cn->u.st.relation_uri)
141 pr_cdata("rel.", pr, client_data);
142 pr_cdata(cn->u.st.relation, pr, client_data);
143 pr_n("</value>\n", pr, client_data, 0);
145 if (cn->u.st.relation_uri)
147 pr_n("<identifier>", pr, client_data, level+4);
148 pr_cdata(cn->u.st.relation_uri, pr, client_data);
149 pr_n("</identifier>\n", pr, client_data, 0);
151 cql_to_xml_mod(cn->u.st.modifiers,
152 pr, client_data, level+4);
154 pr_n("</relation>\n", pr, client_data, level+2);
158 pr_n("<term>", pr, client_data, level+2);
159 pr_cdata(cn->u.st.term, pr, client_data);
160 pr_n("</term>\n", pr, client_data, 0);
162 if (cn->u.st.extra_terms)
164 struct cql_node *n = cn->u.st.extra_terms;
165 for (; n; n = n->u.st.extra_terms)
167 pr_n("<term>", pr, client_data, level+2);
168 pr_cdata(n->u.st.term, pr, client_data);
169 pr_n("</term>\n", pr, client_data, 0);
172 pr_n("</searchClause>\n", pr, client_data, level);
175 pr_n("<triple>\n", pr, client_data, level);
176 if (cn->u.boolean.value)
178 pr_n("<boolean>\n", pr, client_data, level+2);
180 pr_n("<value>", pr, client_data, level+4);
181 pr_cdata(cn->u.boolean.value, pr, client_data);
182 pr_n("</value>\n", pr, client_data, 0);
184 cql_to_xml_mod(cn->u.boolean.modifiers,
185 pr, client_data, level+4);
187 pr_n("</boolean>\n", pr, client_data, level+2);
189 if (cn->u.boolean.left)
191 printf ("%*s<leftOperand>\n", level+2, "");
192 cql_to_xml_r(cn->u.boolean.left, pr, client_data, level+4);
193 printf ("%*s</leftOperand>\n", level+2, "");
195 if (cn->u.boolean.right)
197 printf ("%*s<rightOperand>\n", level+2, "");
198 cql_to_xml_r(cn->u.boolean.right, pr, client_data, level+4);
199 printf ("%*s</rightOperand>\n", level+2, "");
201 pr_n("</triple>\n", pr, client_data, level);
205 void cql_to_xml(struct cql_node *cn,
206 void (*pr)(const char *buf, void *client_data),
209 cql_to_xml_r(cn, pr, client_data, 0);
212 void cql_to_xml_stdio(struct cql_node *cn, FILE *f)
214 cql_to_xml(cn, cql_fputs, f);
217 void cql_buf_write_handler (const char *b, void *client_data)
219 struct cql_buf_write_info *info = (struct cql_buf_write_info *)client_data;
221 if (info->off < 0 || (info->off + l >= info->max))
226 memcpy (info->buf + info->off, b, l);
230 int cql_to_xml_buf(struct cql_node *cn, char *out, int max)
232 struct cql_buf_write_info info;
236 cql_to_xml(cn, cql_buf_write_handler, &info);
238 info.buf[info.off] = '\0';
245 * indent-tabs-mode: nil
247 * vim: shiftwidth=4 tabstop=8 expandtab