1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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_to_xml_r(struct cql_node *cn,
122 void (*pr)(const char *buf, void *client_data),
123 void *client_data, int level)
130 pr_n("<searchClause>\n", pr, client_data, level);
131 prefixes(cn, pr, client_data, level+2);
134 pr_n("<index>", pr, client_data, level+2);
135 pr_cdata(cn->u.st.index, pr, client_data);
136 pr_n("</index>\n", pr, client_data, 0);
138 if (cn->u.st.relation)
140 pr_n("<relation>\n", pr, client_data, level+2);
141 pr_n("<value>", pr, client_data, level+4);
142 if (cn->u.st.relation_uri)
143 pr_cdata("rel.", pr, client_data);
144 pr_cdata(cn->u.st.relation, pr, client_data);
145 pr_n("</value>\n", pr, client_data, 0);
147 if (cn->u.st.relation_uri)
149 pr_n("<identifier>", pr, client_data, level+4);
150 pr_cdata(cn->u.st.relation_uri, pr, client_data);
151 pr_n("</identifier>\n", pr, client_data, 0);
153 cql_to_xml_mod(cn->u.st.modifiers,
154 pr, client_data, level+4);
156 pr_n("</relation>\n", pr, client_data, level+2);
160 pr_n("<term>", pr, client_data, level+2);
161 pr_cdata(cn->u.st.term, pr, client_data);
162 pr_n("</term>\n", pr, client_data, 0);
164 if (cn->u.st.extra_terms)
166 struct cql_node *n = cn->u.st.extra_terms;
167 for (; n; n = n->u.st.extra_terms)
169 pr_n("<term>", pr, client_data, level+2);
170 pr_cdata(n->u.st.term, pr, client_data);
171 pr_n("</term>\n", pr, client_data, 0);
174 pr_n("</searchClause>\n", pr, client_data, level);
177 pr_n("<triple>\n", pr, client_data, level);
178 if (cn->u.boolean.value)
180 pr_n("<boolean>\n", pr, client_data, level+2);
182 pr_n("<value>", pr, client_data, level+4);
183 pr_cdata(cn->u.boolean.value, pr, client_data);
184 pr_n("</value>\n", pr, client_data, 0);
186 cql_to_xml_mod(cn->u.boolean.modifiers,
187 pr, client_data, level+4);
189 pr_n("</boolean>\n", pr, client_data, level+2);
191 if (cn->u.boolean.left)
193 printf ("%*s<leftOperand>\n", level+2, "");
194 cql_to_xml_r(cn->u.boolean.left, pr, client_data, level+4);
195 printf ("%*s</leftOperand>\n", level+2, "");
197 if (cn->u.boolean.right)
199 printf ("%*s<rightOperand>\n", level+2, "");
200 cql_to_xml_r(cn->u.boolean.right, pr, client_data, level+4);
201 printf ("%*s</rightOperand>\n", level+2, "");
203 pr_n("</triple>\n", pr, client_data, level);
207 void cql_to_xml(struct cql_node *cn,
208 void (*pr)(const char *buf, void *client_data),
211 cql_to_xml_r(cn, pr, client_data, 0);
214 void cql_to_xml_stdio(struct cql_node *cn, FILE *f)
216 cql_to_xml(cn, cql_fputs, f);
219 void cql_buf_write_handler (const char *b, void *client_data)
221 struct cql_buf_write_info *info = (struct cql_buf_write_info *)client_data;
223 if (info->off < 0 || (info->off + l >= info->max))
228 memcpy (info->buf + info->off, b, l);
232 int cql_to_xml_buf(struct cql_node *cn, char *out, int max)
234 struct cql_buf_write_info info;
238 cql_to_xml(cn, cql_buf_write_handler, &info);
240 info.buf[info.off] = '\0';
247 * c-file-style: "Stroustrup"
248 * indent-tabs-mode: nil
250 * vim: shiftwidth=4 tabstop=8 expandtab