2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: d1_write.c,v 1.19 2002-10-08 20:14:44 adam Exp $
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
14 #define IDSGML_MARGIN 75
16 #define PRETTY_FORMAT 0
18 static int wordlen(char *b, int max)
22 while (l < max && !d1_isspace(*b))
27 static void indent (WRBUF b, int col)
30 for (i = 0; i<col; i++)
34 static void wrbuf_write_cdata(WRBUF b, const char *msg, int len)
38 for (i = 0; i < len; i++)
43 wrbuf_puts (b, """);
46 wrbuf_puts (b, "'");
49 wrbuf_puts (b, ">");
52 wrbuf_puts (b, "<");
55 wrbuf_puts (b, "&");
58 wrbuf_putc(b, msg[i]);
63 static void wrbuf_put_cdata(WRBUF b, const char *msg)
65 wrbuf_write_cdata (b, msg, strlen(msg));
68 static void wrbuf_put_xattr(WRBUF b, data1_xattr *p)
70 for (; p; p = p->next)
73 if (p->what == DATA1I_xmltext)
74 wrbuf_puts (b, p->name);
76 wrbuf_put_cdata (b, p->name);
81 if (p->what == DATA1I_text)
82 wrbuf_put_cdata (b, p->value);
84 wrbuf_puts (b, p->value);
90 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
95 for (c = n->child; c; c = c->next)
99 if (c->which == DATA1N_preprocess)
105 wrbuf_puts (b, "<?");
106 wrbuf_put_cdata (b, c->u.preprocess.target);
107 wrbuf_put_xattr (b, c->u.preprocess.attributes);
110 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
113 wrbuf_puts (b, "?>\n");
115 else if (c->which == DATA1N_tag)
117 if (select && !c->u.tag.node_selected)
120 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
122 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
132 wrbuf_put_cdata (b, tag);
133 wrbuf_put_xattr (b, c->u.tag.attributes);
137 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
143 wrbuf_put_cdata(b, tag);
146 wrbuf_puts (b, "\n");
149 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
151 char *p = c->u.data.data;
152 int l = c->u.data.len;
156 if (pretty_format && !c->u.data.formatted_text)
158 if (c->which == DATA1N_comment)
159 wrbuf_puts (b, "<!--");
160 switch (c->u.data.what)
163 wrbuf_write(b, c->u.data.data, c->u.data.len);
166 if (!pretty_format || c->u.data.formatted_text)
168 wrbuf_write_cdata (b, p, l);
176 while (l && d1_isspace(*p))
180 /* break if we cross margin and word is not too long */
181 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
182 wlen < IDSGML_MARGIN)
184 wrbuf_puts (b, "\n");
194 while (l && !d1_isspace(*p))
207 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
212 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
216 if (c->which == DATA1N_comment)
218 wrbuf_puts(b, "-->");
227 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
229 WRBUF b = data1_get_wrbuf (dh);
233 if (!data1_is_xmlmode (dh))
236 wrbuf_puts (b, n->u.root.type);
237 wrbuf_puts (b, ">\n");
239 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
241 if (!data1_is_xmlmode (dh))
243 wrbuf_puts (b, "</");
244 wrbuf_puts (b, n->u.root.type);
245 wrbuf_puts (b, ">\n");