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 CQL to XCQL conversion.
19 static void pr_n(const char *buf,
20 void (*pr)(const char *buf, void *client_data),
21 void *client_data, int n)
25 (*pr)(" ", client_data);
26 (*pr)(buf, client_data);
29 static void pr_cdata(const char *buf,
30 void (*pr)(const char *buf, void *client_data),
33 const char *src = buf;
40 (*pr)("&", client_data);
43 (*pr)("<", client_data);
46 (*pr)(">", client_data);
51 (*pr)(bf, client_data);
57 static void prefixes(struct cql_node *cn,
58 void (*pr)(const char *buf, void *client_data),
59 void *client_data, int level)
62 if (cn->u.st.index_uri)
64 pr_n("<prefixes>\n", pr, client_data, level);
67 pr_n("<prefix>\n", pr, client_data, level+2);
68 pr_n("<identifier>", pr, client_data, level+4);
69 pr_cdata(cn->u.st.index_uri, pr, client_data);
70 pr_n("</identifier>\n", pr, client_data, 0);
71 pr_n("</prefix>\n", pr, client_data, level+2);
73 if (cn->u.st.relation_uri && cn->u.st.relation)
76 pr_n("<prefixes>\n", pr, client_data, level);
77 pr_n("<prefix>\n", pr, client_data, level+2);
78 pr_n("<name>", pr, client_data, level+4);
79 pr_cdata("rel", pr, client_data);
80 pr_n("</name>\n", pr, client_data, 0);
81 pr_n("<identifier>", pr, client_data, level+4);
82 pr_cdata(cn->u.st.relation_uri, pr, client_data);
83 pr_n("</identifier>\n", pr, client_data, 0);
84 pr_n("</prefix>\n", pr, client_data, level+2);
87 pr_n("</prefixes>\n", pr, client_data, level);
90 static void cql_to_xml_mod(struct cql_node *m,
91 void (*pr)(const char *buf, void *client_data),
92 void *client_data, int level)
96 pr_n("<modifiers>\n", pr, client_data, level);
97 for (; m; m = m->u.st.modifiers)
99 pr_n("<modifier>\n", pr, client_data, level+2);
100 pr_n("<type>", pr, client_data, level+4);
101 pr_cdata(m->u.st.index, pr, client_data);
102 pr_n("</type>\n", pr, client_data, 0);
103 if (m->u.st.relation)
105 pr_n("<comparison>", pr, client_data, level+4);
106 pr_cdata(m->u.st.relation, pr, client_data);
107 pr_n("</comparison>\n", pr, client_data, 0);
111 pr_n("<value>", pr, client_data, level+4);
112 pr_cdata(m->u.st.term, pr, client_data);
113 pr_n("</value>\n", pr, client_data, 0);
115 pr_n("</modifier>\n", pr, client_data, level+2);
117 pr_n("</modifiers>\n", pr, client_data, level);
121 static void cql_sort_to_xml(struct cql_node *cn,
122 void (*pr)(const char *buf, void *client_data),
123 void *client_data, int level)
127 pr_n("<sortKeys>\n", pr, client_data, level);
128 for (; cn; cn = cn->u.sort.next)
130 pr_n("<key>\n", pr, client_data, level+2);
132 if (cn->u.sort.index)
134 pr_n("<index>", pr, client_data, level+4);
135 pr_cdata(cn->u.sort.index, pr, client_data);
136 pr_n("</index>\n", pr, client_data, 0);
138 cql_to_xml_mod(cn->u.sort.modifiers,
139 pr, client_data, level+6);
141 pr_n("</key>\n", pr, client_data, level+2);
143 pr_n("</sortKeys>\n", pr, client_data, level);
147 static void cql_to_xml_r(struct cql_node *cn,
148 void (*pr)(const char *buf, void *client_data),
149 void *client_data, int level,
150 struct cql_node *sort_node)
157 pr_n("<searchClause>\n", pr, client_data, level);
158 prefixes(cn, pr, client_data, level+2);
161 pr_n("<index>", pr, client_data, level+2);
162 pr_cdata(cn->u.st.index, pr, client_data);
163 pr_n("</index>\n", pr, client_data, 0);
165 if (cn->u.st.relation)
167 pr_n("<relation>\n", pr, client_data, level+2);
168 pr_n("<value>", pr, client_data, level+4);
169 if (cn->u.st.relation_uri)
170 pr_cdata("rel.", pr, client_data);
171 pr_cdata(cn->u.st.relation, pr, client_data);
172 pr_n("</value>\n", pr, client_data, 0);
174 if (cn->u.st.relation_uri)
176 pr_n("<identifier>", pr, client_data, level+4);
177 pr_cdata(cn->u.st.relation_uri, pr, client_data);
178 pr_n("</identifier>\n", pr, client_data, 0);
180 cql_to_xml_mod(cn->u.st.modifiers,
181 pr, client_data, level+4);
183 pr_n("</relation>\n", pr, client_data, level+2);
187 pr_n("<term>", pr, client_data, level+2);
188 pr_cdata(cn->u.st.term, pr, client_data);
189 pr_n("</term>\n", pr, client_data, 0);
191 if (cn->u.st.extra_terms)
193 struct cql_node *n = cn->u.st.extra_terms;
194 for (; n; n = n->u.st.extra_terms)
196 pr_n("<term>", pr, client_data, level+2);
197 pr_cdata(n->u.st.term, pr, client_data);
198 pr_n("</term>\n", pr, client_data, 0);
201 cql_sort_to_xml(sort_node, pr, client_data, level+2);
202 pr_n("</searchClause>\n", pr, client_data, level);
205 pr_n("<triple>\n", pr, client_data, level);
206 if (cn->u.boolean.value)
208 pr_n("<boolean>\n", pr, client_data, level+2);
210 pr_n("<value>", pr, client_data, level+4);
211 pr_cdata(cn->u.boolean.value, pr, client_data);
212 pr_n("</value>\n", pr, client_data, 0);
214 cql_to_xml_mod(cn->u.boolean.modifiers,
215 pr, client_data, level+4);
217 pr_n("</boolean>\n", pr, client_data, level+2);
219 if (cn->u.boolean.left)
221 printf ("%*s<leftOperand>\n", level+2, "");
222 cql_to_xml_r(cn->u.boolean.left, pr, client_data, level+4, 0);
223 printf ("%*s</leftOperand>\n", level+2, "");
225 if (cn->u.boolean.right)
227 printf ("%*s<rightOperand>\n", level+2, "");
228 cql_to_xml_r(cn->u.boolean.right, pr, client_data, level+4, 0);
229 printf ("%*s</rightOperand>\n", level+2, "");
231 cql_sort_to_xml(sort_node, pr, client_data, level+2);
232 pr_n("</triple>\n", pr, client_data, level);
235 cql_to_xml_r(cn->u.sort.search, pr, client_data, level, cn);
239 void cql_to_xml(struct cql_node *cn,
240 void (*pr)(const char *buf, void *client_data),
243 cql_to_xml_r(cn, pr, client_data, 0, 0);
246 void cql_to_xml_stdio(struct cql_node *cn, FILE *f)
248 cql_to_xml(cn, cql_fputs, f);
251 void cql_buf_write_handler (const char *b, void *client_data)
253 struct cql_buf_write_info *info = (struct cql_buf_write_info *)client_data;
255 if (info->off < 0 || (info->off + l >= info->max))
260 memcpy (info->buf + info->off, b, l);
264 int cql_to_xml_buf(struct cql_node *cn, char *out, int max)
266 struct cql_buf_write_info info;
270 cql_to_xml(cn, cql_buf_write_handler, &info);
272 info.buf[info.off] = '\0';
279 * c-file-style: "Stroustrup"
280 * indent-tabs-mode: nil
282 * vim: shiftwidth=4 tabstop=8 expandtab