1 /* $Id: d1_write.c,v 1.10 2007-01-15 15:10:14 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 void wrbuf_write_tag(WRBUF b, const char *tag, int opening)
76 /* see if we must fix the tag.. The grs.marc filter produces
77 a data1 tree with not well-formed XML */
78 if (*tag >= '0' && *tag <= '9')
80 for (i = 0; tag[i]; i++)
81 if (strchr( " <>$,()[]", tag[i]))
88 wrbuf_puts(b, " value=\"");
89 wrbuf_xmlputs(b, tag);
97 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
102 for (c = n->child; c; c = c->next)
106 if (c->which == DATA1N_preprocess)
110 wrbuf_puts (b, "<?");
111 wrbuf_xmlputs (b, c->u.preprocess.target);
112 wrbuf_put_xattr (b, c->u.preprocess.attributes);
115 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
118 wrbuf_puts (b, "?>\n");
120 else if (c->which == DATA1N_tag)
122 if (select && !c->u.tag.node_selected)
125 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
127 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
135 wrbuf_write_tag(b, tag, 1);
136 wrbuf_put_xattr (b, c->u.tag.attributes);
140 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
146 wrbuf_write_tag(b, tag, 0);
149 wrbuf_puts (b, "\n");
152 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
154 char *p = c->u.data.data;
155 int l = c->u.data.len;
159 if (pretty_format && !c->u.data.formatted_text)
161 if (c->which == DATA1N_comment)
162 wrbuf_puts (b, "<!--");
163 switch (c->u.data.what)
166 wrbuf_write(b, c->u.data.data, c->u.data.len);
169 if (!pretty_format || c->u.data.formatted_text)
171 wrbuf_xmlputs_n (b, p, l);
179 while (l && d1_isspace(*p))
183 /* break if we cross margin and word is not too long */
184 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
185 wlen < IDSGML_MARGIN)
187 wrbuf_puts (b, "\n");
197 while (l && !d1_isspace(*p))
210 wrbuf_xmlputs_n(b, c->u.data.data, c->u.data.len);
215 wrbuf_xmlputs_n(b, c->u.data.data, c->u.data.len);
219 if (c->which == DATA1N_comment)
221 wrbuf_puts(b, "-->");
230 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
232 WRBUF b = data1_get_wrbuf (dh);
236 if (!data1_is_xmlmode (dh))
239 wrbuf_write_tag(b, n->u.root.type, 1);
240 wrbuf_puts (b, ">\n");
242 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
244 if (!data1_is_xmlmode (dh))
246 wrbuf_puts (b, "</");
247 wrbuf_write_tag(b, n->u.root.type, 0);
248 wrbuf_puts (b, ">\n");
256 * indent-tabs-mode: nil
258 * vim: shiftwidth=4 tabstop=8 expandtab