1 /* $Id: d1_write.c,v 1.6 2005-01-15 19:38:18 adam Exp $
2 Copyright (C) 1995-2005
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_put_xattr(WRBUF b, data1_xattr *p)
52 for (; p; p = p->next)
55 if (p->what == DATA1I_xmltext)
56 wrbuf_puts (b, p->name);
58 wrbuf_xmlputs (b, p->name);
63 if (p->what == DATA1I_text)
64 wrbuf_xmlputs (b, p->value);
66 wrbuf_puts (b, p->value);
72 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
77 for (c = n->child; c; c = c->next)
81 if (c->which == DATA1N_preprocess)
86 wrbuf_xmlputs (b, c->u.preprocess.target);
87 wrbuf_put_xattr (b, c->u.preprocess.attributes);
90 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
93 wrbuf_puts (b, "?>\n");
95 else if (c->which == DATA1N_tag)
97 if (select && !c->u.tag.node_selected)
100 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
102 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
110 wrbuf_xmlputs (b, tag);
111 wrbuf_put_xattr (b, c->u.tag.attributes);
115 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
121 wrbuf_xmlputs(b, tag);
124 wrbuf_puts (b, "\n");
127 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
129 char *p = c->u.data.data;
130 int l = c->u.data.len;
134 if (pretty_format && !c->u.data.formatted_text)
136 if (c->which == DATA1N_comment)
137 wrbuf_puts (b, "<!--");
138 switch (c->u.data.what)
141 wrbuf_write(b, c->u.data.data, c->u.data.len);
144 if (!pretty_format || c->u.data.formatted_text)
146 wrbuf_xmlputs_n (b, p, l);
154 while (l && d1_isspace(*p))
158 /* break if we cross margin and word is not too long */
159 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
160 wlen < IDSGML_MARGIN)
162 wrbuf_puts (b, "\n");
172 while (l && !d1_isspace(*p))
185 wrbuf_xmlputs_n(b, c->u.data.data, c->u.data.len);
190 wrbuf_xmlputs_n(b, c->u.data.data, c->u.data.len);
194 if (c->which == DATA1N_comment)
196 wrbuf_puts(b, "-->");
205 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
207 WRBUF b = data1_get_wrbuf (dh);
211 if (!data1_is_xmlmode (dh))
214 wrbuf_puts (b, n->u.root.type);
215 wrbuf_puts (b, ">\n");
217 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
219 if (!data1_is_xmlmode (dh))
221 wrbuf_puts (b, "</");
222 wrbuf_puts (b, n->u.root.type);
223 wrbuf_puts (b, ">\n");