1 /* $Id: d1_write.c,v 1.2 2002-10-22 13:19:50 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 */
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)
121 wrbuf_puts (b, "<?");
122 wrbuf_put_cdata (b, c->u.preprocess.target);
123 wrbuf_put_xattr (b, c->u.preprocess.attributes);
126 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
129 wrbuf_puts (b, "?>\n");
131 else if (c->which == DATA1N_tag)
133 if (select && !c->u.tag.node_selected)
136 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
138 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
148 wrbuf_put_cdata (b, tag);
149 wrbuf_put_xattr (b, c->u.tag.attributes);
153 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
159 wrbuf_put_cdata(b, tag);
162 wrbuf_puts (b, "\n");
165 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
167 char *p = c->u.data.data;
168 int l = c->u.data.len;
172 if (pretty_format && !c->u.data.formatted_text)
174 if (c->which == DATA1N_comment)
175 wrbuf_puts (b, "<!--");
176 switch (c->u.data.what)
179 wrbuf_write(b, c->u.data.data, c->u.data.len);
182 if (!pretty_format || c->u.data.formatted_text)
184 wrbuf_write_cdata (b, p, l);
192 while (l && d1_isspace(*p))
196 /* break if we cross margin and word is not too long */
197 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
198 wlen < IDSGML_MARGIN)
200 wrbuf_puts (b, "\n");
210 while (l && !d1_isspace(*p))
223 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
228 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
232 if (c->which == DATA1N_comment)
234 wrbuf_puts(b, "-->");
243 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
245 WRBUF b = data1_get_wrbuf (dh);
249 if (!data1_is_xmlmode (dh))
252 wrbuf_puts (b, n->u.root.type);
253 wrbuf_puts (b, ">\n");
255 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
257 if (!data1_is_xmlmode (dh))
259 wrbuf_puts (b, "</");
260 wrbuf_puts (b, n->u.root.type);
261 wrbuf_puts (b, ">\n");