1 /* $Id: d1_write.c,v 1.4 2004-09-28 10:15:03 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* converts data1 tree to XML record */
27 #include <idzebra/data1.h>
28 #include <yaz/wrbuf.h>
30 #define IDSGML_MARGIN 75
32 #define PRETTY_FORMAT 0
34 static int wordlen(char *b, int max)
38 while (l < max && !d1_isspace(*b))
43 static void indent (WRBUF b, int col)
46 for (i = 0; i<col; i++)
50 static void wrbuf_write_cdata(WRBUF b, const char *msg, int len)
54 for (i = 0; i < len; i++)
59 wrbuf_puts (b, """);
62 wrbuf_puts (b, "'");
65 wrbuf_puts (b, ">");
68 wrbuf_puts (b, "<");
71 wrbuf_puts (b, "&");
74 wrbuf_putc(b, msg[i]);
79 static void wrbuf_put_cdata(WRBUF b, const char *msg)
81 wrbuf_write_cdata (b, msg, strlen(msg));
84 static void wrbuf_put_xattr(WRBUF b, data1_xattr *p)
86 for (; p; p = p->next)
89 if (p->what == DATA1I_xmltext)
90 wrbuf_puts (b, p->name);
92 wrbuf_put_cdata (b, p->name);
97 if (p->what == DATA1I_text)
98 wrbuf_put_cdata (b, p->value);
100 wrbuf_puts (b, p->value);
106 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
111 for (c = n->child; c; c = c->next)
115 if (c->which == DATA1N_preprocess)
119 wrbuf_puts (b, "<?");
120 wrbuf_put_cdata (b, c->u.preprocess.target);
121 wrbuf_put_xattr (b, c->u.preprocess.attributes);
124 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
127 wrbuf_puts (b, "?>\n");
129 else if (c->which == DATA1N_tag)
131 if (select && !c->u.tag.node_selected)
134 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
136 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
144 wrbuf_put_cdata (b, tag);
145 wrbuf_put_xattr (b, c->u.tag.attributes);
149 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
155 wrbuf_put_cdata(b, tag);
158 wrbuf_puts (b, "\n");
161 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
163 char *p = c->u.data.data;
164 int l = c->u.data.len;
168 if (pretty_format && !c->u.data.formatted_text)
170 if (c->which == DATA1N_comment)
171 wrbuf_puts (b, "<!--");
172 switch (c->u.data.what)
175 wrbuf_write(b, c->u.data.data, c->u.data.len);
178 if (!pretty_format || c->u.data.formatted_text)
180 wrbuf_write_cdata (b, p, l);
188 while (l && d1_isspace(*p))
192 /* break if we cross margin and word is not too long */
193 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
194 wlen < IDSGML_MARGIN)
196 wrbuf_puts (b, "\n");
206 while (l && !d1_isspace(*p))
219 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
224 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
228 if (c->which == DATA1N_comment)
230 wrbuf_puts(b, "-->");
239 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
241 WRBUF b = data1_get_wrbuf (dh);
245 if (!data1_is_xmlmode (dh))
248 wrbuf_puts (b, n->u.root.type);
249 wrbuf_puts (b, ">\n");
251 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
253 if (!data1_is_xmlmode (dh))
255 wrbuf_puts (b, "</");
256 wrbuf_puts (b, n->u.root.type);
257 wrbuf_puts (b, ">\n");