1 /* $Id: xcqlutil.c,v 1.6 2005-06-25 15:46:06 adam Exp $
2 Copyright (C) 1995-2005, Index Data ApS
5 This file is part of the YAZ toolkit.
12 * \brief Implements CQL to XCQL conversion.
21 static void pr_n(const char *buf,
22 void (*pr)(const char *buf, void *client_data),
23 void *client_data, int n)
27 (*pr)(" ", client_data);
28 (*pr)(buf, client_data);
31 static void pr_cdata(const char *buf,
32 void (*pr)(const char *buf, void *client_data),
35 const char *src = buf;
42 (*pr)("&", client_data);
45 (*pr)("<", client_data);
48 (*pr)(">", client_data);
53 (*pr)(bf, client_data);
59 static void prefixes(struct cql_node *cn,
60 void (*pr)(const char *buf, void *client_data),
61 void *client_data, int level)
64 if (cn->u.st.index_uri)
66 pr_n("<prefixes>\n", pr, client_data, level);
69 pr_n("<prefix>\n", pr, client_data, level+2);
70 pr_n("<identifier>", pr, client_data, level+4);
71 pr_cdata(cn->u.st.index_uri, pr, client_data);
72 pr_n("</identifier>\n", pr, client_data, 0);
73 pr_n("</prefix>\n", pr, client_data, level+2);
75 if (cn->u.st.relation_uri && cn->u.st.relation)
78 pr_n("<prefixes>\n", pr, client_data, level);
79 pr_n("<prefix>\n", pr, client_data, level+2);
80 pr_n("<name>", pr, client_data, level+4);
81 pr_cdata("rel", pr, client_data);
82 pr_n("</name>\n", pr, client_data, 0);
83 pr_n("<identifier>", pr, client_data, level+4);
84 pr_cdata(cn->u.st.relation_uri, pr, client_data);
85 pr_n("</identifier>\n", pr, client_data, 0);
86 pr_n("</prefix>\n", pr, client_data, level+2);
89 pr_n("</prefixes>\n", pr, client_data, level);
92 static void cql_to_xml_mod(struct cql_node *m,
93 void (*pr)(const char *buf, void *client_data),
94 void *client_data, int level)
98 pr_n("<modifiers>\n", pr, client_data, level);
99 for (; m; m = m->u.st.modifiers)
101 pr_n("<modifier>\n", pr, client_data, level+2);
102 pr_n("<type>", pr, client_data, level+4);
103 pr_cdata(m->u.st.index, pr, client_data);
104 pr_n("</type>\n", pr, client_data, 0);
105 if (m->u.st.relation)
107 pr_n("<relation>", pr, client_data, level+4);
108 pr_cdata(m->u.st.relation, pr, client_data);
109 pr_n("</relation>\n", pr, client_data, 0);
113 pr_n("<value>", pr, client_data, level+4);
114 pr_cdata(m->u.st.term, pr, client_data);
115 pr_n("</value>\n", pr, client_data, 0);
117 pr_n("</modifier>\n", pr, client_data, level+2);
119 pr_n("</modifiers>\n", pr, client_data, level);
123 static void cql_to_xml_r(struct cql_node *cn,
124 void (*pr)(const char *buf, void *client_data),
125 void *client_data, int level)
132 pr_n("<searchClause>\n", pr, client_data, level);
133 prefixes(cn, pr, client_data, level+2);
136 pr_n("<index>", pr, client_data, level+2);
137 pr_cdata(cn->u.st.index, pr, client_data);
138 pr_n("</index>\n", pr, client_data, 0);
140 if (cn->u.st.relation)
142 pr_n("<relation>\n", pr, client_data, level+2);
143 pr_n("<value>", pr, client_data, level+4);
144 if (cn->u.st.relation_uri)
145 pr_cdata("rel.", pr, client_data);
146 pr_cdata(cn->u.st.relation, pr, client_data);
147 pr_n("</value>\n", pr, client_data, 0);
149 if (cn->u.st.relation_uri)
151 pr_n("<identifier>", pr, client_data, level+4);
152 pr_cdata(cn->u.st.relation_uri, pr, client_data);
153 pr_n("</identifier>\n", pr, client_data, 0);
155 cql_to_xml_mod(cn->u.st.modifiers,
156 pr, client_data, level+4);
158 pr_n("</relation>\n", pr, client_data, level+2);
162 pr_n("<term>", pr, client_data, level+2);
163 pr_cdata(cn->u.st.term, pr, client_data);
164 pr_n("</term>\n", pr, client_data, 0);
166 pr_n("</searchClause>\n", pr, client_data, level);
169 pr_n("<triple>\n", pr, client_data, level);
170 if (cn->u.boolean.value)
172 pr_n("<boolean>\n", pr, client_data, level+2);
174 pr_n("<value>", pr, client_data, level+4);
175 pr_cdata(cn->u.boolean.value, pr, client_data);
176 pr_n("</value>\n", pr, client_data, 0);
178 cql_to_xml_mod(cn->u.boolean.modifiers,
179 pr, client_data, level+4);
181 pr_n("</boolean>\n", pr, client_data, level+2);
183 if (cn->u.boolean.left)
185 printf ("%*s<leftOperand>\n", level+2, "");
186 cql_to_xml_r(cn->u.boolean.left, pr, client_data, level+4);
187 printf ("%*s</leftOperand>\n", level+2, "");
189 if (cn->u.boolean.right)
191 printf ("%*s<rightOperand>\n", level+2, "");
192 cql_to_xml_r(cn->u.boolean.right, pr, client_data, level+4);
193 printf ("%*s</rightOperand>\n", level+2, "");
195 pr_n("</triple>\n", pr, client_data, level);
199 void cql_to_xml(struct cql_node *cn,
200 void (*pr)(const char *buf, void *client_data),
203 cql_to_xml_r(cn, pr, client_data, 0);
206 void cql_to_xml_stdio(struct cql_node *cn, FILE *f)
208 cql_to_xml(cn, cql_fputs, f);
211 void cql_buf_write_handler (const char *b, void *client_data)
213 struct cql_buf_write_info *info = (struct cql_buf_write_info *)client_data;
215 if (info->off < 0 || (info->off + l >= info->max))
220 memcpy (info->buf + info->off, b, l);
224 int cql_to_xml_buf(struct cql_node *cn, char *out, int max)
226 struct cql_buf_write_info info;
230 cql_to_xml(cn, cql_buf_write_handler, &info);
232 info.buf[info.off] = '\0';
239 * indent-tabs-mode: nil
241 * vim: shiftwidth=4 tabstop=8 expandtab