1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* converts data1 tree to XML record */
24 #include <idzebra/data1.h>
25 #include <yaz/wrbuf.h>
27 #define IDSGML_MARGIN 75
29 #define PRETTY_FORMAT 0
31 static int wordlen(char *b, int max)
35 while (l < max && !d1_isspace(*b))
40 static void indent (WRBUF b, int col)
43 for (i = 0; i<col; i++)
47 static void wrbuf_put_xattr(WRBUF b, data1_xattr *p)
49 for (; p; p = p->next)
52 if (p->what == DATA1I_xmltext)
53 wrbuf_puts (b, p->name);
55 wrbuf_xmlputs (b, p->name);
60 if (p->what == DATA1I_text)
61 wrbuf_xmlputs (b, p->value);
63 wrbuf_puts (b, p->value);
69 static void wrbuf_write_tag(WRBUF b, const char *tag, int opening)
73 /* see if we must fix the tag.. The grs.marc filter produces
74 a data1 tree with not well-formed XML */
75 if (*tag >= '0' && *tag <= '9')
77 for (i = 0; tag[i]; i++)
78 if (strchr( " <>$,()[]", tag[i]))
85 wrbuf_puts(b, " value=\"");
86 wrbuf_xmlputs(b, tag);
94 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
99 for (c = n->child; c; c = c->next)
103 if (c->which == DATA1N_preprocess)
107 wrbuf_puts (b, "<?");
108 wrbuf_xmlputs (b, c->u.preprocess.target);
109 wrbuf_put_xattr (b, c->u.preprocess.attributes);
112 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
115 wrbuf_puts (b, "?>\n");
117 else if (c->which == DATA1N_tag)
119 if (select && !c->u.tag.node_selected)
122 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
124 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
132 wrbuf_write_tag(b, tag, 1);
133 wrbuf_put_xattr (b, c->u.tag.attributes);
137 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
143 wrbuf_write_tag(b, tag, 0);
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_xmlputs_n (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_xmlputs_n(b, c->u.data.data, c->u.data.len);
212 wrbuf_xmlputs_n(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_write_tag(b, n->u.root.type, 1);
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_write_tag(b, n->u.root.type, 0);
245 wrbuf_puts (b, ">\n");
253 * c-file-style: "Stroustrup"
254 * indent-tabs-mode: nil
256 * vim: shiftwidth=4 tabstop=8 expandtab